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 |
1 |
private static final ICharacterMatcher WHITESPACE = new WhitespaceMatcher( |
41 |
|
false); |
42 |
1 |
private static final ICharacterMatcher LINE_SEPARATOR = new AsciiCharacterMatcher( |
43 |
|
"\n\r"); |
44 |
1 |
private static final ICharacterMatcher NOT_LINE_SEPARATOR = new InverseMatcher( |
45 |
|
LINE_SEPARATOR); |
46 |
1 |
private static final ICharacterMatcher KEY_VALUE_SEPARATOR = new AsciiCharacterMatcher( |
47 |
|
"=:"); |
48 |
1 |
private static final ICharacterMatcher SEPARATOR = new AsciiCharacterMatcher( |
49 |
|
"=:\r\n"); |
50 |
1 |
private static final ICharacterMatcher COMMENT = new AsciiCharacterMatcher( |
51 |
|
"#!"); |
52 |
1 |
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 |
79 |
this(new InputStreamReader(ins)); |
67 |
79 |
} |
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 |
5 |
this(new InputStreamReader(ins, encoding)); |
85 |
5 |
} |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
public LocalizedPropertiesLoader(Reader reader) |
94 |
84 |
{ |
95 |
84 |
_extendedReader = new ExtendedReader(new BufferedReader(reader)); |
96 |
84 |
} |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
public void load(Map properties) |
108 |
|
throws IOException |
109 |
|
{ |
110 |
2414 |
while(!isAtEndOfStream()) |
111 |
|
{ |
112 |
|
|
113 |
|
|
114 |
2332 |
int nextChar = _extendedReader.peek(); |
115 |
2332 |
if (COMMENT.matches((char) nextChar)) |
116 |
|
{ |
117 |
1042 |
_extendedReader.skipCharacters(NOT_LINE_SEPARATOR); |
118 |
1042 |
continue; |
119 |
|
} |
120 |
|
|
121 |
1290 |
_extendedReader.skipCharacters(WHITESPACE); |
122 |
1290 |
if (!isAtEndOfLine()) |
123 |
|
{ |
124 |
|
|
125 |
|
|
126 |
247 |
String key = readQuotedLine(WHITESPACE_OR_SEPARATOR); |
127 |
247 |
_extendedReader.skipCharacters(WHITESPACE); |
128 |
|
|
129 |
|
|
130 |
|
|
131 |
247 |
nextChar = _extendedReader.peek(); |
132 |
247 |
if (nextChar > 0 |
133 |
|
&& KEY_VALUE_SEPARATOR.matches((char) nextChar)) |
134 |
|
{ |
135 |
239 |
_extendedReader.read(); |
136 |
239 |
_extendedReader.skipCharacters(WHITESPACE); |
137 |
|
} |
138 |
|
|
139 |
|
|
140 |
247 |
String value = readQuotedLine(LINE_SEPARATOR); |
141 |
|
|
142 |
245 |
properties.put(key, value); |
143 |
|
} |
144 |
1288 |
_extendedReader.skipCharacters(LINE_SEPARATOR); |
145 |
1288 |
} |
146 |
82 |
} |
147 |
|
|
148 |
|
private boolean isAtEndOfStream() |
149 |
|
throws IOException |
150 |
|
{ |
151 |
2414 |
int nextChar = _extendedReader.peek(); |
152 |
2414 |
return (nextChar < 0); |
153 |
|
} |
154 |
|
|
155 |
|
private boolean isAtEndOfLine() |
156 |
|
throws IOException |
157 |
|
{ |
158 |
1290 |
int nextChar = _extendedReader.peek(); |
159 |
1290 |
if (nextChar < 0) return true; |
160 |
1290 |
return LINE_SEPARATOR.matches((char) nextChar); |
161 |
|
} |
162 |
|
|
163 |
|
private String readQuotedLine(ICharacterMatcher terminators) |
164 |
|
throws IOException |
165 |
|
{ |
166 |
494 |
StringBuffer buf = new StringBuffer(); |
167 |
|
|
168 |
|
while(true) |
169 |
|
{ |
170 |
|
|
171 |
10321 |
int nextChar = _extendedReader.peek(); |
172 |
|
|
173 |
|
|
174 |
10321 |
if (nextChar < 0 || terminators.matches((char) nextChar)) break; |
175 |
|
|
176 |
|
try |
177 |
|
{ |
178 |
|
|
179 |
9829 |
char ch = readQuotedChar(); |
180 |
9824 |
buf.append(ch); |
181 |
|
} |
182 |
3 |
catch (IgnoreCharacterException e) |
183 |
|
{ |
184 |
|
|
185 |
9824 |
} |
186 |
9827 |
} |
187 |
|
|
188 |
492 |
return buf.toString(); |
189 |
|
} |
190 |
|
|
191 |
|
private char readQuotedChar() |
192 |
|
throws IOException, IgnoreCharacterException |
193 |
|
{ |
194 |
9829 |
int nextChar = _extendedReader.read(); |
195 |
9829 |
if (nextChar < 0) throw new IgnoreCharacterException(); |
196 |
9829 |
char ch = (char) nextChar; |
197 |
|
|
198 |
|
|
199 |
9829 |
if (ch != '\\') return ch; |
200 |
|
|
201 |
|
|
202 |
12 |
nextChar = _extendedReader.read(); |
203 |
|
|
204 |
|
|
205 |
12 |
if (nextChar < 0) throw new IgnoreCharacterException(); |
206 |
|
|
207 |
12 |
ch = (char) nextChar; |
208 |
12 |
switch(ch) |
209 |
|
{ |
210 |
|
case 'u': |
211 |
3 |
char res = 0; |
212 |
8 |
for(int i = 0; i < 4; i++) |
213 |
|
{ |
214 |
7 |
nextChar = _extendedReader.read(); |
215 |
7 |
if (nextChar < 0) |
216 |
1 |
throw new IllegalArgumentException( |
217 |
|
"Malformed \\uxxxx encoding."); |
218 |
6 |
char digitChar = (char) nextChar; |
219 |
6 |
int digit = HEX_DIGITS |
220 |
|
.indexOf(Character.toUpperCase(digitChar)); |
221 |
6 |
if (digit < 0) |
222 |
1 |
throw new IllegalArgumentException( |
223 |
|
"Malformed \\uxxxx encoding."); |
224 |
5 |
res = (char) (res * 16 + digit); |
225 |
|
} |
226 |
1 |
return res; |
227 |
|
|
228 |
|
case '\r': |
229 |
|
|
230 |
0 |
nextChar = _extendedReader.peek(); |
231 |
0 |
if (nextChar == '\n') _extendedReader.read(); |
232 |
|
case '\n': |
233 |
3 |
_extendedReader.skipCharacters(WHITESPACE); |
234 |
3 |
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 |
6 |
return ch; |
244 |
|
} |
245 |
|
} |
246 |
|
|
247 |
|
|
248 |
|
|
249 |
|
|
250 |
|
|
251 |
6 |
private static class IgnoreCharacterException extends Exception |
252 |
|
{ |
253 |
|
|
254 |
|
private static final long serialVersionUID = 8366308710256427596L; |
255 |
|
} |
256 |
|
} |