001: /*
002: * Copyright 2005-2007 Noelios Consulting.
003: *
004: * The contents of this file are subject to the terms of the Common Development
005: * and Distribution License (the "License"). You may not use this file except in
006: * compliance with the License.
007: *
008: * You can obtain a copy of the license at
009: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
010: * language governing permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL HEADER in each file and
013: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
014: * applicable, add the following below this CDDL HEADER, with the fields
015: * enclosed by brackets "[]" replaced with your own identifying information:
016: * Portions Copyright [yyyy] [name of copyright owner]
017: */
018:
019: package org.restlet.ext.freemarker;
020:
021: import java.io.BufferedWriter;
022: import java.io.IOException;
023: import java.io.OutputStream;
024: import java.io.OutputStreamWriter;
025: import java.io.Writer;
026:
027: import org.restlet.data.MediaType;
028: import org.restlet.resource.OutputRepresentation;
029:
030: import freemarker.template.Configuration;
031: import freemarker.template.Template;
032: import freemarker.template.TemplateException;
033:
034: /**
035: * FreeMarker template representation. Useful for dynamic string-based
036: * representations.
037: *
038: * @see <a href="http://freemarker.org/">FreeMarker home page</a>
039: * @author Jerome Louvel (contact@noelios.com)
040: */
041: public class TemplateRepresentation extends OutputRepresentation {
042: /** The template's name. */
043: private String templateName;
044:
045: /** The FreeMarker configuration. */
046: private Configuration config;
047:
048: /** The template's data model. */
049: private Object dataModel;
050:
051: /**
052: * Constructor.
053: *
054: * @param templateName
055: * The FreeMarker template's name. The full path is resolved by
056: * the configuration.
057: * @param config
058: * The FreeMarker configuration.
059: * @param dataModel
060: * The template's data model.
061: * @param mediaType
062: * The representation's media type.
063: */
064: public TemplateRepresentation(String templateName,
065: Configuration config, Object dataModel, MediaType mediaType) {
066: super (mediaType);
067: this .config = config;
068: this .dataModel = dataModel;
069: this .templateName = templateName;
070: }
071:
072: /**
073: * Returns the template's data model.
074: *
075: * @return The template's data model.
076: */
077: public Object getDataModel() {
078: return this .dataModel;
079: }
080:
081: /**
082: * Sets the template's data model.
083: *
084: * @param dataModel
085: * The template's data model.
086: * @return The template's data model.
087: */
088: public Object setDataModel(Object dataModel) {
089: this .dataModel = dataModel;
090: return dataModel;
091: }
092:
093: /**
094: * Writes the datum as a stream of bytes.
095: *
096: * @param outputStream
097: * The stream to use when writing.
098: */
099: public void write(OutputStream outputStream) throws IOException {
100: Writer tmplWriter = null;
101:
102: try {
103: Template template = config.getTemplate(templateName);
104: if (getCharacterSet() != null) {
105: tmplWriter = new BufferedWriter(new OutputStreamWriter(
106: outputStream, getCharacterSet().getName()));
107: } else {
108: tmplWriter = new BufferedWriter(new OutputStreamWriter(
109: outputStream, template.getEncoding()));
110: }
111:
112: template.process(getDataModel(), tmplWriter);
113: tmplWriter.flush();
114: } catch (TemplateException te) {
115: throw new IOException("Template processing error "
116: + te.getMessage());
117: }
118: }
119:
120: }
|