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.velocity;
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: import java.util.Map;
027: import java.util.TreeMap;
028:
029: import org.apache.velocity.Template;
030: import org.apache.velocity.VelocityContext;
031: import org.apache.velocity.app.VelocityEngine;
032: import org.restlet.data.MediaType;
033: import org.restlet.resource.OutputRepresentation;
034:
035: /**
036: * Velocity template representation. Useful for dynamic string-based
037: * representations.
038: *
039: * @see <a href="http://velocity.apache.org/">Velocity home page</a>
040: * @author Jerome Louvel (contact@noelios.com)
041: */
042: public class TemplateRepresentation extends OutputRepresentation {
043: /** The template's name. */
044: private String templateName;
045:
046: /** The Velocity engine. */
047: private VelocityEngine engine;
048:
049: /** The template's data model. */
050: private Map<String, Object> dataModel;
051:
052: /**
053: * Constructor.
054: *
055: * @param templateName
056: * The Velocity template's name. The full path is resolved by
057: * the configuration.
058: * @param mediaType
059: * The representation's media type.
060: */
061: public TemplateRepresentation(String templateName,
062: MediaType mediaType) {
063: this (templateName, new TreeMap<String, Object>(), mediaType);
064: }
065:
066: /**
067: * Constructor.
068: *
069: * @param templateName
070: * The Velocity template's name. The full path is resolved by
071: * the configuration.
072: * @param dataModel
073: * The Velocity template's data model.
074: * @param mediaType
075: * The representation's media type.
076: */
077: public TemplateRepresentation(String templateName,
078: Map<String, Object> dataModel, MediaType mediaType) {
079: super (mediaType);
080: this .engine = new VelocityEngine();
081: this .dataModel = dataModel;
082: this .templateName = templateName;
083: }
084:
085: /**
086: * Returns the Velocity engine.
087: *
088: * @return The Velocity engine.
089: */
090: public VelocityEngine getEngine() {
091: return this .engine;
092: }
093:
094: /**
095: * Returns the template's data model.
096: *
097: * @return The template's data model.
098: */
099: public Map<String, Object> getDataModel() {
100: return this .dataModel;
101: }
102:
103: /**
104: * Sets the template's data model.
105: *
106: * @param dataModel
107: * The template's data model.
108: * @return The template's data model.
109: */
110: public Map<String, Object> setDataModel(
111: Map<String, Object> dataModel) {
112: this .dataModel = dataModel;
113: return dataModel;
114: }
115:
116: /**
117: * Writes the datum as a stream of bytes.
118: *
119: * @param outputStream
120: * The stream to use when writing.
121: */
122: public void write(OutputStream outputStream) throws IOException {
123: Writer tmplWriter = null;
124:
125: try {
126: // Initialize the engine
127: getEngine().init();
128:
129: // Create the context
130: VelocityContext context = new VelocityContext(
131: getDataModel());
132:
133: // Load the template
134: Template template = engine.getTemplate(templateName);
135: if (getCharacterSet() != null) {
136: tmplWriter = new BufferedWriter(new OutputStreamWriter(
137: outputStream, getCharacterSet().getName()));
138: } else {
139: tmplWriter = new BufferedWriter(new OutputStreamWriter(
140: outputStream, template.getEncoding()));
141: }
142:
143: // Process the template
144: template.merge(context, tmplWriter);
145: tmplWriter.flush();
146: } catch (Exception e) {
147: throw new IOException("Template processing error. "
148: + e.getMessage());
149: }
150: }
151:
152: }
|