Skip to content

Commit 447d998

Browse files
author
Niall Pemberton
committed
IO-162 Remove the static setDefaultEncoding() accessor - IMO this is a bad practice. Add two constructors to compensate
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1004874 13f79535-47bb-0310-9956-ffa450edef68
1 parent 8a15299 commit 447d998

2 files changed

Lines changed: 41 additions & 27 deletions

File tree

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

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -91,28 +91,12 @@ public class XmlStreamReader extends Reader {
9191
};
9292

9393

94-
private static String staticDefaultEncoding = null;
95-
9694
private Reader reader;
9795

9896
private String encoding;
9997

10098
private final String defaultEncoding;
10199

102-
/**
103-
* Sets the default encoding to use if none is set in HTTP content-type, XML
104-
* prolog and the rules based on content-type are not adequate.
105-
* <p>
106-
* If it is set to NULL the content-type based rules are used.
107-
* <p>
108-
* By default it is NULL.
109-
*
110-
* @param encoding charset encoding to default to.
111-
*/
112-
public static void setDefaultEncoding(String encoding) {
113-
staticDefaultEncoding = encoding;
114-
}
115-
116100
/**
117101
* Returns the default encoding to use if none is set in HTTP content-type,
118102
* XML prolog and the rules based on content-type are not adequate.
@@ -121,8 +105,8 @@ public static void setDefaultEncoding(String encoding) {
121105
*
122106
* @return the default encoding to use.
123107
*/
124-
public static String getDefaultEncoding() {
125-
return staticDefaultEncoding;
108+
public String getDefaultEncoding() {
109+
return defaultEncoding;
126110
}
127111

128112
/**
@@ -184,7 +168,39 @@ public XmlStreamReader(InputStream is) throws IOException {
184168
* be determined according to the specs.
185169
*/
186170
public XmlStreamReader(InputStream is, boolean lenient) throws IOException {
187-
defaultEncoding = staticDefaultEncoding;
171+
this(is, lenient, null);
172+
}
173+
174+
/**
175+
* Creates a Reader for a raw InputStream.
176+
* <p>
177+
* It follows the same logic used for files.
178+
* <p>
179+
* If lenient detection is indicated and the detection above fails as per
180+
* specifications it then attempts the following:
181+
* <p>
182+
* If the content type was 'text/html' it replaces it with 'text/xml' and
183+
* tries the detection again.
184+
* <p>
185+
* Else if the XML prolog had a charset encoding that encoding is used.
186+
* <p>
187+
* Else if the content type had a charset encoding that encoding is used.
188+
* <p>
189+
* Else 'UTF-8' is used.
190+
* <p>
191+
* If lenient detection is indicated an XmlStreamReaderException is never
192+
* thrown.
193+
*
194+
* @param is InputStream to create a Reader from.
195+
* @param lenient indicates if the charset encoding detection should be
196+
* relaxed.
197+
* @param defaultEncoding The default encoding
198+
* @throws IOException thrown if there is a problem reading the stream.
199+
* @throws XmlStreamReaderException thrown if the charset encoding could not
200+
* be determined according to the specs.
201+
*/
202+
public XmlStreamReader(InputStream is, boolean lenient, String defaultEncoding) throws IOException {
203+
this.defaultEncoding = defaultEncoding;
188204
doRawStream(is, lenient);
189205
}
190206

@@ -206,7 +222,7 @@ public XmlStreamReader(InputStream is, boolean lenient) throws IOException {
206222
* the URL.
207223
*/
208224
public XmlStreamReader(URL url) throws IOException {
209-
this(url.openConnection());
225+
this(url.openConnection(), null);
210226
}
211227

212228
/**
@@ -224,11 +240,12 @@ public XmlStreamReader(URL url) throws IOException {
224240
* the lenient parameter for details.
225241
*
226242
* @param conn URLConnection to create a Reader from.
243+
* @param defaultEncoding The default encoding
227244
* @throws IOException thrown if there is a problem reading the stream of
228245
* the URLConnection.
229246
*/
230-
public XmlStreamReader(URLConnection conn) throws IOException {
231-
defaultEncoding = staticDefaultEncoding;
247+
public XmlStreamReader(URLConnection conn, String defaultEncoding) throws IOException {
248+
this.defaultEncoding = defaultEncoding;
232249
boolean lenient = true;
233250
String contentType = conn.getContentType();
234251
if (conn instanceof HttpURLConnection || contentType != null) {
@@ -296,8 +313,7 @@ public XmlStreamReader(InputStream is, String httpContentType)
296313
*/
297314
public XmlStreamReader(InputStream is, String httpContentType,
298315
boolean lenient, String defaultEncoding) throws IOException {
299-
this.defaultEncoding = (defaultEncoding == null) ? staticDefaultEncoding
300-
: defaultEncoding;
316+
this.defaultEncoding = defaultEncoding;
301317
doHttpStream(is, httpContentType, lenient);
302318
}
303319

src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,7 @@ public void _testAlternateDefaultEncoding(String cT, String bomEnc,
219219
try {
220220
InputStream is = getXmlStream(bomEnc, (prologEnc == null) ? XML1
221221
: XML3, streamEnc, prologEnc);
222-
XmlStreamReader.setDefaultEncoding(alternateEnc);
223-
XmlStreamReader xmlReader = new XmlStreamReader(is, cT, false);
222+
XmlStreamReader xmlReader = new XmlStreamReader(is, cT, false, alternateEnc);
224223
if (!streamEnc.equals("UTF-16")) {
225224
// we can not assert things here because UTF-8, US-ASCII and
226225
// ISO-8859-1 look alike for the chars used for detection
@@ -230,7 +229,6 @@ public void _testAlternateDefaultEncoding(String cT, String bomEnc,
230229
streamEnc.length()), streamEnc);
231230
}
232231
} finally {
233-
XmlStreamReader.setDefaultEncoding(null);
234232
}
235233
}
236234

0 commit comments

Comments
 (0)