public class XmlStreamReader extends Reader
IMPORTANT: 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, XmlStreamReader handles it and things work in all parsers).
The XmlStreamReader 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 a 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.
To build an instance, use XmlStreamReader.Builder.
Originally developed for ROME under Apache License 2.0.
XmlStreamReader.Builder,
XmlStreamWriter| Modifier and Type | Class and Description |
|---|---|
static class |
XmlStreamReader.Builder
Builds a new
XmlStreamWriter. |
| Modifier and Type | Field and Description |
|---|---|
static Pattern |
ENCODING_PATTERN
Pattern capturing the encoding of the
'xml' processing instruction. |
| Constructor and Description |
|---|
XmlStreamReader(URL url)
Constructs a Reader using the InputStream of a URL.
|
XmlStreamReader(URLConnection urlConnection,
String defaultEncoding)
Constructs a Reader using the InputStream of a URLConnection.
|
| Modifier and Type | Method and Description |
|---|---|
static XmlStreamReader.Builder |
builder()
Constructs a new
XmlStreamReader.Builder. |
(package private) String |
calculateHttpEncoding(String bomEnc,
String xmlGuessEnc,
String xmlEnc,
boolean lenient,
String httpContentType)
Calculates the HTTP encoding.
|
(package private) String |
calculateRawEncoding(String bomEnc,
String xmlGuessEnc,
String xmlEnc)
Calculate the raw encoding.
|
void |
close()
Closes the XmlStreamReader stream.
|
(package private) static String |
getContentTypeEncoding(String httpContentType)
Gets the charset parameter value,
null if not present, null if httpContentType is null. |
(package private) static String |
getContentTypeMime(String httpContentType)
Gets the MIME type or
null if httpContentType is null. |
String |
getDefaultEncoding()
Gets 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.
|
String |
getEncoding()
Gets the charset encoding of the XmlStreamReader.
|
(package private) static boolean |
isAppXml(String mime)
Tests if the MIME type belongs to the APPLICATION XML family.
|
(package private) static boolean |
isTextXml(String mime)
Tests if the MIME type belongs to the TEXT XML family.
|
int |
read(char[] buf,
int offset,
int len)
Reads the underlying reader's
read(char[], int, int) method. |
public static final Pattern ENCODING_PATTERN
'xml' processing instruction.
See also the NT-EncName XML specification.
Note the documented pattern is:
EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')*
However this does not match all the aliases that are supported by Java. For example, '437', 'ISO_8859-1:1987' and
'ebcdic-de-273+euro'.
public XmlStreamReader(URL url) throws IOException
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.
url - URL to create a Reader from.NullPointerException - if the input is null.IOException - thrown if there is a problem reading the stream of the URL.public XmlStreamReader(URLConnection urlConnection, String defaultEncoding) throws IOException
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.
urlConnection - URLConnection to create a Reader from.defaultEncoding - The default encodingNullPointerException - if the input is null.IOException - thrown if there is a problem reading the stream of the URLConnection.public static XmlStreamReader.Builder builder()
XmlStreamReader.Builder.XmlStreamReader.Builder.String calculateHttpEncoding(String bomEnc, String xmlGuessEnc, String xmlEnc, boolean lenient, String httpContentType) throws IOException
bomEnc - BOM encodingxmlGuessEnc - XML Guess encodingxmlEnc - XML encodinglenient - indicates if the charset encoding detection should be relaxed.httpContentType - The HTTP content typeIOException - thrown if there is a problem reading the stream.String calculateRawEncoding(String bomEnc, String xmlGuessEnc, String xmlEnc) throws IOException
bomEnc - BOM encodingxmlGuessEnc - XML Guess encodingxmlEnc - XML encodingIOException - thrown if there is a problem reading the stream.public void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableclose in class ReaderIOException - thrown if there was a problem closing the stream.static String getContentTypeEncoding(String httpContentType)
null if not present, null if httpContentType is null.httpContentType - the HTTP content typestatic String getContentTypeMime(String httpContentType)
null if httpContentType is null.httpContentType - the HTTP content typepublic String getDefaultEncoding()
If it is null the content-type based rules are used.
public String getEncoding()
static boolean isAppXml(String mime)
mime - The mime typestatic boolean isTextXml(String mime)
mime - The mime typepublic int read(char[] buf,
int offset,
int len)
throws IOException
read(char[], int, int) method.read in class Readerbuf - the buffer to read the characters intooffset - The start offsetlen - The number of bytes to readIOException - if an I/O error occurs.