org.pentaho.platform.util.xml
Class XmlHelper

java.lang.Object
  extended by org.pentaho.platform.util.xml.XmlHelper

public class XmlHelper
extends java.lang.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 Summary
XmlHelper()
           
 
Method Summary
static java.lang.String createXmlProcessingInstruction(java.lang.String version, java.lang.String encoding)
           
static java.lang.String decode(java.lang.String string)
           
static void decode(java.lang.String[] strings)
           
static java.lang.String encode(java.lang.String string)
           
static void encode(java.lang.String[] strings)
           
static java.lang.String getEncoding(java.io.File f)
           
static java.lang.String getEncoding(java.io.InputStream inStream)
           
static java.lang.String getEncoding(java.lang.String xml)
          Find the character encoding specification in the xml String.
static java.lang.String getEncoding(java.lang.String xml, java.lang.String defaultEncoding)
          Find the character encoding specification in the xml String.
static java.io.InputStream getLocalizedFile(java.lang.String fullPath, java.util.Locale locale, IDocumentResourceLoader loader)
           
static java.io.InputStream getLocalizedXsl(java.lang.String path, java.lang.String xslName, IDocumentResourceLoader loader)
          Get the File object corresponding to the path, filename (xslName), and locale.
static java.lang.String listToXML(java.util.List l)
           
static java.lang.String listToXML(java.util.List l, java.lang.String indent)
           
static java.lang.String mapToXML(java.util.Map m)
           
static java.lang.String mapToXML(java.util.Map mp, java.lang.String indent)
           
static java.lang.String readEncodingProcessingInstruction(java.io.InputStream inStream)
          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.
static java.lang.StringBuffer transformXml(java.io.InputStream xslInStream, java.io.InputStream docInStrm, java.util.Map params, javax.xml.transform.URIResolver resolver)
          Use the transform specified by xslSrc and transform the document specified by docSrc, and return the resulting document.
static java.lang.StringBuffer transformXml(java.lang.String xslName, java.lang.String xslPath, java.io.InputStream docInStrm, java.util.Map params, IDocumentResourceLoader loader)
          Use the transform specified by xslPath and xslName and transform the document specified by docInStrm, and return the resulting document.
static java.lang.StringBuffer transformXml(java.lang.String xslName, java.lang.String xslPath, java.lang.String strDocument, java.util.Map params, IDocumentResourceLoader loader)
          Use the transform specified by xslName and transform the document specified by docInStrm, and return the resulting document.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XmlHelper

public XmlHelper()
Method Detail

listToXML

public static java.lang.String listToXML(java.util.List l)
                                  throws java.lang.UnsupportedOperationException
Throws:
java.lang.UnsupportedOperationException

listToXML

public static java.lang.String listToXML(java.util.List l,
                                         java.lang.String indent)
                                  throws java.lang.UnsupportedOperationException
Throws:
java.lang.UnsupportedOperationException

mapToXML

public static java.lang.String mapToXML(java.util.Map m)
                                 throws java.lang.UnsupportedOperationException
Throws:
java.lang.UnsupportedOperationException

mapToXML

public static java.lang.String mapToXML(java.util.Map mp,
                                        java.lang.String indent)
                                 throws java.lang.UnsupportedOperationException
Throws:
java.lang.UnsupportedOperationException

decode

public static void decode(java.lang.String[] strings)

decode

public static java.lang.String decode(java.lang.String string)

encode

public static void encode(java.lang.String[] strings)

encode

public static java.lang.String encode(java.lang.String string)

getEncoding

public static java.lang.String getEncoding(java.io.File f)
                                    throws java.io.IOException
Throws:
java.io.IOException

getEncoding

public static java.lang.String getEncoding(java.io.InputStream inStream)
                                    throws java.io.IOException
Throws:
java.io.IOException

getEncoding

public static java.lang.String getEncoding(java.lang.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 java.lang.String getEncoding(java.lang.String xml,
                                           java.lang.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 java.lang.String readEncodingProcessingInstruction(java.io.InputStream inStream)
                                                          throws java.io.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:
java.io.IOException

transformXml

public static final java.lang.StringBuffer transformXml(java.lang.String xslName,
                                                        java.lang.String xslPath,
                                                        java.lang.String strDocument,
                                                        java.util.Map params,
                                                        IDocumentResourceLoader loader)
                                                 throws javax.xml.transform.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:
javax.xml.transform.TransformerException - If attempt to transform the document fails.

transformXml

public static final java.lang.StringBuffer transformXml(java.lang.String xslName,
                                                        java.lang.String xslPath,
                                                        java.io.InputStream docInStrm,
                                                        java.util.Map params,
                                                        IDocumentResourceLoader loader)
                                                 throws javax.xml.transform.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:
javax.xml.transform.TransformerException - If attempt to transform the document fails.

transformXml

public static final java.lang.StringBuffer transformXml(java.io.InputStream xslInStream,
                                                        java.io.InputStream docInStrm,
                                                        java.util.Map params,
                                                        javax.xml.transform.URIResolver resolver)
                                                 throws javax.xml.transform.TransformerConfigurationException,
                                                        javax.xml.transform.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:
javax.xml.transform.TransformerConfigurationException - if the TransformerFactory fails to create a Transformer.
javax.xml.transform.TransformerException - if actual transform fails.

getLocalizedXsl

public static final java.io.InputStream getLocalizedXsl(java.lang.String path,
                                                        java.lang.String xslName,
                                                        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 java.io.InputStream getLocalizedFile(java.lang.String fullPath,
                                                   java.util.Locale locale,
                                                   IDocumentResourceLoader loader)

createXmlProcessingInstruction

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