Package com.yahoo.text
Class XML
- java.lang.Object
-
- com.yahoo.text.XML
-
public class XML extends java.lang.Object
Static XML utility methods- Author:
- Bjorn Borud, Vegard Havdal, bratseth, Steinar Knutsen
-
-
Constructor Summary
Constructors Constructor Description XML()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static org.w3c.dom.Element
getChild(org.w3c.dom.Element e, java.lang.String name)
Returns the first child with the given name, or null if nonestatic java.util.List<org.w3c.dom.Element>
getChildren(org.w3c.dom.Element spec)
Returns the child Element objects from a w3c dom specstatic java.util.List<org.w3c.dom.Element>
getChildren(org.w3c.dom.Element spec, java.lang.String name)
Returns the child Element objects with given name from a w3c dom specstatic org.w3c.dom.Document
getDocument(java.io.File xmlFile)
Returns the parsed Document from an XML filestatic org.w3c.dom.Document
getDocument(java.io.Reader reader)
Returns the parsed Document from an XML filestatic org.w3c.dom.Document
getDocument(java.lang.String xmlString)
Returns the Document of the string XML payloadstatic javax.xml.parsers.DocumentBuilder
getDocumentBuilder()
Creates a new XML DocumentBuilderstatic javax.xml.parsers.DocumentBuilder
getDocumentBuilder(java.lang.String implementation, java.lang.ClassLoader classLoader)
Creates a new XML DocumentBuilderstatic java.lang.String
getNodePath(org.w3c.dom.Node n, java.lang.String sep)
Returns the path to the given xml node, where each node name is separated by the given separator string.static java.lang.String
getValue(org.w3c.dom.Element e)
Gets the string contents of the given Element.static boolean
isName(java.lang.CharSequence possibleName)
Check whether the name of a tag or attribute conforms to XML 1.1 (Second Edition).static java.lang.String
xmlEscape(java.lang.String s1)
Replaces the characters that need to be escaped with their corresponding character entities.static java.lang.String
xmlEscape(java.lang.String s1, boolean isAttribute)
Replaces the characters that need to be escaped with their corresponding character entities.static java.lang.String
xmlEscape(java.lang.String s1, boolean isAttribute, boolean escapeLowAscii)
Replaces the characters that need to be escaped with their corresponding character entities.static java.lang.String
xmlEscape(java.lang.String s1, boolean isAttribute, boolean escapeLowAscii, char stripCharacter)
Replaces the characters that need to be escaped with their corresponding character entities.static java.lang.String
xmlEscape(java.lang.String string, boolean isAttribute, boolean escapeLowAscii, java.lang.StringBuilder buffer)
Replaces the following: all ascii codes less than 32 except 9 (tab), 10 (nl) and 13 (cr) if escapeLowAscii istrue
ampersand (&) less than (<) larger than (>) double quotes (") if isAttribute istrue
with character entities.static java.lang.String
xmlEscape(java.lang.String string, boolean isAttribute, boolean escapeLowAscii, java.lang.StringBuilder buffer, int stripCodePoint)
Replaces the following: all ascii codes less than 32 except 9 (tab), 10 (nl) and 13 (cr) if escapeLowAscii istrue
ampersand (&) less than (<) larger than (>) double quotes (") if isAttribute istrue
with character entities.static java.lang.String
xmlEscape(java.lang.String s1, boolean isAttribute, char stripCharacter)
Replaces the characters that need to be escaped with their corresponding character entities.static java.lang.String
xmlEscape(java.lang.String string, boolean isAttribute, java.lang.StringBuilder buffer)
Replaces the following: all ascii codes less than 32 except 9 (tab), 10 (nl) and 13 (cr) ampersand (&) less than (<) larger than (>) double quotes (") if isAttribute istrue
with character entities.
-
-
-
Method Detail
-
xmlEscape
public static java.lang.String xmlEscape(java.lang.String s1)
Replaces the characters that need to be escaped with their corresponding character entities.- Parameters:
s1
- String possibly containing characters that need to be escaped in XML- Returns:
- Returns the input string with special characters that need to be escaped replaced by character entities.
-
xmlEscape
public static java.lang.String xmlEscape(java.lang.String s1, boolean isAttribute)
Replaces the characters that need to be escaped with their corresponding character entities.- Parameters:
s1
- String possibly containing characters that need to be escaped in XMLisAttribute
- Is the input string to be used as an attribute?- Returns:
- Returns the input string with special characters that need to be escaped replaced by character entities
-
xmlEscape
public static java.lang.String xmlEscape(java.lang.String s1, boolean isAttribute, char stripCharacter)
Replaces the characters that need to be escaped with their corresponding character entities.- Parameters:
s1
- String possibly containing characters that need to be escaped in XMLisAttribute
- Is the input string to be used as an attribute?stripCharacter
- any occurrence of this character is removed from the string- Returns:
- Returns the input string with special characters that need to be escaped replaced by character entities
-
xmlEscape
public static java.lang.String xmlEscape(java.lang.String s1, boolean isAttribute, boolean escapeLowAscii)
Replaces the characters that need to be escaped with their corresponding character entities.- Parameters:
s1
- String possibly containing characters that need to be escaped in XMLisAttribute
- Is the input string to be used as an attribute?escapeLowAscii
- Should ascii characters below 32 be escaped as well- Returns:
- Returns the input string with special characters that need to be escaped replaced by character entities
-
xmlEscape
public static java.lang.String xmlEscape(java.lang.String s1, boolean isAttribute, boolean escapeLowAscii, char stripCharacter)
Replaces the characters that need to be escaped with their corresponding character entities.- Parameters:
s1
- String possibly containing characters that need to be escaped in XMLisAttribute
- Is the input string to be used as an attribute?escapeLowAscii
- Should ascii characters below 32 be escaped as wellstripCharacter
- any occurrence of this character is removed from the string- Returns:
- Returns the input string with special characters that need to be escaped replaced by character entities
-
xmlEscape
public static java.lang.String xmlEscape(java.lang.String string, boolean isAttribute, java.lang.StringBuilder buffer)
Replaces the following:- all ascii codes less than 32 except 9 (tab), 10 (nl) and 13 (cr)
- ampersand (&)
- less than (<)
- larger than (>)
- double quotes (") if isAttribute is
true
-
xmlEscape
public static java.lang.String xmlEscape(java.lang.String string, boolean isAttribute, boolean escapeLowAscii, java.lang.StringBuilder buffer)
Replaces the following:- all ascii codes less than 32 except 9 (tab), 10 (nl) and 13 (cr) if
escapeLowAscii is
true
- ampersand (&)
- less than (<)
- larger than (>)
- double quotes (") if isAttribute is
true
- all ascii codes less than 32 except 9 (tab), 10 (nl) and 13 (cr) if
escapeLowAscii is
-
xmlEscape
public static java.lang.String xmlEscape(java.lang.String string, boolean isAttribute, boolean escapeLowAscii, java.lang.StringBuilder buffer, int stripCodePoint)
Replaces the following:- all ascii codes less than 32 except 9 (tab), 10 (nl) and 13 (cr) if
escapeLowAscii is
true
- ampersand (&)
- less than (<)
- larger than (>)
- double quotes (") if isAttribute is
true
- Parameters:
stripCodePoint
- any occurrence of this character is removed from the string
- all ascii codes less than 32 except 9 (tab), 10 (nl) and 13 (cr) if
escapeLowAscii is
-
getDocument
public static org.w3c.dom.Document getDocument(java.io.File xmlFile)
Returns the parsed Document from an XML file- Throws:
java.lang.IllegalArgumentException
- if the file cannot be opened or parsed
-
getDocument
public static org.w3c.dom.Document getDocument(java.io.Reader reader)
Returns the parsed Document from an XML file- Throws:
java.lang.IllegalArgumentException
- if the file cannot be opened or parsed
-
getDocument
public static org.w3c.dom.Document getDocument(java.lang.String xmlString)
Returns the Document of the string XML payload
-
getDocumentBuilder
public static javax.xml.parsers.DocumentBuilder getDocumentBuilder()
Creates a new XML DocumentBuilder- Returns:
- a DocumentBuilder
- Throws:
java.lang.RuntimeException
- if we fail to create one
-
getDocumentBuilder
public static javax.xml.parsers.DocumentBuilder getDocumentBuilder(java.lang.String implementation, java.lang.ClassLoader classLoader)
Creates a new XML DocumentBuilder- Parameters:
implementation
- which jaxp implementation should be usedclassLoader
- which class loader should be used when getting a new DocumentBuilder- Returns:
- a DocumentBuilder
- Throws:
java.lang.RuntimeException
- if we fail to create one
-
getChildren
public static java.util.List<org.w3c.dom.Element> getChildren(org.w3c.dom.Element spec)
Returns the child Element objects from a w3c dom spec- Returns:
- List of elements. Empty list (never null) if none found or if the given element is null
-
getChildren
public static java.util.List<org.w3c.dom.Element> getChildren(org.w3c.dom.Element spec, java.lang.String name)
Returns the child Element objects with given name from a w3c dom spec- Returns:
- List of elements. Empty list (never null) if none found or the given element is null
-
getValue
public static java.lang.String getValue(org.w3c.dom.Element e)
Gets the string contents of the given Element. Returns "", never null if the element is null, or has no content
-
getChild
public static org.w3c.dom.Element getChild(org.w3c.dom.Element e, java.lang.String name)
Returns the first child with the given name, or null if none
-
getNodePath
public static java.lang.String getNodePath(org.w3c.dom.Node n, java.lang.String sep)
Returns the path to the given xml node, where each node name is separated by the given separator string.- Parameters:
n
- The xml node to find path tosep
- The separator string- Returns:
- The path to the xml node as a String
-
isName
public static boolean isName(java.lang.CharSequence possibleName)
Check whether the name of a tag or attribute conforms to XML 1.1 (Second Edition). This does not check against reserved names, it only checks the set of characters used.- Parameters:
possibleName
- a possibly valid XML name- Returns:
- true if the name may be used as an XML tag or attribute name
-
-