1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package com.sun.syndication.io; |
18 |
|
|
19 |
|
import java.io.*; |
20 |
|
import java.net.URL; |
21 |
|
import java.net.URLConnection; |
22 |
|
import java.net.HttpURLConnection; |
23 |
|
import java.util.regex.Pattern; |
24 |
|
import java.util.regex.Matcher; |
25 |
|
import java.text.MessageFormat; |
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
@author |
49 |
|
@version |
50 |
|
|
51 |
|
|
|
|
| 33% |
Uncovered Elements: 211 (315) |
Complexity: 106 |
Complexity Density: 0,55 |
|
52 |
|
public class XmlReader extends Reader { |
53 |
|
private static final int BUFFER_SIZE = 4096; |
54 |
|
|
55 |
|
private static final String UTF_8 = "UTF-8"; |
56 |
|
private static final String US_ASCII = "US-ASCII"; |
57 |
|
private static final String UTF_16BE = "UTF-16BE"; |
58 |
|
private static final String UTF_16LE = "UTF-16LE"; |
59 |
|
private static final String UTF_16 = "UTF-16"; |
60 |
|
|
61 |
|
private static String _staticDefaultEncoding = null; |
62 |
|
|
63 |
|
private Reader _reader; |
64 |
|
private String _encoding; |
65 |
|
private String _defaultEncoding; |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
@param |
77 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
78 |
0
|
public static void setDefaultEncoding(String encoding) {... |
79 |
0
|
_staticDefaultEncoding = encoding; |
80 |
|
} |
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
@return |
90 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
91 |
0
|
public static String getDefaultEncoding() {... |
92 |
0
|
return _staticDefaultEncoding; |
93 |
|
} |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
@param |
105 |
|
@throws |
106 |
|
|
107 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
108 |
61693
|
public XmlReader(File file) throws IOException {... |
109 |
61693
|
this(new FileInputStream(file)); |
110 |
|
} |
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
@param |
121 |
|
@throws |
122 |
|
|
123 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
124 |
61693
|
public XmlReader(InputStream is) throws IOException {... |
125 |
61693
|
this(is,true); |
126 |
|
} |
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
@param |
147 |
|
@param |
148 |
|
@throws |
149 |
|
@throws |
150 |
|
|
151 |
|
|
|
|
| 37,5% |
Uncovered Elements: 5 (8) |
Complexity: 3 |
Complexity Density: 0,5 |
|
152 |
61693
|
public XmlReader(InputStream is,boolean lenient) throws IOException, XmlReaderException {... |
153 |
61693
|
_defaultEncoding = _staticDefaultEncoding; |
154 |
61693
|
try { |
155 |
61693
|
doRawStream(is,lenient); |
156 |
|
} |
157 |
|
catch (XmlReaderException ex) { |
158 |
0
|
if (!lenient) { |
159 |
0
|
throw ex; |
160 |
|
} |
161 |
|
else { |
162 |
0
|
doLenientDetection(null,ex); |
163 |
|
} |
164 |
|
} |
165 |
|
} |
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
@param |
180 |
|
@throws |
181 |
|
|
182 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
183 |
0
|
public XmlReader(URL url) throws IOException {... |
184 |
0
|
this(url.openConnection()); |
185 |
|
} |
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
@param |
200 |
|
@throws |
201 |
|
|
202 |
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 6 |
Complexity Density: 0,46 |
|
203 |
0
|
public XmlReader(URLConnection conn) throws IOException {... |
204 |
0
|
_defaultEncoding = _staticDefaultEncoding; |
205 |
0
|
boolean lenient = true; |
206 |
0
|
if (conn instanceof HttpURLConnection) { |
207 |
0
|
try { |
208 |
0
|
doHttpStream(conn.getInputStream(),conn.getContentType(),lenient); |
209 |
|
} |
210 |
|
catch (XmlReaderException ex) { |
211 |
0
|
doLenientDetection(conn.getContentType(),ex); |
212 |
|
} |
213 |
|
} |
214 |
|
else |
215 |
0
|
if (conn.getContentType()!=null) { |
216 |
0
|
try { |
217 |
0
|
doHttpStream(conn.getInputStream(),conn.getContentType(),lenient); |
218 |
|
} |
219 |
|
catch (XmlReaderException ex) { |
220 |
0
|
doLenientDetection(conn.getContentType(),ex); |
221 |
|
} |
222 |
|
} |
223 |
|
else { |
224 |
0
|
try { |
225 |
0
|
doRawStream(conn.getInputStream(),lenient); |
226 |
|
} |
227 |
|
catch (XmlReaderException ex) { |
228 |
0
|
doLenientDetection(null,ex); |
229 |
|
} |
230 |
|
} |
231 |
|
} |
232 |
|
|
233 |
|
|
234 |
|
|
235 |
|
|
236 |
|
|
237 |
|
|
238 |
|
|
239 |
|
|
240 |
|
|
241 |
|
|
242 |
|
|
243 |
|
@param |
244 |
|
@param |
245 |
|
@throws |
246 |
|
|
247 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
248 |
0
|
public XmlReader(InputStream is,String httpContentType) throws IOException {... |
249 |
0
|
this(is,httpContentType,true); |
250 |
|
} |
251 |
|
|
252 |
|
|
253 |
|
|
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
|
258 |
|
|
259 |
|
|
260 |
|
|
261 |
|
|
262 |
|
|
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
|
267 |
|
|
268 |
|
|
269 |
|
|
270 |
|
|
271 |
|
|
272 |
|
|
273 |
|
@param |
274 |
|
@param |
275 |
|
@param |
276 |
|
@throws |
277 |
|
@throws |
278 |
|
|
279 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0,5 |
|
280 |
0
|
public XmlReader(InputStream is,String httpContentType,boolean lenient, String defaultEncoding)... |
281 |
|
throws IOException, XmlReaderException { |
282 |
0
|
_defaultEncoding = (defaultEncoding == null) ? _staticDefaultEncoding : defaultEncoding; |
283 |
0
|
try { |
284 |
0
|
doHttpStream(is,httpContentType,lenient); |
285 |
|
} |
286 |
|
catch (XmlReaderException ex) { |
287 |
0
|
if (!lenient) { |
288 |
0
|
throw ex; |
289 |
|
} |
290 |
|
else { |
291 |
0
|
doLenientDetection(httpContentType,ex); |
292 |
|
} |
293 |
|
} |
294 |
|
} |
295 |
|
|
296 |
|
|
297 |
|
|
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
|
302 |
|
|
303 |
|
|
304 |
|
|
305 |
|
|
306 |
|
|
307 |
|
|
308 |
|
|
309 |
|
|
310 |
|
|
311 |
|
|
312 |
|
|
313 |
|
|
314 |
|
|
315 |
|
|
316 |
|
|
317 |
|
@param |
318 |
|
@param |
319 |
|
@param |
320 |
|
@throws |
321 |
|
@throws |
322 |
|
|
323 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
324 |
0
|
public XmlReader(InputStream is, String httpContentType, boolean lenient)... |
325 |
|
throws IOException, XmlReaderException { |
326 |
0
|
this(is, httpContentType, lenient, null); |
327 |
|
} |
328 |
|
|
|
|
| 0% |
Uncovered Elements: 27 (27) |
Complexity: 7 |
Complexity Density: 0,47 |
|
329 |
0
|
private void doLenientDetection(String httpContentType,XmlReaderException ex) throws IOException {... |
330 |
0
|
if (httpContentType!=null) { |
331 |
0
|
if (httpContentType.startsWith("text/html")) { |
332 |
0
|
httpContentType = httpContentType.substring("text/html".length()); |
333 |
0
|
httpContentType = "text/xml" + httpContentType; |
334 |
0
|
try { |
335 |
0
|
doHttpStream(ex.getInputStream(),httpContentType,true); |
336 |
0
|
ex = null; |
337 |
|
} |
338 |
|
catch (XmlReaderException ex2) { |
339 |
0
|
ex = ex2; |
340 |
|
} |
341 |
|
} |
342 |
|
} |
343 |
0
|
if (ex!=null) { |
344 |
0
|
String encoding = ex.getXmlEncoding(); |
345 |
0
|
if (encoding==null) { |
346 |
0
|
encoding = ex.getContentTypeEncoding(); |
347 |
|
} |
348 |
0
|
if (encoding==null) { |
349 |
0
|
encoding = (_defaultEncoding == null) ? UTF_8 : _defaultEncoding; |
350 |
|
} |
351 |
0
|
prepareReader(ex.getInputStream(),encoding); |
352 |
|
} |
353 |
|
} |
354 |
|
|
355 |
|
|
356 |
|
|
357 |
|
|
358 |
|
@return |
359 |
|
|
360 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
361 |
0
|
public String getEncoding() {... |
362 |
0
|
return _encoding; |
363 |
|
} |
364 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
365 |
|
public int read(char[] buf,int offset,int len) throws IOException {... |
366 |
|
return _reader.read(buf,offset,len); |
367 |
|
} |
368 |
|
|
369 |
|
|
370 |
|
|
371 |
|
|
372 |
|
@throws |
373 |
|
|
374 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
375 |
61691
|
public void close() throws IOException {... |
376 |
61691
|
_reader.close(); |
377 |
|
} |
378 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0,17 |
|
379 |
61693
|
private void doRawStream(InputStream is,boolean lenient) throws IOException {... |
380 |
61693
|
BufferedInputStream pis = new BufferedInputStream(is, BUFFER_SIZE); |
381 |
61693
|
String bomEnc = getBOMEncoding(pis); |
382 |
61693
|
String xmlGuessEnc = getXMLGuessEncoding(pis); |
383 |
61693
|
String xmlEnc = getXmlProlog(pis,xmlGuessEnc); |
384 |
61693
|
String encoding = calculateRawEncoding(bomEnc, xmlGuessEnc, xmlEnc, pis); |
385 |
61693
|
prepareReader(pis,encoding); |
386 |
|
} |
387 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 1 |
Complexity Density: 0,12 |
|
388 |
0
|
private void doHttpStream(InputStream is,String httpContentType,boolean lenient) throws IOException {... |
389 |
0
|
BufferedInputStream pis = new BufferedInputStream(is, BUFFER_SIZE); |
390 |
0
|
String cTMime = getContentTypeMime(httpContentType); |
391 |
0
|
String cTEnc = getContentTypeEncoding(httpContentType); |
392 |
0
|
String bomEnc = getBOMEncoding(pis); |
393 |
0
|
String xmlGuessEnc = getXMLGuessEncoding(pis); |
394 |
0
|
String xmlEnc = getXmlProlog(pis,xmlGuessEnc); |
395 |
0
|
String encoding = calculateHttpEncoding(cTMime, cTEnc, bomEnc, xmlGuessEnc, xmlEnc, pis,lenient); |
396 |
0
|
prepareReader(pis,encoding); |
397 |
|
} |
398 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
399 |
61693
|
private void prepareReader(InputStream is,String encoding) throws IOException {... |
400 |
61693
|
_reader = new InputStreamReader(is,encoding); |
401 |
61693
|
_encoding = encoding; |
402 |
|
} |
403 |
|
|
404 |
|
|
|
|
| 26,8% |
Uncovered Elements: 30 (41) |
Complexity: 19 |
Complexity Density: 0,9 |
|
405 |
61693
|
private String calculateRawEncoding(String bomEnc, String xmlGuessEnc, String xmlEnc, InputStream is) throws IOException {... |
406 |
61693
|
String encoding; |
407 |
61693
|
if (bomEnc==null) { |
408 |
61693
|
if (xmlGuessEnc==null || xmlEnc==null) { |
409 |
0
|
encoding = (_defaultEncoding == null) ? UTF_8 : _defaultEncoding; |
410 |
|
} |
411 |
|
else |
412 |
61693
|
if (xmlEnc.equals(UTF_16) && (xmlGuessEnc.equals(UTF_16BE) || xmlGuessEnc.equals(UTF_16LE))) { |
413 |
5
|
encoding = xmlGuessEnc; |
414 |
|
} |
415 |
|
else { |
416 |
61688
|
encoding = xmlEnc; |
417 |
|
} |
418 |
|
} |
419 |
|
else |
420 |
0
|
if (bomEnc.equals(UTF_8)) { |
421 |
0
|
if (xmlGuessEnc!=null && !xmlGuessEnc.equals(UTF_8)) { |
422 |
0
|
throw new XmlReaderException(RAW_EX_1.format(new Object[]{bomEnc,xmlGuessEnc,xmlEnc}), |
423 |
|
bomEnc,xmlGuessEnc,xmlEnc,is); |
424 |
|
} |
425 |
0
|
if (xmlEnc!=null && !xmlEnc.equals(UTF_8)) { |
426 |
0
|
throw new XmlReaderException(RAW_EX_1.format(new Object[]{bomEnc,xmlGuessEnc,xmlEnc}), |
427 |
|
bomEnc,xmlGuessEnc,xmlEnc,is); |
428 |
|
} |
429 |
0
|
encoding = UTF_8; |
430 |
|
} |
431 |
|
else |
432 |
0
|
if (bomEnc.equals(UTF_16BE) || bomEnc.equals(UTF_16LE)) { |
433 |
0
|
if (xmlGuessEnc!=null && !xmlGuessEnc.equals(bomEnc)) { |
434 |
0
|
throw new IOException(RAW_EX_1.format(new Object[]{bomEnc,xmlGuessEnc,xmlEnc})); |
435 |
|
} |
436 |
0
|
if (xmlEnc!=null && !xmlEnc.equals(UTF_16) && !xmlEnc.equals(bomEnc)) { |
437 |
0
|
throw new XmlReaderException(RAW_EX_1.format(new Object[]{bomEnc,xmlGuessEnc,xmlEnc}), |
438 |
|
bomEnc,xmlGuessEnc,xmlEnc,is); |
439 |
|
} |
440 |
0
|
encoding =bomEnc; |
441 |
|
} |
442 |
|
else { |
443 |
0
|
throw new XmlReaderException(RAW_EX_2.format(new Object[]{bomEnc,xmlGuessEnc,xmlEnc}), |
444 |
|
bomEnc,xmlGuessEnc,xmlEnc,is); |
445 |
|
} |
446 |
61693
|
return encoding; |
447 |
|
} |
448 |
|
|
449 |
|
|
|
|
| 0% |
Uncovered Elements: 35 (35) |
Complexity: 12 |
Complexity Density: 0,63 |
|
450 |
0
|
private String calculateHttpEncoding(String cTMime, String cTEnc, String bomEnc, String xmlGuessEnc, String xmlEnc, InputStream is,boolean lenient) throws IOException {... |
451 |
0
|
String encoding; |
452 |
0
|
if (lenient & xmlEnc!=null) { |
453 |
0
|
encoding = xmlEnc; |
454 |
|
} |
455 |
|
else { |
456 |
0
|
boolean appXml = isAppXml(cTMime); |
457 |
0
|
boolean textXml = isTextXml(cTMime); |
458 |
0
|
if (appXml || textXml) { |
459 |
0
|
if (cTEnc==null) { |
460 |
0
|
if (appXml) { |
461 |
0
|
encoding = calculateRawEncoding(bomEnc, xmlGuessEnc, xmlEnc, is); |
462 |
|
} |
463 |
|
else { |
464 |
0
|
encoding = (_defaultEncoding == null) ? US_ASCII : _defaultEncoding; |
465 |
|
} |
466 |
|
} |
467 |
|
else |
468 |
0
|
if (bomEnc!=null && (cTEnc.equals(UTF_16BE) || cTEnc.equals(UTF_16LE))) { |
469 |
0
|
throw new XmlReaderException(HTTP_EX_1.format(new Object[]{cTMime,cTEnc,bomEnc,xmlGuessEnc,xmlEnc}), |
470 |
|
cTMime,cTEnc,bomEnc,xmlGuessEnc,xmlEnc,is); |
471 |
|
} |
472 |
|
else |
473 |
0
|
if (cTEnc.equals(UTF_16)) { |
474 |
0
|
if (bomEnc!=null && bomEnc.startsWith(UTF_16)) { |
475 |
0
|
encoding = bomEnc; |
476 |
|
} |
477 |
|
else { |
478 |
0
|
throw new XmlReaderException(HTTP_EX_2.format(new Object[]{cTMime,cTEnc,bomEnc,xmlGuessEnc,xmlEnc}), |
479 |
|
cTMime,cTEnc,bomEnc,xmlGuessEnc,xmlEnc,is); |
480 |
|
} |
481 |
|
} |
482 |
|
else { |
483 |
0
|
encoding = cTEnc; |
484 |
|
} |
485 |
|
} |
486 |
|
else { |
487 |
0
|
throw new XmlReaderException(HTTP_EX_3.format(new Object[]{cTMime,cTEnc,bomEnc,xmlGuessEnc,xmlEnc}), |
488 |
|
cTMime,cTEnc,bomEnc,xmlGuessEnc,xmlEnc,is); |
489 |
|
} |
490 |
|
} |
491 |
0
|
return encoding; |
492 |
|
} |
493 |
|
|
494 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0,4 |
|
495 |
0
|
private static String getContentTypeMime(String httpContentType) {... |
496 |
0
|
String mime = null; |
497 |
0
|
if (httpContentType!=null) { |
498 |
0
|
int i = httpContentType.indexOf(";"); |
499 |
0
|
mime = ((i==-1) ? httpContentType : httpContentType.substring(0,i)).trim(); |
500 |
|
} |
501 |
0
|
return mime; |
502 |
|
} |
503 |
|
|
504 |
|
private static final Pattern CHARSET_PATTERN = Pattern.compile("charset=([.[^; ]]*)"); |
505 |
|
|
506 |
|
|
|
|
| 0% |
Uncovered Elements: 21 (21) |
Complexity: 8 |
Complexity Density: 0,73 |
|
507 |
0
|
private static String getContentTypeEncoding(String httpContentType) {... |
508 |
0
|
String encoding = null; |
509 |
0
|
if (httpContentType!=null) { |
510 |
0
|
int i = httpContentType.indexOf(";"); |
511 |
0
|
if (i>-1) { |
512 |
0
|
String postMime = httpContentType.substring(i+1); |
513 |
0
|
Matcher m = CHARSET_PATTERN.matcher(postMime); |
514 |
0
|
encoding = (m.find()) ? m.group(1) : null; |
515 |
0
|
encoding = (encoding!=null) ? encoding.toUpperCase() : null; |
516 |
|
} |
517 |
0
|
if (encoding != null && |
518 |
|
((encoding.startsWith("\"") && encoding.endsWith("\"")) || |
519 |
|
(encoding.startsWith("'") && encoding.endsWith("'")) |
520 |
|
)) { |
521 |
0
|
encoding = encoding.substring(1, encoding.length() - 1); |
522 |
|
} |
523 |
|
} |
524 |
0
|
return encoding; |
525 |
|
} |
526 |
|
|
527 |
|
|
528 |
|
|
|
|
| 53,8% |
Uncovered Elements: 12 (26) |
Complexity: 8 |
Complexity Density: 0,4 |
|
529 |
61693
|
private static String getBOMEncoding(BufferedInputStream is) throws IOException {... |
530 |
61693
|
String encoding = null; |
531 |
61693
|
int[] bytes = new int[3]; |
532 |
61693
|
is.mark(3); |
533 |
61693
|
bytes[0] = is.read(); |
534 |
61693
|
bytes[1] = is.read(); |
535 |
61693
|
bytes[2] = is.read(); |
536 |
|
|
537 |
61693
|
if (bytes[0] == 0xFE && bytes[1] == 0xFF) { |
538 |
0
|
encoding = UTF_16BE; |
539 |
0
|
is.reset(); |
540 |
0
|
is.read(); |
541 |
0
|
is.read(); |
542 |
|
} |
543 |
|
else |
544 |
61693
|
if (bytes[0] == 0xFF && bytes[1] == 0xFE) { |
545 |
0
|
encoding = UTF_16LE; |
546 |
0
|
is.reset(); |
547 |
0
|
is.read(); |
548 |
0
|
is.read(); |
549 |
|
} |
550 |
|
else |
551 |
61693
|
if (bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) { |
552 |
0
|
encoding = UTF_8; |
553 |
|
} |
554 |
|
else { |
555 |
61693
|
is.reset(); |
556 |
|
} |
557 |
61693
|
return encoding; |
558 |
|
} |
559 |
|
|
560 |
|
|
|
|
| 85,7% |
Uncovered Elements: 3 (21) |
Complexity: 13 |
Complexity Density: 0,87 |
|
561 |
61693
|
private static String getXMLGuessEncoding(BufferedInputStream is) throws IOException {... |
562 |
61693
|
String encoding = null; |
563 |
61693
|
int[] bytes = new int[4]; |
564 |
61693
|
is.mark(4); |
565 |
61693
|
bytes[0] = is.read(); |
566 |
61693
|
bytes[1] = is.read(); |
567 |
61693
|
bytes[2] = is.read(); |
568 |
61693
|
bytes[3] = is.read(); |
569 |
61693
|
is.reset(); |
570 |
|
|
571 |
61693
|
if (bytes[0] == 0x00 && bytes[1] == 0x3C && bytes[2] == 0x00 && bytes[3] == 0x3F) { |
572 |
5
|
encoding = UTF_16BE; |
573 |
|
} |
574 |
|
else |
575 |
61688
|
if (bytes[0] == 0x3C && bytes[1] == 0x00 && bytes[2] == 0x3F && bytes[3] == 0x00) { |
576 |
0
|
encoding = UTF_16LE; |
577 |
|
} |
578 |
|
else |
579 |
61688
|
if (bytes[0] == 0x3C && bytes[1] == 0x3F && bytes[2] == 0x78 && bytes[3] == 0x6D) { |
580 |
61688
|
encoding = UTF_8; |
581 |
|
} |
582 |
61693
|
return encoding; |
583 |
|
} |
584 |
|
|
585 |
|
|
586 |
|
private static final Pattern ENCODING_PATTERN = |
587 |
|
Pattern.compile("<\\?xml.*encoding[\\s]*=[\\s]*((?:\".[^\"]*\")|(?:'.[^']*'))", Pattern.MULTILINE); |
588 |
|
|
589 |
|
|
|
|
| 80,4% |
Uncovered Elements: 9 (46) |
Complexity: 10 |
Complexity Density: 0,31 |
|
590 |
61693
|
private static String getXmlProlog(BufferedInputStream is,String guessedEnc) throws IOException {... |
591 |
61693
|
String encoding = null; |
592 |
61693
|
if (guessedEnc!=null) { |
593 |
61693
|
byte[] bytes = new byte[BUFFER_SIZE]; |
594 |
61693
|
is.mark(BUFFER_SIZE); |
595 |
61693
|
int offset = 0; |
596 |
61693
|
int max = BUFFER_SIZE; |
597 |
61693
|
int c = is.read(bytes,offset,max); |
598 |
61693
|
int firstGT = -1; |
599 |
123386
|
while (c!=-1 && firstGT==-1 && offset< BUFFER_SIZE) { |
600 |
61693
|
offset += c; |
601 |
61693
|
max -= c; |
602 |
61693
|
c = is.read(bytes,offset,max); |
603 |
61693
|
firstGT = new String(bytes, 0, offset).indexOf(">"); |
604 |
|
} |
605 |
61693
|
if (firstGT == -1) { |
606 |
0
|
if (c == -1) { |
607 |
0
|
throw new IOException("Unexpected end of XML stream"); |
608 |
|
} |
609 |
|
else { |
610 |
0
|
throw new IOException("XML prolog or ROOT element not found on first " + offset + " bytes"); |
611 |
|
} |
612 |
|
} |
613 |
61693
|
int bytesRead = offset; |
614 |
61693
|
if (bytesRead>0) { |
615 |
61693
|
is.reset(); |
616 |
61693
|
Reader reader = new InputStreamReader(new ByteArrayInputStream(bytes,0,firstGT + 1), guessedEnc); |
617 |
61693
|
BufferedReader bReader = new BufferedReader(reader); |
618 |
61693
|
StringBuffer prolog = new StringBuffer(); |
619 |
61693
|
String line = bReader.readLine(); |
620 |
123386
|
while (line != null) { |
621 |
61693
|
prolog.append(line); |
622 |
61693
|
line = bReader.readLine(); |
623 |
|
} |
624 |
61693
|
Matcher m = ENCODING_PATTERN.matcher(prolog); |
625 |
61693
|
if (m.find()) { |
626 |
61693
|
encoding = m.group(1).toUpperCase(); |
627 |
61693
|
encoding = encoding.substring(1,encoding.length()-1); |
628 |
|
} |
629 |
|
} |
630 |
|
} |
631 |
61693
|
return encoding; |
632 |
|
} |
633 |
|
|
634 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
635 |
0
|
private static boolean isAppXml(String mime) {... |
636 |
0
|
return mime!=null && |
637 |
|
(mime.equals("application/xml") || |
638 |
|
mime.equals("application/xml-dtd") || |
639 |
|
mime.equals("application/xml-external-parsed-entity") || |
640 |
|
(mime.startsWith("application/") && mime.endsWith("+xml"))); |
641 |
|
} |
642 |
|
|
643 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
644 |
0
|
private static boolean isTextXml(String mime) {... |
645 |
0
|
return mime!=null && |
646 |
|
(mime.equals("text/xml") || |
647 |
|
mime.equals("text/xml-external-parsed-entity") || |
648 |
|
(mime.startsWith("text/") && mime.endsWith("+xml"))); |
649 |
|
} |
650 |
|
|
651 |
|
private static final MessageFormat RAW_EX_1 = new MessageFormat( |
652 |
|
"Invalid encoding, BOM [{0}] XML guess [{1}] XML prolog [{2}] encoding mismatch"); |
653 |
|
|
654 |
|
private static final MessageFormat RAW_EX_2 = new MessageFormat( |
655 |
|
"Invalid encoding, BOM [{0}] XML guess [{1}] XML prolog [{2}] unknown BOM"); |
656 |
|
|
657 |
|
private static final MessageFormat HTTP_EX_1 = new MessageFormat( |
658 |
|
"Invalid encoding, CT-MIME [{0}] CT-Enc [{1}] BOM [{2}] XML guess [{3}] XML prolog [{4}], BOM must be NULL"); |
659 |
|
|
660 |
|
private static final MessageFormat HTTP_EX_2 = new MessageFormat( |
661 |
|
"Invalid encoding, CT-MIME [{0}] CT-Enc [{1}] BOM [{2}] XML guess [{3}] XML prolog [{4}], encoding mismatch"); |
662 |
|
|
663 |
|
private static final MessageFormat HTTP_EX_3 = new MessageFormat( |
664 |
|
"Invalid encoding, CT-MIME [{0}] CT-Enc [{1}] BOM [{2}] XML guess [{3}] XML prolog [{4}], Invalid MIME"); |
665 |
|
|
666 |
|
} |