Package edu.indiana.lib.twinpeaks.util
Class DomUtils
- java.lang.Object
-
- edu.indiana.lib.twinpeaks.util.DomUtils
-
public class DomUtils extends Object
-
-
Field Summary
Fields Modifier and Type Field Description static StringENCODINGstatic StringINPUT_ENCODING
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static voidaddEntity(Element element, String entity)Add an entity to a specified Element.static voidaddText(Element element, String text)Add Text object to an Element.static voidcopyDocument(Document source, Document target)Copy an XML document, adding it as a child of the target document rootstatic voidcopyDocumentNode(Node source, Document target)Copy a Node from one source document, adding it to the document root of a different, target Documentstatic ElementcreateElement(Document document, String name)Create a new elementstatic ElementcreateElement(Element parent, String name)Add a new element to the given parentstatic DocumentcreateXmlDocument()Start a new XML Document (with root name = xml)static DocumentcreateXmlDocument(String rootName)Start a new XML Document.static StringgetAllText(Node parent)Get any text associated with this element and it's children.static StringgetAllTextAtNode(Node parent)Get the text associated with this element, at all suboordinate levelsstatic StringgetAttribute(Element element, String name)Get an Attribute from an Element.static NodegetChildSiblingByName(Node currentNode, String tagName)Search for a named child of a given nodestatic ElementgetElement(Element element, String name)Return the first named Element found.static NodeListgetElementList(Element element, String name)Return a list of named Elements.static NodeListgetElementListNS(String namespace, Element element, String name)Return a list of specified namespace:Elementsstatic ElementgetElementNS(String namespace, Element element, String name)Return the first named Element found - namespace awarestatic StringgetFirstText(Node parent)Get the first text node associated with this elementstatic NodegetNextNodeByName(Node currentNode, String tagName)Deprecated.static NodegetNextSiblingByName(Node currentNode, String tagName)Search our next siblings for a given nodestatic NodegetPreviousNodeByName(Node currentNode, String tagName)Search up the tree for a given nodestatic NodegetPreviousSiblingByName(Node currentNode, String tagName)Search earlier siblings for a given nodestatic StringgetText(Node parent)Get the text associated with this element, at this level onlystatic StringgetText(Node parent, String elementName)Get the text associated with a specified element, at this level onlystatic StringgetTextNodeByNumber(Node parent, int number)Get the specified text node associated with this elementstatic StringgetTextNS(String namespace, Node parent, String elementName)Get the text associated with a specified element, at this level only - namespace awarestatic DocumentBuildergetXmlDocumentBuilder()Get a DOM Document builder.static DocumentBuildergetXmlDocumentBuilder(boolean namespaceAware)Get a DOM Document builder.static DocumentBuildergetXmlDocumentBuilderNS()Get a DOM Document builder - namespace awarestatic StringnormalizeText(String update)"Normalize" XML text node content to create a simple stringstatic StringnormalizeText(String original, String update)"Normalize" XML text node content to create a simple stringstatic DocumentparseHtmlBytes(byte[] html)Parse HTML text (from a raw byte array) into a Document.static DocumentparseHtmlString(String html)Parse HTML text (from a String) into a Document.static DocumentparseXmlBytes(byte[] xml)Parse XML text (from a raw byte array) into a Document.static DocumentparseXmlBytesNS(byte[] xml)Parse XML text (from a raw byte array) into a Document - namespace aware.static DocumentparseXmlFile(String filename)Parse an XML file into a Document.static DocumentparseXmlReader(Reader xmlReader)Parse XML text (from a Reader) into a Document.static DocumentparseXmlStream(InputStream xmlStream)Parse XML text (from an input stream) into a Document.static DocumentparseXmlStreamNS(InputStream xmlStream)Parse XML text (from an input stream) into a Document - namespace aware.static DocumentparseXmlString(String xml)Parse XML text (from a string) into a Document.NoderemoveNode(Node node)Remove this node from its parent.static ListselectElementsByAttributeValue(Element element, String name, String attribute, String value)Return a list of named Elements with a specific attribute value.static ListselectElementsByAttributeValue(Element element, String name, String attribute, String value, boolean returnFirst)Return a list of named Elements with a specific attribute value.static ListselectElementsByAttributeValueNS(String namespace, Element element, String name, String attribute, String value)Return a list of named Elements with a specific attribute value (namespace aware)static ListselectElementsByAttributeValueNS(String namespace, Element element, String name, String attribute, String value, boolean returnFirst)Return a list of named Elements with a specific attribute value (namespace aware)static ElementselectFirstElementByAttributeValue(Element element, String name, String attribute, String value)Return the first named Element with a specific attribute value.static ElementselectFirstElementByAttributeValueNS(String namespace, Element element, String name, String attribute, String value)Return the first named Element with a specific attribute value (namespace aware) namespace The namespace URIstatic Stringserialize(Object object)Write formatted XML text to a String.static voidserializeXml(Node node, OutputStream target)Write formatted XML text to supplied OutputStream.static voidserializeXml(Node node, Writer writer)Write formatted XML text to supplied Writer.static voidsetAttribute(Element element, String name, String value)Set an Attribute in an Elementstatic StringtextSearch(Node parent, boolean recursiveSearch)Get the text associated with this element at this level only, or recursivley, searching through all child elements
-
-
-
Field Detail
-
INPUT_ENCODING
public static final String INPUT_ENCODING
- See Also:
- Constant Field Values
-
ENCODING
public static final String ENCODING
- See Also:
- Constant Field Values
-
-
Method Detail
-
createElement
public static Element createElement(Document document, String name)
Create a new element- Parameters:
document- Document to contain the new elementname- the element name- Returns:
- new Element
-
createElement
public static Element createElement(Element parent, String name)
Add a new element to the given parent- Parameters:
parent- the parent Elementname- the child name- Returns:
- new Element
-
addText
public static void addText(Element element, String text)
Add Text object to an Element.- Parameters:
element- the containing elementtext- the text to add
-
addEntity
public static void addEntity(Element element, String entity)
Add an entity to a specified Element. (egDomUtils.addEntity(element, "nbsp");)- Parameters:
element- the containing elemententity- the entity to add
-
normalizeText
public static String normalizeText(String update)
"Normalize" XML text node content to create a simple string- Parameters:
update- Text to add to the original string- Returns:
- Concatenated contents (trimmed, pagination characters (\r, \n, etc.) removed, with a space seperator)
-
normalizeText
public static String normalizeText(String original, String update)
"Normalize" XML text node content to create a simple string- Parameters:
original- Original textupdate- Text to add to the original string- Returns:
- Concatenated contents (trimmed, pagination characters (\r, \n, etc.) removed, with a space seperator)
-
getText
public static String getText(Node parent)
Get the text associated with this element, at this level only- Parameters:
parent- the node containing text- Returns:
- Text (trimmed of leading/trailing whitespace, null if none)
-
getText
public static String getText(Node parent, String elementName)
Get the text associated with a specified element, at this level only- Parameters:
parent- the node containing textelementName- Element with the text we want to fetch- Returns:
- Text (trimmed of leading/trailing whitespace, null if none)
-
getTextNS
public static String getTextNS(String namespace, Node parent, String elementName)
Get the text associated with a specified element, at this level only - namespace aware- Parameters:
namespace- Namespace URIparent- the node containing textelementName- Element with the text we want to fetch- Returns:
- Text (trimmed of leading/trailing whitespace, null if none)
-
getAllTextAtNode
public static String getAllTextAtNode(Node parent)
Get the text associated with this element, at all suboordinate levels- Parameters:
parent- the node containing text- Returns:
- Text (trimmed of leading/trailing whitespace, null if none)
-
textSearch
public static String textSearch(Node parent, boolean recursiveSearch)
Get the text associated with this element at this level only, or recursivley, searching through all child elements- Parameters:
parent- the node containing textrecursiveSearch- Search all child elements?- Returns:
- Text (trimmed of leading/trailing whitespace, null if none)
-
getFirstText
public static String getFirstText(Node parent)
Get the first text node associated with this element- Parameters:
parent- the node containing text- Returns:
- Text (trimmed of leanding/trailing whitespace, null if none)
-
getTextNodeByNumber
public static String getTextNodeByNumber(Node parent, int number)
Get the specified text node associated with this element- Parameters:
parent- the node containing textnumber- The text node to fetch (1st, 2nd, etc)- Returns:
- Text (trimmed of leanding/trailing whitespace, null if none)
-
getAllText
public static String getAllText(Node parent)
Get any text associated with this element and it's children. Null if none.- Parameters:
parent- the node containing text- Returns:
- Text
-
getAttribute
public static String getAttribute(Element element, String name)
Get an Attribute from an Element. Returns an empty String if none found- Parameters:
element- the containing Elementname- the attribute name- Returns:
- Attribute as a String
-
setAttribute
public static void setAttribute(Element element, String name, String value)
Set an Attribute in an Element- Parameters:
element- the containing Elementname- the attribute namevalue- the attribute value
-
getElementList
public static NodeList getElementList(Element element, String name)
Return a list of named Elements.- Parameters:
element- the containing Elementname- the tag name- Returns:
- NodeList of matching elements
-
getElementListNS
public static NodeList getElementListNS(String namespace, Element element, String name)
Return a list of specified namespace:Elements- Parameters:
namespace- Namespace URIelement- the containing Elementname- the tag name- Returns:
- NodeList of matching elements
-
selectElementsByAttributeValue
public static List selectElementsByAttributeValue(Element element, String name, String attribute, String value)
Return a list of named Elements with a specific attribute value.- Parameters:
element- the containing Elementname- the tag nameattribute- Attribute namevalue- Attribute value- Returns:
- List of matching elements
-
selectFirstElementByAttributeValue
public static Element selectFirstElementByAttributeValue(Element element, String name, String attribute, String value)
Return the first named Element with a specific attribute value.- Parameters:
element- the containing Elementname- the tag nameattribute- Attribute namevalue- Attribute value- Returns:
- The first matching Element (null if none)
-
selectElementsByAttributeValue
public static List selectElementsByAttributeValue(Element element, String name, String attribute, String value, boolean returnFirst)
Return a list of named Elements with a specific attribute value.- Parameters:
element- the containing Elementname- the tag nameattribute- Attribute namevalue- Attribute valuereturnFirst- Return only the first matching value?- Returns:
- List of matching elements
-
selectElementsByAttributeValueNS
public static List selectElementsByAttributeValueNS(String namespace, Element element, String name, String attribute, String value)
Return a list of named Elements with a specific attribute value (namespace aware)- Parameters:
namespace- Namespace URIelement- the containing Elementname- the tag nameattribute- Attribute namevalue- Attribute value- Returns:
- List of matching elements
-
selectFirstElementByAttributeValueNS
public static Element selectFirstElementByAttributeValueNS(String namespace, Element element, String name, String attribute, String value)
Return the first named Element with a specific attribute value (namespace aware) namespace The namespace URI- Parameters:
element- the containing Elementname- the tag nameattribute- Attribute namevalue- Attribute value- Returns:
- The first matching Element (null if none)
-
selectElementsByAttributeValueNS
public static List selectElementsByAttributeValueNS(String namespace, Element element, String name, String attribute, String value, boolean returnFirst)
Return a list of named Elements with a specific attribute value (namespace aware)- Parameters:
namespace- Namespace URIelement- the containing Elementname- the tag nameattribute- Attribute namevalue- Attribute valuereturnFirst- Return only the first matching value?- Returns:
- List of matching elements
-
getElement
public static Element getElement(Element element, String name)
Return the first named Element found.- Parameters:
element- the containing Elementname- the tag name- Returns:
- matching Element (null if none)
-
getElementNS
public static Element getElementNS(String namespace, Element element, String name)
Return the first named Element found - namespace aware- Parameters:
namespace- Namespace URIelement- the containing Elementname- the tag name- Returns:
- matching Element (null if none)
-
removeNode
public Node removeNode(Node node)
Remove this node from its parent.- Parameters:
node- the node to remove- Returns:
- Node removed
-
getPreviousNodeByName
public static Node getPreviousNodeByName(Node currentNode, String tagName)
Search up the tree for a given node- Parameters:
currentNode- Starting point for our searchtagName- Node name to look up- Returns:
- matching Node (null if none)
-
getPreviousSiblingByName
public static Node getPreviousSiblingByName(Node currentNode, String tagName)
Search earlier siblings for a given node- Parameters:
currentNode- Starting point for our searchtagName- Node name to look up- Returns:
- matching Node (null if none)
-
getNextSiblingByName
public static Node getNextSiblingByName(Node currentNode, String tagName)
Search our next siblings for a given node- Parameters:
currentNode- Starting point for our searchtagName- Node name to look up- Returns:
- matching Node (null if none)
-
getNextNodeByName
public static Node getNextNodeByName(Node currentNode, String tagName)
Deprecated.Search across the tree for a given sibling- Parameters:
currentNode- Starting point for our searchtagName- Node name to look up- Returns:
- matching Node (null if none)
-
getChildSiblingByName
public static Node getChildSiblingByName(Node currentNode, String tagName)
Search for a named child of a given node- Parameters:
currentNode- Starting point for our searchtagName- Node name to look up- Returns:
- matching Node (null if none)
-
getXmlDocumentBuilder
public static DocumentBuilder getXmlDocumentBuilder() throws DomException
Get a DOM Document builder.- Returns:
- The DocumentBuilder
- Throws:
DomException
-
getXmlDocumentBuilderNS
public static DocumentBuilder getXmlDocumentBuilderNS() throws DomException
Get a DOM Document builder - namespace aware- Returns:
- The DocumentBuilder
- Throws:
DomException
-
getXmlDocumentBuilder
public static DocumentBuilder getXmlDocumentBuilder(boolean namespaceAware) throws DomException
Get a DOM Document builder.- Parameters:
namespaceAware- true if we're to handle namespace details- Returns:
- The DocumentBuilder
- Throws:
DomException
-
createXmlDocument
public static Document createXmlDocument() throws DomException
Start a new XML Document (with root name = xml)- Returns:
- the Document
- Throws:
DomException
-
createXmlDocument
public static Document createXmlDocument(String rootName) throws DomException
Start a new XML Document.- Parameters:
rootName- The name of the Document root Element (created here)- Returns:
- the Document
- Throws:
DomException
-
copyDocument
public static void copyDocument(Document source, Document target)
Copy an XML document, adding it as a child of the target document root- Parameters:
source- Document to copytarget- Document to contain copy
-
copyDocumentNode
public static void copyDocumentNode(Node source, Document target)
Copy a Node from one source document, adding it to the document root of a different, target Document- Parameters:
source- Document to copytarget- Document to contain copy
-
parseXmlStream
public static Document parseXmlStream(InputStream xmlStream) throws DomException
Parse XML text (from an input stream) into a Document.- Parameters:
xmlStream- The XML text stream- Returns:
- DOM Document
- Throws:
DomException
-
parseXmlStreamNS
public static Document parseXmlStreamNS(InputStream xmlStream) throws DomException
Parse XML text (from an input stream) into a Document - namespace aware.- Parameters:
xmlStream- The XML text stream- Returns:
- DOM Document
- Throws:
DomException
-
parseXmlReader
public static Document parseXmlReader(Reader xmlReader) throws DomException
Parse XML text (from a Reader) into a Document.- Parameters:
xmlReader- The XML Reader- Returns:
- DOM Document
- Throws:
DomException
-
parseXmlBytes
public static Document parseXmlBytes(byte[] xml) throws DomException
Parse XML text (from a raw byte array) into a Document.- Parameters:
xml- The XML text- Returns:
- DOM Document
- Throws:
DomException
-
parseXmlBytesNS
public static Document parseXmlBytesNS(byte[] xml) throws DomException
Parse XML text (from a raw byte array) into a Document - namespace aware.- Parameters:
xml- The XML text- Returns:
- DOM Document
- Throws:
DomException
-
parseXmlString
public static Document parseXmlString(String xml) throws DomException
Parse XML text (from a string) into a Document.- Parameters:
xml- The XML text- Returns:
- DOM Document
- Throws:
DomException
-
parseXmlFile
public static Document parseXmlFile(String filename) throws DomException
Parse an XML file into a Document.- Parameters:
filename- - The filename to parse- Returns:
- DOM Document
- Throws:
DomException
-
parseHtmlBytes
public static Document parseHtmlBytes(byte[] html) throws DomException
Parse HTML text (from a raw byte array) into a Document.- Parameters:
html- The HTML text- Returns:
- DOM Document
- Throws:
DomException-The used to be:
parseHtmlStream(new ByteArrayInputStream(html));
-
parseHtmlString
public static Document parseHtmlString(String html) throws DomException
Parse HTML text (from a String) into a Document.- Parameters:
html- The HTML text- Returns:
- DOM Document
- Throws:
DomException-This used to be:
return parseHtmlReader(new StringReader(html));
-
serializeXml
public static void serializeXml(Node node, OutputStream target) throws DomException
Write formatted XML text to supplied OutputStream.- Parameters:
node- Node to writetarget- stream to write to- Throws:
DomException
-
serializeXml
public static void serializeXml(Node node, Writer writer) throws DomException
Write formatted XML text to supplied Writer.- Parameters:
node- the Node to writewriter- Writer the document is written to- Throws:
DomException
-
serialize
public static String serialize(Object object) throws DomException
Write formatted XML text to a String.- Parameters:
object- The XML Document, HTML Document, or Element to write- Returns:
- String containing the formatted document text
- Throws:
DomException
-
-