Important Note
This class is not related in any way to the org.xml.sax.XMLReader. This one IS a character stream.
All this has to be done without consuming characters from the stream, if not the XML parser will not recognized the document as a valid XML. This is not 100% true, but it's close enough (UTF-8 BOM is not handled by all parsers right now, XmlReader handles it and things work in all parsers).
The XmlReader class handles the charset encoding of XML documents in Files, raw streams and HTTP streams by offering a wide set of constructors.
By default the charset encoding detection is lenient, the constructor with the lenient flag can be used for an script (following HTTP MIME and XML specifications). All this is nicely explained by Mark Pilgrim in his blog, Determining the character encoding of a feed.
| Modifier and Type | Field and Description |
|---|---|
| private static final int | |
| private static final Pattern | |
| private static final String | |
| private final String | |
| private String | |
| private static final Pattern | |
| private static final MessageFormat | |
| private static final MessageFormat | |
| private static final MessageFormat | |
| private static final MessageFormat | |
| private static final MessageFormat | |
| private Reader | |
| private static String | |
| private static final String | |
| private static final String | |
| private static final String | |
| private static final String | |
| private static final String |
| Access | Constructor and Description |
|---|---|
| public | |
| public | XmlReader(final InputStream
InputStream to create a Reader from. is)Creates a Reader for a raw InputStream. |
| public | XmlReader(final InputStream
InputStream to create a Reader from. is, final boolean indicates if the charset encoding detection should be relaxed. lenient, final String default encoding to use if one cannot be detected. defaultEncoding)Creates a Reader for a raw InputStream and uses the provided default encoding if none is determined. |
| public | XmlReader(final InputStream
InputStream to create a Reader from. is, final boolean indicates if the charset encoding detection should be relaxed. lenient)Creates a Reader for a raw InputStream. |
| public | |
| public | XmlReader(final URL
URL to create a Reader from. url, final Map<String, String> optional Map of headers to set on http request. requestHeaders)
Deprecated
As of release 2.0.0 and will be removed in version 3.0.0 due to too many problems with this approach.
Creates a Reader using the InputStream of a URL.
|
| public | XmlReader(final URLConnection
URLConnection to create a Reader from. conn)
Deprecated
As of release 2.0.0 and will be removed in version 3.0.0 due to too many problems with this approach.
Creates a Reader using the InputStream of a URLConnection.
|
| public | XmlReader(final URLConnection
URLConnection to create a Reader from. conn, final Map<String, String> optional Map of headers to set on http request. requestHeaders)
Deprecated
As of release 2.0.0 and will be removed in version 3.0.0 due to too many problems with this approach.
Creates a Reader using the InputStream of a URLConnection.
|
| public | XmlReader(final InputStream
InputStream to create the reader from. is, final String content-type header to use for the resolution of the charset encoding. httpContentType)Creates a Reader using an InputStream and the associated content-type header. |
| public | XmlReader(final InputStream
InputStream to create the reader from. is, final String content-type header to use for the resolution of the charset encoding. httpContentType, final boolean indicates if the charset encoding detection should be relaxed. lenient, final String default encoding to use if one cannot be detected. defaultEncoding)Creates a Reader using an InputStream and the associated content-type header. |
| public | XmlReader(final InputStream
InputStream to create the reader from. is, final String content-type header to use for the resolution of the charset encoding. httpContentType, final boolean indicates if the charset encoding detection should be relaxed. lenient)Creates a Reader using an InputStream and the associated content-type header. |
| Modifier and Type | Method and Description |
|---|---|
| private String | calculateHttpEncoding(final String cTMime, final String cTEnc, final String bomEnc, final String xmlGuessEnc, final String xmlEnc, final InputStream is, final boolean lenient)
|
| private String | calculateRawEncoding(final String bomEnc, final String xmlGuessEnc, final String xmlEnc, final InputStream is)
|
| public void | close()
Implements abstract java. Implements java. |
| private void | |
| private void | |
| private void | |
| private static String | |
| private static String | |
| private static String | |
| public static String | Returns: the default encoding to use.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. |
| public String | |
| private static String | |
| pack-priv static String | |
| private static boolean | |
| private static boolean | |
| private void | |
| public int | read(final char[]
Destination buffer buf, final int Offset at which to start storing characters offset, final int Maximum number of characters to read len)Implements abstract java. |
| public static void | setDefaultEncoding(final String
charset encoding to default to. encoding)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. |
| private void |
| BUFFER_SIZE | back to summary |
|---|---|
| private static final int BUFFER_SIZE | |
| CHARSET_PATTERN | back to summary |
|---|---|
| private static final Pattern CHARSET_PATTERN | |
| CP1047 | back to summary |
|---|---|
| private static final String CP1047 | |
| defaultEncoding | back to summary |
|---|---|
| private final String defaultEncoding | |
| encoding | back to summary |
|---|---|
| private String encoding | |
| ENCODING_PATTERN | back to summary |
|---|---|
| private static final Pattern ENCODING_PATTERN | |
| HTTP_EX_1 | back to summary |
|---|---|
| private static final MessageFormat HTTP_EX_1 | |
| HTTP_EX_2 | back to summary |
|---|---|
| private static final MessageFormat HTTP_EX_2 | |
| HTTP_EX_3 | back to summary |
|---|---|
| private static final MessageFormat HTTP_EX_3 | |
| RAW_EX_1 | back to summary |
|---|---|
| private static final MessageFormat RAW_EX_1 | |
| RAW_EX_2 | back to summary |
|---|---|
| private static final MessageFormat RAW_EX_2 | |
| reader | back to summary |
|---|---|
| private Reader reader | |
| staticDefaultEncoding | back to summary |
|---|---|
| private static String staticDefaultEncoding | |
| US_ASCII | back to summary |
|---|---|
| private static final String US_ASCII | |
| UTF_16 | back to summary |
|---|---|
| private static final String UTF_16 | |
| UTF_16BE | back to summary |
|---|---|
| private static final String UTF_16BE | |
| UTF_16LE | back to summary |
|---|---|
| private static final String UTF_16LE | |
| UTF_8 | back to summary |
|---|---|
| private static final String UTF_8 | |
| XmlReader | back to summary |
|---|---|
| public XmlReader(final File file) throws IOException Creates a Reader for a File. It looks for the UTF-8 BOM first, if none sniffs the XML prolog charset, if this is also missing defaults to UTF-8. It does a lenient charset encoding detection, check the constructor with the lenient parameter for details.
| |
| XmlReader | back to summary |
|---|---|
| public XmlReader(final InputStream is) throws IOException Creates a Reader for a raw InputStream. It follows the same logic used for files. It does a lenient charset encoding detection, check the constructor with the lenient parameter for details.
| |
| XmlReader | back to summary |
|---|---|
| public XmlReader(final InputStream is, final boolean lenient, final String defaultEncoding) throws IOException, XmlReaderException Creates a Reader for a raw InputStream and uses the provided default encoding if none is determined. It follows the same logic used for files. If lenient detection is indicated and the detection above fails as per specifications it then attempts the following: If the content type was 'text/html' it replaces it with 'text/xml' and tries the detection again. Else if the XML prolog had a charset encoding that encoding is used. Else if the content type had a charset encoding that encoding is used. Else 'UTF-8' is used. If lenient detection is indicated an XmlReaderException is never thrown.
| |
| XmlReader | back to summary |
|---|---|
| public XmlReader(final InputStream is, final boolean lenient) throws IOException, XmlReaderException Creates a Reader for a raw InputStream. It follows the same logic used for files. If lenient detection is indicated and the detection above fails as per specifications it then attempts the following: If the content type was 'text/html' it replaces it with 'text/xml' and tries the detection again. Else if the XML prolog had a charset encoding that encoding is used. Else if the content type had a charset encoding that encoding is used. Else 'UTF-8' is used. If lenient detection is indicated an XmlReaderException is never thrown.
| |
| XmlReader | back to summary |
|---|---|
| public XmlReader(final URL url) throws IOException
Deprecated As of release 2.0.0 and will be removed in version 3.0.0 due to too many problems with this approach. Creates a Reader using the InputStream of a URL. If the URL is not of type HTTP and there is not 'content-type' header in the fetched data it uses the same logic used for Files. If the URL is a HTTP Url or there is a 'content-type' header in the fetched data it uses the same logic used for an InputStream with content-type. It does a lenient charset encoding detection, check the constructor with the lenient parameter for details.
| |
| XmlReader | back to summary |
|---|---|
| public XmlReader(final URL url, final Map<String, String> requestHeaders) throws IOException
Deprecated As of release 2.0.0 and will be removed in version 3.0.0 due to too many problems with this approach. Creates a Reader using the InputStream of a URL. If the URL is not of type HTTP and there is not 'content-type' header in the fetched data it uses the same logic used for Files. If the URL is a HTTP Url or there is a 'content-type' header in the fetched data it uses the same logic used for an InputStream with content-type. It does a lenient charset encoding detection, check the constructor with the lenient parameter for details.
| |
| XmlReader | back to summary |
|---|---|
| public XmlReader(final URLConnection conn) throws IOException
Deprecated As of release 2.0.0 and will be removed in version 3.0.0 due to too many problems with this approach. Creates a Reader using the InputStream of a URLConnection. If the URLConnection is not of type HttpURLConnection and there is not 'content-type' header in the fetched data it uses the same logic used for files. If the URLConnection is a HTTP Url or there is a 'content-type' header in the fetched data it uses the same logic used for an InputStream with content-type. It does a lenient charset encoding detection, check the constructor with the lenient parameter for details.
| |
| XmlReader | back to summary |
|---|---|
| public XmlReader(final URLConnection conn, final Map<String, String> requestHeaders) throws IOException
Deprecated As of release 2.0.0 and will be removed in version 3.0.0 due to too many problems with this approach. Creates a Reader using the InputStream of a URLConnection. If the URLConnection is not of type HttpURLConnection and there is not 'content-type' header in the fetched data it uses the same logic used for files. If the URLConnection is a HTTP Url or there is a 'content-type' header in the fetched data it uses the same logic used for an InputStream with content-type. It does a lenient charset encoding detection, check the constructor with the lenient parameter for details.
| |
| XmlReader | back to summary |
|---|---|
| public XmlReader(final InputStream is, final String httpContentType) throws IOException Creates a Reader using an InputStream and the associated content-type header. First it checks if the stream has BOM. If there is not BOM checks the content-type encoding. If there is not content-type encoding checks the XML prolog encoding. If there is not XML prolog encoding uses the default encoding mandated by the content-type MIME type. It does a lenient charset encoding detection, check the constructor with the lenient parameter for details.
| |
| XmlReader | back to summary |
|---|---|
| public XmlReader(final InputStream is, final String httpContentType, final boolean lenient, final String defaultEncoding) throws IOException, XmlReaderException Creates a Reader using an InputStream and the associated content-type header. First it checks if the stream has BOM. If there is not BOM checks the content-type encoding. If there is not content-type encoding checks the XML prolog encoding. If there is not XML prolog encoding uses the default encoding mandated by the content-type MIME type. If lenient detection is indicated and the detection above fails as per specifications it then attempts the following: If the content type was 'text/html' it replaces it with 'text/xml' and tries the detection again. Else if the XML prolog had a charset encoding that encoding is used. Else if the content type had a charset encoding that encoding is used. Else 'UTF-8' is used. If lenient detection is indicated and XmlReaderException is never thrown.
| |
| XmlReader | back to summary |
|---|---|
| public XmlReader(final InputStream is, final String httpContentType, final boolean lenient) throws IOException, XmlReaderException Creates a Reader using an InputStream and the associated content-type header. First it checks if the stream has BOM. If there is not BOM checks the content-type encoding. If there is not content-type encoding checks the XML prolog encoding. If there is not XML prolog encoding uses the default encoding mandated by the content-type MIME type. If lenient detection is indicated and the detection above fails as per specifications it then attempts the following: If the content type was 'text/html' it replaces it with 'text/xml' and tries the detection again. Else if the XML prolog had a charset encoding that encoding is used. Else if the content type had a charset encoding that encoding is used. Else 'UTF-8' is used. If lenient detection is indicated and XmlReaderException is never thrown.
| |
| calculateHttpEncoding | back to summary |
|---|---|
| private String calculateHttpEncoding(final String cTMime, final String cTEnc, final String bomEnc, final String xmlGuessEnc, final String xmlEnc, final InputStream is, final boolean lenient) throws IOException | |
| calculateRawEncoding | back to summary |
|---|---|
| private String calculateRawEncoding(final String bomEnc, final String xmlGuessEnc, final String xmlEnc, final InputStream is) throws IOException | |
| close | back to summary |
|---|---|
| public void close() throws IOException Implements abstract java. Implements java. Closes the XmlReader stream.
| |
| doHttpStream | back to summary |
|---|---|
| private void doHttpStream(final InputStream is, final String httpContentType, final boolean lenient) throws IOException | |
| doLenientDetection | back to summary |
|---|---|
| private void doLenientDetection(String httpContentType, XmlReaderException ex) throws IOException | |
| doRawStream | back to summary |
|---|---|
| private void doRawStream(final InputStream is, final boolean lenient) throws IOException | |
| getBOMEncoding | back to summary |
|---|---|
| private static String getBOMEncoding(final BufferedInputStream is) throws IOException | |
| getContentTypeEncoding | back to summary |
|---|---|
| private static String getContentTypeEncoding(final String httpContentType) | |
| getContentTypeMime | back to summary |
|---|---|
| private static String getContentTypeMime(final String httpContentType) | |
| getDefaultEncoding | back to summary |
|---|---|
| public static String getDefaultEncoding() 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. If it is NULL the content-type based rules are used.
| |
| getEncoding | back to summary |
|---|---|
| public String getEncoding() Returns the charset encoding of the XmlReader.
| |
| getXMLGuessEncoding | back to summary |
|---|---|
| private static String getXMLGuessEncoding(final BufferedInputStream is) throws IOException | |
| getXmlProlog | back to summary |
|---|---|
| pack-priv static String getXmlProlog(final InputStream is, final String guessedEnc) throws IOException | |
| isAppXml | back to summary |
|---|---|
| private static boolean isAppXml(final String mime) | |
| isTextXml | back to summary |
|---|---|
| private static boolean isTextXml(final String mime) | |
| prepareReader | back to summary |
|---|---|
| private void prepareReader(final InputStream is, final String encoding) throws IOException | |
| read | back to summary |
|---|---|
| public int read(final char[] buf, final int offset, final int len) throws IOException Implements abstract java. Doc from java. Reads characters into a portion of an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached. If
| |
| setDefaultEncoding | back to summary |
|---|---|
| public static void setDefaultEncoding(final String encoding) 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. If it is set to NULL the content-type based rules are used. By default it is NULL.
| |
| setRequestHeader | back to summary |
|---|---|
| private void setRequestHeader(final URLConnection conn, final Map<String, String> requestHeaders) | |