com.xmlmind.util
Class XMLUtil

java.lang.Object
  extended by com.xmlmind.util.XMLUtil

public final class XMLUtil
extends java.lang.Object

A collection of utility functions (static methods) related to XML.


Method Summary
static java.io.Reader createReader(java.io.InputStream in, java.lang.String fallbackEncoding, java.lang.String[] encoding)
          Creates a reader allowing to read the contents of specified text source.
static java.lang.String detectEncoding(byte[] bytes, int byteCount, int[] bomLength)
          Detect XML encoding by examining specified bytes which have been read at the very start of an XML file.
static java.lang.String loadChars(java.io.Reader in)
          Load the characters contained in specified source.
static java.lang.String loadText(java.io.File file, java.lang.String[] encoding)
          Equivalent to loadText(FileUtil.fileToURL(file), encoding).
static java.lang.String loadText(java.io.InputStream in, java.lang.String fallbackEncoding, java.lang.String[] encoding)
          Loads the contents of specified text source.
static java.lang.String loadText(java.net.URL url, java.lang.String[] encoding)
          Loads the contents of specified text file.
static javax.xml.parsers.SAXParser newSAXParser()
          Equivalent to newSAXParser(true, false, false).
static javax.xml.parsers.SAXParser newSAXParser(boolean namespaceAware, boolean validating, boolean xIncludeAware)
          Convenience method: creates and returns a SAXParser.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

newSAXParser

public static javax.xml.parsers.SAXParser newSAXParser()
                                                throws javax.xml.parsers.ParserConfigurationException,
                                                       org.xml.sax.SAXException
Equivalent to newSAXParser(true, false, false).

Throws:
javax.xml.parsers.ParserConfigurationException
org.xml.sax.SAXException

newSAXParser

public static javax.xml.parsers.SAXParser newSAXParser(boolean namespaceAware,
                                                       boolean validating,
                                                       boolean xIncludeAware)
                                                throws javax.xml.parsers.ParserConfigurationException,
                                                       org.xml.sax.SAXException
Convenience method: creates and returns a SAXParser.

Parameters:
namespaceAware - specifies whether the parser produced by this code will provide support for XML namespaces
validating - specifies whether the parser produced by this code will validate documents against their DTD
xIncludeAware - specifies whether the parser produced by this code will process XIncludes
Returns:
newly created SAXParser
Throws:
javax.xml.parsers.ParserConfigurationException - if a parser cannot be created which satisfies the requested configuration
org.xml.sax.SAXException - for SAX errors

loadText

public static java.lang.String loadText(java.io.File file,
                                        java.lang.String[] encoding)
                                 throws java.io.IOException
Equivalent to loadText(FileUtil.fileToURL(file), encoding).

Throws:
java.io.IOException

loadText

public static java.lang.String loadText(java.net.URL url,
                                        java.lang.String[] encoding)
                                 throws java.io.IOException
Loads the contents of specified text file.

Unlike URLUtil.loadString(java.net.URL), this method implements the detection of the encoding.

Note that the detection of the encoding always works because it uses a fallback value.

Parameters:
url - the location of the text file
encoding - the detected encoding is copied there. May be null.
Returns:
the contents of the text file
Throws:
java.io.IOException - if there is an I/O problem

loadText

public static java.lang.String loadText(java.io.InputStream in,
                                        java.lang.String fallbackEncoding,
                                        java.lang.String[] encoding)
                                 throws java.io.IOException
Loads the contents of specified text source.

This method implements the detection of the encoding.

Note that the detection of the encoding always works because it uses a fallback value.

Parameters:
in - the text source
fallbackEncoding - the fallback encoding May be null.
encoding - the detected encoding is copied there. May be null.
Returns:
the contents of the text source
Throws:
java.io.IOException - if there is an I/O problem
See Also:
loadText(URL, String[])

loadChars

public static java.lang.String loadChars(java.io.Reader in)
                                  throws java.io.IOException
Load the characters contained in specified source.

Parameters:
in - the character source
Returns:
the contents of the character source
Throws:
java.io.IOException - if there is an I/O problem

createReader

public static java.io.Reader createReader(java.io.InputStream in,
                                          java.lang.String fallbackEncoding,
                                          java.lang.String[] encoding)
                                   throws java.io.IOException
Creates a reader allowing to read the contents of specified text source.

This method implements the detection of the encoding.

Note that the detection of the encoding always works because it uses a fallback value.

Parameters:
in - the text source
encoding - the detected encoding is copied there. May be null.
Returns:
a reader allowing to read the contents of the text source. This reader will automatically skip the BOM if any.
Throws:
java.io.IOException - if there is an I/O problem

detectEncoding

public static java.lang.String detectEncoding(byte[] bytes,
                                              int byteCount,
                                              int[] bomLength)
Detect XML encoding by examining specified bytes which have been read at the very start of an XML file.

Parameters:
bytes - bytes read at the very start of an XML file
byteCount - number of bytes read at the very start of an XML file
bomLength - the length of the BOM is stored as the first element of this array. This allows to skip the BOM. May be null.
Returns:
encoding if detected; null otherwise