Class DomUtils

java.lang.Object
eu.europa.esig.dss.xml.utils.DomUtils

public final class DomUtils extends Object
The utils for dealing with org.w3c.dom objects
  • Method Details

    • registerNamespace

      @Deprecated public static boolean registerNamespace(eu.europa.esig.dss.xml.common.definition.DSSNamespace namespace)
      Deprecated.
      since DSS 6.5. Please use XPathUtils#registerNamespace method instead.
      This method allows to register a namespace and associated prefix. If the prefix exists already it is replaced.
      Parameters:
      namespace - namespace object with the prefix and the URI
      Returns:
      true if this map did not already contain the specified element
    • getSecureDocumentBuilderFactory

      public static DocumentBuilderFactory getSecureDocumentBuilderFactory()
      This method returns a new instance of DocumentBuilderFactory with configured security features
      Returns:
      an instance of DocumentBuilderFactory with enabled security features
    • getSecureTransformerFactory

      public static TransformerFactory getSecureTransformerFactory()
      This method returns a new instance of TransformerFactory with secured features enabled
      Returns:
      an instance of TransformerFactory with enabled secure features
    • getSecureTransformer

      public static Transformer getSecureTransformer()
      This method returns a new instance of Transformer with secured features enabled
      Returns:
      an instance of Transformer with enabled secure features
    • startsWithXmlPreamble

      public static boolean startsWithXmlPreamble(byte[] byteArray)
      Checks if the given byteArray content starts with an XML Preamble '<' Processes values with or without BOM-encoding NOTE: does not check XML-conformity of the whole file call isDOM(byteArray) for a deep check
      Parameters:
      byteArray - byte array to verify
      Returns:
      TRUE if the provided byte array starts from xmlPreamble, FALSE otherwise
    • startsWithXmlPreamble

      public static boolean startsWithXmlPreamble(eu.europa.esig.dss.model.DSSDocument document)
      Checks if the given document starts with an XML Preamble '<' Processes values with or without BOM-encoding NOTE: does not check XML-conformity of the whole file call isDOM(DSSDocument) for a deep check
      Parameters:
      document - DSSDocument to verify
      Returns:
      TRUE if the provided document starts from xmlPreamble, FALSE otherwise
    • buildDOM

      public static Document buildDOM()
      Creates the new empty Document.
      Returns:
      a new empty Document
    • buildDOM

      public static Document buildDOM(String xmlString)
      This method returns the Document created based on the XML string.
      Parameters:
      xmlString - The string representing the dssDocument to be created.
      Returns:
      a new Document with the xmlString content
    • buildDOM

      public static Document buildDOM(byte[] bytes)
      This method returns the Document created based on byte array.
      Parameters:
      bytes - The bytes array representing the dssDocument to be created.
      Returns:
      a new Document with the bytes content
    • buildDOM

      public static Document buildDOM(InputStream inputStream)
      This method returns the Document created based on the XML InputStream.
      Parameters:
      inputStream - The inputStream stream representing the dssDocument to be created.
      Returns:
      a new Document from InputStream @
    • buildDOM

      public static Document buildDOM(eu.europa.esig.dss.model.DSSDocument dssDocument)
      This method returns the Document created based on the DSSDocument.
      Parameters:
      dssDocument - The DSS representation of the document from which the dssDocument is created.
      Returns:
      a new Document from DSSDocument
    • isDOM

      public static boolean isDOM(byte[] bytes)
      This method returns true if the binaries contains a Document
      Parameters:
      bytes - the binaries to be tested
      Returns:
      true if the binaries represent an XML
    • isDOM

      public static boolean isDOM(eu.europa.esig.dss.model.DSSDocument dssDocument)
      This method returns true if the provided document is a valid XML Document
      Parameters:
      dssDocument - DSSDocument to be tested
      Returns:
      true if the document is an XML
    • setAttributeNS

      public static void setAttributeNS(Element element, eu.europa.esig.dss.xml.common.definition.DSSNamespace namespace, eu.europa.esig.dss.xml.common.definition.DSSAttribute attribute, String value)
      This method adds an attribute with the namespace and the value
      Parameters:
      element - the element where the attribute is needed
      namespace - the used namespace for the attribute
      attribute - the attribute to be added
      value - the value for the given attribute
    • addElement

      public static Element addElement(Document document, Element parentDom, eu.europa.esig.dss.xml.common.definition.DSSNamespace namespace, eu.europa.esig.dss.xml.common.definition.DSSElement element)
      This method creates and adds a new XML Element
      Parameters:
      document - root document
      parentDom - parent node
      namespace - namespace definition
      element - the type of element name
      Returns:
      added element
    • adoptChildren

      public static List<Node> adoptChildren(Element parentElement, Node toBeAdopted)
      Adopts all children of the toBeAdopted Node, excluding the Node itself.
      Parameters:
      parentElement - Element to be extended with children values
      toBeAdopted - Node containing children to be adopted
      Returns:
      a list of Nodes adopted by the parentElement
    • createXPathExpression

      @Deprecated public static XPathExpression createXPathExpression(String xpathString)
      Deprecated.
      since DSS 6.5. Please use eu.europa.esig.dss.xml.utils.xpath.XPathUtils class instead.
      This method creates a new instance of XPathExpression with the given xpath expression
      Parameters:
      xpathString - XPath query string
      Returns:
      an instance of XPathExpression for the given xpathString
    • getValue

      @Deprecated public static String getValue(Node xmlNode, String xPathString)
      Deprecated.
      since DSS 6.5. Please use XPathUtils#getValue(Node xmlNode, XPathQuery xPathQuery) method instead.
      Returns the String value of the corresponding to the XPath query.
      Parameters:
      xmlNode - The node where the search should be performed.
      xPathString - XPath query string
      Returns:
      string value of the XPath query
    • getNodeList

      @Deprecated public static NodeList getNodeList(Node xmlNode, String xPathString)
      Deprecated.
      since DSS 6.5. Please use XPathUtils#getNodeList(Node xmlNode, XPathQuery xPathQuery) method instead.
      Returns the NodeList corresponding to the XPath query.
      Parameters:
      xmlNode - The node where the search should be performed.
      xPathString - XPath query string
      Returns:
      the NodeList corresponding to the XPath query
    • getNode

      @Deprecated public static Node getNode(Node xmlNode, String xPathString)
      Deprecated.
      since DSS 6.5. Please use XPathUtils#getNode(Node xmlNode, XPathQuery xPathQuery) method instead.
      Returns the Node corresponding to the XPath query.
      Parameters:
      xmlNode - The node where the search should be performed.
      xPathString - XPath query string
      Returns:
      the Node corresponding to the XPath query.
    • getElement

      @Deprecated public static Element getElement(Node xmlNode, String xPathString)
      Deprecated.
      since DSS 6.5. Please use XPathUtils#getElement(Node xmlNode, XPathQuery xPathQuery) method instead.
      Returns the Element corresponding to the XPath query.
      Parameters:
      xmlNode - The node where the search should be performed.
      xPathString - XPath query string
      Returns:
      the Element corresponding to the XPath query
    • isNotEmpty

      @Deprecated public static boolean isNotEmpty(Node xmlNode, String xPathString)
      Deprecated.
      since DSS 6.5. To be removed.
      Returns true if the xpath query contains something
      Parameters:
      xmlNode - the current node
      xPathString - the expected child node
      Returns:
      true if the current node has any filled child node
    • getNodesAmount

      @Deprecated public static int getNodesAmount(Node xmlNode, String xPathString)
      Deprecated.
      since DSS 6.5. Please use XPathUtils#getNodesAmount(Node xmlNode, XPathQuery xPathQuery) method instead.
      Returns an amount of found nodes matching the xPathString
      Parameters:
      xmlNode - the current node
      xPathString - the expected child node
      Returns:
      an amount of returned nodes
    • addTextElement

      public static Element addTextElement(Document document, Element parentDom, eu.europa.esig.dss.xml.common.definition.DSSNamespace namespace, eu.europa.esig.dss.xml.common.definition.DSSElement element, String value)
      This method creates and adds a new XML Element with text value
      Parameters:
      document - root document
      parentDom - parent node
      namespace - namespace
      element - element type
      value - element text node value
      Returns:
      added element
    • setTextNode

      public static void setTextNode(Document document, Element parentDom, String text)
      This method sets a text node to the given DOM element.
      Parameters:
      document - root document
      parentDom - parent node
      text - text to be added
    • createXMLGregorianCalendar

      public static XMLGregorianCalendar createXMLGregorianCalendar(Date date)
      Converts a given Date to a new XMLGregorianCalendar.
      Parameters:
      date - the date to be converted
      Returns:
      the new XMLGregorianCalendar or null
    • getDate

      public static Date getDate(String text)
      This method allows to convert the given text (XML representation of a date) to the Date.
      Parameters:
      text - the text representing the XML date
      Returns:
      Date converted or null
    • getChildrenNames

      @Deprecated public static List<String> getChildrenNames(Node xmlNode, String xPathString)
      Deprecated.
      since DSS 6.5. Please use XPathUtils#getChildrenNames(Node xmlNode, XPathQuery xPathQuery) method instead.
      This method returns the list of children's names for a given Node.
      Parameters:
      xmlNode - The node where the search should be performed.
      xPathString - XPath query string
      Returns:
      List of children's names
    • writeDocumentTo

      public static void writeDocumentTo(Document dom, OutputStream os)
      This method writes the Document content to an outputStream
      Parameters:
      dom - the Document to be writed
      os - the OutputStream @ if any error occurred
    • createDssDocumentFromDomDocument

      public static eu.europa.esig.dss.model.DSSDocument createDssDocumentFromDomDocument(Document document, String name)
      This method creates a new InMemoryDocument with the Document content and the given name
      Parameters:
      document - the Document to store
      name - the output filename
      Returns:
      a new instance of InMemoryDocument with the XML and the given filename
    • xmlToString

      public static String xmlToString(Node node)
      This method allows to convert an XML Node to a String.
      Parameters:
      node - Node to be converted
      Returns:
      String representation of the node
    • getCurrentNamespaces

      @Deprecated public static Map<String,String> getCurrentNamespaces()
      Deprecated.
      since DSS 6.5. Please use XPathUtils#getCurrentNamespaces#getPrefixMap method instead.
      This method returns stored namespace definitions
      Returns:
      a map with the prefix and the related URI
    • getXPathByIdAttribute

      @Deprecated public static String getXPathByIdAttribute(String uri)
      Deprecated.
      since DSS 6.5. Please see eu.europa.esig.dss.xml.common.xpath.item.XPathQueryByIdentifier.
      Returns case-insensitive xPath expression
      Parameters:
      uri - to find
      Returns:
      String xPath expression
    • getId

      public static String getId(String uri)
      Gets Id value from the given URI reference Ex. "#signature" = "signature"
      Parameters:
      uri - String representing a URI reference (e.g. "#r-signature-1")
      Returns:
      String Id
    • getElementById

      @Deprecated public static Element getElementById(Node node, String id)
      Deprecated.
      since DSS 6.5. Please use XPathUtils.getElementById(Node node, String id) method instead
      Extract an element from the given document node with the given Id. The method is namespace independent.
      Parameters:
      node - Node containing the element with the Id
      id - String id of an element to find
      Returns:
      Element with the given Id, NULL if unique result is not found
    • startsFromHash

      public static boolean startsFromHash(String uri)
      Returns TRUE if the provided uri starts from the hash "#" character
      Parameters:
      uri - String to be checked
      Returns:
      TRUE if uri is starts from "#", FALSE otherwise
    • isElementReference

      public static boolean isElementReference(String uri)
      Returns TRUE if the provided uri refers to an element in the signature
      Parameters:
      uri - String to be checked
      Returns:
      TRUE if uri is referred to an element, FALSE otherwise
    • toElementReference

      public static String toElementReference(String uri)
      This method translates the given String to a local element reference with the given URI.

      Ex.: "r-123-id" to "#r-123-id" "sample.xml" to "#sample.xml" "#r-xades-enveloped" to "#r-xades-enveloped"

      Parameters:
      uri - String
      Returns:
      String
    • isXPointerQuery

      public static boolean isXPointerQuery(String uriValue)
      Indicates if the given URI is an XPointer query.
      Parameters:
      uriValue - URI to be analysed
      Returns:
      true if it is an XPointer query
    • getXPointerId

      public static String getXPointerId(String uri)
      Method getXPointerId See org.apache.xml.security.utils.resolver.implementations.ResolverXPointer
      Parameters:
      uri - String
      Returns:
      xpointerId to search.
    • isRootXPointer

      public static boolean isRootXPointer(String uri)
      This method checks if the XPointer refers the document root. See org.apache.xml.security.utils.resolver.implementations.ResolverXPointer
      Parameters:
      uri - String URI to verify
      Returns:
      TRUE if the XPointer refers the document root, FALSE otherwise
    • createElementNS

      public static Element createElementNS(Document documentDom, eu.europa.esig.dss.xml.common.definition.DSSNamespace namespace, eu.europa.esig.dss.xml.common.definition.DSSElement element)
      Creates an element with the given namespace
      Parameters:
      documentDom - Document to add the element into
      namespace - DSSNamespace namespace to be defined
      element - DSSElement to add
      Returns:
      created Element with the namespace
    • addNamespaceAttribute

      public static void addNamespaceAttribute(Element element, eu.europa.esig.dss.xml.common.definition.DSSNamespace namespace)
      Adds a namespace attribute to the element
      Parameters:
      element - Element to add a namespace to
      namespace - DSSNamespace to add
    • excludeComments

      public static Document excludeComments(Node node)
      Returns a Document with excluded comments. NOTE: the method modifies the original node!
      Parameters:
      node - Node to process
      Returns:
      Document without comments
    • browseRecursivelyForNamespaceWithUri

      public static eu.europa.esig.dss.xml.common.definition.DSSNamespace browseRecursivelyForNamespaceWithUri(Element element, String uri)
      This method browses through element looking for a namespace with the target uri and returns DSSNamespace if found
      Parameters:
      element - Element to search for a namespace in
      uri - String URI of the namespace to look for
      Returns:
      DSSNamespace if the target namespace has been found, null otherwise
    • serializeNode

      public static byte[] serializeNode(Node xmlNode)
      This method performs the serialization of the given node
      Parameters:
      xmlNode - The node to be serialized.
      Returns:
      the serialized bytes
    • getNodeBytes

      public static byte[] getNodeBytes(Node node)
      Returns bytes of the given node
      Parameters:
      node - Node to get bytes for
      Returns:
      byte array
    • createDeepCopy

      public static Element createDeepCopy(Element element)
      This method creates a deep copy of the Document for the given Element, and returns back the corresponding Element from the copied instance of the Document. This method addresses a canonicalization issue on document copies, reported in SANTUARIO-139.
      Parameters:
      element - Element to get a deep copy for
      Returns:
      Element