1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package nl.hippo.portal.util;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.InputStreamReader;
21 import java.io.Reader;
22 import java.io.StringReader;
23 import java.util.Map;
24
25 /***
26 * Utility class which can replace embedded ${<propertyname>} type properties in files or strings.
27 *
28 * @author adouma
29 * @version $Id: PropertiesReplacer.java 6122 2007-04-18 14:34:48Z pduin $
30 */
31 public class PropertiesReplacer
32 {
33 /***
34 * Replace embedded ${<propertyname>} type properties with a provided map of propertyname/value objects.
35 * <br/>
36 * Delagates to {@link #replaceProperties(Reader, Map)} using an InputStreamReader wrapping the InputStream input parameter.
37 * <br/>
38 * @param in the InputStream input
39 * @param properties map containing the property/value objects
40 * @return String containing replaced properties.
41 * @throws IOException
42 */
43 public static String replaceProperties(InputStream in, Map properties)
44 throws IOException
45 {
46 return replaceProperties(new InputStreamReader(in),properties);
47 }
48
49 /***
50 * Replace embedded ${<propertyname>} type properties with a provided map of propertyname/value objects.
51 * <br/>
52 * Delagates to {@link #replaceProperties(Reader, Map)} using an StringReader wrapping the String input parameter.
53 * <br/>
54 * @param in the String input
55 * @param properties map containing the property/value objects
56 * @return String containing replaced properties.
57 * @throws IOException
58 */
59 public static String replaceProperties(String in, Map properties)
60 throws IOException
61 {
62 return replaceProperties(new StringReader(in),properties);
63 }
64
65 /***
66 * Replace embedded ${<propertyname>} type properties with a provided map of propertyname/value objects.
67 * <br/>
68 * The property value is written out using the Object.toString() method.
69 * <br/>
70 * @param in the Reader input
71 * @param properties map containing the property/value objects
72 * @return String containing replaced properties.
73 * @throws IOException
74 */
75 public static String replaceProperties(Reader in, Map properties)
76 throws IOException
77 {
78 StringBuffer buffer = new StringBuffer();
79 StringBuffer property = null;
80
81 int i;
82 char ch;
83
84 while ( ( i = in.read() ) != -1 )
85 {
86 ch = (char)i;
87 if ( ch != '$' )
88 {
89 buffer.append(ch);
90 }
91 else
92 {
93 if ( ( i = in.read() ) != -1 )
94 {
95 ch = (char)i;
96 if ( ch == '{' )
97 {
98
99 if ( property == null )
100 {
101 property = new StringBuffer();
102 }
103 else
104 {
105 property.setLength(0);
106 }
107 boolean found = false;
108 int len = 0;
109 while ( ( i = in.read() ) != -1 )
110 {
111 ch = (char)i;
112 if ( ch =='}'||Character.isWhitespace(ch))
113 {
114 break;
115 }
116 property.append(ch);
117 len++;
118 }
119 if ( len > 0 && i != -1 && ch == '}' )
120 {
121 Object value = properties.get(property.toString());
122 if ( value != null )
123 {
124 buffer.append(value.toString());
125 found = true;
126 }
127 }
128 if ( !found || i == -1 || ch != '}' )
129 {
130 buffer.append("${");
131 if ( len > 0 )
132 {
133 buffer.append(property.toString());
134 }
135 if ( i != -1 )
136 {
137 buffer.append(ch);
138 }
139 }
140 if ( i == -1 )
141 {
142 break;
143 }
144 }
145 else
146 {
147 buffer.append('$');
148 buffer.append(ch);
149 }
150 }
151 else
152 {
153 break;
154 }
155 }
156 }
157 return buffer.toString();
158 }
159
160 }