Class XMLUtils


  • public final class XMLUtils
    extends Object
    • Method Detail

      • getDirectChildElement

        public static Element getDirectChildElement​(Node parentNode,
                                                    String localName,
                                                    String namespace)
        Gets a direct child with specified localname and namespace.

        Parameters:
        parentNode - the node where to start the search
        localName - local name of the child to get
        namespace - the namespace of the child to get
        Returns:
        the node or null if not such node found
      • getElementText

        public static String getElementText​(Element e)
        Return the text content of an Element, or null if no such text content exists
      • getInputSourceFromURI

        public static InputSource getInputSourceFromURI​(String uri)
        Utility to get the bytes uri. Does NOT handle authenticated URLs, use getInputSourceFromURI(uri, username, password)
        Parameters:
        uri - the resource to get
      • setNamespace

        public static String setNamespace​(Element element,
                                          String namespace,
                                          String prefix)
        Set a namespace/prefix on an element if it is not set already. First off, it searches for the element for the prefix associated with the specified namespace. If the prefix isn't null, then this is returned. Otherwise, it creates a new attribute using the namespace/prefix passed as parameters.
        Parameters:
        element -
        namespace -
        prefix -
        Returns:
        the prefix associated with the set namespace
      • getIDFromReference

        public static String getIDFromReference​(String ref)
        Turn a reference (eg "#5") into an ID (eg "5").
        Parameters:
        ref -
        Returns:
        ref trimmed and with the leading "#" removed, or null if not correctly formed
      • findElementById

        public static Element findElementById​(Node startNode,
                                              String value,
                                              boolean checkMultipleElements)
        Returns the single element that contains an Id with value uri and namespace. The Id can be either a wsu:Id or an Id with no namespace. This is a replacement for a XPath Id lookup with the given namespace. It's somewhat faster than XPath, and we do not deal with prefixes, just with the real namespace URI If checkMultipleElements is true and there are multiple elements, we LOG.a warning and return null as this can be used to get around the signature checking.
        Parameters:
        startNode - Where to start the search
        value - Value of the Id attribute
        checkMultipleElements - If true then go through the entire tree and return null if there are multiple elements with the same Id
        Returns:
        The found element if there was exactly one match, or null otherwise
      • findElement

        public static Element findElement​(Node startNode,
                                          String name,
                                          String namespace)
        Returns the first element that matches name and namespace.

        This is a replacement for a XPath lookup //name with the given namespace. It's somewhat faster than XPath, and we do not deal with prefixes, just with the real namespace URI

        Parameters:
        startNode - Where to start the search
        name - Local name of the element
        namespace - Namespace URI of the element
        Returns:
        The found element or null
      • findElements

        public static List<Element> findElements​(Node startNode,
                                                 String name,
                                                 String namespace)
        Returns all elements that match name and namespace.

        This is a replacement for a XPath lookup //name with the given namespace. It's somewhat faster than XPath, and we do not deal with prefixes, just with the real namespace URI

        Parameters:
        startNode - Where to start the search
        name - Local name of the element
        namespace - Namespace URI of the element
        Returns:
        The found elements (or an empty list)
      • findSAMLAssertionElementById

        public static Element findSAMLAssertionElementById​(Node startNode,
                                                           String value)
        Returns the single SAMLAssertion element that contains an AssertionID/ID that matches the supplied parameter.
        Parameters:
        startNode - Where to start the search
        value - Value of the AssertionID/ID attribute
        Returns:
        The found element if there was exactly one match, or null otherwise