01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.wicket.util.template;
18:
19: import java.util.Map;
20:
21: import org.apache.wicket.util.resource.AbstractStringResourceStream;
22: import org.apache.wicket.util.string.interpolator.MapVariableInterpolator;
23:
24: /**
25: * Represents a text template that can do variable interpolation.
26: *
27: * @author Eelco Hillenius
28: * @author Jonathan Locke
29: */
30: public abstract class TextTemplate extends AbstractStringResourceStream {
31: private static final long serialVersionUID = 1L;
32:
33: /**
34: * Construct.
35: */
36: public TextTemplate() {
37: }
38:
39: /**
40: * Construct.
41: *
42: * @param contentType
43: * The mime type of this resource, such as "image/jpeg" or
44: * "text/html".
45: */
46: public TextTemplate(String contentType) {
47: super (contentType);
48: }
49:
50: /**
51: * Interpolate the map of variables with the content and return the
52: * resulting string without replacing the content. Variables are denoted in
53: * this string by the syntax ${variableName}. The contents will be altered
54: * by replacing each variable of the form ${variableName} with the value
55: * returned by variables.getValue("variableName").
56: *
57: * @param variables
58: * The variables to interpolate
59: * @return the result of the interpolation
60: */
61: public String asString(Map variables) {
62: if (variables != null) {
63: return new MapVariableInterpolator(getString(), variables)
64: .toString();
65: }
66: return getString();
67: }
68:
69: /**
70: * @see org.apache.wicket.util.resource.AbstractResourceStream#asString()
71: */
72: public String asString() {
73: return getString();
74: }
75:
76: /**
77: * Gets the string resource.
78: *
79: * @return The string resource
80: */
81: public abstract String getString();
82:
83: /**
84: * Interpolates values into this text template.
85: *
86: * @param variables
87: * Variables to interpolate into this text template
88: * @return This for chaining
89: */
90: public abstract TextTemplate interpolate(Map variables);
91: }
|