Package com.evolveum.midpoint.util
Class DOMUtil
- java.lang.Object
-
- com.evolveum.midpoint.util.DOMUtil
-
public class DOMUtil extends Object
- Since:
- 0.1
- Author:
- Igor Farinic, Radovan Semancik
-
-
Field Summary
-
Constructor Summary
Constructors Constructor Description DOMUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Map<String,String>
allNamespaceDeclarations(Node node)
static void
checkValidXmlChars(String stringValue)
static boolean
compareDocument(Document a, Document b, boolean considerNamespacePrefixes, boolean considerWhitespaces)
static boolean
compareElement(Element a, Element b, boolean considerNamespacePrefixes)
static boolean
compareElement(Element a, Element b, boolean considerNamespacePrefixes, boolean considerWhitespaces)
static boolean
compareElementList(List<Element> aList, List<Element> bList, boolean considerNamespacePrefixes)
static boolean
compareElementList(List<Element> aList, List<Element> bList, boolean considerNamespacePrefixes, boolean considerWhitespaces)
static boolean
compareTextNodeValues(String a, String b)
static boolean
compareTextNodeValues(String a, String b, boolean considerWhitespaces)
static void
copyContent(Element source, Element destination)
static void
createComment(Element element, String text)
static DocumentBuilder
createDocumentBuilder()
static Element
createElement(QName qname)
static Element
createElement(Document document, QName qname)
static Element
createElement(Document document, QName qname, Element parentElement, Element definitionElement)
static Element
createSubElement(Element parent, QName subElementQName)
static String
escapeInvalidXmlCharsIfPresent(String value)
static Element
findElementRecursive(Element element, QName elementQName)
static void
fixNamespaceDeclarations(Element element)
Take all the namespace declaration of parent elements and put them to this element.static Map<String,String>
getAllNonDefaultNamespaceDeclarations(Node node)
static Map<String,String>
getAllVisibleNamespaceDeclarations(Node node)
Returns all namespace declarations visible from the given node.static String
getAttribute(Element element, String attrName)
static String
getAttribute(Element element, QName attrQname)
static Element
getChildElement(Element element, int index)
static Element
getChildElement(Element element, String localPart)
static Element
getChildElement(Element element, QName qname)
static List<Element>
getChildElements(Element element, QName elementName)
static Document
getDocument()
static Document
getDocument(QName rootElementName)
static Document
getDocument(Node node)
static Optional<Element>
getElement(Element root, QName... path)
static @NotNull List<Element>
getElementsWithoutNamespacePrefix(Element element)
static Element
getFirstChildElement(Node parent)
static Integer
getIntegerValue(Element element)
static Element
getLastChildElement(Node parent)
static Element
getMatchingChildElement(Element element, QName qname)
static List<Element>
getMatchingChildElements(Element element, QName qname)
static Element
getNamedElement(List<Element> elements, QName qname)
static String
getNamespaceDeclarationForPrefix(Element targetElement, String prefix)
static String
getNamespaceDeclarationNamespace(Attr attr)
static String
getNamespaceDeclarationPrefix(Attr attr)
static Map<String,String>
getNamespaceDeclarations(Element element)
Returns map of all namespace declarations from specified element (prefix -> namespace).static Map<String,String>
getNamespaceDeclarationsNonNull(Element element)
static Node
getNextSiblingElement(Node node)
static Element
getOrCreateAsFirstElement(Element parentElement, QName elementQName)
static @NotNull QName
getQName(Element element)
static QName
getQName(Node node)
static QName
getQNameAttribute(Element element, String attributeName)
static QName
getQNameAttribute(Element element, QName attributeName)
static QName
getQNameValue(String textContent, Map<String,String> namespaces)
static QName
getQNameValue(Attr attr)
static QName
getQNameValue(Element element)
static QName
getQNameWithoutPrefix(Node node)
static String
getSchemaTargetNamespace(Element xsdSchema)
static boolean
hasApplicationAttributes(Element element)
static boolean
hasChildElements(Node node)
static boolean
hasNamespaceDeclarationForPrefix(Element targetElement, String prefix)
static boolean
hasNoPrefix(Element top)
static boolean
hasXsiType(Element element)
static boolean
isElementName(Element element, QName name)
static boolean
isEmpty(Attr attr)
static boolean
isEmpty(Element element)
static boolean
isJunk(Node node)
static boolean
isMarkedAsIncomplete(Element element)
static boolean
isNamespaceDefinition(Attr attr)
static boolean
isNil(Element element)
static boolean
isPrefixUsed(Element targetElement, String prefix)
static Collection<Attr>
listApplicationAttributes(Element element)
static @NotNull List<Element>
listChildElements(Node node)
Returned list contains non-null objects.static String
lookupOrCreateNamespaceDeclaration(Element element, String namespaceUri, String preferredPrefix, Element definitionElement, boolean allowUseOfDefaultNamespace)
static void
normalize(Node node, boolean keepWhitespaces)
static Document
parse(InputStream inputStream)
static Document
parseDocument(String doc)
static Document
parseFile(File file)
static Document
parseFile(String filePath)
static StringBuffer
printDom(Node node)
static StringBuffer
printDom(Node node, boolean indent, boolean omitXmlDeclaration)
static void
removeXsiType(Element element)
static QName
resolveQName(com.evolveum.midpoint.util.DOMUtil.NamespaceResolver namespaceResolver, String qnameStringRepresentation)
static QName
resolveQName(Element element)
static QName
resolveQName(Node domNode, String qnameStringRepresentation)
Resolves a QName.static QName
resolveXsiType(Element element)
static void
serializeDOMToFile(Node node, File file)
static String
serializeDOMToString(Node node)
static String
serializeElementContent(Element element)
Serializes the content of the element to a string (without the enclosing element tags).static void
setAttributeValue(Element element, String name, String value)
static void
setElementTextContent(Element element, String value)
static void
setNamespaceDeclaration(Element element, String prefix, String namespaceUri)
static void
setNamespaceDeclarations(Element element, Map<String,String> rootNamespaceDeclarations)
static void
setQNameAttribute(Element element, String attributeName, QName attributeValue)
static void
setQNameAttribute(Element element, String attributeName, QName attributeValue, Element definitionElement)
static void
setQNameAttribute(Element element, QName attributeName, QName attributeValue)
static void
setQNameAttribute(Element element, QName attributeName, QName attributeValue, Element definitionElement)
static void
setQNameValue(Element element, QName elementValue)
Sets QName value for a given element.static TransformerFactory
setupTransformerFactory()
static void
setXsiType(Element element, QName type)
static String
showDom(List<Element> elements)
static void
validateNonEmptyQName(QName qname, String shortDescription, boolean allowEmptyNamespace)
-
-
-
Field Detail
-
LOGGER
public static final Trace LOGGER
-
W3C_XML_SCHEMA_XMLNS_URI
public static final String W3C_XML_SCHEMA_XMLNS_URI
- See Also:
- Constant Field Values
-
W3C_XML_SCHEMA_XMLNS_PREFIX
public static final String W3C_XML_SCHEMA_XMLNS_PREFIX
- See Also:
- Constant Field Values
-
W3C_XML_XML_URI
public static final String W3C_XML_XML_URI
- See Also:
- Constant Field Values
-
W3C_XML_XML_PREFIX
public static final String W3C_XML_XML_PREFIX
- See Also:
- Constant Field Values
-
NS_W3C_XSI_PREFIX
public static final String NS_W3C_XSI_PREFIX
- See Also:
- Constant Field Values
-
XSI_TYPE
public static final QName XSI_TYPE
-
XSI_NIL
public static final QName XSI_NIL
-
HACKED_XSI_TYPE
public static final String HACKED_XSI_TYPE
- See Also:
- Constant Field Values
-
IS_LIST_ATTRIBUTE_NAME
public static final String IS_LIST_ATTRIBUTE_NAME
- See Also:
- Constant Field Values
-
IS_INCOMPLETE_ATTRIBUTE_NAME
public static final String IS_INCOMPLETE_ATTRIBUTE_NAME
- See Also:
- Constant Field Values
-
NS_W3C_XML_SCHEMA_PREFIX
public static final String NS_W3C_XML_SCHEMA_PREFIX
- See Also:
- Constant Field Values
-
XSD_SCHEMA_ELEMENT
public static final QName XSD_SCHEMA_ELEMENT
-
XSD_ANNOTATION_ELEMENT
public static final QName XSD_ANNOTATION_ELEMENT
-
XSD_APPINFO_ELEMENT
public static final QName XSD_APPINFO_ELEMENT
-
XSD_DOCUMENTATION_ELEMENT
public static final QName XSD_DOCUMENTATION_ELEMENT
-
XSD_IMPORT_ELEMENT
public static final QName XSD_IMPORT_ELEMENT
-
XSD_INCLUDE_ELEMENT
public static final QName XSD_INCLUDE_ELEMENT
-
XSD_ATTR_TARGET_NAMESPACE
public static final QName XSD_ATTR_TARGET_NAMESPACE
-
XSD_ATTR_NAMESPACE
public static final QName XSD_ATTR_NAMESPACE
-
XSD_ATTR_SCHEMA_LOCATION
public static final QName XSD_ATTR_SCHEMA_LOCATION
-
XSD_DECIMAL
public static final QName XSD_DECIMAL
-
XSD_STRING
public static final QName XSD_STRING
-
XSD_INTEGER
public static final QName XSD_INTEGER
-
XSD_INT
public static final QName XSD_INT
-
XSD_LONG
public static final QName XSD_LONG
-
XSD_SHORT
public static final QName XSD_SHORT
-
XSD_FLOAT
public static final QName XSD_FLOAT
-
XSD_DOUBLE
public static final QName XSD_DOUBLE
-
XSD_BOOLEAN
public static final QName XSD_BOOLEAN
-
XSD_BASE64BINARY
public static final QName XSD_BASE64BINARY
-
XSD_DATETIME
public static final QName XSD_DATETIME
-
XSD_DURATION
public static final QName XSD_DURATION
-
XSD_BYTE
public static final QName XSD_BYTE
-
XSD_QNAME
public static final QName XSD_QNAME
-
XSD_ANYURI
public static final QName XSD_ANYURI
-
XSD_ANY
public static final QName XSD_ANY
-
XSD_ANYTYPE
public static final QName XSD_ANYTYPE
-
NS_WSDL
public static final String NS_WSDL
- See Also:
- Constant Field Values
-
NS_WSDL_SCHEMA_PREFIX
public static final String NS_WSDL_SCHEMA_PREFIX
- See Also:
- Constant Field Values
-
WSDL_IMPORT_ELEMENT
public static final QName WSDL_IMPORT_ELEMENT
-
WSDL_TYPES_ELEMENT
public static final QName WSDL_TYPES_ELEMENT
-
WSDL_ATTR_NAMESPACE
public static final QName WSDL_ATTR_NAMESPACE
-
WSDL_ATTR_LOCATION
public static final QName WSDL_ATTR_LOCATION
-
-
Method Detail
-
setupTransformerFactory
public static TransformerFactory setupTransformerFactory()
-
serializeDOMToFile
public static void serializeDOMToFile(Node node, File file) throws TransformerFactoryConfigurationError, TransformerException
-
getDocument
public static Document getDocument()
-
createDocumentBuilder
public static DocumentBuilder createDocumentBuilder()
-
parse
public static Document parse(InputStream inputStream) throws IOException
- Throws:
IOException
-
printDom
public static StringBuffer printDom(Node node)
-
printDom
public static StringBuffer printDom(Node node, boolean indent, boolean omitXmlDeclaration)
-
listChildElements
@NotNull public static @NotNull List<Element> listChildElements(Node node)
Returned list contains non-null objects.
-
hasChildElements
public static boolean hasChildElements(Node node)
-
resolveQName
public static QName resolveQName(Node domNode, String qnameStringRepresentation)
Resolves a QName. Contrary to traditional XML handling, a QName without prefix is parsed to a QName without namespace, even if default namespace declaration is present.- Parameters:
domNode
- Provides a context in which we will resolve namespace prefixes (may be null)qnameStringRepresentation
- String representation of a QName (e.g. c:RoleType) (may be null)- Returns:
- parsed QName (or null if string representation is blank)
-
isMarkedAsIncomplete
public static boolean isMarkedAsIncomplete(Element element)
-
getSchemaTargetNamespace
public static String getSchemaTargetNamespace(Element xsdSchema) throws SchemaException
- Throws:
SchemaException
-
resolveQName
public static QName resolveQName(com.evolveum.midpoint.util.DOMUtil.NamespaceResolver namespaceResolver, String qnameStringRepresentation)
-
hasXsiType
public static boolean hasXsiType(Element element)
-
removeXsiType
public static void removeXsiType(Element element)
-
setQNameAttribute
public static void setQNameAttribute(Element element, QName attributeName, QName attributeValue)
-
setQNameAttribute
public static void setQNameAttribute(Element element, String attributeName, QName attributeValue)
-
setQNameAttribute
public static void setQNameAttribute(Element element, QName attributeName, QName attributeValue, Element definitionElement)
-
setQNameAttribute
public static void setQNameAttribute(Element element, String attributeName, QName attributeValue, Element definitionElement)
-
setQNameValue
public static void setQNameValue(Element element, QName elementValue)
Sets QName value for a given element.Contrary to standard XML semantics, namespace-less QNames are specified as simple names without prefix (regardless of default prefix used in the XML document).
- Parameters:
element
- Element whose text content should be set to represent QName valueelementValue
- QName value to be stored into the element
-
lookupOrCreateNamespaceDeclaration
public static String lookupOrCreateNamespaceDeclaration(Element element, String namespaceUri, String preferredPrefix, Element definitionElement, boolean allowUseOfDefaultNamespace)
- Parameters:
element
- Element, on which the namespace declaration is evaluatednamespaceUri
- Namespace URI to be assigned to a prefixpreferredPrefix
- Preferred prefixdefinitionElement
- Element, on which namespace declaration will be created (there should not be any redefinitions between definitionElement and element in order for this to work...)allowUseOfDefaultNamespace
- If we are allowed to use default namespace (i.e. return empty prefix). This is important for QNames, see setQNameValue- Returns:
- prefix that is really used
Returned prefix is never null nor "" if allowUseOfDefaultNamespace is false.
-
isNamespaceDefinition
public static boolean isNamespaceDefinition(Attr attr)
-
setNamespaceDeclaration
public static void setNamespaceDeclaration(Element element, String prefix, String namespaceUri)
-
getNamespaceDeclarations
public static Map<String,String> getNamespaceDeclarations(Element element)
Returns map of all namespace declarations from specified element (prefix -> namespace).
-
getNamespaceDeclarationsNonNull
public static Map<String,String> getNamespaceDeclarationsNonNull(Element element)
-
setNamespaceDeclarations
public static void setNamespaceDeclarations(Element element, Map<String,String> rootNamespaceDeclarations)
-
getAllVisibleNamespaceDeclarations
public static Map<String,String> getAllVisibleNamespaceDeclarations(Node node)
Returns all namespace declarations visible from the given node. Uses recursion for simplicity.
-
getAllNonDefaultNamespaceDeclarations
public static Map<String,String> getAllNonDefaultNamespaceDeclarations(Node node)
-
fixNamespaceDeclarations
public static void fixNamespaceDeclarations(Element element)
Take all the namespace declaration of parent elements and put them to this element.
-
hasNamespaceDeclarationForPrefix
public static boolean hasNamespaceDeclarationForPrefix(Element targetElement, String prefix)
-
getNamespaceDeclarationForPrefix
public static String getNamespaceDeclarationForPrefix(Element targetElement, String prefix)
-
listApplicationAttributes
public static Collection<Attr> listApplicationAttributes(Element element)
-
hasApplicationAttributes
public static boolean hasApplicationAttributes(Element element)
-
getMatchingChildElement
public static Element getMatchingChildElement(Element element, QName qname)
-
getMatchingChildElements
public static List<Element> getMatchingChildElements(Element element, QName qname)
-
getOrCreateAsFirstElement
public static Element getOrCreateAsFirstElement(Element parentElement, QName elementQName)
-
getQNameValue
public static QName getQNameValue(String textContent, Map<String,String> namespaces)
-
createElement
public static Element createElement(Document document, QName qname, Element parentElement, Element definitionElement)
-
compareElement
public static boolean compareElement(Element a, Element b, boolean considerNamespacePrefixes)
-
compareElement
public static boolean compareElement(Element a, Element b, boolean considerNamespacePrefixes, boolean considerWhitespaces)
-
compareDocument
public static boolean compareDocument(Document a, Document b, boolean considerNamespacePrefixes, boolean considerWhitespaces)
-
compareElementList
public static boolean compareElementList(List<Element> aList, List<Element> bList, boolean considerNamespacePrefixes)
-
compareElementList
public static boolean compareElementList(List<Element> aList, List<Element> bList, boolean considerNamespacePrefixes, boolean considerWhitespaces)
-
compareTextNodeValues
public static boolean compareTextNodeValues(String a, String b, boolean considerWhitespaces)
-
normalize
public static void normalize(Node node, boolean keepWhitespaces)
-
isJunk
public static boolean isJunk(Node node)
-
validateNonEmptyQName
public static void validateNonEmptyQName(QName qname, String shortDescription, boolean allowEmptyNamespace)
-
findElementRecursive
public static Element findElementRecursive(Element element, QName elementQName)
-
isNil
public static boolean isNil(Element element)
-
serializeElementContent
public static String serializeElementContent(Element element)
Serializes the content of the element to a string (without the enclosing element tags).
-
isEmpty
public static boolean isEmpty(Element element)
-
isEmpty
public static boolean isEmpty(Attr attr)
-
checkValidXmlChars
public static void checkValidXmlChars(String stringValue)
-
hasNoPrefix
public static boolean hasNoPrefix(Element top)
-
-