org.pentaho.platform.util.xml
Class XmlHelper

java.lang.Object
  extended by org.pentaho.platform.util.xml.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 Summary
XmlHelper()
           
 
Method Summary
static String createXmlProcessingInstruction(String version, String encoding)
           
static String decode(String string)
           
static void decode(String[] strings)
           
static String encode(String string)
           
static void encode(String[] strings)
           
static String getEncoding(File f)
           
static String getEncoding(InputStream inStream)
           
static String getEncoding(String xml)
          Find the character encoding specification in the xml String.
static String getEncoding(String xml, String defaultEncoding)
          Find the character encoding specification in the xml String.
static InputStream getLocalizedFile(String fullPath, Locale locale, IDocumentResourceLoader loader)
           
static InputStream getLocalizedXsl(String path, String xslName, IDocumentResourceLoader loader)
          Get the File object corresponding to the path, filename (xslName), and locale.
static String listToXML(List l)
           
static String listToXML(List l, String indent)
           
static String mapToXML(Map m)
           
static String mapToXML(Map mp, String indent)
           
static String readEncodingProcessingInstruction(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 StringBuffer transformXml(InputStream xslInStream, InputStream docInStrm, Map params, URIResolver resolver)
          Use the transform specified by xslSrc and transform the document specified by docSrc, and return the resulting document.
static StringBuffer transformXml(String xslName, String xslPath, InputStream docInStrm, 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 StringBuffer transformXml(String xslName, String xslPath, String strDocument, 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 String listToXML(List l)
                        throws UnsupportedOperationException
Throws:
UnsupportedOperationException

listToXML

public static String listToXML(List l,
                               String indent)
                        throws UnsupportedOperationException
Throws:
UnsupportedOperationException

mapToXML

public static String mapToXML(Map m)
                       throws UnsupportedOperationException
Throws:
UnsupportedOperationException

mapToXML

public static String mapToXML(Map mp,
                              String indent)
                       throws UnsupportedOperationException
Throws:
UnsupportedOperationException

decode

public static void decode(String[] strings)

decode

public static String decode(String string)

encode

public static void encode(String[] strings)

encode

public static String encode(String string)

getEncoding

public static String getEncoding(File f)
                          throws IOException
Throws:
IOException

getEncoding

public static String getEncoding(InputStream inStream)
                          throws IOException
Throws:
IOException

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,
                                              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,
                                              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.

getLocalizedXsl

public static final InputStream getLocalizedXsl(String path,
                                                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 InputStream getLocalizedFile(String fullPath,
                                           Locale locale,
                                           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)