1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package org.apache.tapestry.util.text; |
16 | |
|
17 | |
import java.io.BufferedReader; |
18 | |
import java.io.IOException; |
19 | |
import java.io.InputStream; |
20 | |
import java.io.InputStreamReader; |
21 | |
import java.io.Reader; |
22 | |
import java.io.UnsupportedEncodingException; |
23 | |
import java.util.Map; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
public class LocalizedPropertiesLoader |
36 | |
{ |
37 | |
|
38 | |
private static final String HEX_DIGITS = "0123456789ABCDEF"; |
39 | |
|
40 | 0 | private static final ICharacterMatcher WHITESPACE = new WhitespaceMatcher( |
41 | |
false); |
42 | 0 | private static final ICharacterMatcher LINE_SEPARATOR = new AsciiCharacterMatcher( |
43 | |
"\n\r"); |
44 | 0 | private static final ICharacterMatcher NOT_LINE_SEPARATOR = new InverseMatcher( |
45 | |
LINE_SEPARATOR); |
46 | 0 | private static final ICharacterMatcher KEY_VALUE_SEPARATOR = new AsciiCharacterMatcher( |
47 | |
"=:"); |
48 | 0 | private static final ICharacterMatcher SEPARATOR = new AsciiCharacterMatcher( |
49 | |
"=:\r\n"); |
50 | 0 | private static final ICharacterMatcher COMMENT = new AsciiCharacterMatcher( |
51 | |
"#!"); |
52 | 0 | private static final ICharacterMatcher WHITESPACE_OR_SEPARATOR = new CompoundMatcher( |
53 | |
new ICharacterMatcher[] { WHITESPACE, SEPARATOR }); |
54 | |
|
55 | |
private ExtendedReader _extendedReader; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
public LocalizedPropertiesLoader(InputStream ins) |
65 | |
{ |
66 | 0 | this(new InputStreamReader(ins)); |
67 | 0 | } |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public LocalizedPropertiesLoader(InputStream ins, String encoding) |
82 | |
throws UnsupportedEncodingException |
83 | |
{ |
84 | 0 | this(new InputStreamReader(ins, encoding)); |
85 | 0 | } |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
public LocalizedPropertiesLoader(Reader reader) |
94 | 0 | { |
95 | 0 | _extendedReader = new ExtendedReader(new BufferedReader(reader)); |
96 | 0 | } |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
public void load(Map properties) |
108 | |
throws IOException |
109 | |
{ |
110 | 0 | while(!isAtEndOfStream()) |
111 | |
{ |
112 | |
|
113 | |
|
114 | 0 | int nextChar = _extendedReader.peek(); |
115 | 0 | if (COMMENT.matches((char) nextChar)) |
116 | |
{ |
117 | 0 | _extendedReader.skipCharacters(NOT_LINE_SEPARATOR); |
118 | 0 | continue; |
119 | |
} |
120 | |
|
121 | 0 | _extendedReader.skipCharacters(WHITESPACE); |
122 | 0 | if (!isAtEndOfLine()) |
123 | |
{ |
124 | |
|
125 | |
|
126 | 0 | String key = readQuotedLine(WHITESPACE_OR_SEPARATOR); |
127 | 0 | _extendedReader.skipCharacters(WHITESPACE); |
128 | |
|
129 | |
|
130 | |
|
131 | 0 | nextChar = _extendedReader.peek(); |
132 | 0 | if (nextChar > 0 |
133 | |
&& KEY_VALUE_SEPARATOR.matches((char) nextChar)) |
134 | |
{ |
135 | 0 | _extendedReader.read(); |
136 | 0 | _extendedReader.skipCharacters(WHITESPACE); |
137 | |
} |
138 | |
|
139 | |
|
140 | 0 | String value = readQuotedLine(LINE_SEPARATOR); |
141 | |
|
142 | 0 | properties.put(key, value); |
143 | |
} |
144 | 0 | _extendedReader.skipCharacters(LINE_SEPARATOR); |
145 | 0 | } |
146 | 0 | } |
147 | |
|
148 | |
private boolean isAtEndOfStream() |
149 | |
throws IOException |
150 | |
{ |
151 | 0 | int nextChar = _extendedReader.peek(); |
152 | 0 | return (nextChar < 0); |
153 | |
} |
154 | |
|
155 | |
private boolean isAtEndOfLine() |
156 | |
throws IOException |
157 | |
{ |
158 | 0 | int nextChar = _extendedReader.peek(); |
159 | 0 | if (nextChar < 0) return true; |
160 | 0 | return LINE_SEPARATOR.matches((char) nextChar); |
161 | |
} |
162 | |
|
163 | |
private String readQuotedLine(ICharacterMatcher terminators) |
164 | |
throws IOException |
165 | |
{ |
166 | 0 | StringBuffer buf = new StringBuffer(); |
167 | |
|
168 | |
while(true) |
169 | |
{ |
170 | |
|
171 | 0 | int nextChar = _extendedReader.peek(); |
172 | |
|
173 | |
|
174 | 0 | if (nextChar < 0 || terminators.matches((char) nextChar)) break; |
175 | |
|
176 | |
try |
177 | |
{ |
178 | |
|
179 | 0 | char ch = readQuotedChar(); |
180 | 0 | buf.append(ch); |
181 | |
} |
182 | 0 | catch (IgnoreCharacterException e) |
183 | |
{ |
184 | |
|
185 | 0 | } |
186 | 0 | } |
187 | |
|
188 | 0 | return buf.toString(); |
189 | |
} |
190 | |
|
191 | |
private char readQuotedChar() |
192 | |
throws IOException, IgnoreCharacterException |
193 | |
{ |
194 | 0 | int nextChar = _extendedReader.read(); |
195 | 0 | if (nextChar < 0) throw new IgnoreCharacterException(); |
196 | 0 | char ch = (char) nextChar; |
197 | |
|
198 | |
|
199 | 0 | if (ch != '\\') return ch; |
200 | |
|
201 | |
|
202 | 0 | nextChar = _extendedReader.read(); |
203 | |
|
204 | |
|
205 | 0 | if (nextChar < 0) throw new IgnoreCharacterException(); |
206 | |
|
207 | 0 | ch = (char) nextChar; |
208 | 0 | switch(ch) |
209 | |
{ |
210 | |
case 'u': |
211 | 0 | char res = 0; |
212 | 0 | for(int i = 0; i < 4; i++) |
213 | |
{ |
214 | 0 | nextChar = _extendedReader.read(); |
215 | 0 | if (nextChar < 0) |
216 | 0 | throw new IllegalArgumentException( |
217 | |
"Malformed \\uxxxx encoding."); |
218 | 0 | char digitChar = (char) nextChar; |
219 | 0 | int digit = HEX_DIGITS |
220 | |
.indexOf(Character.toUpperCase(digitChar)); |
221 | 0 | if (digit < 0) |
222 | 0 | throw new IllegalArgumentException( |
223 | |
"Malformed \\uxxxx encoding."); |
224 | 0 | res = (char) (res * 16 + digit); |
225 | |
} |
226 | 0 | return res; |
227 | |
|
228 | |
case '\r': |
229 | |
|
230 | 0 | nextChar = _extendedReader.peek(); |
231 | 0 | if (nextChar == '\n') _extendedReader.read(); |
232 | |
case '\n': |
233 | 0 | _extendedReader.skipCharacters(WHITESPACE); |
234 | 0 | throw new IgnoreCharacterException(); |
235 | |
|
236 | |
case 't': |
237 | 0 | return '\t'; |
238 | |
case 'n': |
239 | 0 | return '\n'; |
240 | |
case 'r': |
241 | 0 | return '\r'; |
242 | |
default: |
243 | 0 | return ch; |
244 | |
} |
245 | |
} |
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | 0 | private static class IgnoreCharacterException extends Exception |
252 | |
{ |
253 | |
|
254 | |
private static final long serialVersionUID = 8366308710256427596L; |
255 | |
} |
256 | |
} |