Class XmlDom4JHelper

java.lang.Object
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 Details

    • XmlDom4JHelper

      public XmlDom4JHelper()
  • Method Details

    • getDocFromString

      public static org.dom4j.Document getDocFromString(String strXml, EntityResolver resolver) throws org.pentaho.platform.api.util.XmlParseException
      Create a Document from str.
      Parameters:
      str - String containing the 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.pentaho.platform.api.util.XmlParseException
    • 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
    • transformXml

      protected static final StringBuffer transformXml(StreamSource xslSrc, StreamSource docSrc, Map params, URIResolver resolver) throws TransformerConfigurationException, TransformerException
      Use the transform specified by xslSrc and transform the document specified by docSrc, and return the resulting document.
      Parameters:
      xslSrc - StreamSrc containing the xsl transform
      docSrc - StreamSrc containing the document to be transformed
      params - Map of properties to set on the transform
      resolver - URIResolver instance to resolve URI's in the output document.
      Returns:
      StringBuffer containing the XML results of the transform
      Throws:
      TransformerConfigurationException - if the TransformerFactory fails to create a Transformer.
      TransformerException - if actual transform fails.
    • 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
    • saveDom

      public static void saveDom(org.dom4j.Document doc, OutputStream outputStream, String encoding) throws IOException
      Write an XML document to a file using the specified character encoding.
      Parameters:
      doc - Document to be written
      outputStream - the output stream
      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
    • saveDom

      public static void saveDom(org.dom4j.Document doc, OutputStream outputStream, String encoding, boolean suppressDeclaration) throws IOException
      Throws:
      IOException
    • saveDom

      public static void saveDom(org.dom4j.Document doc, OutputStream outputStream, String encoding, boolean suppressDeclaration, boolean prettyPrint) throws IOException
      Throws:
      IOException
    • closeInputStream

      protected static void closeInputStream(InputStream strm)
      Convenience method to close an input stream and handle (log and throw away) any exceptions. Helps keep code uncluttered.
      Parameters:
      strm - InputStream to be closed
    • 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)
    • convertToDom4JDoc

      public static org.dom4j.Document convertToDom4JDoc(Document doc) throws TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError, org.dom4j.DocumentException
      Throws:
      TransformerConfigurationException
      TransformerException
      TransformerFactoryConfigurationError
      org.dom4j.DocumentException