2.17.2: 2011-06-17

net.sf.basedb.util
Class XMLUtil

java.lang.Object
  extended by net.sf.basedb.util.XMLUtil

public class XMLUtil
extends Object

This class contains some useful methods for handling XML documents.

Version:
2.0
Author:
Nicklas
Last modified
$Date$

Nested Class Summary
private static class XMLUtil.Validator
          This class is used to handle callbacks from the XML parser.
 
Field Summary
private static Logger log
          Log core events.
private static String xmlParserClass
          The parser class we are using.
 
Constructor Summary
XMLUtil()
           
 
Method Summary
static Document createDom(String rootElement, String systemId)
          Create a new Document with root element and System id declaration.
static boolean getBooleanAttribute(Element el, String attribute, boolean defaultValue)
          Get the value of an attribute as a boolean.
static int getIntAttribute(Element el, String attribute, int defaultValue)
          Get the value of an attribute as an integer.
static Document getSchemaValidatedXML(InputStream in, String filename, String... schemaFiles)
          Validate an XML input stream using XML schemas.
static Document getSchemaValidatedXML(URL xmlFile, String... schemaFiles)
          Validate an XML file using XML schemas.
static Document getValidatedXML(InputStream is, URL dtdFile)
          Deprecated. Use getValidatedXML(InputStream, URL, String) instead
static Document getValidatedXML(InputStream is, URL dtdFile, String filename)
          Validate an InputStream against a DTD file and return it as a Document
static Document getValidatedXml(String xml, URL dtdFile)
           
static Document getValidatedXml(URL xmlFile, URL dtdFile)
          Load and validate an XML file against a DTD, and return it as a Document object.
static Document getXml(String xml, URL dtdFile, boolean validate)
          Parse a string containing XML, optionally validating it against a DTD, and return it as a Document object.
static String toXml(Document dom)
          Convert a Document to xml.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private static final Logger log
Log core events.


xmlParserClass

private static final String xmlParserClass
The parser class we are using.

See Also:
Constant Field Values
Constructor Detail

XMLUtil

public XMLUtil()
Method Detail

toXml

public static String toXml(Document dom)
Convert a Document to xml. The encoding is set to UTF-8.

Parameters:
dom - The document
Returns:
The XML

createDom

public static Document createDom(String rootElement,
                                 String systemId)
Create a new Document with root element and System id declaration.

Parameters:
rootElement - The name of the root element tag
systemId - The system id in the DOCTYPE section
Returns:
a Document object

getValidatedXML

public static Document getValidatedXML(InputStream is,
                                       URL dtdFile)
                                throws JDOMException,
                                       IOException
Deprecated. Use getValidatedXML(InputStream, URL, String) instead

Throws:
JDOMException
IOException

getValidatedXml

public static Document getValidatedXml(URL xmlFile,
                                       URL dtdFile)
                                throws JDOMException,
                                       IOException
Load and validate an XML file against a DTD, and return it as a Document object. The path to the files should be given as a URL, not a filesystem path. For a file on the classpath it is easiest done by the Class.getResource(String) method, ie. XMLUtil.class.getResource("/net/sf/basedb/core/the-xml-file.xml").

Parameters:
xmlFile - The URL to the XML file
dtdFile - The URL to the DTD used for validation, the DTD must be encoded in ISO-8859-1
Returns:
A Document object
Throws:
JDOMException - If validation of XML fails.
IOException - If reading the input stream fails.

getValidatedXml

public static Document getValidatedXml(String xml,
                                       URL dtdFile)
                                throws JDOMException,
                                       IOException
Throws:
JDOMException
IOException
See Also:
getXml(String, URL, boolean)

getXml

public static Document getXml(String xml,
                              URL dtdFile,
                              boolean validate)
                       throws JDOMException,
                              IOException
Parse a string containing XML, optionally validating it against a DTD, and return it as a Document object. The path to the DTD should be given as a URL, not a filesystem path. For a file on the classpath it is easiest done by the Class.getResource(String) method, ie. XMLUtil.class.getResource("/net/sf/basedb/core/the-dtd-file.dtd").

Parameters:
xml - The string containing the XML
dtdFile - The URL to the DTD used for validation, the DTD must be encoded in ISO-8859-1
validate - TRUE to validate the xml data
Returns:
A Document object
Throws:
JDOMException - If validation of XML fails.
IOException - If reading the string fails.
Since:
2.15

getValidatedXML

public static Document getValidatedXML(InputStream is,
                                       URL dtdFile,
                                       String filename)
                                throws JDOMException,
                                       IOException
Validate an InputStream against a DTD file and return it as a Document

Parameters:
is - The inputstream to the XML
dtdFile - The URL to the DTD used for validation or null to not valdiate against a DTD. The DTD must be encoded in ISO-8859-1
filename - The filename of the original XML data, used for error reporting only; use null if the filename is not known
Returns:
A Document object
Throws:
JDOMException - If validation of the XML fails.
IOException - If reading the input stream fails.
Since:
2.4

getSchemaValidatedXML

public static Document getSchemaValidatedXML(URL xmlFile,
                                             String... schemaFiles)
                                      throws JDOMException,
                                             IOException
Validate an XML file using XML schemas.

Parameters:
xmlFile - The URL pointing to the XML file to load
schemaFiles - An array of schema file declarations. The array must contain pairs of elements, the first one is the namespace name, and the second element is the URL to the schema definition file
Returns:
A Document object
Throws:
JDOMException - If validation of the XML fails
IOException - If reading the input stream fails
Since:
2.7

getSchemaValidatedXML

public static Document getSchemaValidatedXML(InputStream in,
                                             String filename,
                                             String... schemaFiles)
                                      throws JDOMException,
                                             IOException
Validate an XML input stream using XML schemas.

Parameters:
in - The inputstream to the XML
filename - The filename of the original XML data, used for error reporting only; use null if the filename is not known
schemaFiles - An array of schema file declarations. The array must contain pairs of elements, the first one is the namespace name, and the second element is the URL to the schema definition file
Returns:
A Document object
Throws:
JDOMException - If validation of the XML fails
IOException - If reading the input stream fails
Since:
2.7

getIntAttribute

public static int getIntAttribute(Element el,
                                  String attribute,
                                  int defaultValue)
Get the value of an attribute as an integer. If the attribute is missing or doesn't contain an integer, the default value is returned.

Parameters:
el - The node element which contains the attribute
attribute - The name of the attribute
defaultValue - The default value to return
Returns:
an integer.

getBooleanAttribute

public static boolean getBooleanAttribute(Element el,
                                          String attribute,
                                          boolean defaultValue)
Get the value of an attribute as a boolean. If the attribute is missing or doesn't contain an boolean value, the default value is returned.

Parameters:
el - The node element which contains the attribute
attribute - The name of the attribute
defaultValue - The default value to return
Returns:
TRUE if the attribute has a true value, FALSE if it has a false value

2.17.2: 2011-06-17