Class 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 Detail

    • 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 in
        code - 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 in
        tag - 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 in
        tag - 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 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 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 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 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 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
        attributeName - the subtag to look for
        attributeValue - 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 in
        tag - 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 in
        tag - 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 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 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 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 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 String getTagAttribute​(Node node,
                                             String attribute)
      • 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 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 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 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 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 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 load
        tag - 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 document
        deferNodeExpansion - 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 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 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​(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 tag
        val - 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 tag
        val - 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 tag
        bool - 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 tag
        bool - 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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)
      • stringToTimestamp

        public static Timestamp stringToTimestamp​(String dateString)
      • date2string

        public static String date2string​(Date date)
      • timestamp2string

        public static String timestamp2string​(Timestamp timestamp)
      • 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)
      • 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 - the FileObject instance to check
        Returns:
        true if the file exists, false otherwise
        Throws:
        KettleXMLException - if an error occurred while checking