Package eu.europa.esig.dss
Class DomUtils
java.lang.Object
eu.europa.esig.dss.DomUtils
The utils for dealing with
org.w3c.dom objects-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intValue used to pretty print xades signature -
Method Summary
Modifier and TypeMethodDescriptionstatic ElementaddElement(Document document, Element parentDom, DSSNamespace namespace, DSSElement element) This method creates and adds a new XMLElementstatic voidaddNamespaceAttribute(Element element, DSSNamespace namespace) Adds a namespace attribute to the elementstatic ElementaddTextElement(Document document, Element parentDom, DSSNamespace namespace, DSSElement element, String value) This method creates and adds a new XMLElementwith text valuestatic DSSNamespacebrowseRecursivelyForNamespaceWithUri(Element element, String uri) This method browses throughelementlooking for a namespace with the targeturiand returnsDSSNamespaceif foundstatic DocumentbuildDOM()Creates the new empty Document.static DocumentbuildDOM(byte[] bytes) This method returns theDocumentcreated based on byte array.static DocumentbuildDOM(DSSDocument dssDocument) This method returns theDocumentcreated based on theDSSDocument.static DocumentbuildDOM(InputStream inputStream) This method returns theDocumentcreated based on the XML InputStream.static DocumentThis method returns theDocumentcreated based on the XML string.static DSSDocumentcreateDssDocumentFromDomDocument(Document document, String name) This method creates a new InMemoryDocument with theDocumentcontent and the given namestatic ElementcreateElementNS(Document documentDom, DSSNamespace namespace, DSSElement element) Creates an element with the given namespacestatic XMLGregorianCalendarConverts a givenDateto a newXMLGregorianCalendar.static XPathExpressioncreateXPathExpression(String xpathString) This method creates a new instance of XPathExpression with the given xpath expressionstatic NodeexcludeComments(Node node) Returns a Node with excluded comments.getChildrenNames(Node xmlNode, String xPathString) This method returns the list of children's names for a givenNode.This method returns stored namespace definitionsstatic DateThis method allows to convert the given text (XML representation of a date) to theDate.static ElementgetElement(Node xmlNode, String xPathString) Returns the Element corresponding to the XPath query.static ElementgetElementById(Node node, String id) Extract an element from the given documentnodewith the given Idstatic StringGets Id value from the given URI reference Ex.static NodeReturns the Node corresponding to the XPath query.static NodeListgetNodeList(Node xmlNode, String xPathString) Returns the NodeList corresponding to the XPath query.static intgetNodesAmount(Node xmlNode, String xPathString) Returns an amount of found nodes matching thexPathStringstatic DocumentBuilderFactoryThis method returns a new instance of DocumentBuilderFactory with configured security featuresstatic TransformerThis method returns a new instance of Transformer with secured features enabledstatic TransformerFactoryThis method returns a new instance of TransformerFactory with secured features enabledstatic StringReturns the String value of the corresponding to the XPath query.static StringReturns case-insensitive xPath expressionstatic StringgetXPointerId(String uri) Method getXPointerId Seeorg.apache.xml.security.utils.resolver.implementations.ResolverXPointerstatic booleanisDOM(byte[] bytes) This method returns true if the binaries contains aDocumentstatic booleanisDOM(DSSDocument dssDocument) This method returns true if the provided document is a valid XMLDocumentstatic booleanisElementReference(String uri) Returns TRUE if the providedurirefers to an element in the signaturestatic booleanisNotEmpty(Node xmlNode, String xPathString) Returns true if the xpath query contains somethingstatic booleanisRootXPointer(String uri) This method checks if the XPointer refers the document root.static booleanisXPointerQuery(String uriValue) Indicates if the given URI is an XPointer query.static booleanregisterNamespace(DSSNamespace namespace) This method allows to register a namespace and associated prefix.static voidsetAttributeNS(Element element, DSSNamespace namespace, DSSAttribute attribute, String value) This method adds an attribute with the namespace and the valuestatic voidsetTextNode(Document document, Element parentDom, String text) This method sets a text node to the given DOM element.static booleanstartsFromHash(String uri) Returns TRUE if the provideduristarts from the hash "#" characterstatic booleanstartsWithXmlPreamble(byte[] byteArray) Checks if the givenbyteArraycontent 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 checkstatic booleanstartsWithXmlPreamble(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 checkstatic StringtoElementReference(String uri) This method translates the givenStringto a local element reference with the given URI.static voidwriteDocumentTo(Document dom, OutputStream os) This method writes theDocumentcontent to an outputStreamstatic StringxmlToString(Node node) This method allows to convert an XMLNodeto aString.
-
Field Details
-
TRANSFORMER_INDENT_NUMBER
public static final int TRANSFORMER_INDENT_NUMBERValue used to pretty print xades signature- See Also:
-
-
Method Details
-
registerNamespace
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
This method returns a new instance of DocumentBuilderFactory with configured security features- Returns:
- an instance of DocumentBuilderFactory with enabled security features
-
getSecureTransformerFactory
This method returns a new instance of TransformerFactory with secured features enabled- Returns:
- an instance of TransformerFactory with enabled secure features
-
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 givenbyteArraycontent 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
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-DSSDocumentto verify- Returns:
- TRUE if the provided document starts from xmlPreamble, FALSE otherwise
-
buildDOM
Creates the new empty Document.- Returns:
- a new empty Document
-
buildDOM
This method returns theDocumentcreated based on the XML string.- Parameters:
xmlString- The string representing the dssDocument to be created.- Returns:
- a new
Documentwith the xmlString content
-
buildDOM
This method returns theDocumentcreated based on byte array.- Parameters:
bytes- The bytes array representing the dssDocument to be created.- Returns:
- a new
Documentwith the bytes content
-
buildDOM
This method returns theDocumentcreated based on the XML InputStream.- Parameters:
inputStream- The inputStream stream representing the dssDocument to be created.- Returns:
- a new
DocumentfromInputStream@
-
buildDOM
This method returns theDocumentcreated based on theDSSDocument.- Parameters:
dssDocument- The DSS representation of the document from which the dssDocument is created.- Returns:
- a new
DocumentfromDSSDocument
-
isDOM
public static boolean isDOM(byte[] bytes) This method returns true if the binaries contains aDocument- Parameters:
bytes- the binaries to be tested- Returns:
- true if the binaries represent an XML
-
isDOM
This method returns true if the provided document is a valid XMLDocument- Parameters:
dssDocument-DSSDocumentto be tested- Returns:
- true if the document is an XML
-
setAttributeNS
public static void setAttributeNS(Element element, DSSNamespace namespace, DSSAttribute attribute, String value) This method adds an attribute with the namespace and the value- Parameters:
element- the element where the attribute is needednamespace- the used namespace for the attributeattribute- the attribute to be addedvalue- the value for the given attribute
-
addElement
public static Element addElement(Document document, Element parentDom, DSSNamespace namespace, DSSElement element) This method creates and adds a new XMLElement- Parameters:
document- root documentparentDom- parent nodenamespace- namespace definitionelement- the type of element name- Returns:
- added element
-
createXPathExpression
This method creates a new instance of XPathExpression with the given xpath expression- Parameters:
xpathString- XPath query string- Returns:
- an instance of
XPathExpressionfor the given xpathString @ if
-
getValue
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
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
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
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
Returns true if the xpath query contains something- Parameters:
xmlNode- the current nodexPathString- the expected child node- Returns:
- true if the current node has any filled child node
-
getNodesAmount
Returns an amount of found nodes matching thexPathString- Parameters:
xmlNode- the current nodexPathString- the expected child node- Returns:
- an amount of returned nodes
-
addTextElement
public static Element addTextElement(Document document, Element parentDom, DSSNamespace namespace, DSSElement element, String value) This method creates and adds a new XMLElementwith text value- Parameters:
document- root documentparentDom- parent nodenamespace- namespaceelement- element typevalue- element text node value- Returns:
- added element
-
setTextNode
This method sets a text node to the given DOM element.- Parameters:
document- root documentparentDom- parent nodetext- text to be added
-
createXMLGregorianCalendar
Converts a givenDateto a newXMLGregorianCalendar.- Parameters:
date- the date to be converted- Returns:
- the new
XMLGregorianCalendaror null
-
getDate
This method allows to convert the given text (XML representation of a date) to theDate.- Parameters:
text- the text representing the XML date- Returns:
Dateconverted or null
-
getChildrenNames
This method returns the list of children's names for a givenNode.- Parameters:
xmlNode- The node where the search should be performed.xPathString- XPath query string- Returns:
Listof children's names
-
writeDocumentTo
This method writes theDocumentcontent to an outputStream- Parameters:
dom- theDocumentto be writedos- the OutputStream @ if any error occurred
-
createDssDocumentFromDomDocument
This method creates a new InMemoryDocument with theDocumentcontent and the given name- Parameters:
document- theDocumentto storename- the output filename- Returns:
- a new instance of InMemoryDocument with the XML and the given filename
-
xmlToString
This method allows to convert an XMLNodeto aString.- Parameters:
node-Nodeto be converted- Returns:
Stringrepresentation of the node
-
getCurrentNamespaces
This method returns stored namespace definitions- Returns:
- a map with the prefix and the related URI
-
getXPathByIdAttribute
Returns case-insensitive xPath expression- Parameters:
uri- to find- Returns:
StringxPath expression
-
getId
Gets Id value from the given URI reference Ex. "#signature" = "signature" -
getElementById
Extract an element from the given documentnodewith the given Id -
startsFromHash
Returns TRUE if the provideduristarts from the hash "#" character- Parameters:
uri-Stringto be checked- Returns:
- TRUE if
uriis starts from "#", FALSE otherwise
-
isElementReference
Returns TRUE if the providedurirefers to an element in the signature- Parameters:
uri-Stringto be checked- Returns:
- TRUE if
uriis referred to an element, FALSE otherwise
-
toElementReference
This method translates the givenStringto 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" -
isXPointerQuery
Indicates if the given URI is an XPointer query.- Parameters:
uriValue- URI to be analysed- Returns:
- true if it is an XPointer query
-
getXPointerId
Method getXPointerId Seeorg.apache.xml.security.utils.resolver.implementations.ResolverXPointer- Parameters:
uri-String- Returns:
- xpointerId to search.
-
isRootXPointer
This method checks if the XPointer refers the document root. Seeorg.apache.xml.security.utils.resolver.implementations.ResolverXPointer- Parameters:
uri-StringURI to verify- Returns:
- TRUE if the XPointer refers the document root, FALSE otherwise
-
createElementNS
public static Element createElementNS(Document documentDom, DSSNamespace namespace, DSSElement element) Creates an element with the given namespace- Parameters:
documentDom-Documentto add the element intonamespace-DSSNamespacenamespace to be definedelement-DSSElementto add- Returns:
- created
Elementwith the namespace
-
addNamespaceAttribute
Adds a namespace attribute to the element- Parameters:
element-Elementto add a namespace tonamespace-DSSNamespaceto add
-
excludeComments
Returns a Node with excluded comments. NOTE: the method modifies the originalnode!- Parameters:
node-Nodeto process- Returns:
- binaries of the
nodewithout comments
-
browseRecursivelyForNamespaceWithUri
This method browses throughelementlooking for a namespace with the targeturiand returnsDSSNamespaceif found- Parameters:
element-Elementto search for a namespace inuri-StringURI of the namespace to look for- Returns:
DSSNamespaceif the target namespace has been found, null otherwise
-