|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.ibm.are.common.XmlUtils
public final class XmlUtils
The XmlUtils
class provides a variety of XML utility methods used by other ARE classes.
Nested Class Summary | |
---|---|
protected static class |
XmlUtils.SingleNode
The SingleNode class encapsulates a single XML node as well as a
string value. |
Field Summary | |
---|---|
static java.lang.String |
COPYRIGHT
Copyright for class bytecode |
Constructor Summary | |
---|---|
XmlUtils()
|
Method Summary | |
---|---|
static org.w3c.dom.Document |
createNewDocument()
Creates a new XML document |
static org.w3c.dom.Element[] |
findAllChildElements(org.w3c.dom.Node parent,
java.lang.String searchNodeName)
Find all child elements with the given name within the direct children of the parent node. |
static org.w3c.dom.Element |
findChildElement(org.w3c.dom.Node parent,
java.lang.String searchNodeName)
Find the first child element with the given name within the direct children of the parent node. |
static org.w3c.dom.Node |
findNodeInList(org.w3c.dom.NodeList theList,
java.lang.String searchNodeName)
Find the first node with the given name in the node list. |
static org.w3c.dom.Node[] |
findNodesInList(org.w3c.dom.NodeList theList,
java.lang.String searchNodeName)
Find all nodes with the given name in the node list. |
static org.w3c.dom.Node |
findNodeWithAttributeValue(org.w3c.dom.NodeList theList,
java.lang.String searchNodeName,
java.lang.String attributeName,
java.lang.String attributeValue)
Searches the node list for a node with the specified name that contains an attribute with the specified name and value. |
static org.w3c.dom.Node |
findParentNode(org.w3c.dom.Node startNode,
java.lang.String parentName)
Recursively searches back up the XML node tree, searching for a parent node that matches the specified search name. |
static java.lang.String |
getItemFromMap(org.w3c.dom.NamedNodeMap nodeMap,
java.lang.String whichItem)
Retrieves the value for the specified attribute from the provided node map |
static java.lang.String |
getItemFromMap(org.w3c.dom.NamedNodeMap nodeMap,
java.lang.String whichItem,
java.lang.String defaultValue)
Retrieves the value for the specified attribute from the provided node map |
static java.lang.String |
getItemFromNode(org.w3c.dom.Node theNode,
java.lang.String whichItem)
Retrieves the value for the specified attribute from the provided node |
static java.lang.String |
getItemFromNode(org.w3c.dom.Node theNode,
java.lang.String whichItem,
java.lang.String defaultValue)
Retrieves the value for the specified attribute from the provided node |
static java.lang.String |
getNodeTextContent(org.w3c.dom.Node node)
An alternative to the Node.getTextContent method (DOM3/JDK1.5)
Note that this method only works with the simplest method
to retrieve text content. |
static com.ibm.are.xml.Reference |
getReferenceFromName(com.ibm.are.xml.XmlParsingEngine parsingEngine,
java.lang.String name)
Retrieves the specified reference from the global context of the provided parsing engine. |
static java.lang.String |
getRequiredAttribute(org.w3c.dom.Node theNode,
java.lang.String attributeName)
Retrieves the value for the specified attribute from the given node. |
static org.w3c.dom.Document |
getXmlDocumentFromFile(java.lang.String xmlFileName)
Retrieves an XML document from a file |
static org.w3c.dom.Document |
getXmlDocumentFromJar(java.lang.String xmlFileName,
java.lang.ClassLoader loader)
Retrieves an XML document from a file in a JAR |
static org.w3c.dom.Document |
getXmlDocumentFromStream(java.io.InputStream xmlFileStream)
Builds and returns an XML document from the provided input stream |
static org.w3c.dom.Document |
getXmlDocumentFromUrl(java.net.URL xmlFileUrl)
Retrieves an XML document from a URL |
static int |
pruneNodes(org.w3c.dom.Node topNode,
java.lang.String nodeName,
java.lang.String attributeKey,
java.lang.String valueToMatch)
Prunes all child nodes that contain the specified attribute name and value |
static boolean |
setItemInMap(org.w3c.dom.NamedNodeMap nodeMap,
java.lang.String whichItem,
java.lang.String value)
Sets the value for the specified attribute in the provided node map |
static boolean |
setItemInNode(org.w3c.dom.Node theNode,
java.lang.String whichItem,
java.lang.String value)
Sets the value for the specified attribute in the provided node |
static void |
setNodeTextContent(org.w3c.dom.Node node,
java.lang.String text)
An alternative to the Node.setTextContent method (DOM3/JDK1.5)
Note that this is a very simple solution which
should only be used for a simple text node. |
static org.w3c.dom.Node |
sortNodes(org.w3c.dom.Node topNode,
java.lang.String childNodeName,
java.lang.String attributeToSortOn)
Sorts a set of child nodes based on the specified attribute value |
static void |
verifyNotEmpty(java.lang.String stringValue,
java.lang.String exceptionMsg)
Verifies the specified string is not null or empty |
static void |
verifyNotNull(java.lang.Object objToVerify,
java.lang.String exceptionMsg)
Verifies the specified object reference is not null |
static void |
verifyNull(java.lang.Object objToVerify,
java.lang.String exceptionMsg)
Verifies the specified object reference is null |
static void |
writeDocumentToXmlFile(org.w3c.dom.Document xmlDoc,
java.io.File xmlFile)
Writes the XML document to the specified file |
static void |
writeXmlDocumentToStream(org.w3c.dom.Document xmlDoc,
java.io.Writer outWriter)
Writes the XML document to the provided writer |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final java.lang.String COPYRIGHT
Constructor Detail |
---|
public XmlUtils()
Method Detail |
---|
public static java.lang.String getItemFromNode(org.w3c.dom.Node theNode, java.lang.String whichItem)
theNode
- The node to retrieve the attribute value fromwhichItem
- The name of the attribute to retrieve the value of
null
if the specified attribute cannot be found in the provided node.public static java.lang.String getItemFromNode(org.w3c.dom.Node theNode, java.lang.String whichItem, java.lang.String defaultValue)
theNode
- The node to retrieve the attribute value fromwhichItem
- The name of the attribute to retrieve the value ofdefaultValue
- The default value to return if the specified attribute
cannot be found in the provided node
public static java.lang.String getItemFromMap(org.w3c.dom.NamedNodeMap nodeMap, java.lang.String whichItem)
nodeMap
- The node map to retrieve the attribute value fromwhichItem
- The name of the attribute to retrieve the value of
null
if the specified attribute cannot be found in the provided node map.public static java.lang.String getItemFromMap(org.w3c.dom.NamedNodeMap nodeMap, java.lang.String whichItem, java.lang.String defaultValue)
nodeMap
- The node map to retrieve the attribute value fromwhichItem
- The name of the attribute to retrieve the value ofdefaultValue
- The default value to return if the specified attribute
cannot be found in the provided node map
public static boolean setItemInMap(org.w3c.dom.NamedNodeMap nodeMap, java.lang.String whichItem, java.lang.String value)
nodeMap
- The node map to set the attribute value inwhichItem
- The attribute whose value will be setvalue
- The value to set
true
if the attribute value was set, false
if it was not.
Typically the attribute value cannot be set due to the attribute itself not
existing in the given node map.public static boolean setItemInNode(org.w3c.dom.Node theNode, java.lang.String whichItem, java.lang.String value)
theNode
- The node to set the attribute value inwhichItem
- The attribute whose value will be setvalue
- The value to set
true
if the attribute value was set, false
if it was not.
Typically the attribute value cannot be set due to the attribute itself not
existing in the given node.public static org.w3c.dom.Node findNodeInList(org.w3c.dom.NodeList theList, java.lang.String searchNodeName)
theList
- The list of XML nodes to searchsearchNodeName
- The node name to search for
null
is returned.public static org.w3c.dom.Node[] findNodesInList(org.w3c.dom.NodeList theList, java.lang.String searchNodeName)
theList
- The list of XML nodes to searchsearchNodeName
- The node name to search for
public static org.w3c.dom.Element findChildElement(org.w3c.dom.Node parent, java.lang.String searchNodeName)
parent
- The parent XML node to start the search fromsearchNodeName
- The node name to search for
null
is returned.public static org.w3c.dom.Element[] findAllChildElements(org.w3c.dom.Node parent, java.lang.String searchNodeName)
parent
- The parent XML node to start the search fromsearchNodeName
- The node name to search for
public static org.w3c.dom.Node findParentNode(org.w3c.dom.Node startNode, java.lang.String parentName)
startNode
- The node to start the search fromparentName
- The name of the parent node to search for
null
if no match was found.public static org.w3c.dom.Node findNodeWithAttributeValue(org.w3c.dom.NodeList theList, java.lang.String searchNodeName, java.lang.String attributeName, java.lang.String attributeValue)
theList
- The list of XML nodes to searchsearchNodeName
- The name of the node to search forattributeName
- The name of the attribute within the node to search forattributeValue
- The value of the attribute to search for
null
if no match was foundpublic static org.w3c.dom.Document createNewDocument() throws java.lang.Exception
java.lang.Exception
- If an exception occurs during the creation of the XML documentpublic static org.w3c.dom.Document getXmlDocumentFromJar(java.lang.String xmlFileName, java.lang.ClassLoader loader)
xmlFileName
- The name of the XML fileloader
- The class loader to use when searching for the XML file
resource. If null
is specified for the class loader,
then the class loader for the XmlUtils
class is used.
null
if an exception occurred.public static org.w3c.dom.Document getXmlDocumentFromFile(java.lang.String xmlFileName)
xmlFileName
- The name of the XML file
null
if an exception occurred.public static org.w3c.dom.Document getXmlDocumentFromUrl(java.net.URL xmlFileUrl)
xmlFileUrl
- The URL to the XML file
null
if an exception occurred.public static org.w3c.dom.Document getXmlDocumentFromStream(java.io.InputStream xmlFileStream)
xmlFileStream
- The input stream to build the XML document from
null
if an exception occurred.public static void writeXmlDocumentToStream(org.w3c.dom.Document xmlDoc, java.io.Writer outWriter) throws java.lang.Exception
xmlDoc
- The XML document to writeoutWriter
- The writer to write the XML document to
java.lang.Exception
- If an exception occurs while writing the XML documentpublic static void writeDocumentToXmlFile(org.w3c.dom.Document xmlDoc, java.io.File xmlFile) throws java.lang.Exception
xmlDoc
- The XML document to writexmlFile
- The name of the file to write the XML document to
java.lang.Exception
- If an exception occurs while writing the XML documentpublic static org.w3c.dom.Node sortNodes(org.w3c.dom.Node topNode, java.lang.String childNodeName, java.lang.String attributeToSortOn)
topNode
- The parent of the child nodes that need to be sortedchildNodeName
- The name of the child nodes that will be sortedattributeToSortOn
- The name of the attribute whose value will be
used to sort the child nodes.
public static int pruneNodes(org.w3c.dom.Node topNode, java.lang.String nodeName, java.lang.String attributeKey, java.lang.String valueToMatch)
topNode
- The parent of the child nodes that need to be prunednodeName
- The name of the node to evaluate and potentially pruneattributeKey
- The name of the attribute to inspectvalueToMatch
- The attribute value to match, where a match means the
node is pruned (removed) from the XML document.
public static void verifyNull(java.lang.Object objToVerify, java.lang.String exceptionMsg)
objToVerify
- The object to verifyexceptionMsg
- If the object is not null, the detail message for the exception
com.ibm.are.xml.XmlFormatException
- If the object is not nullpublic static void verifyNotNull(java.lang.Object objToVerify, java.lang.String exceptionMsg)
objToVerify
- The object to verifyexceptionMsg
- If the object is null, the detail message for the exception
com.ibm.are.xml.XmlFormatException
- If the object is nullpublic static void verifyNotEmpty(java.lang.String stringValue, java.lang.String exceptionMsg)
stringValue
- The string to verifyexceptionMsg
- If the string is null or empty, the detail message for the exception that is thrown
com.ibm.are.xml.XmlFormatException
- If the string is null or emptypublic static java.lang.String getRequiredAttribute(org.w3c.dom.Node theNode, java.lang.String attributeName)
theNode
- The node to retrieve the attribute value fromattributeName
- The name of the attribute to retrieve the value of
com.ibm.are.xml.XmlFormatException
- If the specified attribute does not exist in the nodepublic static com.ibm.are.xml.Reference getReferenceFromName(com.ibm.are.xml.XmlParsingEngine parsingEngine, java.lang.String name)
parsingEngine
- The parsing engine whose global context contains the specified referencename
- The name of the reference to retrieve
com.ibm.are.xml.XmlFormatException
- If the specified reference cannot be foundpublic static java.lang.String getNodeTextContent(org.w3c.dom.Node node)
Node.getTextContent
method (DOM3/JDK1.5)
Note that this method only works with the simplest method
to retrieve text content. The return value is undefined for
more complex cases, for example 'node with a non-text child node'.
node
- The XML node to get the text content from
public static void setNodeTextContent(org.w3c.dom.Node node, java.lang.String text)
Node.setTextContent
method (DOM3/JDK1.5)
Note that this is a very simple solution which
should only be used for a simple text node. This method
first removes any text node type children of the given node, and
then adds a text node (the content) to the given node.
node
- The XML node to set the text content fortext
- The text to set
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |