/*
 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     dragos
 *
 * $Id$
 */

package org.nuxeo.ecm.platform.rendering.template;

import org.nuxeo.ecm.platform.ec.notification.email.templates.NuxeoTemplatesLoader;
import org.nuxeo.ecm.platform.rendering.RenderingContext;
import org.nuxeo.ecm.platform.rendering.RenderingEngine;
import org.nuxeo.ecm.platform.rendering.RenderingException;
import org.nuxeo.ecm.platform.rendering.RenderingResult;

import freemarker.template.Configuration;

/**
 * Base class for RenderingEngine implementation that will work with freemarker.
 *
 * @author <a href="mailto:dm@nuxeo.com">Dragos Mihalache</a>
 */
public abstract class FreemarkerRenderingEngine implements RenderingEngine {

    protected Configuration cfg;

    /**
     *  TODO : It works like this but this default implementation should
     *  return just a <code>new Configuration()</code>
     *  There should be a class that extends this class and overrides this
     *  but that brokes it right now.
     *
     * @return
     * @throws Exception
     */
    public Configuration createConfiguration() throws Exception {
        Configuration config = new Configuration();
        config.setTemplateLoader(new NuxeoTemplatesLoader());
        return config;
    }


    protected abstract FreemarkerRenderingJob createJob(RenderingContext ctx) throws RenderingException;

    public RenderingResult process(RenderingContext ctx)
            throws RenderingException {
        try {
            if (cfg == null) {
                cfg = createConfiguration();
            }
            FreemarkerRenderingJob job = createJob(ctx);
            cfg.getTemplate(job.getTemplate()).process(ctx, job.getWriter());
            return job.getResult();
        } catch (RenderingException e) {
            throw e;
        } catch (Exception e) {
            throw new RenderingException("Freemarker processing failed", e);
        }
    }

}

@@ -1338,17 +1338,16 @@
 *%0A     *
- 
  TODO : 
@@ -1407,23 +1407,22 @@
 ould
-%0A     * 
  return
+%0A     *
  jus
@@ -1457,24 +1457,16 @@
 )%3C/code%3E
-%0A     * 
  There s
@@ -1486,16 +1486,23 @@
 ass that
+%0A     *
  extends
@@ -1531,24 +1531,16 @@
 des this
-%0A     * 
  but tha
@@ -1569,16 +1569,55 @@
 %0A     *%0A
+     * TODO: write a clear TODO%0A     *%0A
      * @
@@ -1832,24 +1832,68 @@
 sLoader());%0A
+        config.setDefaultEncoding(%22UTF-8%22);%0A
         retu
@@ -1910,17 +1910,16 @@
 %0A    %7D%0A%0A
-%0A
     prot
@@ -1987,16 +1987,28 @@
 ext ctx)
+%0A           
  throws 
@@ -2335,18 +2335,65 @@
 te()
-).process(
+, cfg.getDefaultEncoding()).process(%0A                    
 ctx,

make freemarker use UTF-8 instead of platform encoding