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: }
|