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 int
DEFAULT_RETRY_ATTEMPTS
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
addTagValue(String tag, boolean bool)
Build an XML string (including a carriage return) for a certain tag boolean valuestatic String
addTagValue(String tag, boolean bool, boolean cr)
Build an XML string for a certain tag boolean valuestatic String
addTagValue(String tag, byte[] val)
Build an XML string (including a carriage return) for a certain tag binary (byte[]) valuestatic String
addTagValue(String tag, byte[] val, boolean cr)
Build an XML string (including a carriage return) for a certain tag binary (byte[]) valuestatic String
addTagValue(String tag, double d)
Build an XML string (with carriage return) for a certain tag double valuestatic String
addTagValue(String tag, double d, boolean cr)
Build an XML string for a certain tag double valuestatic String
addTagValue(String tag, int i)
Build an XML string (with carriage return) for a certain tag integer valuestatic String
addTagValue(String tag, int i, boolean cr)
Build an XML string for a certain tag integer valuestatic String
addTagValue(String tag, long l)
Build an XML string for a certain tag long integer valuestatic String
addTagValue(String tag, long l, boolean cr)
Build an XML string for a certain tag long integer valuestatic String
addTagValue(String tag, String val)
Build an XML string (including a carriage return) for a certain tag String valuestatic String
addTagValue(String tag, String val, boolean cr, String... attributes)
Build an XML string for a certain tag String valuestatic String
addTagValue(String tag, BigDecimal val)
Build an XML string (including a carriage return) for a certain tag BigDecimal valuestatic String
addTagValue(String tag, BigDecimal val, boolean cr)
Build an XML string (including a carriage return) for a certain tag BigDecimal valuestatic String
addTagValue(String tag, Date date)
Build an XML string (with carriage return) for a certain tag Date valuestatic String
addTagValue(String tag, Date date, boolean cr)
Build an XML string for a certain tag Date valuestatic String
addTagValue(KettleAttributeInterface tag, boolean bool)
Build an XML string (including a carriage return) for a certain tag boolean valuestatic String
addTagValue(KettleAttributeInterface tag, int i)
Build an XML string (with carriage return) for a certain tag integer valuestatic String
addTagValue(KettleAttributeInterface tag, String val)
Build an XML string (including a carriage return) for a certain tag String valuestatic void
appendReplacedChars(StringBuilder value, String string)
static String
buildCDATA(String string)
static StringBuilder
buildCDATA(StringBuilder builder, String string)
static boolean
checkFile(org.apache.commons.vfs2.FileObject fileObject)
Checks if a givenFileObject
instance corresponds to an existing file.static String
closeTag(String tag)
static StringBuilder
closeTag(StringBuilder builder, String tag)
static int
countNodes(Node n, String tag)
Count nodes with a certain tagstatic DocumentBuilder
createDocumentBuilder(boolean namespaceAware, boolean deferNodeExpansion)
static String
date2string(Date date)
static String
encodeBinaryData(byte[] val)
static String
formatNode(Node node)
static Node
getLastSubNode(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 String
getNodeValue(Node n)
Find the value entry in a nodestatic Node
getNodeWithAttributeValue(Node n, String tag, String attributeName, String attributeValue)
Get node child with a certain subtag set to a certain valuestatic Node
getNodeWithTagValue(Node n, String tag, String subtag, String subtagvalue, int nr)
Get node child with a certain subtag set to a certain valuestatic 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 childstatic 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, String tag)
Get the value of a tag in a nodestatic 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
getTagValue(Node n, KettleAttributeInterface code)
Get the value of a tag in a nodestatic String
getTagValueWithAttribute(Node n, String tag, String attribute)
Get the value of a tag in a nodestatic String
getXMLHeader()
The header string to specify encoding in UTF-8 for XML filesstatic String
getXMLHeader(String encoding)
The header string to specify encoding in an XML filestatic Document
loadXMLFile(File resource)
static Document
loadXMLFile(InputStream inputStream)
Read in an XML file from the passed input stream and return an XML documentstatic Document
loadXMLFile(InputStream inputStream, String systemID, boolean ignoreEntities, boolean namespaceAware)
Load a file into an XML documentstatic Document
loadXMLFile(String filename)
Load a file into an XML documentstatic Document
loadXMLFile(URL resource)
Load a file into an XML documentstatic Document
loadXMLFile(org.apache.commons.vfs2.FileObject fileObject)
Load a file into an XML documentstatic Document
loadXMLFile(org.apache.commons.vfs2.FileObject fileObject, String systemID, boolean ignoreEntities, boolean namespaceAware)
Load a file into an XML documentstatic Document
loadXMLString(String string)
Calls loadXMLString with deferNodeExpansion = TRUEstatic Document
loadXMLString(String string, Boolean namespaceAware, Boolean deferNodeExpansion)
Load a String into an XML documentstatic Node
loadXMLString(String xml, String tag)
Loads the XML document in parameter xml and returns the 'tag' entry.static Document
loadXMLString(DocumentBuilder db, String string)
static String
openTag(String tag)
static StringBuilder
openTag(StringBuilder builder, String tag)
static StringBuilder
openTag(StringBuilder builder, String tag, Map<String,String> attributes)
static byte[]
stringToBinary(String string)
Convert a XML encoded binary string back to binary formatstatic Date
stringToDate(String dateString)
static Timestamp
stringToTimestamp(String dateString)
static String
timestamp2string(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 KettleXMLException
Checks if a given
FileObject
instance corresponds to an existing file.- Parameters:
fileObject
- theFileObject
instance to check- Returns:
true
if the file exists,false
otherwise- Throws:
KettleXMLException
- if an error occurred while checking
-
-