public abstract class XMLUtil extends Object implements XMLConstants
static utilities to help manage common constructs.
| Modifier and Type | Field and Description |
|---|---|
static String |
DTD |
ATTRIBUTES, AUTOGENERATED_DONOTEDIT_S, BANNER_S, CATALOG_XML, CML_SIUNITS, CML_UNITS, ELEMENTS, FPX_REAL, JAVA_ARRAY, JAVA_BOOL, JAVA_BOOLEAN, JAVA_DOUB, JAVA_DOUBLE, JAVA_INT, JAVA_INTEGER, JAVA_STRING, PATTERN_ANYURI, PATTERN_QNAME, SVG_NS, SVG_NS_BASE, SVG_XPATH, TYPES, U_ANGSTROM, U_CELSIUS, U_DEGREE, U_KCAL, WARNING_S, X_OR, XHTML_NS, XLINK_NS, XLINK_PREFIX, XML_SUFF, XMLNS, XPATH_XSD, XSD_ANNOTATION, XSD_ANYURI, XSD_ATTRIBUTE, XSD_ATTRIBUTE_GROUP, XSD_BOOLEAN, XSD_COMPLEX_TYPE, XSD_DATE, XSD_DOCUMENTATION, XSD_DOUBLE, XSD_ELEMENT, XSD_ENUMERATION, XSD_EXTENSION, XSD_FLOAT, XSD_INTEGER, XSD_LENGTH, XSD_LIST, XSD_MAXEXCLUSIVE, XSD_MAXINCLUSIVE, XSD_MINEXCLUSIVE, XSD_MININCLUSIVE, XSD_NONNEGATIVEINTEGER, XSD_NS, XSD_PATTERN, XSD_POSITIVE_NUMBER, XSD_POSITIVEINTEGER, XSD_PREFIX, XSD_QNAME, XSD_RESTRICTION, XSD_SIMPLE_CONTENT, XSD_SIMPLE_TYPE, XSD_STRING, XSD_SUFF, XSD_UNION, XSD_XMLNSC_AMP, C_APOS, C_ATSIGN, C_BACKSLASH, C_BACKSPACE, C_CARET, C_COLON, C_COMMA, C_DEL, C_DOLLAR, C_EQUALS, C_FORMFEED, C_HASH, C_LANGLE, C_LBRAK, C_LCURLY, C_LSQUARE, C_MINUS, C_NBSP, C_NEWLINE, C_NL, C_PERCENT, C_PERIOD, C_PIPE, C_PLUS, C_POUND, C_QUERY, C_QUOT, C_RANGLE, C_RBRAK, C_RCURLY, C_RETURN, C_RSQUARE, C_SEMICOLON, C_SHRIEK, C_SLASH, C_SPACE, C_STAR, C_TAB, C_TILDE, C_UNDER, EPS, F_S, NONWHITEPUNC, NONWHITEPUNC0, NONWHITEPUNC0REGEX, ONE_THIRD, PUNC, S_AMP, S_APOS, S_ATSIGN, S_BACKSLASH, S_CARET, S_COLON, S_COMMA, S_DOLLAR, S_EMPTY, S_EQUALS, S_FORMFEED, S_HASH, S_LANGLE, S_LBRAK, S_LCURLY, S_LSQUARE, S_MINUS, S_NEWLINE, S_NL, S_PERCENT, S_PERIOD, S_PIPE, S_PLUS, S_POUND, S_QUERY, S_QUOT, S_RANGLE, S_RBRAK, S_RCURLY, S_RETURN, S_RSQUARE, S_SEMICOLON, S_SHRIEK, S_SLASH, S_SPACE, S_STAR, S_TAB, S_TILDE, S_UNDER, S_WHITEREGEX, TWO_THIRDS, U_S, WHITESPACE| Constructor and Description |
|---|
XMLUtil() |
| Modifier and Type | Method and Description |
|---|---|
static String |
addMissingEndTags(String s,
String tag)
adds missing end tag
crude - adds /> and then deletes any />(/tag)
|
static void |
BUG(String message)
bug report.
|
static String |
capitalize(String name)
capitalize name e.g.
|
static void |
checkAttributeNames(nu.xom.Element element,
List<String> allowedAttributeNames)
checks that attribute names are in allowed list.
|
static void |
checkChildElementNames(nu.xom.Element element,
List<String> allowedChildNodeNames)
checks that childNode names are in allowed list.
|
static void |
checkPrefixedName(String name)
checks that name is QName.
|
static String |
compareAttributesCanonically(nu.xom.Element refNode,
nu.xom.Element testNode,
String xpath)
compare attributes on two elements.
|
static String |
compareChildNodesCanonically(nu.xom.Element refNode,
nu.xom.Element testNode,
String xpath)
compare child nodes recursively
|
static String |
compareNamespacesCanonically(nu.xom.Element refNode,
nu.xom.Element testNode,
String xpath)
compare namespaces on two elements
|
static String |
compareNonElementNodesCanonically(nu.xom.Node refNode,
nu.xom.Node testNode,
String xpath)
compare non-element nodes.
|
static void |
copyAttributes(nu.xom.Element from,
nu.xom.Element to)
copies attributes of 'from' to 'to'
|
static void |
copyAttributesFromTo(nu.xom.Element from,
nu.xom.Element to)
copies attributes of 'from' to 'to'
|
static void |
debug(nu.xom.Element el)
Deprecated.
use debug(el, message) instead
|
static void |
debug(nu.xom.Element el,
File file,
int indent) |
static void |
debug(nu.xom.Element el,
OutputStream os,
int indent)
debug an element.
|
static void |
debug(nu.xom.Element el,
String message)
debug an element.
|
static void |
debugPreserveWhitespace(nu.xom.Element element) |
static void |
debugToErr(nu.xom.Element el)
debug an element.
|
static void |
deleteAttribute(nu.xom.Element element,
String attName)
it attribute exists detach it.
|
static void |
detach(nu.xom.Element element) |
static nu.xom.Document |
ensureDocument(nu.xom.Element element)
gets Document for element.
|
static String |
equalsCanonically(nu.xom.Element refElement,
nu.xom.Element testElement,
boolean stripWhite)
tests 2 XML objects for equality using recursive descent.
|
static String |
equalsCanonically(nu.xom.Element refElement,
nu.xom.Element testElement,
boolean stripWhite,
String xpath)
tests 2 XML objects for equality using recursive descent.
|
static String |
equalsCanonically(File refFile,
nu.xom.Element testElement,
boolean stripWhite)
compares XML element against reference file for equality using recursive descent.
|
static String |
equalsCanonically(File refFile,
File testFile,
boolean stripWhite)
compares two XML files for equality using recursive descent.
|
static String |
equalsCanonically(String refNodeXML,
nu.xom.Element testElement,
boolean stripWhite)
tests 2 XML objects for equality using recursive descent.
|
static String |
getCanonicalString(nu.xom.Node node)
get XOM default canonical string.
|
static List<nu.xom.Node> |
getChildNodes(nu.xom.Element el)
convenience routine to get child nodes (iterating through getChild(i) is
fragile if children are removed)
|
static String |
getCommonLeadingString(String s1,
String s2) |
static nu.xom.Text |
getFirstTextDescendant(nu.xom.Node node)
gets first text descendant of element.
|
static String |
getFirstValue(nu.xom.Element element,
String xpath,
nu.xom.XPathContext xPathContext)
convenience method to extract value of the first of one-or-more nodes.
|
static nu.xom.Node |
getFollowingSibling(nu.xom.Node current)
get next sibling.
|
static nu.xom.Text |
getLastTextDescendant(nu.xom.Node node)
gets last text descendant of element.
|
static String |
getLocalName(String s)
get localName from qualified name.
|
static nu.xom.Node |
getPrecedingSibling(nu.xom.Node current)
get previous sibling.
|
static String |
getPrefix(String s)
get prefix from qualified name.
|
static List<nu.xom.Element> |
getQueryElements(nu.xom.Node node,
String xpath)
convenience routine to get query Elements
returns empty list if ANY nodes are not elements
|
static List<nu.xom.Element> |
getQueryElements(nu.xom.Node node,
String xpath,
nu.xom.XPathContext context)
convenience routine to get query Elements
returns empty list if ANY nodes are not elements
|
static List<nu.xom.Node> |
getQueryNodes(nu.xom.Node node,
String xpath)
convenience routine to get query nodes (iterating through get(i) is
fragile if nodes are removed)
|
static List<nu.xom.Node> |
getQueryNodes(nu.xom.Node node,
String xpath,
nu.xom.XPathContext context)
convenience routine to get query nodes (iterating thorugh get(i) is
fragile if nodes are removed)
|
static nu.xom.Element |
getSingleElement(nu.xom.Element element,
String xpath) |
static nu.xom.Element |
getSingleElement(nu.xom.Element element,
String xpath,
nu.xom.XPathContext xPathContext)
convenience method to get exactly one element.
|
static String |
getSingleValue(nu.xom.Element element,
String xpath)
convenience method to extract value of exactly one node..
|
static String |
getSingleValue(nu.xom.Element element,
String xpath,
nu.xom.XPathContext xPathContext)
convenience method to extract value of exactly one node.
|
static String |
getXMLContent(nu.xom.Element element)
sets text content of element.
|
static nu.xom.Document |
getXMLResource(String filename)
convenience method to get resource from XMLFile.
|
static String |
isXMLFile(File file)
check file is existing file, not null or directory,
|
static String |
makeAbstractName(String name)
create local Abstract class name.
|
static String |
makeId(String s)
make id from string.
|
static String |
normalizeSpace(String value) |
static nu.xom.Element |
normalizeWhitespaceInTextNodes(nu.xom.Element element) |
static void |
outputQuietly(nu.xom.Element elem,
File file,
int indent)
convenience method to avoid trapping exception
|
static nu.xom.Document |
parseQuietlyToDocument(File xmlFile) |
static nu.xom.Document |
parseQuietlyToDocument(InputStream is) |
static nu.xom.Document |
parseQuietlyToDocumentWithoutDTD(File file) |
static nu.xom.Document |
parseResourceQuietlyToDocument(String resource) |
static nu.xom.Element |
parseXML(String xmlString)
parses XML string into element.
|
static String |
removeScripts(String s) |
static String |
removeTags(String tag,
String ss) |
static void |
removeWhitespaceNodes(nu.xom.Element element)
remeoves all whitespace-only text nodes.
|
static void |
setXMLContent(nu.xom.Element element,
String s)
sets text content of element.
|
static String |
stripDTD(String s)
Removes DOCTYPE
DOCTYPE can cause problems by requiring to load DTD from URL which can
take many seconds or, if offline, can cause failure to parse.
|
static nu.xom.Document |
stripDTDAndOtherProblematicXMLHeadings(String s)
Deprecated.
|
static nu.xom.Element |
stripDTDAndParse(String s)
Removes DOCTYPE and then parses
DOCTYPE can cause problems by requiring to load DTD from URL which can
take many seconds or, if offline, can cause failure to parse.
|
static void |
stripTrailingWhitespaceinTexts(nu.xom.Element element)
some formatted XML introduces spurious WS after text strings
|
static Object[] |
toArray(nu.xom.Elements elements,
Object[] obj)
converts an Elements to a java array.
|
static String |
toXMLString(nu.xom.Element element) |
static void |
transferChildren(nu.xom.Element from,
nu.xom.Element to)
transfers children of 'from' to 'to'.
|
public static final String DTD
public static final void checkPrefixedName(String name)
name - of XMLName
not colonizedpublic static String getPrefix(String s)
s - public static String getLocalName(String s)
s - public static String getSingleValue(nu.xom.Element element, String xpath, nu.xom.XPathContext xPathContext)
element - xpath - xPathContext - defines prefix/namespace used in querypublic static String getSingleValue(nu.xom.Element element, String xpath)
element - xpath - public static String getFirstValue(nu.xom.Element element, String xpath, nu.xom.XPathContext xPathContext)
element - xpath - xPathContext - defines prefix/namespace used in querypublic static nu.xom.Element getSingleElement(nu.xom.Element element,
String xpath,
nu.xom.XPathContext xPathContext)
element - xpath - xPathContext - defines prefix/namespace used in querypublic static final Object[] toArray(nu.xom.Elements elements, Object[] obj)
elements - obj - type of array (e.g. "new CMLAtom[0]"public static void debug(nu.xom.Element el)
el - the elementpublic static void debugToErr(nu.xom.Element el)
el - the elementpublic static void debug(nu.xom.Element el,
String message)
el - the elementmessage - public static void debug(nu.xom.Element el,
OutputStream os,
int indent)
throws IOException
el - the elementos - output streamindent - indentationIOExceptionpublic static void debug(nu.xom.Element el,
File file,
int indent)
throws IOException
IOExceptionpublic static void outputQuietly(nu.xom.Element elem,
File file,
int indent)
elem - file - indent - public static nu.xom.Document getXMLResource(String filename) throws IOException
filename - relative to current class hierarchy.IOExceptionpublic static List<nu.xom.Node> getChildNodes(nu.xom.Element el)
el - may be nullpublic static nu.xom.Element parseXML(String xmlString) throws RuntimeException
xmlString - RuntimeExceptionpublic static List<nu.xom.Node> getQueryNodes(nu.xom.Node node, String xpath, nu.xom.XPathContext context)
node - (can be null)xpath - xpath relative to nodecontext - public static List<nu.xom.Node> getQueryNodes(nu.xom.Node node, String xpath)
node - xpath - public static List<nu.xom.Element> getQueryElements(nu.xom.Node node, String xpath)
node - xpath - public static List<nu.xom.Element> getQueryElements(nu.xom.Node node, String xpath, nu.xom.XPathContext context)
node - xpath - public static nu.xom.Node getFollowingSibling(nu.xom.Node current)
current - may be nullpublic static nu.xom.Node getPrecedingSibling(nu.xom.Node current)
current - public static nu.xom.Text getLastTextDescendant(nu.xom.Node node)
node - public static nu.xom.Text getFirstTextDescendant(nu.xom.Node node)
node - public static void transferChildren(nu.xom.Element from,
nu.xom.Element to)
from - (will be left with no children)to - (will gain 'from' children appended after any existing
childrenpublic static void copyAttributes(nu.xom.Element from,
nu.xom.Element to)
throws IllegalArgumentException
from - to - IllegalArgumentException - null argumentspublic static String getCanonicalString(nu.xom.Node node) throws IllegalArgumentException
node - IllegalArgumentExceptionpublic static void removeWhitespaceNodes(nu.xom.Element element)
element - to strip whitespace frompublic static void setXMLContent(nu.xom.Element element,
String s)
element - s - RuntimeException - if element already has element contentpublic static String getXMLContent(nu.xom.Element element)
element - RuntimeException - if element already has element contentpublic static String toXMLString(nu.xom.Element element)
public static void BUG(String message)
message - public static String makeId(String s)
s - public static String makeAbstractName(String name)
name - public static String capitalize(String name)
name - public static String equalsCanonically(String refNodeXML, nu.xom.Element testElement, boolean stripWhite)
refNodeXML - xml serialization of first ElementtestElement - second ElementstripWhite - if true remove w/s nodespublic static String equalsCanonically(nu.xom.Element refElement, nu.xom.Element testElement, boolean stripWhite)
refElement - first nodetestElement - second nodestripWhite - if true remove w/s nodespublic static String equalsCanonically(nu.xom.Element refElement, nu.xom.Element testElement, boolean stripWhite, String xpath)
refElement - first nodetestElement - second nodestripWhite - if true remove w/s nodespublic static String equalsCanonically(File refFile, File testFile, boolean stripWhite)
refFile - testFile - stripWhite - public static String equalsCanonically(File refFile, nu.xom.Element testElement, boolean stripWhite)
refFile - testElement - stripWhite - public static String isXMLFile(File file)
file - to checkpublic static String compareNamespacesCanonically(nu.xom.Element refNode, nu.xom.Element testNode, String xpath)
refNode - testNode - xpath - current ancestry of refNodepublic static String compareAttributesCanonically(nu.xom.Element refNode, nu.xom.Element testNode, String xpath)
refNode - testNode - xpath - current ancestry of refNodepublic static String compareChildNodesCanonically(nu.xom.Element refNode, nu.xom.Element testNode, String xpath)
refNode - testNode - xpath - current ancestry of refNodepublic static String compareNonElementNodesCanonically(nu.xom.Node refNode, nu.xom.Node testNode, String xpath)
refNode - testNode - xpath - current ancestry of refNodepublic static void stripTrailingWhitespaceinTexts(nu.xom.Element element)
element - public static nu.xom.Element getSingleElement(nu.xom.Element element,
String xpath)
public static void detach(nu.xom.Element element)
public static nu.xom.Document ensureDocument(nu.xom.Element element)
if none exists, creates one. Note that if the element already has ancestry a Document is created for the rootElement. That may mean that XOM.Serializer writes the whole document.
element - public static nu.xom.Document parseQuietlyToDocument(InputStream is)
public static nu.xom.Document parseResourceQuietlyToDocument(String resource)
public static nu.xom.Document parseQuietlyToDocument(File xmlFile)
@Deprecated public static nu.xom.Document stripDTDAndOtherProblematicXMLHeadings(String s) throws IOException
<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN'
'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>
s - IOExceptionpublic static String stripDTD(String s)
<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN' 'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>
s - public static nu.xom.Element stripDTDAndParse(String s)
<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN' 'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>
s - public static String addMissingEndTags(String s, String tag)
String s = "<a><meta></a>";
s = XMLUtil.addMissingEndTags(s, "meta");
Assert.assertEquals("<a><meta/></a>", s);
s - tag - public static void debugPreserveWhitespace(nu.xom.Element element)
public static nu.xom.Element normalizeWhitespaceInTextNodes(nu.xom.Element element)
public static void copyAttributesFromTo(nu.xom.Element from,
nu.xom.Element to)
from - to - public static void deleteAttribute(nu.xom.Element element,
String attName)
element - attName - public static nu.xom.Document parseQuietlyToDocumentWithoutDTD(File file)
public static void checkAttributeNames(nu.xom.Element element,
List<String> allowedAttributeNames)
element - allowedAttributeNames - RuntimeException - if unknown attributepublic static void checkChildElementNames(nu.xom.Element element,
List<String> allowedChildNodeNames)
element - allowedChildNodeNames - can include element names, #text, - other node types are always allowedRuntimeException - if unknown childElementCopyright © 1994–2024 Peter Murray-Rust. All rights reserved.