Skip to content

Commit 8a15299

Browse files
author
Niall Pemberton
committed
IO-162 Refactor lenient processing - simplifies the code and avoids reprocessing the stream
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1004871 13f79535-47bb-0310-9956-ffa450edef68
1 parent 3777327 commit 8a15299

1 file changed

Lines changed: 34 additions & 45 deletions

File tree

src/main/java/org/apache/commons/io/input/XmlStreamReader.java

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,7 @@ public XmlStreamReader(InputStream is) throws IOException {
185185
*/
186186
public XmlStreamReader(InputStream is, boolean lenient) throws IOException {
187187
defaultEncoding = staticDefaultEncoding;
188-
try {
189-
doRawStream(is, lenient);
190-
} catch (XmlStreamReaderException ex) {
191-
if (!lenient) {
192-
throw ex;
193-
} else {
194-
doLenientDetection(null, is, ex);
195-
}
196-
}
188+
doRawStream(is, lenient);
197189
}
198190

199191
/**
@@ -238,20 +230,11 @@ public XmlStreamReader(URL url) throws IOException {
238230
public XmlStreamReader(URLConnection conn) throws IOException {
239231
defaultEncoding = staticDefaultEncoding;
240232
boolean lenient = true;
241-
InputStream is = conn.getInputStream();
242233
String contentType = conn.getContentType();
243234
if (conn instanceof HttpURLConnection || contentType != null) {
244-
try {
245-
doHttpStream(is, contentType, lenient);
246-
} catch (XmlStreamReaderException ex) {
247-
doLenientDetection(contentType, is, ex);
248-
}
235+
doHttpStream(conn.getInputStream(), contentType, lenient);
249236
} else {
250-
try {
251-
doRawStream(is, lenient);
252-
} catch (XmlStreamReaderException ex) {
253-
doLenientDetection(null, is, ex);
254-
}
237+
doRawStream(conn.getInputStream(), lenient);
255238
}
256239
}
257240

@@ -315,15 +298,7 @@ public XmlStreamReader(InputStream is, String httpContentType,
315298
boolean lenient, String defaultEncoding) throws IOException {
316299
this.defaultEncoding = (defaultEncoding == null) ? staticDefaultEncoding
317300
: defaultEncoding;
318-
try {
319-
doHttpStream(is, httpContentType, lenient);
320-
} catch (XmlStreamReaderException ex) {
321-
if (!lenient) {
322-
throw ex;
323-
} else {
324-
doLenientDetection(httpContentType, is, ex);
325-
}
326-
}
301+
doHttpStream(is, httpContentType, lenient);
327302
}
328303

329304
/**
@@ -371,34 +346,32 @@ public XmlStreamReader(InputStream is, String httpContentType,
371346
* the charset encoding.
372347
* @param is the unconsumed InputStream
373348
* @param ex The thrown exception
349+
* @return the encoding
374350
* @throws IOException thrown if there is a problem reading the stream.
375351
*/
376-
private void doLenientDetection(String httpContentType, InputStream is,
352+
private String doLenientDetection(String httpContentType, InputStream is,
377353
XmlStreamReaderException ex) throws IOException {
378354
if (httpContentType != null) {
379355
if (httpContentType.startsWith("text/html")) {
380356
httpContentType = httpContentType.substring("text/html"
381357
.length());
382358
httpContentType = "text/xml" + httpContentType;
383359
try {
384-
doHttpStream(is, httpContentType, true);
385-
ex = null;
360+
return calculateHttpEncoding(httpContentType, ex.getBomEncoding(),
361+
ex.getXmlGuessEncoding(), ex.getXmlEncoding(), true);
386362
} catch (XmlStreamReaderException ex2) {
387363
ex = ex2;
388364
}
389365
}
390366
}
391-
if (ex != null) {
392-
String encoding = ex.getXmlEncoding();
393-
if (encoding == null) {
394-
encoding = ex.getContentTypeEncoding();
395-
}
396-
if (encoding == null) {
397-
encoding = (defaultEncoding == null) ? UTF_8 : defaultEncoding;
398-
}
399-
this.encoding = encoding;
400-
this.reader = new InputStreamReader(is, encoding);
367+
String encoding = ex.getXmlEncoding();
368+
if (encoding == null) {
369+
encoding = ex.getContentTypeEncoding();
370+
}
371+
if (encoding == null) {
372+
encoding = (defaultEncoding == null) ? UTF_8 : defaultEncoding;
401373
}
374+
return encoding;
402375
}
403376

404377
/**
@@ -448,7 +421,15 @@ private void doRawStream(InputStream is, boolean lenient)
448421
String bomEnc = bom.getBOMCharsetName();
449422
String xmlGuessEnc = pis.getBOMCharsetName();
450423
String xmlEnc = getXmlProlog(pis, xmlGuessEnc);
451-
this.encoding = calculateRawEncoding(bomEnc, xmlGuessEnc, xmlEnc);
424+
try {
425+
this.encoding = calculateRawEncoding(bomEnc, xmlGuessEnc, xmlEnc);
426+
} catch (XmlStreamReaderException ex) {
427+
if (lenient) {
428+
this.encoding = doLenientDetection(null, is, ex);
429+
} else {
430+
throw ex;
431+
}
432+
}
452433
this.reader = new InputStreamReader(is, encoding);
453434
}
454435

@@ -468,8 +449,16 @@ private void doHttpStream(InputStream is, String httpContentType,
468449
String bomEnc = bom.getBOMCharsetName();
469450
String xmlGuessEnc = pis.getBOMCharsetName();
470451
String xmlEnc = getXmlProlog(pis, xmlGuessEnc);
471-
this.encoding = calculateHttpEncoding(httpContentType, bomEnc,
472-
xmlGuessEnc, xmlEnc, lenient);
452+
try {
453+
this.encoding = calculateHttpEncoding(httpContentType, bomEnc,
454+
xmlGuessEnc, xmlEnc, lenient);
455+
} catch (XmlStreamReaderException ex) {
456+
if (lenient) {
457+
this.encoding = doLenientDetection(httpContentType, is, ex);
458+
} else {
459+
throw ex;
460+
}
461+
}
473462
this.reader = new InputStreamReader(is, encoding);
474463
}
475464

0 commit comments

Comments
 (0)