001: /*
002: * $Id: VariableInterpolator.java 455983 2005-02-22 17:48:25Z jonl $
003: * $Revision: 455983 $ $Date: 2005-02-22 18:48:25 +0100 (Tue, 22 Feb 2005) $
004: *
005: * ==============================================================================
006: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007: * use this file except in compliance with the License. You may obtain a copy of
008: * the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package wicket.util.string.interpolator;
019:
020: /**
021: * Base class for variable interpolators. An interpolator substitutes values
022: * into a string. So a variable interpolator substitutes the values of one or
023: * more variables into a string.
024: * <p>
025: * The String to interpolate (substitute in) is passed to the
026: * VariableInterpolator constructor. Variables are denoted in this string by the
027: * syntax ${variableName}. A subclass provides an implementation for the
028: * abstract method getValue(String variableName). The toString() method then
029: * performs an interpolation by replacing each variable of the form
030: * ${variableName} with the value returned by getValue("variableName").
031: *
032: * @author Jonathan Locke
033: */
034: public abstract class VariableInterpolator {
035: /** The string to interpolate within */
036: protected final String string;
037:
038: /**
039: * Constructor
040: *
041: * @param string
042: * String to interpolate with variable values
043: */
044: public VariableInterpolator(final String string) {
045: this .string = string;
046: }
047:
048: /**
049: * Gets a value for a variable name during interpolation
050: *
051: * @param variableName
052: * The variable
053: * @return The value
054: */
055: protected abstract String getValue(String variableName);
056:
057: /**
058: * Interpolate using variables
059: *
060: * @return The interpolated string
061: */
062: public String toString() {
063: // Result buffer
064: final StringBuffer buffer = new StringBuffer();
065:
066: // For each occurrences of "${"
067: int start;
068: int pos = 0;
069:
070: while ((start = string.indexOf("${", pos)) != -1) {
071: // Append text before possible variable
072: buffer.append(string.substring(pos, start));
073:
074: // Position is now where we found the "${"
075: pos = start;
076:
077: // Get start and end of variable name
078: final int startVariableName = start + 2;
079: final int endVariableName = string.indexOf('}',
080: startVariableName);
081:
082: // Found a close brace?
083: if (endVariableName != -1) {
084: // Get variable name inside brackets
085: final String variableName = string.substring(
086: startVariableName, endVariableName);
087:
088: // Get value of variable
089: final String value = getValue(variableName);
090:
091: // If there's no value
092: if (value == null) {
093: // Leave variable uninterpolated, allowing multiple
094: // interpolators to
095: // do their work on the same string
096: buffer.append("${" + variableName + "}");
097: } else {
098: // Append variable value
099: buffer.append(value);
100: }
101:
102: // Move past variable
103: pos = endVariableName + 1;
104: } else {
105: break;
106: }
107: }
108:
109: // Append anything that might be left
110: if (pos < string.length()) {
111: buffer.append(string.substring(pos));
112: }
113:
114: // Convert result to String
115: return buffer.toString();
116: }
117: }
|