001/*
002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     narcis
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.ec.notification.email;
023
024import java.io.StringWriter;
025import java.io.Writer;
026
027import org.nuxeo.ecm.platform.rendering.RenderingContext;
028import org.nuxeo.ecm.platform.rendering.RenderingResult;
029import org.nuxeo.ecm.platform.rendering.impl.DefaultRenderingResult;
030import org.nuxeo.ecm.platform.rendering.template.DocumentRenderingEngine;
031import org.nuxeo.ecm.platform.rendering.template.FreemarkerRenderingJob;
032import freemarker.template.Configuration;
033
034/**
035 * @author <a href="mailto:npaslaru@nuxeo.com">Narcis Paslaru</a>
036 */
037public class NotificationsRenderingEngine extends DocumentRenderingEngine {
038
039    private final String template;
040
041    public NotificationsRenderingEngine(String template) {
042        this.template = template;
043    }
044
045    @Override
046    public Configuration createConfiguration() {
047        Configuration cfg = super.createConfiguration();
048        cfg.setSharedVariable("htmlEscape", new HtmlEscapeMethod());
049        return cfg;
050    }
051
052    @Override
053    protected FreemarkerRenderingJob createJob(RenderingContext ctx) {
054        return new NotifsRenderingJob("ftl");
055    }
056
057    public String getFormatName() {
058        // TODO Auto-generated method stub
059        return null;
060    }
061
062    class NotifsRenderingJob extends DefaultRenderingResult implements FreemarkerRenderingJob {
063
064        private static final long serialVersionUID = -7133062841713259967L;
065
066        final Writer strWriter = new StringWriter();
067
068        NotifsRenderingJob(String formatName) {
069            super(formatName);
070        }
071
072        @Override
073        public Object getOutcome() {
074            return strWriter.toString();
075        }
076
077        public RenderingResult getResult() {
078            return this;
079        }
080
081        public String getTemplate() {
082            return template;
083        }
084
085        public Writer getWriter() {
086            return strWriter;
087        }
088    }
089
090}