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    public void execute(Environment env, @SuppressWarnings("rawtypes") Map params, TemplateModel[] loopVars,
089            TemplateDirectiveBody body) throws TemplateException, IOException {
090
091        SimpleScalar scalar = (SimpleScalar) params.get("syntax");
092        if (scalar != null) {
093            scalar.getAsString();
094        }
095
096        scalar = (SimpleScalar) params.get("src");
097        String src = null;
098        if (scalar != null) {
099            src = scalar.getAsString();
100        }
101
102        ComplexPropertyTemplate complex = (ComplexPropertyTemplate) params.get("property");
103        Property property = null;
104        if (complex != null) {
105            property = (Property) complex.getAdaptedObject(null);
106        }
107
108        FreemarkerEngine engine = (FreemarkerEngine) env.getCustomAttribute(FreemarkerEngine.RENDERING_ENGINE_KEY);
109        if (engine == null) {
110            throw new TemplateModelException("Not in a nuxeo rendering context");
111        }
112
113        try {
114            if (property != null) {
115                // TODO XXX implement property support (with caching)
116                throw new UnsupportedOperationException("Not Yet Implemented");
117                // URL url = PropertyURL.getURL(ctxModel.getDocument(), property.getPath());
118                // tr.transform(url, env.getOut(), ctxModel.getContext());
119            } else if (src == null) {
120                if (body == null) {
121                    throw new TemplateModelException(
122                            "Transform directive must have either a content either a valid 'src' attribute");
123                }
124                // render body to get back the result
125                StringWriter writer = new StringWriter();
126                body.render(writer);
127                String content = writer.getBuffer().toString();
128                transform(new StringReader(content), env.getOut());
129            } else {
130                if (src.contains(":/")) {
131                    URL url = engine.getResourceLocator().getResourceURL(src);
132                    if (url != null) {
133                        transform(url, env.getOut());
134                    } else {
135                        throw new IllegalArgumentException("Cannot resolve the src attribute: " + src);
136                    }
137                } else {
138                    File file = engine.getResourceLocator().getResourceFile(src);
139                    if (file != null) {
140                        transform(file.toURI().toURL(), env.getOut());
141                    } else {
142                        throw new IllegalArgumentException("Cannot resolve the src attribute: " + src);
143                    }
144                }
145            }
146        } catch (RenderingException e) {
147            throw new TemplateException("Running wiki transformer failed", e, env);
148        }
149    }
150
151}