Class XmlHelper


  • public class XmlHelper
    extends Object
    A set of static methods for performing various operations on DOM Documents and XML text (in the form of streams, Strings, and files). The operations include creating DOM Documents (dom4j) transforming DOM Documents creating XML from Objects, Lists and Maps creating Lists or Maps from XML getting an XML node's text
    Author:
    mbatchel/jdixon
    • Constructor Detail

      • XmlHelper

        public XmlHelper()
    • Method Detail

      • decode

        public static void decode​(String[] strings)
      • encode

        public static void encode​(String[] strings)
      • getEncoding

        public static String getEncoding​(String xml)
        Find the character encoding specification in the xml String. If it exists, return the character encoding. Otherwise, return null.
        Parameters:
        xml - String containing the xml
        Returns:
        String containing the character encoding in the xml processing instruction if it exists, else null.
      • getEncoding

        public static String getEncoding​(String xml,
                                         String defaultEncoding)
        Find the character encoding specification in the xml String. If it exists, return the character encoding. Otherwise, return the system encoding.
        Parameters:
        xml - String containing the xml
        defaultEncoding - Encoding to use if there is no encoding in the xml document
        Returns:
        String containing the character encoding in the xml processing instruction, or defaultEncoding if there is no encoding in the xml document. If defaultEncoding is also null, then it returns the value in LocaleHelper.getSystemEncoding(). if it exists, else the system encoding.
      • readEncodingProcessingInstruction

        public static String readEncodingProcessingInstruction​(InputStream inStream)
                                                        throws IOException
        WARNING: if the inStream instance does not support mark/reset, when this method returns, subsequent reads on inStream will be 256 bytes into the stream. This may not be the expected behavior. FileInputStreams are an example of an InputStream that does not support mark/reset. InputStreams that do support mark/reset will be reset to the beginning of the stream when this method returns.
        Parameters:
        inStream -
        Returns:
        Throws:
        IOException
      • transformXml

        public static final StringBuffer transformXml​(String xslName,
                                                      String xslPath,
                                                      String strDocument,
                                                      Map params,
                                                      org.pentaho.platform.api.engine.IDocumentResourceLoader loader)
                                               throws TransformerException
        Use the transform specified by xslName and transform the document specified by docInStrm, and return the resulting document.
        Parameters:
        xslName - String containing the name of a file in the repository containing the xsl transform
        xslPath - String containing the path to the file identifyied by xslName
        uri - String containing the URI of a resource containing the document to be transformed
        params - Map of properties to set on the transform
        session - IPentahoSession containing a URIResolver instance to resolve URI's in the output document.
        Returns:
        StringBuffer containing the XML results of the transform. Null if there was an error.
        Throws:
        TransformerException - If attempt to transform the document fails.
      • transformXml

        public static final StringBuffer transformXml​(String xslName,
                                                      String xslPath,
                                                      InputStream docInStrm,
                                                      Map params,
                                                      org.pentaho.platform.api.engine.IDocumentResourceLoader loader)
                                               throws TransformerException
        Use the transform specified by xslPath and xslName and transform the document specified by docInStrm, 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
        session - IPentahoSession containing a URIResolver instance to resolve URI's in the output document.
        Returns:
        StringBuffer containing the XML results of the transform. Null if there was an error.
        Throws:
        TransformerException - If attempt to transform the document fails.
      • transformXml

        public static final StringBuffer transformXml​(InputStream xslInStream,
                                                      InputStream docInStrm,
                                                      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:
        xslInStream - InputStream containing the xsl transform
        docInStrm - InputStream 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.
      • 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.
      • getLocalizedXsl

        public static final InputStream getLocalizedXsl​(String path,
                                                        String xslName,
                                                        org.pentaho.platform.api.engine.IDocumentResourceLoader loader)
        Get the File object corresponding to the path, filename (xslName), and locale. The path is relative to the solution path.
        Parameters:
        path -
        xslName -
        Returns:
      • getLocalizedFile

        public static InputStream getLocalizedFile​(String fullPath,
                                                   Locale locale,
                                                   org.pentaho.platform.api.engine.IDocumentResourceLoader loader)
      • createXmlProcessingInstruction

        public static String createXmlProcessingInstruction​(String version,
                                                            String encoding)
        Parameters:
        version -
        encoding -
        Returns:
        String Xml Processing instruction text with the specified version (usually 1.0) and encoding (for instance, UTF-8)