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