commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r1004871 - /commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/XmlStreamReader.java
Date Wed, 06 Oct 2010 01:20:43 GMT
Author: niallp
Date: Wed Oct  6 01:20:43 2010
New Revision: 1004871

URL: http://svn.apache.org/viewvc?rev=1004871&view=rev
Log:
IO-162 Refactor lenient processing - simplifies the code and avoids reprocessing the stream

Modified:
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/XmlStreamReader.java

Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/XmlStreamReader.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/XmlStreamReader.java?rev=1004871&r1=1004870&r2=1004871&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/XmlStreamReader.java
(original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/XmlStreamReader.java
Wed Oct  6 01:20:43 2010
@@ -185,15 +185,7 @@ public class XmlStreamReader extends Rea
      */
     public XmlStreamReader(InputStream is, boolean lenient) throws IOException {
         defaultEncoding = staticDefaultEncoding;
-        try {
-            doRawStream(is, lenient);
-        } catch (XmlStreamReaderException ex) {
-            if (!lenient) {
-                throw ex;
-            } else {
-                doLenientDetection(null, is, ex);
-            }
-        }
+        doRawStream(is, lenient);
     }
 
     /**
@@ -238,20 +230,11 @@ public class XmlStreamReader extends Rea
     public XmlStreamReader(URLConnection conn) throws IOException {
         defaultEncoding = staticDefaultEncoding;
         boolean lenient = true;
-        InputStream is = conn.getInputStream();
         String contentType = conn.getContentType();
         if (conn instanceof HttpURLConnection || contentType != null) {
-            try {
-                doHttpStream(is, contentType, lenient);
-            } catch (XmlStreamReaderException ex) {
-                doLenientDetection(contentType, is, ex);
-            }
+            doHttpStream(conn.getInputStream(), contentType, lenient);
         } else {
-            try {
-                doRawStream(is, lenient);
-            } catch (XmlStreamReaderException ex) {
-                doLenientDetection(null, is, ex);
-            }
+            doRawStream(conn.getInputStream(), lenient);
         }
     }
 
@@ -315,15 +298,7 @@ public class XmlStreamReader extends Rea
             boolean lenient, String defaultEncoding) throws IOException {
         this.defaultEncoding = (defaultEncoding == null) ? staticDefaultEncoding
                 : defaultEncoding;
-        try {
-            doHttpStream(is, httpContentType, lenient);
-        } catch (XmlStreamReaderException ex) {
-            if (!lenient) {
-                throw ex;
-            } else {
-                doLenientDetection(httpContentType, is, ex);
-            }
-        }
+        doHttpStream(is, httpContentType, lenient);
     }
 
     /**
@@ -371,9 +346,10 @@ public class XmlStreamReader extends Rea
      *        the charset encoding.
      * @param is the unconsumed InputStream
      * @param ex The thrown exception
+     * @return the encoding
      * @throws IOException thrown if there is a problem reading the stream.
      */
-    private void doLenientDetection(String httpContentType, InputStream is,
+    private String doLenientDetection(String httpContentType, InputStream is,
             XmlStreamReaderException ex) throws IOException {
         if (httpContentType != null) {
             if (httpContentType.startsWith("text/html")) {
@@ -381,24 +357,21 @@ public class XmlStreamReader extends Rea
                         .length());
                 httpContentType = "text/xml" + httpContentType;
                 try {
-                    doHttpStream(is, httpContentType, true);
-                    ex = null;
+                    return calculateHttpEncoding(httpContentType, ex.getBomEncoding(),
+                            ex.getXmlGuessEncoding(), ex.getXmlEncoding(), true);
                 } catch (XmlStreamReaderException ex2) {
                     ex = ex2;
                 }
             }
         }
-        if (ex != null) {
-            String encoding = ex.getXmlEncoding();
-            if (encoding == null) {
-                encoding = ex.getContentTypeEncoding();
-            }
-            if (encoding == null) {
-                encoding = (defaultEncoding == null) ? UTF_8 : defaultEncoding;
-            }
-            this.encoding = encoding;
-            this.reader = new InputStreamReader(is, encoding);
+        String encoding = ex.getXmlEncoding();
+        if (encoding == null) {
+            encoding = ex.getContentTypeEncoding();
+        }
+        if (encoding == null) {
+            encoding = (defaultEncoding == null) ? UTF_8 : defaultEncoding;
         }
+        return encoding;
     }
 
     /**
@@ -448,7 +421,15 @@ public class XmlStreamReader extends Rea
         String bomEnc      = bom.getBOMCharsetName();
         String xmlGuessEnc = pis.getBOMCharsetName();
         String xmlEnc = getXmlProlog(pis, xmlGuessEnc);
-        this.encoding = calculateRawEncoding(bomEnc, xmlGuessEnc, xmlEnc);
+        try {
+            this.encoding = calculateRawEncoding(bomEnc, xmlGuessEnc, xmlEnc);
+        } catch (XmlStreamReaderException ex) {
+            if (lenient) {
+                this.encoding = doLenientDetection(null, is, ex);
+            } else {
+                throw ex;
+            }
+        }
         this.reader = new InputStreamReader(is, encoding);
     }
 
@@ -468,8 +449,16 @@ public class XmlStreamReader extends Rea
         String bomEnc      = bom.getBOMCharsetName();
         String xmlGuessEnc = pis.getBOMCharsetName();
         String xmlEnc = getXmlProlog(pis, xmlGuessEnc);
-        this.encoding = calculateHttpEncoding(httpContentType, bomEnc,
-                xmlGuessEnc, xmlEnc, lenient);
+        try {
+            this.encoding = calculateHttpEncoding(httpContentType, bomEnc,
+                    xmlGuessEnc, xmlEnc, lenient);
+        } catch (XmlStreamReaderException ex) {
+            if (lenient) {
+                this.encoding = doLenientDetection(httpContentType, is, ex);
+            } else {
+                throw ex;
+            }
+        }
         this.reader = new InputStreamReader(is, encoding);
     }
 



Mime
View raw message