Package es.gob.afirma.utils
Class UtilsXML
- java.lang.Object
-
- es.gob.afirma.utils.UtilsXML
-
public final class UtilsXML extends java.lang.ObjectClass that contains methods related to the manage of XML elements.
Project:Library for the integration with the services of @Firma, eVisor and TS@.
- Version:
- 1.1, 18/04/2022.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static org.w3c.dom.ElementcreateChild(org.w3c.dom.Element e, java.lang.String childName)Method that creates a child element from a parent element.static org.w3c.dom.ElementcreateElementValue(org.w3c.dom.Element e, java.lang.String elementName, java.lang.String elementValue)Method that creates a new element as a child of a parent element.static voiddeleteNodesNotUsed(org.w3c.dom.Element xmlNode, java.lang.String[] optionalNodeTypes)Method that removes all the nodes and tags not used from a parent XML element.static booleanexistsElement(org.w3c.dom.Element e, java.lang.String elementName)Method that checks if a element exists from a parent element.static java.lang.StringgetAttributeValue(org.w3c.dom.Element element, java.lang.String attributeName)Method that obtains the value of an attribute of a XML element.static java.lang.StringgetAttributeValue(org.w3c.dom.Element element, java.lang.String elementPath, java.lang.String attributeName)Method that obtains the value of an attribute of a XML element.static org.w3c.dom.ElementgetChildElement(org.w3c.dom.Element parentElement, java.lang.String childElementName, java.lang.String signatureId, boolean isRequired)Method that obtains certain element as a child of a parent element.static java.util.List<org.w3c.dom.Element>getChildElements(org.w3c.dom.Element parentElement, java.lang.String childElementName)Method that obtains certain elements as childs of a parent element.static org.w3c.dom.NodeListgetChildNodesByLocalNames(org.w3c.dom.Node element, java.lang.String path)Method that obtains a list with the child elements localized in certainlocal-namepath for certain root element.static org.w3c.dom.DocumentgetDocument(java.io.InputStream is)Method that obtains aDocumentobject from an input stream.static org.w3c.dom.DocumentgetDocumentWithXsdValidation(java.io.File xsdSchema, java.io.InputStream xml)Method that obtains aDocumentobject from an input stream and validates XML document against an XSD schema.static org.w3c.dom.ElementgetElement(org.w3c.dom.Element element, java.lang.String elementName)Method that obtains the child element from a parent element.static java.lang.StringgetElementValue(org.w3c.dom.Element e)Method that obtains the value of a XML element.static java.lang.StringgetElementValue(org.w3c.dom.Element e, java.lang.String elementName)Method that obtains the value of a child element from a parent element.static org.w3c.dom.ElementgetFirstElementNode(org.w3c.dom.Element element)Method that obtains the first child XML element from a parent element.static java.lang.StringgetNodeXPath(org.w3c.dom.Node node)Method that obtains the absolute path of a node.static java.lang.StringgetRelativeXPath(org.w3c.dom.Node nodeChild, org.w3c.dom.Node parent)Method that obtains the relative path of a child node from a parent node.static org.w3c.dom.ElementinsertAttributeValue(org.w3c.dom.Element xmlNode, java.lang.String attributePath, java.lang.String value)Method that adds an attribute to certain node.static org.w3c.dom.ElementinsertValueElement(org.w3c.dom.Element element, java.lang.String elementName, java.lang.String value)Method that inserts a element as a child into a XML element.static org.w3c.dom.DocumentnewDocument()Method that instancies a newDocumentobject.static org.w3c.dom.DocumentparseDocument(java.io.Reader input)Method that obtains a XML document from an input stream.static org.w3c.dom.DocumentparseXMLDocument(java.lang.String xml)Parse a XML Document to aDocumentinstance.static voidremoveAfirmaAttribute(org.w3c.dom.Element element)Method that removes the allafirmaNodeTypenodes from a parent XML element.static org.w3c.dom.ElementremoveElement(org.w3c.dom.Element e, java.lang.String elementName)Method that deletes a child element from a parent element.static org.w3c.dom.ElementreplaceElementValue(org.w3c.dom.Element e, java.lang.String elementName, java.lang.String elementValue)Method that replaces an element with a new value.static org.w3c.dom.ElementsearchChild(org.w3c.dom.Element e, java.lang.String childName)Method that searches a child element from a parent element.static java.util.List<org.w3c.dom.Element>searchChildElements(org.w3c.dom.Element e)Method that obtains a list with the child elements from a parent element.static java.util.List<java.lang.Object>searchListChilds(org.w3c.dom.Element e, java.lang.String childElementName)Method that obtains a list with the child elements localized on the first level of the hierarchical child structure from a parent element.static java.lang.StringtoXMLString(java.lang.Object xmlElement, java.lang.String rootName, boolean asAttributes)Method that obtains a string with the values of a XML element.static java.lang.StringtransformDOMtoString(org.w3c.dom.Document doc)Method that generates a string with XML format from a XML document.static java.lang.StringtransformDOMtoString(org.w3c.dom.Element xmlElement, boolean omitXmlDeclaration)Method that generates a string with XML format from a DOM tree structure.
-
-
-
Method Detail
-
parseDocument
public static org.w3c.dom.Document parseDocument(java.io.Reader input) throws TransformersExceptionMethod that obtains a XML document from an input stream.- Parameters:
input- Parameter that represents the input stream.- Returns:
- an object that represents the XML document.
- Throws:
TransformersException- If the method fails.
-
getElementValue
public static java.lang.String getElementValue(org.w3c.dom.Element e)
Method that obtains the value of a XML element.- Parameters:
e- Parameter that represents the XML element.- Returns:
- the value of the XML element, or
nullif the XML element hasn't any value.
-
getElementValue
public static java.lang.String getElementValue(org.w3c.dom.Element e, java.lang.String elementName)Method that obtains the value of a child element from a parent element. The child element must be localized on the first level of the hierarchical child structure.- Parameters:
e- Parameter that represents the parent element.elementName- Parameter that represents the name of the child element.- Returns:
- the value of the child element or
nullif the element hasn't any value or the child element doesn't exist.
-
getElement
public static org.w3c.dom.Element getElement(org.w3c.dom.Element element, java.lang.String elementName)Method that obtains the child element from a parent element. The child element must be localized on the first level of the hierarchical child structure.- Parameters:
element- Parameter that represents the parent element.elementName- Parameter that represents the name of the child element.- Returns:
- the child element or
nullif the child element doesn't exist.
-
searchChild
public static org.w3c.dom.Element searchChild(org.w3c.dom.Element e, java.lang.String childName)Method that searches a child element from a parent element.- Parameters:
e- Parameter that represents the parent element.childName- Parameter that represents the name of the child element to find.- Returns:
- the child element or
nullif the child element doesn't exist.
-
searchListChilds
public static java.util.List<java.lang.Object> searchListChilds(org.w3c.dom.Element e, java.lang.String childElementName)Method that obtains a list with the child elements localized on the first level of the hierarchical child structure from a parent element.- Parameters:
e- Parameter that represents the parent element.childElementName- Parameter that represents the name of the child elements to find.- Returns:
- the list with the found child elements.
-
searchChildElements
public static java.util.List<org.w3c.dom.Element> searchChildElements(org.w3c.dom.Element e)
Method that obtains a list with the child elements from a parent element.- Parameters:
e- Parameter that represents the parent element.- Returns:
- the list with the found child elements.
-
createChild
public static org.w3c.dom.Element createChild(org.w3c.dom.Element e, java.lang.String childName)Method that creates a child element from a parent element.- Parameters:
e- Parameter that represents the parent element.childName- Parameter that represents the name of the element to create.- Returns:
- the new child element.
-
existsElement
public static boolean existsElement(org.w3c.dom.Element e, java.lang.String elementName)Method that checks if a element exists from a parent element.- Parameters:
e- Parameter that represents the parent element.elementName- Parameter that represents the name of the element to check.- Returns:
- a boolean that indicates if the element exists (true) or not (false).
-
removeElement
public static org.w3c.dom.Element removeElement(org.w3c.dom.Element e, java.lang.String elementName)Method that deletes a child element from a parent element.- Parameters:
e- Parameter that represents the parent element.elementName- Parameter that represents the name of the element to remove.- Returns:
- the removed element, or
nullif the element cannot be removed.
-
replaceElementValue
public static org.w3c.dom.Element replaceElementValue(org.w3c.dom.Element e, java.lang.String elementName, java.lang.String elementValue)Method that replaces an element with a new value. If the element doesn't exist previously, the method creates it.- Parameters:
e- Parameter that represents the parent element.elementName- Parameter that represents the name of the element to replace.elementValue- Parameter that represents the new value of the element.- Returns:
- the parent element with the new child element.
-
createElementValue
public static org.w3c.dom.Element createElementValue(org.w3c.dom.Element e, java.lang.String elementName, java.lang.String elementValue)Method that creates a new element as a child of a parent element.- Parameters:
e- Parameter that represents the parent element.elementName- Parameter that represents the name of the element to create.elementValue- Parameter that represents the value of the element to create.- Returns:
- the parent element with the new child element.
-
toXMLString
public static java.lang.String toXMLString(java.lang.Object xmlElement, java.lang.String rootName, boolean asAttributes) throws TransformersExceptionMethod that obtains a string with the values of a XML element.- Parameters:
xmlElement- Parameter that represents the XML element.rootName- Parameter that represents the name of the root element. It cannot benull.asAttributes- Parameter that indicates if the values of the XML element must be defined as attributes (true) or as elements (false).- Returns:
- the generated string.
- Throws:
TransformersException- If the method fails.
-
transformDOMtoString
public static java.lang.String transformDOMtoString(org.w3c.dom.Element xmlElement, boolean omitXmlDeclaration) throws TransformersExceptionMethod that generates a string with XML format from a DOM tree structure.- Parameters:
xmlElement- Parameter that represents the XML element to process.omitXmlDeclaration- Parameter that specifies whether the XSLT processor should output an XML declaration (true) or not (false).- Returns:
- the generated string.
- Throws:
TransformersException- If the method fails.
-
transformDOMtoString
public static java.lang.String transformDOMtoString(org.w3c.dom.Document doc) throws TransformersExceptionMethod that generates a string with XML format from a XML document.- Parameters:
doc- Parameter that represents the XML document.- Returns:
- the generated string.
- Throws:
TransformersException- If the method fails.
-
deleteNodesNotUsed
public static void deleteNodesNotUsed(org.w3c.dom.Element xmlNode, java.lang.String[] optionalNodeTypes)Method that removes all the nodes and tags not used from a parent XML element. The method find allafirmaNodeTypenodes and deletes them whether the type is equal than one of the indicated types.- Parameters:
xmlNode- Parameter that represents the parent XML element.optionalNodeTypes- Parameter that represents the array with the types of the nodes to remove.
-
removeAfirmaAttribute
public static void removeAfirmaAttribute(org.w3c.dom.Element element)
Method that removes the allafirmaNodeTypenodes from a parent XML element.- Parameters:
element- Parameter that represents the parent XML element.
-
insertAttributeValue
public static org.w3c.dom.Element insertAttributeValue(org.w3c.dom.Element xmlNode, java.lang.String attributePath, java.lang.String value)Method that adds an attribute to certain node.- Parameters:
xmlNode- Parameter that represents the node.attributePath- Parameter that represents the path of the attribute name.value- Parameter that represents the value of the attribute.- Returns:
- the updated XML element.
-
insertValueElement
public static org.w3c.dom.Element insertValueElement(org.w3c.dom.Element element, java.lang.String elementName, java.lang.String value)Method that inserts a element as a child into a XML element.- Parameters:
element- Parameter that represents the parent XML element.elementName- Parameter that represents the name of the element to insert.value- Parameter that represents the value of the element to insert.- Returns:
- the parent XML element with the new child element.
-
getAttributeValue
public static java.lang.String getAttributeValue(org.w3c.dom.Element element, java.lang.String elementPath, java.lang.String attributeName)Method that obtains the value of an attribute of a XML element.- Parameters:
element- Parameter that represents the parent XML element.elementPath- Parameter that represents the XPath path of the element to find.attributeName- Parameter that represents the name of the attribute to find.- Returns:
- the value of the attribute.
-
getAttributeValue
public static java.lang.String getAttributeValue(org.w3c.dom.Element element, java.lang.String attributeName)Method that obtains the value of an attribute of a XML element.- Parameters:
element- Parameter that represents the parent XML element.attributeName- Parameter that represents the name of the attribute to find.- Returns:
- the value of the attribute.
-
getRelativeXPath
public static java.lang.String getRelativeXPath(org.w3c.dom.Node nodeChild, org.w3c.dom.Node parent)Method that obtains the relative path of a child node from a parent node.- Parameters:
nodeChild- Parameter that represents the child node.parent- Parameter that represents the parent node.- Returns:
- the relative path of the child node.
-
getNodeXPath
public static java.lang.String getNodeXPath(org.w3c.dom.Node node)
Method that obtains the absolute path of a node.- Parameters:
node- Parameter that represents the node.- Returns:
- the absolute path of the node.
-
getFirstElementNode
public static org.w3c.dom.Element getFirstElementNode(org.w3c.dom.Element element)
Method that obtains the first child XML element from a parent element.- Parameters:
element- Parameter that represents the parent element.- Returns:
- the found child XML element.
-
getDocument
public static org.w3c.dom.Document getDocument(java.io.InputStream is) throws javax.xml.transform.TransformerExceptionMethod that obtains aDocumentobject from an input stream.- Parameters:
is- Parameter that represents the input stream.- Returns:
- the
Documentobject associated. - Throws:
javax.xml.transform.TransformerException- If the method fails.
-
newDocument
public static org.w3c.dom.Document newDocument() throws javax.xml.parsers.ParserConfigurationExceptionMethod that instancies a newDocumentobject.- Returns:
- a new
Documentobject. - Throws:
javax.xml.parsers.ParserConfigurationException- If the method fails.
-
getDocumentWithXsdValidation
public static org.w3c.dom.Document getDocumentWithXsdValidation(java.io.File xsdSchema, java.io.InputStream xml) throws TransformersExceptionMethod that obtains aDocumentobject from an input stream and validates XML document against an XSD schema.- Parameters:
xsdSchema- xsd schema definition.xml- xml to convert.- Returns:
- the
Documentobject. - Throws:
TransformersException- If the process fails.
-
parseXMLDocument
public static org.w3c.dom.Document parseXMLDocument(java.lang.String xml)
Parse a XML Document to aDocumentinstance.- Parameters:
xml- string with XML content.- Returns:
- a
documentinstance if the string given is a XML document or null otherwise.
-
getChildElement
public static org.w3c.dom.Element getChildElement(org.w3c.dom.Element parentElement, java.lang.String childElementName, java.lang.String signatureId, boolean isRequired) throws SigningExceptionMethod that obtains certain element as a child of a parent element.- Parameters:
parentElement- Parameter that represents the parent element.childElementName- Parameter that represents the name of the element to find.signatureId- Parameter that represents theIdattribute of the signature.isRequired- Parameter that indicates whether the elements is required (true) or not (false).- Returns:
- an object that represents the found element, or
null. - Throws:
SigningException- if child is required and it is not found.
-
getChildElements
public static java.util.List<org.w3c.dom.Element> getChildElements(org.w3c.dom.Element parentElement, java.lang.String childElementName)Method that obtains certain elements as childs of a parent element.- Parameters:
parentElement- Parameter that represents the parent element.childElementName- Parameter that represents the name of the element to find.- Returns:
- a list with the found elements.
-
getChildNodesByLocalNames
public static org.w3c.dom.NodeList getChildNodesByLocalNames(org.w3c.dom.Node element, java.lang.String path) throws javax.xml.xpath.XPathExpressionExceptionMethod that obtains a list with the child elements localized in certainlocal-namepath for certain root element.- Parameters:
element- Parameter that represents the root element.path- Parameter that represents the path.- Returns:
- a list with the child elements localized in the
local-namepath for the root element. - Throws:
javax.xml.xpath.XPathExpressionException- If some expression cannot be evaluated
-
-