Package com.sun.identity.shared.xml
Class XMLUtils
- java.lang.Object
-
- com.sun.identity.shared.xml.XMLUtils
-
public class XMLUtils extends Object
This class contains utilities to parse XML documents
-
-
Constructor Summary
Constructors Constructor Description XMLUtils()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static SAXSourcecreateSAXSource(InputSource source)Creates a SAXSource instance based on the incoming InputSource, which later on can be safely used by JAXB unmarshalling.static SetdecodeAttributeSet(Set set)static SetencodeAttributeSet(Set set, Debug debug)static StringescapeSpecialCharacters(String text)Removes invalid XML characters from the input text and then replaces XML special character&,<,>,",'with corresponding entity references.StringgetATTR_VALUE_PAIR_NODE()static Set<String>getAttributeValuePair(Node node)Method to get Values within AttributeValuePair as a java.util.Setstatic Set<String>getAttributeValuePair(Node node, boolean unescape)Method to get Values within AttributeValuePair as a java.util.Set Ifunescape is set to false, xml escaped chars will not be unescaped. static NodegetChildNode(Node parentNode, String childName)static SetgetChildNodes(Node parentNode, String childName)static StringgetChildrenValue(Element element)Gets the children value of an element.static ListgetElementsByTagNameNS1(Element element, String nsName, String tagName)This method searches children of Element element for element with tagName and namespaceURI nsName.static StringgetElementString(Element element)Gets the value of an element.static StringgetElementValue(Element element)Gets the value of an element.static NodegetNamedChildNode(Node parentNode, String childNodeName, String attrName, String attrValue)Returns a child node that has the given node name and give attribute name and value.static StringgetNodeAttributeValue(Node node, String attrName)static StringgetNodeAttributeValueNS(Node node, String namespaceURI, String attrName)Gets attribute value of a node.static NodegetRootNode(Document doc, String nodeName)static DocumentBuildergetSafeDocumentBuilder(boolean validating)Provides a secure DocumentBuilder implementation, which is protected against different types of entity expansion attacks and makes sure that only locally available DTDs can be referenced within the XML document.static SAXParsergetSafeSAXParser(boolean validating)Provides a secure SAXParser instance, which is protected against different types of entity expension, DoS attacks and makes sure that only locally available DTDs can be referenced within the XML document.static TransformerFactorygetTransformerFactory()Provides a cachedTransformerFactoryinstance for the current thread.static StringgetValueOfValueNode(Node n)Method to get the value of "Value" nodestatic StringgetValueOfValueNode(Node n, boolean unescape)Method to get the value of "Value" node Ifunescape is set to false, xml escaped chars will not be unescaped. static StringgetValueOfValueNodeNoTrim(Node n)Method to get the value of "Value" nodestatic StringgetValueOfValueNodeNoTrim(Node n, boolean unescape)Method to get the value of "Value" node Ifunescape is set to false, xml escaped chars will not be unescaped. static DocumentgetXMLDocument(InputStream in)static booleanhasElementChild(Node node)Checks if a node has a child of ELEMENT type.static booleanisValidating()static DocumentnewDocument()Obtains a new instance of a DOM Document objectstatic SetparseAttributesTag(Node n)This method parse an Attributes tag, DTD for Attribute is as follows.static Map<String,Set<String>>parseAttributeValuePairTags(Node parentNode)static Stringprint(Node node)Print a Node tree recursively using UTF-8 encoding.static Stringprint(Node node, String encoding)Prints a Node tree recursively.static StringprintAttributeValue(Element node, String prefix)Print SAML Attribute Element and replace its prefix with the input prefix.static StringremoveInvalidXMLChars(String text)Remove invalid XML characters from a string.static StringremoveNullCharAtEnd(String st)static DocumenttoDOMDocument(InputStream is, Debug debug)Converts the XML document from an input stream to DOM Document format.static DocumenttoDOMDocument(String xmlString, Debug debug)Converts the XML document from a String format to DOM Document format.static StringunescapeSpecialCharacters(String text)Returns unescape special character text.
-
-
-
Method Detail
-
getATTR_VALUE_PAIR_NODE
public String getATTR_VALUE_PAIR_NODE()
-
isValidating
public static boolean isValidating()
-
toDOMDocument
public static Document toDOMDocument(String xmlString, Debug debug)
Converts the XML document from a String format to DOM Document format.- Parameters:
xmlString- is the XML document in a String formatdebug- is the debug object used for logging debug info- Returns:
- Document is the DOM object obtained by converting the String XML Returns null if xmlString is null. Returns null if there are any parser errores.
-
toDOMDocument
public static Document toDOMDocument(InputStream is, Debug debug)
Converts the XML document from an input stream to DOM Document format.- Parameters:
is- is the InputStream that contains XML document- Returns:
- Document is the DOM object obtained by parsing the input stream. Returns null if there are any parser errores.
-
parseAttributesTag
public static Set parseAttributesTag(Node n)
This method parse an Attributes tag, DTD for Attribute is as follows.< !-- This DTD defines the DPro Attribute tag. Unique Declaration name for DOCTYPE tag: "Directory Pro 5.0 Attributes DTD" --> < !ELEMENT Attributes (Attribute)+> < !ELEMENT Attribute EMPTY> < !ATTLIST Attribute name NMTOKEN #REQUIRED >- Parameters:
n- Node- Returns:
- Set Set of the attribute names
-
parseAttributeValuePairTags
public static Map<String,Set<String>> parseAttributeValuePairTags(Node parentNode)
- Parameters:
parentNode- is the element tag that contains all the AttirbuteValuePair tags as children- Returns:
- Map Returns the AV pairs in a Map where each entry in the Map is an AV pair. The key is the attribute name and the value is a Set of String objects.
-
newDocument
public static Document newDocument() throws ParserConfigurationException
Obtains a new instance of a DOM Document object- Returns:
- a new instance of a DOM Document object
- Throws:
Exception- if an error occurs while constructing a new documentParserConfigurationException
-
getXMLDocument
public static Document getXMLDocument(InputStream in) throws Exception
- Throws:
Exception
-
hasElementChild
public static boolean hasElementChild(Node node)
Checks if a node has a child of ELEMENT type.- Parameters:
node- a node- Returns:
- true if the node has a child of ELEMENT type
-
getNamedChildNode
public static Node getNamedChildNode(Node parentNode, String childNodeName, String attrName, String attrValue)
Returns a child node that has the given node name and give attribute name and value.
-
getElementValue
public static String getElementValue(Element element)
Gets the value of an element. This method returns a concatenated String from all its TEXT children.- Parameters:
element- a DOM tree element.- Returns:
- A String that contained in its TEXT children; or null if an error occurred.
-
getChildrenValue
public static String getChildrenValue(Element element)
Gets the children value of an element. This method returns a concatenated String from all its children.- Parameters:
element- a DOM tree element.- Returns:
- A String that contained in its TEXT children; or null if an error occurred.
-
getElementString
public static String getElementString(Element element)
Gets the value of an element. This method returns a concatenated String from all its TEXT children.- Parameters:
element- a DOM tree element.- Returns:
- A String that contained in its TEXT children; or null if an error occurred or the input contain non Node.TEXT_NODE node.
-
getNodeAttributeValueNS
public static String getNodeAttributeValueNS(Node node, String namespaceURI, String attrName)
Gets attribute value of a node.- Parameters:
node- a nodenamespaceURI- attribute namespace URIattrName- attribute name- Returns:
- attribute value
-
getAttributeValuePair
public static Set<String> getAttributeValuePair(Node node)
Method to get Values within AttributeValuePair as a java.util.Set
-
getAttributeValuePair
public static Set<String> getAttributeValuePair(Node node, boolean unescape)
Method to get Values within AttributeValuePair as a java.util.Set Ifunescape is set to false, xml escaped chars will not be unescaped.
-
getValueOfValueNode
public static String getValueOfValueNode(Node n)
Method to get the value of "Value" node
-
getValueOfValueNode
public static String getValueOfValueNode(Node n, boolean unescape)
Method to get the value of "Value" node Ifunescape is set to false, xml escaped chars will not be unescaped.
-
getValueOfValueNodeNoTrim
public static String getValueOfValueNodeNoTrim(Node n)
Method to get the value of "Value" node
-
getValueOfValueNodeNoTrim
public static String getValueOfValueNodeNoTrim(Node n, boolean unescape)
Method to get the value of "Value" node Ifunescape is set to false, xml escaped chars will not be unescaped.
-
getElementsByTagNameNS1
public static List getElementsByTagNameNS1(Element element, String nsName, String tagName)
This method searches children of Element element for element with tagName and namespaceURI nsName. It searchs one level down only.- Parameters:
element- The root elementnsName- NamespaceURItagName- A String representing the name of the tag to be searched for.- Returns:
- A List of elements that meet the criterial.
-
printAttributeValue
public static String printAttributeValue(Element node, String prefix)
Print SAML Attribute Element and replace its prefix with the input prefix.- Parameters:
node- A DOM tree Nodeprefix- A String representing the new prefix- Returns:
- An xml String representation of the DOM tree.
-
print
public static String print(Node node)
Print a Node tree recursively using UTF-8 encoding.- Parameters:
node- A DOM tree Node- Returns:
- An xml String representation of the DOM tree.
-
print
public static String print(Node node, String encoding)
Prints a Node tree recursively.- Parameters:
node- A DOM tree Nodeencoding- character encoding- Returns:
- An xml String representation of the DOM tree.
-
unescapeSpecialCharacters
public static String unescapeSpecialCharacters(String text)
Returns unescape special character text.- Parameters:
text- String to be unescaped.- Returns:
- unescape special character text.
-
escapeSpecialCharacters
public static String escapeSpecialCharacters(String text)
Removes invalid XML characters from the input text and then replaces XML special character&,<,>,",'with corresponding entity references.- Parameters:
text- The input that needs to be escaped. May be null.- Returns:
- String with the special characters replaced with entity references. May be null.
-
removeInvalidXMLChars
public static String removeInvalidXMLChars(String text)
Remove invalid XML characters from a string.- Parameters:
text- the text to cleanse.- Returns:
- cleansed text or the original string if it is null or empty
-
getSafeDocumentBuilder
public static DocumentBuilder getSafeDocumentBuilder(boolean validating) throws ParserConfigurationException
Provides a secure DocumentBuilder implementation, which is protected against different types of entity expansion attacks and makes sure that only locally available DTDs can be referenced within the XML document.- Parameters:
validating- Whether the returned DocumentBuilder should validate input.- Returns:
- A secure DocumentBuilder instance.
- Throws:
ParserConfigurationException- In case xerces does not support one of the required features.
-
getSafeSAXParser
public static SAXParser getSafeSAXParser(boolean validating) throws ParserConfigurationException, SAXException
Provides a secure SAXParser instance, which is protected against different types of entity expension, DoS attacks and makes sure that only locally available DTDs can be referenced within the XML document.- Parameters:
validating- Whether the returned DocumentBuilder should validate input.- Returns:
- A secure SAXParser instance.
- Throws:
ParserConfigurationException- In case Xerces does not support one of the required features.SAXException- In case Xerces does not support one of the required features.
-
getTransformerFactory
public static TransformerFactory getTransformerFactory()
Provides a cachedTransformerFactoryinstance for the current thread.- Returns:
- A cached
TransformerFactoryinstance.
-
createSAXSource
public static SAXSource createSAXSource(InputSource source) throws JAXBException
Creates a SAXSource instance based on the incoming InputSource, which later on can be safely used by JAXB unmarshalling. The SAXSource will be protected against different types of entity expansion, DoS attacks and makes sure that only locally available DTDs can be referenced within the XML document.- Parameters:
source- The InputSource to be unmarshalled by JAXB- Returns:
- A safe SAXSource instance
- Throws:
JAXBException- In case an error occurs while creating the SAXSource
-
-