|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.opensaml.xml.util.XMLHelper
public final class XMLHelper
A helper class for working with W3C DOM objects.
Field Summary | |
---|---|
private static DatatypeFactory |
dataTypeFactory
JAXP DatatypeFactory. |
static String |
LIST_DELIMITERS
A string which contains the valid delimiters for the XML Schema 'list' type. |
private static Map<String,Object> |
prettyPrintParams
DOM configuration parameters used by LSSerializer in pretty print format output. |
Constructor Summary | |
---|---|
private |
XMLHelper()
Constructor. |
Method Summary | |
---|---|
static void |
adoptElement(Element adoptee,
Document adopter)
Adopts an element into a document if the child is not already in the document. |
static void |
appendChildElement(Element parentElement,
Element childElement)
Appends the child Element to the parent Element, adopting the child Element into the parent's Document if needed. |
static void |
appendNamespaceDeclaration(Element domElement,
String namespaceURI,
String prefix)
Adds a namespace declaration (xmlns:) attribute to the given element. |
static void |
appendTextContent(Element domElement,
String textContent)
Creates a text node with the given content and appends it as child to the given element. |
static Attr |
constructAttribute(Document owningDocument,
QName attributeName)
Constructs an attribute owned by the given document with the given name. |
static Attr |
constructAttribute(Document document,
String namespaceURI,
String localName,
String prefix)
Constructs an attribute owned by the given document with the given name. |
static Element |
constructElement(Document document,
QName elementName)
Constructs an element, rooted in the given document, with the given name. |
static Element |
constructElement(Document document,
String namespaceURI,
String localName,
String prefix)
Constructs an element, rooted in the given document, with the given information. |
static QName |
constructQName(String qname,
Element owningElement)
Constructs a QName from a string (attribute element content) value. |
static QName |
constructQName(String namespaceURI,
String localName,
String prefix)
Constructs a QName. |
static QName |
constructQName(String qname,
XMLObject owningObject)
Constructs a QName from a string (attribute or element content) value. |
static long |
durationToLong(String duration)
Converts a lexical duration, as defined by XML Schema 1.0, into milliseconds. |
static Boolean |
getAttributeValueAsBoolean(Attr attribute)
Parses the attribute's value. |
static List<String> |
getAttributeValueAsList(Attr attribute)
Gets the value of a list-type attribute as a list. |
static QName |
getAttributeValueAsQName(Attr attribute)
Constructs a QName from an attributes value. |
static Map<QName,List<Element>> |
getChildElements(Element root)
Gets the child elements of the given element in a single iteration. |
static List<Element> |
getChildElementsByTagName(Element root,
String localName)
Gets the child nodes with the given local tag name. |
static List<Element> |
getChildElementsByTagNameNS(Element root,
String namespaceURI,
String localName)
Gets the child nodes with the given namespace qualified tag name. |
static DatatypeFactory |
getDataTypeFactory()
Gets a static instance of a JAXP DatatypeFactory. |
static Element |
getElementAncestor(Node currentNode)
Gets the ancestor element node to the given node. |
static List<String> |
getElementContentAsList(Element element)
Gets the value of a list-type element as a list. |
static QName |
getElementContentAsQName(Element element)
Constructs a QName from an element's adjacent Text child nodes. |
static Element |
getFirstChildElement(Node n)
Gets the first child Element of the node, skipping any Text nodes such as whitespace. |
static Attr |
getIdAttribute(Element domElement)
Gets the ID attribute of a DOM element. |
static Locale |
getLanguage(Element element)
Gets the lcoale currently active for the element. |
static DOMImplementationLS |
getLSDOMImpl(Node node)
Get the DOM Level 3 Load/Save DOMImplementationLS for the given node. |
static LSSerializer |
getLSSerializer(DOMImplementationLS domImplLS,
Map<String,Object> serializerParams)
Obtain a the DOM, level 3, Load/Save serializer LSSerializer instance from the
given DOMImplementationLS instance. |
static Element |
getNextSiblingElement(Node n)
Gets the next sibling Element of the node, skipping any Text nodes such as whitespace. |
static QName |
getNodeQName(Node domNode)
Gets the QName for the given DOM node. |
private static Map<String,Object> |
getPrettyPrintParams()
Create the parameters set used in pretty print formatting of an LSSerializer. |
static QName |
getXSIType(Element e)
Gets the XSI type for a given element if it has one. |
static boolean |
hasXSIType(Element e)
Checks if the given element has an xsi:type defined for it. |
static boolean |
isElementNamed(Element e,
String ns,
String localName)
Shortcut for checking a DOM element node's namespace and local name. |
static String |
longToDuration(long duration)
Converts a duration in milliseconds to a lexical duration, as defined by XML Schema 1.0. |
static String |
lookupNamespaceURI(Element startingElement,
Element stopingElement,
String prefix)
Looks up the namespace URI associated with the given prefix starting at the given element. |
static String |
lookupNamespaceURI(Element startingElement,
String prefix)
Looks up the namespace URI associated with the given prefix starting at the given element. |
static String |
lookupPrefix(Element startingElement,
Element stopingElement,
String namespaceURI)
Looks up the namespace prefix associated with the given URI starting at the given element. |
static String |
lookupPrefix(Element startingElement,
String namespaceURI)
Looks up the namespace prefix associated with the given URI starting at the given element. |
static void |
marshallAttribute(QName attributeName,
List<String> attributeValues,
Element domElement,
boolean isIDAttribute)
Marshall an attribute name and value to a DOM Element. |
static void |
marshallAttribute(QName attributeName,
String attributeValue,
Element domElement,
boolean isIDAttribute)
Marshall an attribute name and value to a DOM Element. |
static void |
marshallAttributeMap(AttributeMap attributeMap,
Element domElement)
Marshall the attributes represented by the indicated AttributeMap into the indicated DOM Element. |
static String |
nodeToString(Node node)
Converts a Node into a String using the DOM, level 3, Load/Save serializer. |
static String |
prettyPrintXML(Node node)
Pretty prints the XML node. |
static String |
qnameToContentString(QName qname)
Converts a QName into a string that can be used for attribute values or element content. |
static void |
rootNamespaces(Element domElement)
Ensures that all the visibly used namespaces referenced by the given Element or its descendants are declared by the given Element or one of its descendants. |
private static void |
rootNamespaces(Element domElement,
Element upperNamespaceSearchBound)
Recursively called function that ensures all the visibly used namespaces referenced by the given Element or its descendants are declared if they don't appear in the list of already resolved namespaces. |
static void |
unmarshallToAttributeMap(AttributeMap attributeMap,
Attr attribute)
Unmarshall a DOM Attr to an AttributeMap. |
static void |
writeNode(Node node,
OutputStream output)
Writes a Node out to an OutputStream using the DOM, level 3, Load/Save serializer. |
static void |
writeNode(Node node,
OutputStream output,
Map<String,Object> serializerParams)
Writes a Node out to an OutputStream using the DOM, level 3, Load/Save serializer. |
static void |
writeNode(Node node,
Writer output)
Writes a Node out to a Writer using the DOM, level 3, Load/Save serializer. |
static void |
writeNode(Node node,
Writer output,
Map<String,Object> serializerParams)
Writes a Node out to a Writer using the DOM, level 3, Load/Save serializer. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String LIST_DELIMITERS
private static Map<String,Object> prettyPrintParams
private static DatatypeFactory dataTypeFactory
Constructor Detail |
---|
private XMLHelper()
Method Detail |
---|
public static DatatypeFactory getDataTypeFactory()
public static boolean hasXSIType(Element e)
e
- the DOM element
public static QName getXSIType(Element e)
e
- the element
public static Attr getIdAttribute(Element domElement)
domElement
- the DOM element
public static QName getNodeQName(Node domNode)
domNode
- the DOM node
public static Locale getLanguage(Element element)
element
- element to retrieve local information for
public static Attr constructAttribute(Document owningDocument, QName attributeName)
owningDocument
- the owning documentattributeName
- the name of that attribute
public static Attr constructAttribute(Document document, String namespaceURI, String localName, String prefix)
document
- the owning documentnamespaceURI
- the URI for the namespace the attribute is inlocalName
- the local nameprefix
- the prefix of the namespace that attribute is in
public static QName getAttributeValueAsQName(Attr attribute)
attribute
- the attribute with a QName value
public static Boolean getAttributeValueAsBoolean(Attr attribute)
attribute
- attribute whose value will be converted to a boolean
public static List<String> getAttributeValueAsList(Attr attribute)
attribute
- attribute whose value will be turned into a list
public static void marshallAttribute(QName attributeName, String attributeValue, Element domElement, boolean isIDAttribute)
attributeName
- the attribute name in QName formattributeValue
- the attribute valuedomElement
- the target element to which to marshallisIDAttribute
- flag indicating whether the attribute being marshalled should be handled as an ID-typed
attributepublic static void marshallAttribute(QName attributeName, List<String> attributeValues, Element domElement, boolean isIDAttribute)
attributeName
- the attribute name in QName formattributeValues
- the attribute valuesdomElement
- the target element to which to marshallisIDAttribute
- flag indicating whether the attribute being marshalled should be handled as an ID-typed
attributepublic static void marshallAttributeMap(AttributeMap attributeMap, Element domElement)
attributeMap
- the AttributeMapdomElement
- the target Elementpublic static void unmarshallToAttributeMap(AttributeMap attributeMap, Attr attribute)
attributeMap
- the target AttributeMapattribute
- the target DOM Attrpublic static QName getElementContentAsQName(Element element)
element
- the element with a QName value
public static List<String> getElementContentAsList(Element element)
element
- element whose value will be turned into a list
public static QName constructQName(String namespaceURI, String localName, String prefix)
namespaceURI
- the namespace of the QNamelocalName
- the local name of the QNameprefix
- the prefix of the QName, may be null
public static QName constructQName(String qname, XMLObject owningObject)
qname
- the QName stringowningObject
- XMLObject, with cached DOM, owning the QName
public static QName constructQName(String qname, Element owningElement)
qname
- the QName stringowningElement
- parent DOM element of the Node which contains the QName value
public static Element constructElement(Document document, QName elementName)
document
- the document containing the elementelementName
- the name of the element, must contain a local name, may contain a namespace URI and prefix
public static Element constructElement(Document document, String namespaceURI, String localName, String prefix)
document
- the document containing the elementnamespaceURI
- the URI of the namespace the element is inlocalName
- the element's local nameprefix
- the prefix of the namespace the element is in
public static void appendChildElement(Element parentElement, Element childElement)
parentElement
- the parent ElementchildElement
- the child Elementpublic static void adoptElement(Element adoptee, Document adopter)
adoptee
- the element to be adoptedadopter
- the document into which the element is adoptedpublic static void appendTextContent(Element domElement, String textContent)
domElement
- the element to recieve the text nodetextContent
- the content for the text nodepublic static void appendNamespaceDeclaration(Element domElement, String namespaceURI, String prefix)
domElement
- the element to add the attribute tonamespaceURI
- the URI of the namespaceprefix
- the prefix for the namespacepublic static String lookupNamespaceURI(Element startingElement, String prefix)
Node.lookupNamespaceURI(java.lang.String)
in that it only those namespaces declared by an xmlns
attribute are inspected. The Node method also checks the namespace a particular node was created in by way of a
call like Document.createElementNS(java.lang.String, java.lang.String)
even if the resulting element
doesn't have an namespace delcaration attribute.
startingElement
- the starting elementprefix
- the prefix to look up
public static String lookupNamespaceURI(Element startingElement, Element stopingElement, String prefix)
Node.lookupNamespaceURI(java.lang.String)
in that it only those namespaces declared by an xmlns
attribute are inspected. The Node method also checks the namespace a particular node was created in by way of a
call like Document.createElementNS(java.lang.String, java.lang.String)
even if the resulting element
doesn't have an namespace delcaration attribute.
startingElement
- the starting elementstopingElement
- the ancestor of the starting element that serves as the upper-bound, inclusive, for the
searchprefix
- the prefix to look up
public static String lookupPrefix(Element startingElement, String namespaceURI)
Node.lookupPrefix(java.lang.String)
in that it only those namespaces declared by an xmlns
attribute are inspected. The Node method also checks the namespace a particular node was created in by way of a
call like Document.createElementNS(java.lang.String, java.lang.String)
even if the resulting element
doesn't have an namespace delcaration attribute.
startingElement
- the starting elementnamespaceURI
- the uri to look up
public static String lookupPrefix(Element startingElement, Element stopingElement, String namespaceURI)
Node.lookupPrefix(java.lang.String)
in that it only those namespaces declared by an xmlns
attribute are inspected. The Node method also checks the namespace a particular node was created in by way of a
call like Document.createElementNS(java.lang.String, java.lang.String)
even if the resulting element
doesn't have an namespace delcaration attribute.
startingElement
- the starting elementstopingElement
- the ancestor of the starting element that serves as the upper-bound, inclusive, for the
searchnamespaceURI
- the uri to look up
public static List<Element> getChildElementsByTagNameNS(Element root, String namespaceURI, String localName)
getChildElements(Element)
.
root
- element to retrieve the children fromnamespaceURI
- namespace URI of the child elementlocalName
- local, tag, name of the child element
public static List<Element> getChildElementsByTagName(Element root, String localName)
getChildElements(Element)
.
root
- element to retrieve the children fromlocalName
- local, tag, name of the child element
public static Map<QName,List<Element>> getChildElements(Element root)
root
- element to get the child elements of
public static Element getElementAncestor(Node currentNode)
currentNode
- the node to retrive the ancestor for
public static String nodeToString(Node node)
node
- the node to be written to a string
public static String prettyPrintXML(Node node)
node
- xml node to print
private static Map<String,Object> getPrettyPrintParams()
public static void writeNode(Node node, Writer output)
node
- the node to write outoutput
- the writer to write the XML topublic static void writeNode(Node node, Writer output, Map<String,Object> serializerParams)
node
- the node to write outoutput
- the writer to write the XML toserializerParams
- parameters to pass to the DOMConfiguration
of the serializer
instance, obtained via LSSerializer.getDomConfig()
. May be null.public static void writeNode(Node node, OutputStream output)
node
- the node to write outoutput
- the output stream to write the XML topublic static void writeNode(Node node, OutputStream output, Map<String,Object> serializerParams)
node
- the node to write outoutput
- the output stream to write the XML toserializerParams
- parameters to pass to the DOMConfiguration
of the serializer
instance, obtained via LSSerializer.getDomConfig()
. May be null.public static LSSerializer getLSSerializer(DOMImplementationLS domImplLS, Map<String,Object> serializerParams)
LSSerializer
instance from the
given DOMImplementationLS
instance.
The serializer instance will be configured with the parameters passed as the serializerParams
argument. It will also be configured with an LSSerializerFilter
that shows all nodes to the filter,
and accepts all nodes shown.
domImplLS
- the DOM Level 3 Load/Save implementation to useserializerParams
- parameters to pass to the DOMConfiguration
of the serializer
instance, obtained via LSSerializer.getDomConfig()
. May be null.
public static DOMImplementationLS getLSDOMImpl(Node node)
DOMImplementationLS
for the given node.
node
- the node to evaluate
public static String qnameToContentString(QName qname)
qname
- the QName to convert to a string
public static void rootNamespaces(Element domElement) throws XMLParserException
domElement
- the element to act as the root of the namespace declarations
XMLParserException
- thrown if a namespace prefix is encountered that can't be resolved to a namespace URIprivate static void rootNamespaces(Element domElement, Element upperNamespaceSearchBound) throws XMLParserException
domElement
- the ElementupperNamespaceSearchBound
- the "root" element of the fragment where namespaces may be rooted
XMLParserException
- thrown if a namespace prefix is encountered that can't be resolved to a namespace URIpublic static boolean isElementNamed(Element e, String ns, String localName)
e
- An element to compare againstns
- An XML namespace to comparelocalName
- A local name to compare
public static Element getFirstChildElement(Node n)
n
- The parent in which to search for children
public static Element getNextSiblingElement(Node n)
n
- The sibling to start with
public static long durationToLong(String duration)
duration
- lexical duration representation
public static String longToDuration(long duration)
duration
- the duration
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |