Source Code Cross Referenced for VelocityEngineUtils.java in  » J2EE » spring-framework-2.0.6 » org » springframework » ui » 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 » J2EE » spring framework 2.0.6 » org.springframework.ui.velocity 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2002-2006 the original author or authors.
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:
017:        package org.springframework.ui.velocity;
018:
019:        import java.io.StringWriter;
020:        import java.io.Writer;
021:        import java.util.Map;
022:
023:        import org.apache.commons.logging.Log;
024:        import org.apache.commons.logging.LogFactory;
025:        import org.apache.velocity.VelocityContext;
026:        import org.apache.velocity.app.VelocityEngine;
027:        import org.apache.velocity.exception.VelocityException;
028:
029:        /**
030:         * Utility class for working with a VelocityEngine.
031:         * Provides convenience methods to merge a Velocity template with a model.
032:         *
033:         * @author Juergen Hoeller
034:         * @since 22.01.2004
035:         */
036:        public abstract class VelocityEngineUtils {
037:
038:            private static final Log logger = LogFactory
039:                    .getLog(VelocityEngineUtils.class);
040:
041:            /**
042:             * Merge the specified Velocity template with the given model and write
043:             * the result to the given Writer.
044:             * @param velocityEngine VelocityEngine to work with
045:             * @param templateLocation the location of template, relative to Velocity's
046:             * resource loader path
047:             * @param model the Map that contains model names as keys and model objects
048:             * as values
049:             * @param writer the Writer to write the result to
050:             * @throws VelocityException if the template wasn't found or rendering failed
051:             */
052:            public static void mergeTemplate(VelocityEngine velocityEngine,
053:                    String templateLocation, Map model, Writer writer)
054:                    throws VelocityException {
055:
056:                try {
057:                    VelocityContext velocityContext = new VelocityContext(model);
058:                    velocityEngine.mergeTemplate(templateLocation,
059:                            velocityContext, writer);
060:                } catch (VelocityException ex) {
061:                    throw ex;
062:                } catch (RuntimeException ex) {
063:                    throw ex;
064:                } catch (Exception ex) {
065:                    logger
066:                            .error(
067:                                    "Why does VelocityEngine throw a generic checked exception, after all?",
068:                                    ex);
069:                    throw new VelocityException(ex.toString());
070:                }
071:            }
072:
073:            /**
074:             * Merge the specified Velocity template with the given model and write
075:             * the result to the given Writer.
076:             * @param velocityEngine VelocityEngine to work with
077:             * @param templateLocation the location of template, relative to Velocity's
078:             * resource loader path
079:             * @param encoding the encoding of the template file
080:             * @param model the Map that contains model names as keys and model objects
081:             * as values
082:             * @param writer the Writer to write the result to
083:             * @throws VelocityException if the template wasn't found or rendering failed
084:             */
085:            public static void mergeTemplate(VelocityEngine velocityEngine,
086:                    String templateLocation, String encoding, Map model,
087:                    Writer writer) throws VelocityException {
088:
089:                try {
090:                    VelocityContext velocityContext = new VelocityContext(model);
091:                    velocityEngine.mergeTemplate(templateLocation, encoding,
092:                            velocityContext, writer);
093:                } catch (VelocityException ex) {
094:                    throw ex;
095:                } catch (RuntimeException ex) {
096:                    throw ex;
097:                } catch (Exception ex) {
098:                    logger
099:                            .error(
100:                                    "Why does VelocityEngine throw a generic checked exception, after all?",
101:                                    ex);
102:                    throw new VelocityException(ex.toString());
103:                }
104:            }
105:
106:            /**
107:             * Merge the specified Velocity template with the given model into a String.
108:             * <p>When using this method to prepare a text for a mail to be sent with Spring's
109:             * mail support, consider wrapping VelocityException in MailPreparationException.
110:             * @param velocityEngine VelocityEngine to work with
111:             * @param templateLocation the location of template, relative to Velocity's
112:             * resource loader path
113:             * @param model the Map that contains model names as keys and model objects
114:             * as values
115:             * @return the result as String
116:             * @throws VelocityException if the template wasn't found or rendering failed
117:             * @see org.springframework.mail.MailPreparationException
118:             */
119:            public static String mergeTemplateIntoString(
120:                    VelocityEngine velocityEngine, String templateLocation,
121:                    Map model) throws VelocityException {
122:
123:                StringWriter result = new StringWriter();
124:                mergeTemplate(velocityEngine, templateLocation, model, result);
125:                return result.toString();
126:            }
127:
128:            /**
129:             * Merge the specified Velocity template with the given model into a String.
130:             * <p>When using this method to prepare a text for a mail to be sent with Spring's
131:             * mail support, consider wrapping VelocityException in MailPreparationException.
132:             * @param velocityEngine VelocityEngine to work with
133:             * @param templateLocation the location of template, relative to Velocity's
134:             * resource loader path
135:             * @param encoding the encoding of the template file
136:             * @param model the Map that contains model names as keys and model objects
137:             * as values
138:             * @return the result as String
139:             * @throws VelocityException if the template wasn't found or rendering failed
140:             * @see org.springframework.mail.MailPreparationException
141:             */
142:            public static String mergeTemplateIntoString(
143:                    VelocityEngine velocityEngine, String templateLocation,
144:                    String encoding, Map model) throws VelocityException {
145:
146:                StringWriter result = new StringWriter();
147:                mergeTemplate(velocityEngine, templateLocation, encoding,
148:                        model, result);
149:                return result.toString();
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.