KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > util > variable > VariableExpander


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.util.variable;
8
9
10 /**
11  * <p>
12  * This class can be used to expand variables in a String.
13  * The standardization of variables in Java and all but
14  * been written down. Variables generally take the form:
15  * </p>
16  *
17  * <pre>${variableName}</pre>
18  *
19  * <p>
20  * However, how the variableName is translated into a String
21  * value is determined by the application or the context.
22  * This class simplifies this by allowing callers to pass
23  * in the method of variable expansion using the {@link
24  * ExpanderStrategy ExpanderStrategy} interface.
25  * </p>
26  *
27  * @author Brian Pontarelli
28  * @since 1.0
29  * @version 1.0
30  */

31 public final class VariableExpander {
32
33     /**
34      * The start of a variable String
35      */

36     public final static String JavaDoc START = "${";
37
38     /**
39      * The end of a variable String
40      */

41     public final static char END = '}';
42
43
44     /**
45      * <p>
46      * Parses the given String and for each variable within the String calls the
47      * expand method of the given {@link ExpanderStrategy ExpanderStrategy}. If
48      * the strategy throws an exception, it is not caught and re-thrown but
49      * simply allowed to be thrown through to the caller.
50      * </p>
51      * <p>
52      * If nulls are returned from the ExpanderStrategy, they are replaced by the
53      * word <code>null</code> in the returned String. Therefore, the strategy
54      * should be intelligent enough to know what the caller desires for null
55      * expansion.
56      * </p>
57      *
58      * @param str The String to expand the variables in
59      * @param strategy The ExpanderStrategy to use for this expansion
60      * @return The fully expanded String
61      * @throws ExpanderException If the ExpanderStrategy throws an ExpanderException
62      */

63     public static String JavaDoc expand(String JavaDoc str, ExpanderStrategy strategy)
64     throws ExpanderException {
65         assert (strategy != null) : "strategy == null";
66
67         if (str == null) {
68             return null;
69         }
70
71         int startIndex = str.indexOf(START);
72         int endIndex = -1;
73         if (startIndex != -1) {
74             endIndex = str.indexOf(END, startIndex + 2);
75         }
76
77         if (startIndex == -1 || endIndex == -1) {
78             return str;
79         }
80
81         int length = str.length();
82         int curPos = 0;
83         String JavaDoc value;
84         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(length);
85         while (startIndex != -1 && endIndex != -1) {
86             value = strategy.expand(str.substring(startIndex + 2, endIndex));
87
88             buf.append(str.substring(curPos, startIndex));
89             buf.append(value);
90
91             curPos = endIndex + 1;
92
93             startIndex = str.indexOf(START, curPos);
94             if (startIndex != -1) {
95                 endIndex = str.indexOf(END, startIndex + 2);
96             }
97         }
98
99         if (curPos < length) {
100             buf.append(str.substring(curPos));
101         }
102
103         return buf.toString();
104     }
105
106     /**
107      * <p>
108      * Parses the given StringBuffer and for each variable within the String calls
109      * the expand method of the given {@link ExpanderStrategy ExpanderStrategy}.
110      * If the strategy throws an exception, it is not caught and re-thrown but
111      * simply allowed to be thrown through to the caller.
112      * </p>
113      * <p>
114      * If nulls are returned from the ExpanderStrategy, they are replaced by the
115      * word <code>null</code> in the returned String. Therefore, the strategy
116      * should be intelligent enough to know what the caller desires for null
117      * expansion.
118      * </p>
119      * <p>
120      * This method does not affect the contents of the StringBuffer passed in.
121      * It makes a duplicate locally
122      * </p>
123      *
124      * @param buf The StringBuffer to expand the variables in
125      * @param strategy The ExpanderStrategy to use for this expansion
126      * @return The fully expanded String
127      * @throws ExpanderException If the ExpanderStrategy throws an ExpanderException
128      */

129     public static String JavaDoc expand(StringBuffer JavaDoc buf, ExpanderStrategy strategy)
130     throws ExpanderException {
131         return expand(buf.toString(), strategy);
132     }
133 }
134
135
Popular Tags