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.string.interpolator;
18:
19: import org.apache.wicket.util.lang.PropertyResolver;
20:
21: /**
22: * Interpolates values into strings that are produced by interpreting property
23: * expressions against a beans model.
24: * <p>
25: * The interpolate(String string, Object model) method takes a string such as
26: * "My name is ${name}" and a beans model such as a Person, and reflects on the
27: * object using any property expressions found inside ${} markers in the string.
28: * In this case, if the Person model had a getName() method, the results of
29: * calling that method would be substituted for ${name}. If getName() returned
30: * Jonathan, interpolate() would then return "My name is Jonathan".
31: *
32: * @author Jonathan Locke
33: */
34: public final class PropertyVariableInterpolator extends
35: VariableInterpolator {
36: /** The model to introspect on */
37: private final Object model;
38:
39: /**
40: * Private constructor to force use of static interpolate method
41: *
42: * @param string
43: * The string to interpolate into
44: * @param model
45: * The model to apply property expressions to
46: */
47: private PropertyVariableInterpolator(final String string,
48: final Object model) {
49: super (string);
50: this .model = model;
51: }
52:
53: /**
54: * Interpolates string, substituting values for property expressions
55: *
56: * @param string
57: * String containing property expressions like ${xyz}
58: * @param object
59: * The object to reflect on
60: * @return The interpolated string
61: */
62: public static String interpolate(final String string,
63: final Object object) {
64: // If there's any reason to go to the expense of property expressions
65: if (string.indexOf("${") != -1) {
66: // Do property expression interpolation
67: return new PropertyVariableInterpolator(string, object)
68: .toString();
69: }
70:
71: // Return simple string
72: return string;
73: }
74:
75: /**
76: * Gets a value for a variable name during interpolation
77: *
78: * @param variableName
79: * The variable
80: * @return The value
81: */
82: protected String getValue(final String variableName) {
83: Object value = PropertyResolver.getValue(variableName, model);
84: return (value != null) ? value.toString() : null;
85: }
86: }
|