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.util.Map;
022:
023: import wicket.Component;
024: import wicket.behavior.StringHeaderContributor;
025: import wicket.model.AbstractReadOnlyDetachableModel;
026: import wicket.model.IModel;
027:
028: /**
029: * A header contributor that will contribute the contents of the given template
030: * interpolated with the provided map of variables.
031: *
032: * @author Eelco Hillenius
033: */
034: public class TextTemplateHeaderContributor extends
035: StringHeaderContributor {
036: private static final long serialVersionUID = 1L;
037:
038: /**
039: * This model holds the template and returns the interpolation of the
040: * template with of any of the
041: */
042: private static final class TemplateModel extends
043: AbstractReadOnlyDetachableModel {
044: private static final long serialVersionUID = 1L;
045:
046: /**
047: * The template to operate on and which makes the contribution.
048: */
049: private final TextTemplate template;
050:
051: /**
052: * The model that holds any variables for interpolation. It should
053: * return a {@link Map} or null.
054: */
055: private final IModel variablesModel;
056:
057: /**
058: * Construct.
059: *
060: * @param template
061: * the template to work on
062: * @param variablesModel
063: * The model that holds any variables for interpolation. It
064: * should return a {@link Map} or null.
065: */
066: protected TemplateModel(TextTemplate template,
067: IModel variablesModel) {
068: if (template == null) {
069: throw new IllegalArgumentException(
070: "argument template must be not null");
071: }
072:
073: this .template = template;
074: this .variablesModel = variablesModel;
075: }
076:
077: /**
078: * @see wicket.model.AbstractDetachableModel#getNestedModel()
079: */
080: public IModel getNestedModel() {
081: return null;
082: }
083:
084: /**
085: * @see wicket.model.AbstractDetachableModel#onAttach()
086: */
087: protected void onAttach() {
088: }
089:
090: /**
091: * @see wicket.model.AbstractDetachableModel#onDetach()
092: */
093: protected void onDetach() {
094: if (variablesModel != null) {
095: variablesModel.detach();
096: }
097: }
098:
099: /**
100: * @see wicket.model.AbstractDetachableModel#onGetObject(wicket.Component)
101: */
102: protected Object onGetObject(Component component) {
103: if (variablesModel != null) {
104: Map variables = (Map) variablesModel
105: .getObject(component);
106: if (variables != null) {
107: return template.asString(variables);
108: }
109: }
110: return template.asString();
111: }
112: }
113:
114: /**
115: * Gets a css header contributor based on the given text template. The
116: * template will be interpolated with the given variables. The content will
117: * be written as the body of a script tag pair.
118: *
119: * @param template
120: * The text template that is the base for the contribution
121: * @param variablesModel
122: * The variables to interpolate
123: * @return The header contributor instance
124: */
125: public static TextTemplateHeaderContributor forCss(
126: TextTemplate template, IModel variablesModel) {
127: return new TextTemplateHeaderContributor(new CssTemplate(
128: template), variablesModel);
129: }
130:
131: /**
132: * Gets a css header contributor that will load the template from the given
133: * file name relative to (/in the same package as) the provided clazz
134: * argument. The template will be interpolated with the given variables. The
135: * content will be written as the body of a script tag pair.
136: *
137: * @param clazz
138: * The class to be used for retrieving the classloader for
139: * loading the packaged template.
140: * @param fileName
141: * The name of the file, relative to the clazz position
142: * @param variablesModel
143: * The variables to interpolate
144: * @return The header contributor instance
145: */
146: public static TextTemplateHeaderContributor forCss(
147: final Class clazz, final String fileName,
148: IModel variablesModel) {
149: return forCss(new PackagedTextTemplate(clazz, fileName),
150: variablesModel);
151: }
152:
153: /**
154: * Gets a javascript header contributor based on the given text template.
155: * The template will be interpolated with the given variables. The content
156: * will be written as the body of a script tag pair.
157: *
158: * @param template
159: * The text template that is the base for the contribution
160: * @param variablesModel
161: * The variables to interpolate
162: * @return The header contributor instance
163: */
164: public static TextTemplateHeaderContributor forJavaScript(
165: TextTemplate template, IModel variablesModel) {
166: return new TextTemplateHeaderContributor(
167: new JavaScriptTemplate(template), variablesModel);
168: }
169:
170: /**
171: * Gets a javascript header contributor that will load the template from the
172: * given file name relative to (/in the same package as) the provided clazz
173: * argument. The template will be interpolated with the given variables. The
174: * content will be written as the body of a script tag pair.
175: *
176: * @param clazz
177: * The class to be used for retrieving the classloader for
178: * loading the packaged template.
179: * @param fileName
180: * The name of the file, relative to the clazz position
181: * @param variablesModel
182: * The variables to interpolate
183: * @return The header contributor instance
184: */
185: public static TextTemplateHeaderContributor forJavaScript(
186: final Class clazz, final String fileName,
187: IModel variablesModel) {
188: return forJavaScript(new PackagedTextTemplate(clazz, fileName),
189: variablesModel);
190: }
191:
192: /**
193: * Construct.
194: *
195: * @param template
196: * The template with the contribution
197: * @param variablesModel
198: * Optional model for variable substitution
199: */
200: protected TextTemplateHeaderContributor(TextTemplate template,
201: IModel variablesModel) {
202: super (new TemplateModel(template, variablesModel));
203: }
204: }
|