001/*
002 * (C) Copyright 2006-2011 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 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.rendering.wiki;
023
024import java.io.BufferedReader;
025import java.io.File;
026import java.io.IOException;
027import java.io.InputStreamReader;
028import java.io.Reader;
029import java.io.StringReader;
030import java.io.StringWriter;
031import java.io.Writer;
032import java.net.URL;
033import java.util.Map;
034
035import org.nuxeo.ecm.core.api.model.Property;
036import org.nuxeo.ecm.platform.rendering.api.RenderingException;
037import org.nuxeo.ecm.platform.rendering.fm.FreemarkerEngine;
038import org.nuxeo.ecm.platform.rendering.fm.adapters.ComplexPropertyTemplate;
039import org.nuxeo.ecm.platform.rendering.wiki.extensions.FreemarkerMacro;
040import org.wikimodel.wem.WikiParserException;
041
042import freemarker.core.Environment;
043import freemarker.template.SimpleScalar;
044import freemarker.template.TemplateDirectiveBody;
045import freemarker.template.TemplateDirectiveModel;
046import freemarker.template.TemplateException;
047import freemarker.template.TemplateModel;
048import freemarker.template.TemplateModelException;
049
050/**
051 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
052 */
053public class WikiTransformer implements TemplateDirectiveModel {
054
055    protected final WikiSerializer serializer;
056
057    public WikiTransformer() {
058        this(new WikiSerializer());
059    }
060
061    public WikiTransformer(WikiSerializer serializer) {
062        this.serializer = serializer;
063        this.serializer.registerMacro(new FreemarkerMacro());
064        // TODO implement and register a JEXL extension
065    }
066
067    public WikiSerializer getSerializer() {
068        return serializer;
069    }
070
071    public void transform(Reader reader, Writer writer) throws RenderingException {
072        try {
073            serializer.serialize(reader, writer);
074        } catch (IOException | WikiParserException e) {
075            throw new RenderingException(e);
076        }
077    }
078
079    public void transform(URL url, Writer writer) throws RenderingException {
080        try (Reader reader = new BufferedReader(new InputStreamReader(url.openStream()))) {
081            transform(reader, writer);
082        } catch (IOException e) {
083            throw new RenderingException(e);
084        }
085    }
086
087    @Override
088    @SuppressWarnings("rawtypes")
089    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
090            throws TemplateException, IOException {
091
092        // TODO: not used for now.
093        String syntax = null;
094        SimpleScalar scalar = (SimpleScalar) params.get("syntax");
095        if (scalar != null) {
096            syntax = scalar.getAsString();
097        }
098
099        scalar = (SimpleScalar) params.get("src");
100        String src = null;
101        if (scalar != null) {
102            src = scalar.getAsString();
103        }
104
105        ComplexPropertyTemplate complex = (ComplexPropertyTemplate) params.get("property");
106        Property property = null;
107        if (complex != null) {
108            property = (Property) complex.getAdaptedObject(null);
109        }
110
111        FreemarkerEngine engine = (FreemarkerEngine) env.getCustomAttribute(FreemarkerEngine.RENDERING_ENGINE_KEY);
112        if (engine == null) {
113            throw new TemplateModelException("Not in a nuxeo rendering context");
114        }
115
116        try {
117            if (property != null) {
118                // TODO XXX implement property support (with caching)
119                throw new UnsupportedOperationException("Not Yet Implemented");
120                // URL url = PropertyURL.getURL(ctxModel.getDocument(), property.getPath());
121                // tr.transform(url, env.getOut(), ctxModel.getContext());
122            } else if (src == null) {
123                if (body == null) {
124                    throw new TemplateModelException(
125                            "Transform directive must have either a content either a valid 'src' attribute");
126                }
127                // render body to get back the result
128                StringWriter writer = new StringWriter();
129                body.render(writer);
130                String content = writer.getBuffer().toString();
131                transform(new StringReader(content), env.getOut());
132            } else {
133                if (src.contains(":/")) {
134                    URL url = engine.getResourceLocator().getResourceURL(src);
135                    if (url != null) {
136                        transform(url, env.getOut());
137                    } else {
138                        throw new IllegalArgumentException("Cannot resolve the src attribute: " + src);
139                    }
140                } else {
141                    File file = engine.getResourceLocator().getResourceFile(src);
142                    if (file != null) {
143                        transform(file.toURI().toURL(), env.getOut());
144                    } else {
145                        throw new IllegalArgumentException("Cannot resolve the src attribute: " + src);
146                    }
147                }
148            }
149        } catch (RenderingException e) {
150            throw new TemplateException("Running wiki transformer failed", e, env);
151        }
152    }
153
154}