Package org.pentaho.platform.util.xml
Class XmlHelper
- java.lang.Object
-
- 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
Constructors Constructor Description XmlHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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, org.pentaho.platform.api.engine.IDocumentResourceLoader loader)
static 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.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, org.pentaho.platform.api.engine.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, org.pentaho.platform.api.engine.IDocumentResourceLoader loader)
Use the transform specified by xslName and transform the document specified by docInStrm, and return the resulting document.protected static StringBuffer
transformXml(StreamSource xslSrc, StreamSource docSrc, Map params, URIResolver resolver)
Use the transform specified by xslSrc and transform the document specified by docSrc, and return the resulting document.
-
-
-
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)
-
encode
public static void encode(String[] strings)
-
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 xmldefaultEncoding
- 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 transformxslPath
- String containing the path to the file identifyied byxslName
uri
- String containing the URI of a resource containing the document to be transformedparams
- Map of properties to set on the transformsession
- 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 transformdocSrc
- StreamSrc containing the document to be transformedparams
- Map of properties to set on the transformsession
- 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 transformdocInStrm
- InputStream containing the document to be transformedparams
- Map of properties to set on the transformresolver
- 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 transformdocSrc
- StreamSrc containing the document to be transformedparams
- Map of properties to set on the transformresolver
- 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)
-
-