Source Code Cross Referenced for PackagedTextTemplate.java in  » J2EE » wicket » wicket » extensions » util » resource » 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 » wicket » wicket.extensions.util.resource 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: StringBufferResourceStream.java 3307 2005-11-30 15:57:34 -0800 (Wed, 30
003:         * Nov 2005) ivaynberg $ $Revision: 3307 $ $Date: 2005-11-30 15:57:34 -0800
004:         * (Wed, 30 Nov 2005) $
005:         * 
006:         * ==============================================================================
007:         * Licensed under the Apache License, Version 2.0 (the "License"); you may not
008:         * use this file except in compliance with the License. You may obtain a copy of
009:         * the License at
010:         * 
011:         * http://www.apache.org/licenses/LICENSE-2.0
012:         * 
013:         * Unless required by applicable law or agreed to in writing, software
014:         * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
015:         * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
016:         * License for the specific language governing permissions and limitations under
017:         * the License.
018:         */
019:        package wicket.extensions.util.resource;
020:
021:        import java.io.IOException;
022:        import java.util.Map;
023:
024:        import org.apache.commons.logging.Log;
025:        import org.apache.commons.logging.LogFactory;
026:
027:        import wicket.util.io.Streams;
028:        import wicket.util.lang.Packages;
029:        import wicket.util.resource.IResourceStream;
030:        import wicket.util.resource.ResourceStreamNotFoundException;
031:        import wicket.util.resource.locator.ClassLoaderResourceStreamLocator;
032:        import wicket.util.string.interpolator.MapVariableInterpolator;
033:
034:        /**
035:         * A string resource that can be appended to.
036:         * 
037:         * @author Eelco Hillenius
038:         */
039:        // TODO cache templates application scoped with a watch
040:        public class PackagedTextTemplate extends TextTemplate {
041:            private static final long serialVersionUID = 1L;
042:
043:            /** log. */
044:            private static final Log log = LogFactory
045:                    .getLog(PackagedTextTemplate.class);
046:
047:            /** class loader stream locator. */
048:            private static final ClassLoaderResourceStreamLocator streamLocator = new ClassLoaderResourceStreamLocator();
049:
050:            /** contents */
051:            private StringBuffer buffer = new StringBuffer();
052:
053:            /**
054:             * Constructor.
055:             * 
056:             * @param clazz
057:             *            The class to be used for retrieving the classloader for
058:             *            loading the packaged template.
059:             * @param fileName
060:             *            The name of the file, relative to the clazz position
061:             */
062:            public PackagedTextTemplate(final Class clazz, final String fileName) {
063:                this (clazz, fileName, "text");
064:            }
065:
066:            /**
067:             * Constructor.
068:             * 
069:             * @param clazz
070:             *            The class to be used for retrieving the classloader for
071:             *            loading the packaged template.
072:             * @param fileName
073:             *            the name of the file, relative to the clazz position
074:             * @param contentType
075:             *            The mime type of this resource, such as "image/jpeg" or
076:             *            "text/html"
077:             */
078:            public PackagedTextTemplate(final Class clazz,
079:                    final String fileName, final String contentType) {
080:                this (clazz, fileName, contentType, null);
081:            }
082:
083:            /**
084:             * Constructor.
085:             * 
086:             * @param clazz
087:             *            The class to be used for retrieving the classloader for
088:             *            loading the packaged template.
089:             * @param fileName
090:             *            the name of the file, relative to the clazz position
091:             * @param contentType
092:             *            The mime type of this resource, such as "image/jpeg" or
093:             *            "text/html"
094:             * @param encoding
095:             *            The file's encoding, e.g. 'UTF-8'
096:             */
097:            public PackagedTextTemplate(final Class clazz,
098:                    final String fileName, final String contentType,
099:                    final String encoding) {
100:                super (contentType);
101:
102:                String path = Packages.absolutePath(clazz, fileName);
103:                IResourceStream stream = streamLocator.locate(clazz, path);
104:
105:                if (stream == null) {
106:                    throw new IllegalArgumentException("resource " + fileName
107:                            + " not found for scope " + clazz + " (path = "
108:                            + path + ")");
109:                }
110:
111:                try {
112:                    if (encoding != null) {
113:                        buffer.append(Streams.readString(stream
114:                                .getInputStream(), encoding));
115:                    } else {
116:                        buffer.append(Streams.readString(stream
117:                                .getInputStream()));
118:                    }
119:                } catch (IOException e) {
120:                    throw new RuntimeException(e);
121:                } catch (ResourceStreamNotFoundException e) {
122:                    throw new RuntimeException(e);
123:                } finally {
124:                    if (stream != null) {
125:                        try {
126:                            stream.close();
127:                        } catch (IOException e) {
128:                            log.error(e.getMessage(), e);
129:                        }
130:                    }
131:                }
132:            }
133:
134:            /**
135:             * Interpolate the map of variables with the content and replace the content
136:             * with the result. Variables are denoted in this string by the syntax
137:             * ${variableName}. The contents will be altered by replacing each variable
138:             * of the form ${variableName} with the value returned by
139:             * variables.getValue("variableName").
140:             * <p>
141:             * WARNING there is no going back to the original contents after the
142:             * interpolation is done. if you need to do different interpolations on the
143:             * same original contents, use method {@link #asString(Map)} instead.
144:             * </p>
145:             * 
146:             * @param variables
147:             *            The variables to interpolate
148:             * @return This for chaining
149:             */
150:            public final TextTemplate interpolate(Map variables) {
151:                if (variables != null) {
152:                    String result = new MapVariableInterpolator(buffer
153:                            .toString(), variables).toString();
154:                    buffer.delete(0, buffer.length());
155:                    buffer.append(result);
156:                }
157:                return this ;
158:            }
159:
160:            /**
161:             * @see wicket.util.resource.IResourceStream#length()
162:             */
163:            public final long length() {
164:                return buffer.length();
165:            }
166:
167:            /**
168:             * @see wicket.util.resource.AbstractStringResourceStream#getString()
169:             */
170:            public String getString() {
171:                return buffer.toString();
172:            }
173:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.