Source Code Cross Referenced for TemplateRepresentation.java in  » Web-Services » restlet-1.0.8 » org » restlet » ext » velocity » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Services » restlet 1.0.8 » org.restlet.ext.velocity 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.