org.pentaho.platform.util.xml.dom4j
Class XmlDom4JHelper

java.lang.Object
  extended by org.pentaho.platform.util.xml.dom4j.XmlDom4JHelper

public class XmlDom4JHelper
extends Object

A set of static methods to help in with: * the construction of XML DOM Documents (org.dom4j.Document) from files, streams, and Strings * in the creation of XML DOM Documents as the result of an XSLT transform * persisting of XML DOM documents to the file system or a Writer. * the encoding of a String of Xml text Design notes: This class should never have any dependencies (i.e. imports) on anything on org.pentaho or com.pentaho or their decendant packages. In general, methods in the class should not attempt to handle exceptions, but should let the exceptions propogate to the caller to be handled there. Please do not use european-reuse in this class. One of the primary design goals for this class was to construct it in a way that it could be used without change outside of the Pentaho platform. Related XML-helper type code that is dependant on the platform should be moved "up" to XmlHelper.


Constructor Summary
XmlDom4JHelper()
           
 
Method Summary
static StringBuffer docToString(Document doc)
          Convert a W3C Document to a String.
static org.dom4j.Document getDocFromFile(File file, EntityResolver resolver)
          Create a Document from the contents of a file.
static org.dom4j.Document getDocFromFile(String path, EntityResolver resolver)
          Create a Document from the contents of a file.
static org.dom4j.Document getDocFromStream(InputStream inStream)
          Create a Document from the contents of an input stream, where the input stream contains valid XML.
static org.dom4j.Document getDocFromStream(InputStream inStream, EntityResolver resolver)
          Create a Document from the contents of an input stream, where the input stream contains valid XML.
static org.dom4j.Document getDocFromString(String strXml, EntityResolver resolver)
          Create a Document from str.
static org.dom4j.Document getDocFromString(String strXml, String encoding, EntityResolver resolver)
          Create a Document from str.
static String getNodeText(String xpath, org.dom4j.Node rootNode)
           
static double getNodeText(String xpath, org.dom4j.Node rootNode, double defaultValue)
           
static long getNodeText(String xpath, org.dom4j.Node rootNode, long defaultValue)
           
static String getNodeText(String xpath, org.dom4j.Node rootNode, String defaultValue)
           
static String getXmlEncodedString(String rawValue)
          convert any character in the XML input (rawValue) whose code position is greater than or equal to 0x080 to its Numeric Character Reference.
static void main(String[] args)
           
static void saveDomToFile(org.dom4j.Document doc, File file, String encoding)
          Write an XML document to a file using the specified character encoding.
static void saveDomToFile(org.dom4j.Document doc, String filePath, String encoding)
          Write an XML document to a file using the specified character encoding.
static void saveDomToWriter(org.dom4j.Document doc, Writer writer)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XmlDom4JHelper

public XmlDom4JHelper()
Method Detail

getDocFromString

public static org.dom4j.Document getDocFromString(String strXml,
                                                  String encoding,
                                                  EntityResolver resolver)
                                           throws XmlParseException
Create a Document from str.

Parameters:
str - String containing the XML that will be used to create the Document
encoding - String specifying the character encoding. The encoding of the xml String can be discovered by calling CleanXmlHelper.getEncoding().
resolver - EntityResolver an instance of an EntityResolver that will resolve any external URIs. See the docs on EntityResolver. null is an acceptable value.
Returns:
Document initialized with the xml in strXml.
Throws:
org.dom4j.DocumentException
UnsupportedEncodingException
XmlParseException

getDocFromString

public static org.dom4j.Document getDocFromString(String strXml,
                                                  EntityResolver resolver)
                                           throws XmlParseException
Create a Document from str.

Parameters:
str - String containing the XML that will be used to create the Document can be discovered by calling CleanXmlHelper.getEncoding().
resolver - EntityResolver an instance of an EntityResolver that will resolve any external URIs. See the docs on EntityResolver. null is an acceptable value.
Returns:
Document initialized with the xml in strXml.
Throws:
org.dom4j.DocumentException
XmlParseException

getDocFromFile

public static org.dom4j.Document getDocFromFile(String path,
                                                EntityResolver resolver)
                                         throws org.dom4j.DocumentException,
                                                IOException
Create a Document from the contents of a file.

Parameters:
path - String containing the path to the file containing XML that will be used to create the Document.
resolver - EntityResolver an instance of an EntityResolver that will resolve any external URIs. See the docs on EntityResolver. null is an acceptable value.
Returns:
Document initialized with the xml in strXml.
Throws:
org.dom4j.DocumentException - if the document isn't valid
IOException

getDocFromFile

public static org.dom4j.Document getDocFromFile(File file,
                                                EntityResolver resolver)
                                         throws org.dom4j.DocumentException,
                                                IOException
Create a Document from the contents of a file.

Parameters:
path - String containing the path to the file containing XML that will be used to create the Document.
resolver - EntityResolver an instance of an EntityResolver that will resolve any external URIs. See the docs on EntityResolver. null is an acceptable value.
Returns:
Document initialized with the xml in strXml.
Throws:
org.dom4j.DocumentException - if the document isn't valid
IOException - if the file doesn't exist

getDocFromStream

public static org.dom4j.Document getDocFromStream(InputStream inStream,
                                                  EntityResolver resolver)
                                           throws org.dom4j.DocumentException,
                                                  IOException
Create a Document from the contents of an input stream, where the input stream contains valid XML.

Parameters:
inStream -
Returns:
Throws:
org.dom4j.DocumentException
IOException

getDocFromStream

public static org.dom4j.Document getDocFromStream(InputStream inStream)
                                           throws org.dom4j.DocumentException,
                                                  IOException
Create a Document from the contents of an input stream, where the input stream contains valid XML.

Parameters:
inStream -
Returns:
Throws:
org.dom4j.DocumentException
IOException

docToString

public static final StringBuffer docToString(Document doc)
                                      throws TransformerConfigurationException,
                                             TransformerException
Convert a W3C Document to a String. Note: if you are working with a dom4j Document, you can use it's asXml() method.

Parameters:
doc - org.w3c.dom.Document to be converted to a String.
Returns:
String representing the XML document.
Throws:
TransformerConfigurationException - If unable to get an instance of a Transformer
TransformerException - If the attempt to transform the document fails.

getXmlEncodedString

public static String getXmlEncodedString(String rawValue)
convert any character in the XML input (rawValue) whose code position is greater than or equal to 0x080 to its Numeric Character Reference. For a description of Numeric Character References see: http://www.w3.org/TR/html4/charset.html#h-5.3.1

Parameters:
rawValue - String containing the XML to be encoded.
Returns:
String containing the encoded XML

saveDomToFile

public static void saveDomToFile(org.dom4j.Document doc,
                                 String filePath,
                                 String encoding)
                          throws IOException
Write an XML document to a file using the specified character encoding.

Parameters:
doc - Document to be written
filePath - path identifying the File that will be the output of the Document
encoding - String specifying the character encoding. Can be null, in which case the default encoding will be used. See http://java.sun.com/j2se/1.5.0/docs/api/java/io/OutputStreamWriter.html
Throws:
IOException - if unable to obtain a FileWriter on the specified file

saveDomToFile

public static void saveDomToFile(org.dom4j.Document doc,
                                 File file,
                                 String encoding)
                          throws IOException
Write an XML document to a file using the specified character encoding.

Parameters:
doc - Document to be written
file - File that will be the output of the Document
encoding - String specifying the character encoding. Can be null, in which case the default encoding will be used. See http://java.sun.com/j2se/1.5.0/docs/api/java/io/OutputStreamWriter.html
Throws:
IOException - if unable to obtain a FileWriter on the specified file

saveDomToWriter

public static void saveDomToWriter(org.dom4j.Document doc,
                                   Writer writer)
                            throws IOException
Throws:
IOException

main

public static void main(String[] args)

getNodeText

public static String getNodeText(String xpath,
                                 org.dom4j.Node rootNode)

getNodeText

public static long getNodeText(String xpath,
                               org.dom4j.Node rootNode,
                               long defaultValue)

getNodeText

public static double getNodeText(String xpath,
                                 org.dom4j.Node rootNode,
                                 double defaultValue)

getNodeText

public static String getNodeText(String xpath,
                                 org.dom4j.Node rootNode,
                                 String defaultValue)