Package org.pentaho.di.core.xml
Class XMLHandler
- java.lang.Object
-
- org.pentaho.di.core.xml.XMLHandler
-
public class XMLHandler extends Object
This class contains a number of (static) methods to facilitate the retrieval of information from XML Node(s).- Since:
- 04-04-2003
- Author:
- Matt
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_RETRY_ATTEMPTS
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringaddTagValue(String tag, boolean bool)Build an XML string (including a carriage return) for a certain tag boolean valuestatic StringaddTagValue(String tag, boolean bool, boolean cr)Build an XML string for a certain tag boolean valuestatic StringaddTagValue(String tag, byte[] val)Build an XML string (including a carriage return) for a certain tag binary (byte[]) valuestatic StringaddTagValue(String tag, byte[] val, boolean cr)Build an XML string (including a carriage return) for a certain tag binary (byte[]) valuestatic StringaddTagValue(String tag, double d)Build an XML string (with carriage return) for a certain tag double valuestatic StringaddTagValue(String tag, double d, boolean cr)Build an XML string for a certain tag double valuestatic StringaddTagValue(String tag, int i)Build an XML string (with carriage return) for a certain tag integer valuestatic StringaddTagValue(String tag, int i, boolean cr)Build an XML string for a certain tag integer valuestatic StringaddTagValue(String tag, long l)Build an XML string for a certain tag long integer valuestatic StringaddTagValue(String tag, long l, boolean cr)Build an XML string for a certain tag long integer valuestatic StringaddTagValue(String tag, String val)Build an XML string (including a carriage return) for a certain tag String valuestatic StringaddTagValue(String tag, String val, boolean cr, String... attributes)Build an XML string for a certain tag String valuestatic StringaddTagValue(String tag, BigDecimal val)Build an XML string (including a carriage return) for a certain tag BigDecimal valuestatic StringaddTagValue(String tag, BigDecimal val, boolean cr)Build an XML string (including a carriage return) for a certain tag BigDecimal valuestatic StringaddTagValue(String tag, Date date)Build an XML string (with carriage return) for a certain tag Date valuestatic StringaddTagValue(String tag, Date date, boolean cr)Build an XML string for a certain tag Date valuestatic StringaddTagValue(KettleAttributeInterface tag, boolean bool)Build an XML string (including a carriage return) for a certain tag boolean valuestatic StringaddTagValue(KettleAttributeInterface tag, int i)Build an XML string (with carriage return) for a certain tag integer valuestatic StringaddTagValue(KettleAttributeInterface tag, String val)Build an XML string (including a carriage return) for a certain tag String valuestatic voidappendReplacedChars(StringBuilder value, String string)static StringbuildCDATA(String string)static StringBuilderbuildCDATA(StringBuilder builder, String string)static booleancheckFile(org.apache.commons.vfs2.FileObject fileObject)Checks if a givenFileObjectinstance corresponds to an existing file.static StringcloseTag(String tag)static StringBuildercloseTag(StringBuilder builder, String tag)static intcountNodes(Node n, String tag)Count nodes with a certain tagstatic DocumentBuildercreateDocumentBuilder(boolean namespaceAware, boolean deferNodeExpansion)static Stringdate2string(Date date)static StringencodeBinaryData(byte[] val)static StringformatNode(Node node)static NodegetLastSubNode(Node n, String tag)Search for the last subnode in the node with a certain tag.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 downstatic StringgetNodeValue(Node n)Find the value entry in a nodestatic NodegetNodeWithAttributeValue(Node n, String tag, String attributeName, String attributeValue)Get node child with a certain subtag set to a certain valuestatic NodegetNodeWithTagValue(Node n, String tag, String subtag, String subtagvalue, int nr)Get node child with a certain subtag set to a certain valuestatic StringgetString()static NodegetSubNode(Node n, String tag)Search for a subnode in the node with a certain tag.static NodegetSubNode(Node n, String tag, String subtag)Search a node for a child of childstatic NodegetSubNodeByNr(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 NodegetSubNodeByNr(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 StringgetTagAttribute(Node node, String attribute)static StringgetTagValue(Node n, String tag)Get the value of a tag in a nodestatic StringgetTagValue(Node n, String tag, String subtag)Search a node for a certain tag, in that subnode search for a certain subtag.static StringgetTagValue(Node n, KettleAttributeInterface code)Get the value of a tag in a nodestatic StringgetTagValueWithAttribute(Node n, String tag, String attribute)Get the value of a tag in a nodestatic StringgetXMLHeader()The header string to specify encoding in UTF-8 for XML filesstatic StringgetXMLHeader(String encoding)The header string to specify encoding in an XML filestatic DocumentloadXMLFile(File resource)static DocumentloadXMLFile(InputStream inputStream)Read in an XML file from the passed input stream and return an XML documentstatic DocumentloadXMLFile(InputStream inputStream, String systemID, boolean ignoreEntities, boolean namespaceAware)Load a file into an XML documentstatic DocumentloadXMLFile(String filename)Load a file into an XML documentstatic DocumentloadXMLFile(URL resource)Load a file into an XML documentstatic DocumentloadXMLFile(org.apache.commons.vfs2.FileObject fileObject)Load a file into an XML documentstatic DocumentloadXMLFile(org.apache.commons.vfs2.FileObject fileObject, String systemID, boolean ignoreEntities, boolean namespaceAware)Load a file into an XML documentstatic DocumentloadXMLString(String string)Calls loadXMLString with deferNodeExpansion = TRUEstatic DocumentloadXMLString(String string, Boolean namespaceAware, Boolean deferNodeExpansion)Load a String into an XML documentstatic NodeloadXMLString(String xml, String tag)Loads the XML document in parameter xml and returns the 'tag' entry.static DocumentloadXMLString(DocumentBuilder db, String string)static StringopenTag(String tag)static StringBuilderopenTag(StringBuilder builder, String tag)static StringBuilderopenTag(StringBuilder builder, String tag, Map<String,String> attributes)static byte[]stringToBinary(String string)Convert a XML encoded binary string back to binary formatstatic DatestringToDate(String dateString)static TimestampstringToTimestamp(String dateString)static Stringtimestamp2string(Timestamp timestamp)
-
-
-
Field Detail
-
DEFAULT_RETRY_ATTEMPTS
public static final int DEFAULT_RETRY_ATTEMPTS
- See Also:
- Constant Field Values
-
-
Method Detail
-
getXMLHeader
public static String getXMLHeader()
The header string to specify encoding in UTF-8 for XML files- Returns:
- The XML header.
-
getXMLHeader
public static 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 String getTagValue(Node n, KettleAttributeInterface code)
Get the value of a tag in a node- Parameters:
n- The node to look incode- The code to look for- Returns:
- The value of the tag or null if nothing was found.
-
getTagValue
public static String getTagValue(Node n, String tag)
Get the value of a tag in a node- Parameters:
n- The node to look intag- The tag to look for- Returns:
- The value of the tag or null if nothing was found.
-
getTagValueWithAttribute
public static String getTagValueWithAttribute(Node n, String tag, String attribute)
Get the value of a tag in a node- Parameters:
n- The node to look intag- The tag to look for- Returns:
- The value of the tag or null if nothing was found.
-
getTagValue
public static 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 intag- The tag to look forsubtag- The subtag to look for- Returns:
- The string of the subtag or null if nothing was found.
-
countNodes
public static int countNodes(Node n, String tag)
Count nodes with a certain tag- Parameters:
n- The node to look intag- The tags to count- Returns:
- The number of nodes found with a certain tag
-
getNodes
public static List<Node> getNodes(Node n, String tag)
Get nodes with a certain tag one level down- Parameters:
n- The node to look intag- The tags to count- Returns:
- The list of nodes found with the specified tag
-
getNodeWithTagValue
public 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- Parameters:
n- The node to search intag- The tag to look forsubtag- The subtag to look forsubtagvalue- The value the subtag should havenr- The nr of occurance of the value- Returns:
- The node found or null if we couldn't find anything.
-
getNodeWithAttributeValue
public static 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 intag- the tag to look forattributeName- the subtag to look forattributeValue- the value the subtag should have- Returns:
- the node found or null if we couldn't find anything
-
getSubNode
public static Node getSubNode(Node n, String tag)
Search for a subnode in the node with a certain tag.- Parameters:
n- The node to look intag- The tag to look for- Returns:
- The subnode if the tag was found, or null if nothing was found.
-
getLastSubNode
public static Node getLastSubNode(Node n, String tag)
Search for the last subnode in the node with a certain tag.- Parameters:
n- the node to look intag- the tag to look for- Returns:
- the last subnode found with the specified tag, or null if nothing was found
-
getSubNode
public static Node getSubNode(Node n, String tag, String subtag)
Search a node for a child of child- Parameters:
n- The node to look intag- The tag to look for in the nodesubtag- 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 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 intag- The tag to countnr- The position in the node- Returns:
- The subnode found or null in case the position was invalid.
-
getSubNodeByNr
public 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)- Parameters:
n- The node to look intag- The tag to countnr- The position in the nodeuseCache- 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 String getNodeValue(Node n)
Find the value entry in a node- Parameters:
n- The node- Returns:
- The value entry as a string
-
loadXMLFile
public static 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 Document loadXMLFile(org.apache.commons.vfs2.FileObject fileObject) throws KettleXMLException
Load a file into an XML document- Parameters:
fileObject- The fileObject to load into a document- Returns:
- the Document if all went well, null if an error occured!
- Throws:
KettleXMLException
-
loadXMLFile
public static Document loadXMLFile(org.apache.commons.vfs2.FileObject fileObject, String systemID, boolean ignoreEntities, boolean namespaceAware) throws KettleXMLException
Load a file into an XML document- Parameters:
fileObject- The fileObject to load into a documentsystemID- 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 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 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 fromsystemID- 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 Document loadXMLFile(File resource) throws KettleXMLException
- Throws:
KettleXMLException
-
loadXMLFile
public static Document loadXMLFile(URL resource) throws KettleXMLException
Load a file into an XML document- Parameters:
resource- The resource to load into a document- Returns:
- the Document if all went well, null if an error occured!
- Throws:
KettleXMLException
-
loadXMLString
public static Document loadXMLString(String string) throws KettleXMLException
Calls loadXMLString with deferNodeExpansion = TRUE- Parameters:
string-- Returns:
- Throws:
KettleXMLException
-
loadXMLString
public static Node loadXMLString(String xml, String tag) throws KettleXMLException
Loads the XML document in parameter xml and returns the 'tag' entry.- Parameters:
xml- the XML to loadtag- the node to return- Returns:
- the requested node
- Throws:
KettleXMLException- in case there is a problem reading the XML
-
loadXMLString
public static 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 documentdeferNodeExpansion- true to defer node expansion, false to not defer.- Returns:
- the Document if all went well, null if an error occurred!
- Throws:
KettleXMLException
-
loadXMLString
public static Document loadXMLString(DocumentBuilder db, String string) throws KettleXMLException
- Throws:
KettleXMLException
-
createDocumentBuilder
public static DocumentBuilder createDocumentBuilder(boolean namespaceAware, boolean deferNodeExpansion) throws KettleXMLException
- Throws:
KettleXMLException
-
getString
public static String getString()
-
addTagValue
public static String addTagValue(String tag, String val, boolean cr, String... attributes)
Build an XML string for a certain tag String value- Parameters:
tag- The XML tagval- The String value of the tagcr- true if a carriage return is desired after the ending tag.- Returns:
- The XML String for the tag.
-
appendReplacedChars
public static void appendReplacedChars(StringBuilder value, String string)
-
addTagValue
public static String addTagValue(KettleAttributeInterface tag, String val)
Build an XML string (including a carriage return) for a certain tag String value- Parameters:
tag- The XML tagval- The String value of the tag- Returns:
- The XML String for the tag.
-
addTagValue
public static String addTagValue(String tag, String val)
Build an XML string (including a carriage return) for a certain tag String value- Parameters:
tag- The XML tagval- The String value of the tag- Returns:
- The XML String for the tag.
-
addTagValue
public static String addTagValue(KettleAttributeInterface tag, boolean bool)
Build an XML string (including a carriage return) for a certain tag boolean value- Parameters:
tag- The XML tagbool- The boolean value of the tag- Returns:
- The XML String for the tag.
-
addTagValue
public static String addTagValue(String tag, boolean bool)
Build an XML string (including a carriage return) for a certain tag boolean value- Parameters:
tag- The XML tagbool- The boolean value of the tag- Returns:
- The XML String for the tag.
-
addTagValue
public static String addTagValue(String tag, boolean bool, boolean cr)
Build an XML string for a certain tag boolean value- Parameters:
tag- The XML tagbool- The boolean value of the tagcr- true if a carriage return is desired after the ending tag.- Returns:
- The XML String for the tag.
-
addTagValue
public static String addTagValue(String tag, long l)
Build an XML string for a certain tag long integer value- Parameters:
tag- The XML tagl- The long integer value of the tag- Returns:
- The XML String for the tag.
-
addTagValue
public static String addTagValue(String tag, long l, boolean cr)
Build an XML string for a certain tag long integer value- Parameters:
tag- The XML tagl- The long integer value of the tagcr- true if a carriage return is desired after the ending tag.- Returns:
- The XML String for the tag.
-
addTagValue
public static String addTagValue(KettleAttributeInterface tag, int i)
Build an XML string (with carriage return) for a certain tag integer value- Parameters:
tag- The XML tagi- The integer value of the tag- Returns:
- The XML String for the tag.
-
addTagValue
public static String addTagValue(String tag, int i)
Build an XML string (with carriage return) for a certain tag integer value- Parameters:
tag- The XML tagi- The integer value of the tag- Returns:
- The XML String for the tag.
-
addTagValue
public static String addTagValue(String tag, int i, boolean cr)
Build an XML string for a certain tag integer value- Parameters:
tag- The XML tagi- The integer value of the tagcr- true if a carriage return is desired after the ending tag.- Returns:
- The XML String for the tag.
-
addTagValue
public static String addTagValue(String tag, double d)
Build an XML string (with carriage return) for a certain tag double value- Parameters:
tag- The XML tagd- The double value of the tag- Returns:
- The XML String for the tag.
-
addTagValue
public static String addTagValue(String tag, double d, boolean cr)
Build an XML string for a certain tag double value- Parameters:
tag- The XML tagd- The double value of the tagcr- true if a carriage return is desired after the ending tag.- Returns:
- The XML String for the tag.
-
addTagValue
public static String addTagValue(String tag, Date date)
Build an XML string (with carriage return) for a certain tag Date value- Parameters:
tag- The XML tagdate- The Date value of the tag- Returns:
- The XML String for the tag.
-
addTagValue
public static String addTagValue(String tag, Date date, boolean cr)
Build an XML string for a certain tag Date value- Parameters:
tag- The XML tagdate- The Date value of the tagcr- true if a carriage return is desired after the ending tag.- Returns:
- The XML String for the tag.
-
addTagValue
public static String addTagValue(String tag, BigDecimal val)
Build an XML string (including a carriage return) for a certain tag BigDecimal value- Parameters:
tag- The XML tagval- The BigDecimal value of the tag- Returns:
- The XML String for the tag.
-
addTagValue
public static 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 tagval- The BigDecimal value of the tag- Returns:
- The XML String for the tag.
-
addTagValue
public static 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 tagval- 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 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 tagval- 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
-
encodeBinaryData
public static String encodeBinaryData(byte[] val) throws IOException
- Throws:
IOException
-
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.
-
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 StringBuilder buildCDATA(StringBuilder builder, String string)
-
openTag
public static StringBuilder openTag(StringBuilder builder, String tag)
-
openTag
public static StringBuilder openTag(StringBuilder builder, String tag, Map<String,String> attributes)
-
closeTag
public static StringBuilder closeTag(StringBuilder builder, String tag)
-
formatNode
public static String formatNode(Node node) throws KettleXMLException
- Throws:
KettleXMLException
-
checkFile
public static boolean checkFile(org.apache.commons.vfs2.FileObject fileObject) throws KettleXMLExceptionChecks if a given
FileObjectinstance corresponds to an existing file.- Parameters:
fileObject- theFileObjectinstance to check- Returns:
trueif the file exists,falseotherwise- Throws:
KettleXMLException- if an error occurred while checking
-
-