You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ni...@apache.org on 2010/10/06 03:51:47 UTC
svn commit: r1004874 - in /commons/proper/io/trunk/src:
main/java/org/apache/commons/io/input/XmlStreamReader.java
test/java/org/apache/commons/io/input/XmlStreamReaderTest.java
Author: niallp
Date: Wed Oct 6 01:51:47 2010
New Revision: 1004874
URL: http://svn.apache.org/viewvc?rev=1004874&view=rev
Log:
IO-162 Remove the static setDefaultEncoding() accessor - IMO this is a bad practice. Add two constructors to compensate
Modified:
commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/XmlStreamReader.java
commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.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=1004874&r1=1004873&r2=1004874&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:51:47 2010
@@ -91,8 +91,6 @@ public class XmlStreamReader extends Rea
};
- private static String staticDefaultEncoding = null;
-
private Reader reader;
private String encoding;
@@ -100,20 +98,6 @@ public class XmlStreamReader extends Rea
private final String defaultEncoding;
/**
- * Sets the default encoding to use if none is set in HTTP content-type, XML
- * prolog and the rules based on content-type are not adequate.
- * <p>
- * If it is set to NULL the content-type based rules are used.
- * <p>
- * By default it is NULL.
- *
- * @param encoding charset encoding to default to.
- */
- public static void setDefaultEncoding(String encoding) {
- staticDefaultEncoding = encoding;
- }
-
- /**
* Returns the default encoding to use if none is set in HTTP content-type,
* XML prolog and the rules based on content-type are not adequate.
* <p>
@@ -121,8 +105,8 @@ public class XmlStreamReader extends Rea
*
* @return the default encoding to use.
*/
- public static String getDefaultEncoding() {
- return staticDefaultEncoding;
+ public String getDefaultEncoding() {
+ return defaultEncoding;
}
/**
@@ -184,7 +168,39 @@ public class XmlStreamReader extends Rea
* be determined according to the specs.
*/
public XmlStreamReader(InputStream is, boolean lenient) throws IOException {
- defaultEncoding = staticDefaultEncoding;
+ this(is, lenient, null);
+ }
+
+ /**
+ * Creates a Reader for a raw InputStream.
+ * <p>
+ * It follows the same logic used for files.
+ * <p>
+ * If lenient detection is indicated and the detection above fails as per
+ * specifications it then attempts the following:
+ * <p>
+ * If the content type was 'text/html' it replaces it with 'text/xml' and
+ * tries the detection again.
+ * <p>
+ * Else if the XML prolog had a charset encoding that encoding is used.
+ * <p>
+ * Else if the content type had a charset encoding that encoding is used.
+ * <p>
+ * Else 'UTF-8' is used.
+ * <p>
+ * If lenient detection is indicated an XmlStreamReaderException is never
+ * thrown.
+ *
+ * @param is InputStream to create a Reader from.
+ * @param lenient indicates if the charset encoding detection should be
+ * relaxed.
+ * @param defaultEncoding The default encoding
+ * @throws IOException thrown if there is a problem reading the stream.
+ * @throws XmlStreamReaderException thrown if the charset encoding could not
+ * be determined according to the specs.
+ */
+ public XmlStreamReader(InputStream is, boolean lenient, String defaultEncoding) throws IOException {
+ this.defaultEncoding = defaultEncoding;
doRawStream(is, lenient);
}
@@ -206,7 +222,7 @@ public class XmlStreamReader extends Rea
* the URL.
*/
public XmlStreamReader(URL url) throws IOException {
- this(url.openConnection());
+ this(url.openConnection(), null);
}
/**
@@ -224,11 +240,12 @@ public class XmlStreamReader extends Rea
* the lenient parameter for details.
*
* @param conn URLConnection to create a Reader from.
+ * @param defaultEncoding The default encoding
* @throws IOException thrown if there is a problem reading the stream of
* the URLConnection.
*/
- public XmlStreamReader(URLConnection conn) throws IOException {
- defaultEncoding = staticDefaultEncoding;
+ public XmlStreamReader(URLConnection conn, String defaultEncoding) throws IOException {
+ this.defaultEncoding = defaultEncoding;
boolean lenient = true;
String contentType = conn.getContentType();
if (conn instanceof HttpURLConnection || contentType != null) {
@@ -296,8 +313,7 @@ public class XmlStreamReader extends Rea
*/
public XmlStreamReader(InputStream is, String httpContentType,
boolean lenient, String defaultEncoding) throws IOException {
- this.defaultEncoding = (defaultEncoding == null) ? staticDefaultEncoding
- : defaultEncoding;
+ this.defaultEncoding = defaultEncoding;
doHttpStream(is, httpContentType, lenient);
}
Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java?rev=1004874&r1=1004873&r2=1004874&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java Wed Oct 6 01:51:47 2010
@@ -219,8 +219,7 @@ public class XmlStreamReaderTest extends
try {
InputStream is = getXmlStream(bomEnc, (prologEnc == null) ? XML1
: XML3, streamEnc, prologEnc);
- XmlStreamReader.setDefaultEncoding(alternateEnc);
- XmlStreamReader xmlReader = new XmlStreamReader(is, cT, false);
+ XmlStreamReader xmlReader = new XmlStreamReader(is, cT, false, alternateEnc);
if (!streamEnc.equals("UTF-16")) {
// we can not assert things here because UTF-8, US-ASCII and
// ISO-8859-1 look alike for the chars used for detection
@@ -230,7 +229,6 @@ public class XmlStreamReaderTest extends
streamEnc.length()), streamEnc);
}
} finally {
- XmlStreamReader.setDefaultEncoding(null);
}
}