Class XMLUtils


  • public class XMLUtils
    extends Object
    This class contains utilities to parse XML documents
    • Constructor Detail

      • XMLUtils

        public XMLUtils()
    • Method Detail

      • getATTR_VALUE_PAIR_NODE

        public String getATTR_VALUE_PAIR_NODE()
      • isValidating

        public static boolean isValidating()
      • toDOMDocument

        public static Document toDOMDocument​(String xmlString,
                                             Debug debug)
        Converts the XML document from a String format to DOM Document format.
        Parameters:
        xmlString - is the XML document in a String format
        debug - is the debug object used for logging debug info
        Returns:
        Document is the DOM object obtained by converting the String XML Returns null if xmlString is null. Returns null if there are any parser errores.
      • toDOMDocument

        public static Document toDOMDocument​(InputStream is,
                                             Debug debug)
        Converts the XML document from an input stream to DOM Document format.
        Parameters:
        is - is the InputStream that contains XML document
        Returns:
        Document is the DOM object obtained by parsing the input stream. Returns null if there are any parser errores.
      • parseAttributesTag

        public static Set parseAttributesTag​(Node n)
        This method parse an Attributes tag, DTD for Attribute is as follows.
          < !-- This DTD defines the DPro Attribute tag.
            Unique Declaration name for DOCTYPE tag:
            "Directory Pro 5.0 Attributes DTD"
          -->
          < !ELEMENT Attributes (Attribute)+>
          < !ELEMENT Attribute EMPTY>
          < !ATTLIST Attribute
               name    NMTOKEN         #REQUIRED
          >
         
        Parameters:
        n - Node
        Returns:
        Set Set of the attribute names
      • parseAttributeValuePairTags

        public static Map<String,​Set<String>> parseAttributeValuePairTags​(Node parentNode)
        Parameters:
        parentNode - is the element tag that contains all the AttirbuteValuePair tags as children
        Returns:
        Map Returns the AV pairs in a Map where each entry in the Map is an AV pair. The key is the attribute name and the value is a Set of String objects.
      • getChildNode

        public static Node getChildNode​(Node parentNode,
                                        String childName)
      • hasElementChild

        public static boolean hasElementChild​(Node node)
        Checks if a node has a child of ELEMENT type.
        Parameters:
        node - a node
        Returns:
        true if the node has a child of ELEMENT type
      • getNamedChildNode

        public static Node getNamedChildNode​(Node parentNode,
                                             String childNodeName,
                                             String attrName,
                                             String attrValue)
        Returns a child node that has the given node name and give attribute name and value.
      • getChildNodes

        public static Set getChildNodes​(Node parentNode,
                                        String childName)
      • getElementValue

        public static String getElementValue​(Element element)
        Gets the value of an element. This method returns a concatenated String from all its TEXT children.
        Parameters:
        element - a DOM tree element.
        Returns:
        A String that contained in its TEXT children; or null if an error occurred.
      • getChildrenValue

        public static String getChildrenValue​(Element element)
        Gets the children value of an element. This method returns a concatenated String from all its children.
        Parameters:
        element - a DOM tree element.
        Returns:
        A String that contained in its TEXT children; or null if an error occurred.
      • getElementString

        public static String getElementString​(Element element)
        Gets the value of an element. This method returns a concatenated String from all its TEXT children.
        Parameters:
        element - a DOM tree element.
        Returns:
        A String that contained in its TEXT children; or null if an error occurred or the input contain non Node.TEXT_NODE node.
      • getNodeAttributeValue

        public static String getNodeAttributeValue​(Node node,
                                                   String attrName)
      • getNodeAttributeValueNS

        public static String getNodeAttributeValueNS​(Node node,
                                                     String namespaceURI,
                                                     String attrName)
        Gets attribute value of a node.
        Parameters:
        node - a node
        namespaceURI - attribute namespace URI
        attrName - attribute name
        Returns:
        attribute value
      • getAttributeValuePair

        public static Set<String> getAttributeValuePair​(Node node)
        Method to get Values within AttributeValuePair as a java.util.Set
      • getAttributeValuePair

        public static Set<String> getAttributeValuePair​(Node node,
                                                        boolean unescape)
        Method to get Values within AttributeValuePair as a java.util.Set If unescape is set to false, xml escaped chars will not be unescaped.
      • getValueOfValueNode

        public static String getValueOfValueNode​(Node n)
        Method to get the value of "Value" node
      • getValueOfValueNode

        public static String getValueOfValueNode​(Node n,
                                                 boolean unescape)
        Method to get the value of "Value" node If unescape is set to false, xml escaped chars will not be unescaped.
      • getValueOfValueNodeNoTrim

        public static String getValueOfValueNodeNoTrim​(Node n)
        Method to get the value of "Value" node
      • getValueOfValueNodeNoTrim

        public static String getValueOfValueNodeNoTrim​(Node n,
                                                       boolean unescape)
        Method to get the value of "Value" node If unescape is set to false, xml escaped chars will not be unescaped.
      • getElementsByTagNameNS1

        public static List getElementsByTagNameNS1​(Element element,
                                                   String nsName,
                                                   String tagName)
        This method searches children of Element element for element with tagName and namespaceURI nsName. It searchs one level down only.
        Parameters:
        element - The root element
        nsName - NamespaceURI
        tagName - A String representing the name of the tag to be searched for.
        Returns:
        A List of elements that meet the criterial.
      • printAttributeValue

        public static String printAttributeValue​(Element node,
                                                 String prefix)
        Print SAML Attribute Element and replace its prefix with the input prefix.
        Parameters:
        node - A DOM tree Node
        prefix - A String representing the new prefix
        Returns:
        An xml String representation of the DOM tree.
      • print

        public static String print​(Node node)
        Print a Node tree recursively using UTF-8 encoding.
        Parameters:
        node - A DOM tree Node
        Returns:
        An xml String representation of the DOM tree.
      • print

        public static String print​(Node node,
                                   String encoding)
        Prints a Node tree recursively.
        Parameters:
        node - A DOM tree Node
        encoding - character encoding
        Returns:
        An xml String representation of the DOM tree.
      • unescapeSpecialCharacters

        public static String unescapeSpecialCharacters​(String text)
        Returns unescape special character text.
        Parameters:
        text - String to be unescaped.
        Returns:
        unescape special character text.
      • escapeSpecialCharacters

        public static String escapeSpecialCharacters​(String text)
        Removes invalid XML characters from the input text and then replaces XML special character &, <, >, ", ' with corresponding entity references.
        Parameters:
        text - The input that needs to be escaped. May be null.
        Returns:
        String with the special characters replaced with entity references. May be null.
      • removeInvalidXMLChars

        public static String removeInvalidXMLChars​(String text)
        Remove invalid XML characters from a string.
        Parameters:
        text - the text to cleanse.
        Returns:
        cleansed text or the original string if it is null or empty
      • encodeAttributeSet

        public static Set encodeAttributeSet​(Set set,
                                             Debug debug)
      • decodeAttributeSet

        public static Set decodeAttributeSet​(Set set)
      • removeNullCharAtEnd

        public static String removeNullCharAtEnd​(String st)
      • getSafeDocumentBuilder

        public static DocumentBuilder getSafeDocumentBuilder​(boolean validating)
                                                      throws ParserConfigurationException
        Provides a secure DocumentBuilder implementation, which is protected against different types of entity expansion attacks and makes sure that only locally available DTDs can be referenced within the XML document.
        Parameters:
        validating - Whether the returned DocumentBuilder should validate input.
        Returns:
        A secure DocumentBuilder instance.
        Throws:
        ParserConfigurationException - In case xerces does not support one of the required features.
      • getSafeSAXParser

        public static SAXParser getSafeSAXParser​(boolean validating)
                                          throws ParserConfigurationException,
                                                 SAXException
        Provides a secure SAXParser instance, which is protected against different types of entity expension, DoS attacks and makes sure that only locally available DTDs can be referenced within the XML document.
        Parameters:
        validating - Whether the returned DocumentBuilder should validate input.
        Returns:
        A secure SAXParser instance.
        Throws:
        ParserConfigurationException - In case Xerces does not support one of the required features.
        SAXException - In case Xerces does not support one of the required features.
      • createSAXSource

        public static SAXSource createSAXSource​(InputSource source)
                                         throws JAXBException
        Creates a SAXSource instance based on the incoming InputSource, which later on can be safely used by JAXB unmarshalling. The SAXSource will be protected against different types of entity expansion, DoS attacks and makes sure that only locally available DTDs can be referenced within the XML document.
        Parameters:
        source - The InputSource to be unmarshalled by JAXB
        Returns:
        A safe SAXSource instance
        Throws:
        JAXBException - In case an error occurs while creating the SAXSource