org.pentaho.di.core.xml
Class XMLHandler

java.lang.Object
  extended by org.pentaho.di.core.xml.XMLHandler

public class XMLHandler
extends Object

This class contains a number of (static final) methods to facilitate the retrieval of information from XML Node(s).

Since:
04-04-2003
Author:
Matt

Constructor Summary
XMLHandler()
           
 
Method Summary
static String addTagValue(KettleAttributeInterface tag, boolean bool)
          Build an XML string (including a carriage return) for a certain tag boolean value
static String addTagValue(KettleAttributeInterface tag, int i)
          Build an XML string (with carriage return) for a certain tag integer value
static String addTagValue(KettleAttributeInterface tag, String val)
          Build an XML string (including a carriage return) for a certain tag String value
static String addTagValue(String tag, BigDecimal val)
          Build an XML string (including a carriage return) for a certain tag BigDecimal value
static String addTagValue(String tag, BigDecimal val, boolean cr)
          Build an XML string (including a carriage return) for a certain tag BigDecimal value
static String addTagValue(String tag, boolean bool)
          Build an XML string (including a carriage return) for a certain tag boolean value
static String addTagValue(String tag, boolean bool, boolean cr)
          Build an XML string for a certain tag boolean value
static String addTagValue(String tag, byte[] val)
          Build an XML string (including a carriage return) for a certain tag binary (byte[]) value
static String addTagValue(String tag, byte[] val, boolean cr)
          Build an XML string (including a carriage return) for a certain tag binary (byte[]) value
static String addTagValue(String tag, Date date)
          Build an XML string (with carriage return) for a certain tag Date value
static String addTagValue(String tag, Date date, boolean cr)
          Build an XML string for a certain tag Date value
static String addTagValue(String tag, double d)
          Build an XML string (with carriage return) for a certain tag double value
static String addTagValue(String tag, double d, boolean cr)
          Build an XML string for a certain tag double value
static String addTagValue(String tag, int i)
          Build an XML string (with carriage return) for a certain tag integer value
static String addTagValue(String tag, int i, boolean cr)
          Build an XML string for a certain tag integer value
static String addTagValue(String tag, long l)
          Build an XML string for a certain tag long integer value
static String addTagValue(String tag, long l, boolean cr)
          Build an XML string for a certain tag long integer value
static String addTagValue(String tag, String val)
          Build an XML string (including a carriage return) for a certain tag String value
static String addTagValue(String tag, String val, boolean cr, String... attributes)
          Build an XML string for a certain tag String value
static void appendReplacedChars(StringBuffer value, String string)
          Take the characters from string val and append them to the value stringbuffer In case a character is not allowed in XML, we convert it to an XML code
static String buildCDATA(String string)
           
static String closeTag(String tag)
           
static int countNodes(Node n, String tag)
          Count nodes with a certain tag
static String date2string(Date date)
           
static String[] getNodeAttributes(Node node)
          Get all the attributes in a certain node (on the root level)
static String[] getNodeElements(Node node)
           
static List<Node> getNodes(Node n, String tag)
          Get nodes with a certain tag one level down
static String getNodeValue(Node n)
          Find the value entry in a node
static Node getNodeWithAttributeValue(Node n, String tag, String attributeName, String attributeValue)
          Get node child with a certain subtag set to a certain value
static Node getNodeWithTagValue(Node n, String tag, String subtag, String subtagvalue, int nr)
          Get node child with a certain subtag set to a certain value
static String getString()
           
static Node getSubNode(Node n, String tag)
          Search for a subnode in the node with a certain tag.
static Node getSubNode(Node n, String tag, String subtag)
          Search a node for a child of child
static Node getSubNodeByNr(Node n, String tag, int nr)
          Get a subnode in a node by nr.
This method uses caching and assumes you loop over subnodes in sequential order (nr is increasing by 1 each call)
static Node getSubNodeByNr(Node n, String tag, int nr, boolean useCache)
          Get a subnode in a node by nr.
It optially allows you to use caching.
Caching assumes that you loop over subnodes in sequential order (nr is increasing by 1 each call)
static String getTagAttribute(Node node, String attribute)
           
static String getTagValue(Node n, KettleAttributeInterface code)
          Get the value of a tag in a node
static String getTagValue(Node n, String tag)
          Get the value of a tag in a node
static String getTagValue(Node n, String tag, String subtag)
          Search a node for a certain tag, in that subnode search for a certain subtag.
static String getTagValueWithAttribute(Node n, String tag, String attribute)
          Get the value of a tag in a node
static String getXMLHeader()
          The header string to specify encoding in UTF-8 for XML files
static String getXMLHeader(String encoding)
          The header string to specify encoding in an XML file
static Document loadXMLFile(File resource)
           
static Document loadXMLFile(org.apache.commons.vfs.FileObject fileObject)
          Load a file into an XML document
static Document loadXMLFile(org.apache.commons.vfs.FileObject fileObject, String systemID, boolean ignoreEntities, boolean namespaceAware)
          Load a file into an XML document
static Document loadXMLFile(InputStream inputStream)
          Read in an XML file from the passed input stream and return an XML document
static Document loadXMLFile(InputStream inputStream, String systemID, boolean ignoreEntities, boolean namespaceAware)
          Load a file into an XML document
static Document loadXMLFile(String filename)
          Load a file into an XML document
static Document loadXMLFile(URL resource)
          Load a file into an XML document
static Document loadXMLString(String string)
          Calls loadXMLString with deferNodeExpansion = TRUE
static Document loadXMLString(String string, Boolean namespaceAware, Boolean deferNodeExpansion)
          Load a String into an XML document
static String openTag(String tag)
           
static byte[] stringToBinary(String string)
          Convert a XML encoded binary string back to binary format
static Date stringToDate(String dateString)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLHandler

public XMLHandler()
Method Detail

getXMLHeader

public static final String getXMLHeader()
The header string to specify encoding in UTF-8 for XML files

Returns:
The XML header.

getXMLHeader

public static final String getXMLHeader(String encoding)
The header string to specify encoding in an XML file

Parameters:
encoding - The desired encoding to use in the XML file
Returns:
The XML header.

getTagValue

public static final String getTagValue(Node n,
                                       KettleAttributeInterface code)
Get the value of a tag in a node

Parameters:
n - The node to look in
tag - The tag to look for
Returns:
The value of the tag or null if nothing was found.

getTagValue

public static final String getTagValue(Node n,
                                       String tag)
Get the value of a tag in a node

Parameters:
n - The node to look in
tag - The tag to look for
Returns:
The value of the tag or null if nothing was found.

getTagValueWithAttribute

public static final String getTagValueWithAttribute(Node n,
                                                    String tag,
                                                    String attribute)
Get the value of a tag in a node

Parameters:
n - The node to look in
tag - The tag to look for
Returns:
The value of the tag or null if nothing was found.

getTagValue

public static final String getTagValue(Node n,
                                       String tag,
                                       String subtag)
Search a node for a certain tag, in that subnode search for a certain subtag. Return the value of that subtag.

Parameters:
n - The node to look in
tag - The tag to look for
subtag - The subtag to look for
Returns:
The string of the subtag or null if nothing was found.

countNodes

public static final int countNodes(Node n,
                                   String tag)
Count nodes with a certain tag

Parameters:
n - The node to look in
tag - The tags to count
Returns:
The number of nodes found with a certain tag

getNodes

public static final List<Node> getNodes(Node n,
                                        String tag)
Get nodes with a certain tag one level down

Parameters:
n - The node to look in
tag - The tags to count
Returns:
The list of nodes found with the specified tag

getNodeWithTagValue

public static final Node getNodeWithTagValue(Node n,
                                             String tag,
                                             String subtag,
                                             String subtagvalue,
                                             int nr)
Get node child with a certain subtag set to a certain value

Parameters:
n - The node to search in
tag - The tag to look for
subtag - The subtag to look for
subtagvalue - The value the subtag should have
nr - The nr of occurance of the value
Returns:
The node found or null if we couldn't find anything.

getNodeWithAttributeValue

public static final Node getNodeWithAttributeValue(Node n,
                                                   String tag,
                                                   String attributeName,
                                                   String attributeValue)
Get node child with a certain subtag set to a certain value

Parameters:
n - The node to search in
tag - The tag to look for
subtag - The subtag to look for
subtagvalue - The value the subtag should have
copyNr - The nr of occurance of the value
Returns:
The node found or null if we couldn't find anything.

getSubNode

public static final Node getSubNode(Node n,
                                    String tag)
Search for a subnode in the node with a certain tag.

Parameters:
n - The node to look in
tag - The tag to look for
Returns:
The subnode if the tag was found, or null if nothing was found.

getSubNode

public static final Node getSubNode(Node n,
                                    String tag,
                                    String subtag)
Search a node for a child of child

Parameters:
n - The node to look in
tag - The tag to look for in the node
subtag - The tag to look for in the children of the node
Returns:
The sub-node found or null if nothing was found.

getSubNodeByNr

public static final Node getSubNodeByNr(Node n,
                                        String tag,
                                        int nr)
Get a subnode in a node by nr.
This method uses caching and assumes you loop over subnodes in sequential order (nr is increasing by 1 each call)

Parameters:
n - The node to look in
tag - The tag to count
nr - The position in the node
Returns:
The subnode found or null in case the position was invalid.

getSubNodeByNr

public static final Node getSubNodeByNr(Node n,
                                        String tag,
                                        int nr,
                                        boolean useCache)
Get a subnode in a node by nr.
It optially allows you to use caching.
Caching assumes that you loop over subnodes in sequential order (nr is increasing by 1 each call)

Parameters:
n - The node to look in
tag - The tag to count
nr - The position in the node
useCache - set this to false if you don't want to use caching. For example in cases where you want to loop over subnodes of a certain tag in reverse or random order.
Returns:
The subnode found or null in case the position was invalid.

getNodeValue

public static final String getNodeValue(Node n)
Find the value entry in a node

Parameters:
n - The node
Returns:
The value entry as a string

getTagAttribute

public static final String getTagAttribute(Node node,
                                           String attribute)

loadXMLFile

public static final Document loadXMLFile(String filename)
                                  throws KettleXMLException
Load a file into an XML document

Parameters:
filename - The filename to load into a document
Returns:
the Document if all went well, null if an error occurred!
Throws:
KettleXMLException

loadXMLFile

public static final Document loadXMLFile(org.apache.commons.vfs.FileObject fileObject)
                                  throws KettleXMLException
Load a file into an XML document

Parameters:
filename - The filename to load into a document
Returns:
the Document if all went well, null if an error occured!
Throws:
KettleXMLException

loadXMLFile

public static final Document loadXMLFile(org.apache.commons.vfs.FileObject fileObject,
                                         String systemID,
                                         boolean ignoreEntities,
                                         boolean namespaceAware)
                                  throws KettleXMLException
Load a file into an XML document

Parameters:
filename - The filename to load into a document
systemId - Provide a base for resolving relative URIs.
ignoreEntities - Ignores external entities and returns an empty dummy.
namespaceAware - support XML namespaces.
Returns:
the Document if all went well, null if an error occured!
Throws:
KettleXMLException

loadXMLFile

public static final Document loadXMLFile(InputStream inputStream)
                                  throws KettleXMLException
Read in an XML file from the passed input stream and return an XML document

Parameters:
inputStream - The filename input stream to read the document from
Returns:
the Document if all went well, null if an error occurred!
Throws:
KettleXMLException

loadXMLFile

public static final Document loadXMLFile(InputStream inputStream,
                                         String systemID,
                                         boolean ignoreEntities,
                                         boolean namespaceAware)
                                  throws KettleXMLException
Load a file into an XML document

Parameters:
inputStream - The stream to load a document from
systemId - Provide a base for resolving relative URIs.
ignoreEntities - Ignores external entities and returns an empty dummy.
namespaceAware - support XML namespaces.
Returns:
the Document if all went well, null if an error occured!
Throws:
KettleXMLException

loadXMLFile

public static final Document loadXMLFile(File resource)
                                  throws KettleXMLException
Throws:
KettleXMLException

loadXMLFile

public static final Document loadXMLFile(URL resource)
                                  throws KettleXMLException
Load a file into an XML document

Parameters:
file - The file to load into a document
Returns:
the Document if all went well, null if an error occured!
Throws:
KettleXMLException

loadXMLString

public static final Document loadXMLString(String string)
                                    throws KettleXMLException
Calls loadXMLString with deferNodeExpansion = TRUE

Parameters:
string -
Returns:
Throws:
KettleXMLException

loadXMLString

public static final Document loadXMLString(String string,
                                           Boolean namespaceAware,
                                           Boolean deferNodeExpansion)
                                    throws KettleXMLException
Load a String into an XML document

Parameters:
string - The XML text to load into a document
Boolean - true to defer node expansion, false to not defer.
Returns:
the Document if all went well, null if an error occurred!
Throws:
KettleXMLException

getString

public static final String getString()

addTagValue

public static final String addTagValue(String tag,
                                       String val,
                                       boolean cr,
                                       String... attributes)
Build an XML string for a certain tag String value

Parameters:
tag - The XML tag
val - The String value of the tag
cr - true if a carriage return is desired after the ending tag.
Returns:
The XML String for the tag.

appendReplacedChars

public static void appendReplacedChars(StringBuffer value,
                                       String string)
Take the characters from string val and append them to the value stringbuffer In case a character is not allowed in XML, we convert it to an XML code

Parameters:
value - the stringbuffer to append to
string - the string to "encode"

addTagValue

public static final String addTagValue(KettleAttributeInterface tag,
                                       String val)
Build an XML string (including a carriage return) for a certain tag String value

Parameters:
tag - The XML tag
val - The String value of the tag
Returns:
The XML String for the tag.

addTagValue

public static final String addTagValue(String tag,
                                       String val)
Build an XML string (including a carriage return) for a certain tag String value

Parameters:
tag - The XML tag
val - The String value of the tag
Returns:
The XML String for the tag.

addTagValue

public static final String addTagValue(KettleAttributeInterface tag,
                                       boolean bool)
Build an XML string (including a carriage return) for a certain tag boolean value

Parameters:
tag - The XML tag
bool - The boolean value of the tag
Returns:
The XML String for the tag.

addTagValue

public static final String addTagValue(String tag,
                                       boolean bool)
Build an XML string (including a carriage return) for a certain tag boolean value

Parameters:
tag - The XML tag
bool - The boolean value of the tag
Returns:
The XML String for the tag.

addTagValue

public static final String addTagValue(String tag,
                                       boolean bool,
                                       boolean cr)
Build an XML string for a certain tag boolean value

Parameters:
tag - The XML tag
bool - The boolean value of the tag
cr - true if a carriage return is desired after the ending tag.
Returns:
The XML String for the tag.

addTagValue

public static final String addTagValue(String tag,
                                       long l)
Build an XML string for a certain tag long integer value

Parameters:
tag - The XML tag
l - The long integer value of the tag
Returns:
The XML String for the tag.

addTagValue

public static final String addTagValue(String tag,
                                       long l,
                                       boolean cr)
Build an XML string for a certain tag long integer value

Parameters:
tag - The XML tag
l - The long integer value of the tag
cr - true if a carriage return is desired after the ending tag.
Returns:
The XML String for the tag.

addTagValue

public static final String addTagValue(KettleAttributeInterface tag,
                                       int i)
Build an XML string (with carriage return) for a certain tag integer value

Parameters:
tag - The XML tag
i - The integer value of the tag
Returns:
The XML String for the tag.

addTagValue

public static final String addTagValue(String tag,
                                       int i)
Build an XML string (with carriage return) for a certain tag integer value

Parameters:
tag - The XML tag
i - The integer value of the tag
Returns:
The XML String for the tag.

addTagValue

public static final String addTagValue(String tag,
                                       int i,
                                       boolean cr)
Build an XML string for a certain tag integer value

Parameters:
tag - The XML tag
i - The integer value of the tag
cr - true if a carriage return is desired after the ending tag.
Returns:
The XML String for the tag.

addTagValue

public static final String addTagValue(String tag,
                                       double d)
Build an XML string (with carriage return) for a certain tag double value

Parameters:
tag - The XML tag
d - The double value of the tag
Returns:
The XML String for the tag.

addTagValue

public static final String addTagValue(String tag,
                                       double d,
                                       boolean cr)
Build an XML string for a certain tag double value

Parameters:
tag - The XML tag
d - The double value of the tag
cr - true if a carriage return is desired after the ending tag.
Returns:
The XML String for the tag.

addTagValue

public static final String addTagValue(String tag,
                                       Date date)
Build an XML string (with carriage return) for a certain tag Date value

Parameters:
tag - The XML tag
date - The Date value of the tag
Returns:
The XML String for the tag.

addTagValue

public static final String addTagValue(String tag,
                                       Date date,
                                       boolean cr)
Build an XML string for a certain tag Date value

Parameters:
tag - The XML tag
date - The Date value of the tag
cr - true if a carriage return is desired after the ending tag.
Returns:
The XML String for the tag.

addTagValue

public static final String addTagValue(String tag,
                                       BigDecimal val)
Build an XML string (including a carriage return) for a certain tag BigDecimal value

Parameters:
tag - The XML tag
val - The BigDecimal value of the tag
Returns:
The XML String for the tag.

addTagValue

public static final String addTagValue(String tag,
                                       BigDecimal val,
                                       boolean cr)
Build an XML string (including a carriage return) for a certain tag BigDecimal value

Parameters:
tag - The XML tag
val - The BigDecimal value of the tag
Returns:
The XML String for the tag.

addTagValue

public static final String addTagValue(String tag,
                                       byte[] val)
                                throws IOException
Build an XML string (including a carriage return) for a certain tag binary (byte[]) value

Parameters:
tag - The XML tag
val - The binary value of the tag
Returns:
The XML String for the tag.
Throws:
IOException - in case there is an Base64 or GZip encoding problem

addTagValue

public static final String addTagValue(String tag,
                                       byte[] val,
                                       boolean cr)
                                throws IOException
Build an XML string (including a carriage return) for a certain tag binary (byte[]) value

Parameters:
tag - The XML tag
val - The binary value of the tag
Returns:
The XML String for the tag.
Throws:
IOException - in case there is an Base64 or GZip encoding problem

getNodeAttributes

public static String[] getNodeAttributes(Node node)
Get all the attributes in a certain node (on the root level)

Parameters:
node - The node to examine
Returns:
an array of strings containing the names of the attributes.

getNodeElements

public static String[] getNodeElements(Node node)

stringToDate

public static Date stringToDate(String dateString)

date2string

public static String date2string(Date date)

stringToBinary

public static byte[] stringToBinary(String string)
                             throws KettleException
Convert a XML encoded binary string back to binary format

Parameters:
string - the (Byte64/GZip) encoded string
Returns:
the decoded binary (byte[]) object
Throws:
KettleException - In case there is a decoding error

buildCDATA

public static String buildCDATA(String string)

openTag

public static final String openTag(String tag)

closeTag

public static final String closeTag(String tag)