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