org.sakaiproject.tool.util
Class DomUtils

java.lang.Object
  extended byorg.sakaiproject.tool.util.DomUtils

public class DomUtils
extends Object


Field Summary
static String ENCODING
           
static String INPUT_ENCODING
           
 
Method Summary
static void addEntity(Element element, String entity)
          Add an entity to a specified Element.
static void addText(Element element, String text)
          Add Text object to an Element.
static Element createElement(Document document, String name)
          Create a new element
static Element createElement(Element parent, String name)
          Add a new element to the given parent
static HTMLDocument createHtmlDocument()
          Start a new HTML Document.
static Document createXmlDocument()
          Start a new XML Document (with root name = xml)
static Document createXmlDocument(String rootName)
          Start a new XML Document.
static String getAllText(Node parent)
          Get any text associated with this element and it's children.
static String getAllTextAtNode(Node parent)
          Get the text associated with this element, at all suboordinate levels
static String getAttribute(Element element, String name)
          Get an Attribute from an Element.
static Element getElement(Element element, String name)
          Return the first named Element found.
static NodeList getElementList(Element element, String name)
          Return a list of named Elements.
static String getFirstText(Node parent)
          Get the first text node associated with this element
static org.apache.xml.serialize.OutputFormat getFormatter(String type, String encoding, boolean doIndent)
          Get a DOM formatter.
static org.apache.xml.serialize.OutputFormat getHtmlFormatter()
          Get (and configure) an HTML formatter.
static Node getNextNodeByName(Node currentNode, String tagName)
          Search down the tree for a given node
static Node getPreviousNodeByName(Node currentNode, String tagName)
          Search up the tree for a given node
static String getText(Node parent)
          Get the text associated with this element, at this level only
static String getTextNodeByNumber(Node parent, int number)
          Get the specified text node associated with this element
static DocumentBuilder getXmlDocumentBuilder()
          Get a DOM Document builder.
static org.apache.xml.serialize.OutputFormat getXmlFormatter()
          Get (and configure) an XML formatter.
static Document parseHtmlBytes(byte[] html)
          Parse HTML text (from a raw byte array) into a Document.
static Document parseHtmlFile(String filename)
          Parse an HTML file
static Document parseHtmlFromInputSource(InputSource in)
          Parse HTML from an InputSource
static Document parseHtmlReader(Reader reader)
          Parse HTML from a Reader
static Document parseHtmlStream(InputStream stream)
          Parse HTML from an InputStream
static Document parseHtmlString(String html)
          Parse HTML text (from a String) into a Document.
static Document parseXmlBytes(byte[] xml)
          Parse XML text (from a raw byte array) into a Document.
static Document parseXmlFile(String filename)
          Parse an XML file into a Document.
static Document parseXmlReader(Reader xmlReader)
          Parse XML text (from a Reader) into a Document.
static Document parseXmlStream(InputStream xmlStream)
          Parse XML text (from an input stream) into a Document.
static Document parseXmlString(String xml)
          Parse XML text (from a string) into a Document.
 Node removeNode(Node node)
          Remove this node from its parent.
static List selectElementsByAttributeValue(Element element, String name, String attribute, String value)
          Return a list of named Elements with a specific attribute value.
static List selectElementsByAttributeValue(Element element, String name, String attribute, String value, boolean returnFirst)
          Return a list of named Elements with a specific attribute value.
static Element selectFirstElementByAttributeValue(Element element, String name, String attribute, String value)
          Return the first named Element with a specific attribute value.
static String serialize(Document document)
          Write formatted HTML or XML text to a String.
static void serializeHtml(Document document, OutputStream target)
          Write formatted HTML text to supplied OutputStream.
static void serializeHtml(Document document, Writer target)
          Write formatted HTML text to supplied Writer.
static void serializeXml(Document document, OutputStream target)
          Write formatted XML text to supplied OutputStream.
static void serializeXml(Document document, String filename)
          Write formatted XML text to specified file.
static void serializeXml(Document document, Writer writer)
          Write formatted XML text to supplied Writer.
static void setAttribute(Element element, String name, String value)
          Set an Attribute in an Element
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
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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 element
name - 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 Element
name - 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 element
text - the text to add

addEntity

public static void addEntity(Element element,
                             String entity)
Add an entity to a specified Element. (eg DomUtils.addEntity(element, "nbsp");)

Parameters:
element - the containing element
entity - the entity to add

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)

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 text
recursiveSearch - Search all chile 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 text
number - 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 Element
name - 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 Element
name - the attribute name
value - the attribute value

getElementList

public static NodeList getElementList(Element element,
                                      String name)
Return a list of named Elements.

Parameters:
element - the containing Element
name - 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 Element
name - the tag name
attribute - Attribute name
value - 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 Element
name - the tag name
attribute - Attribute name
value - 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 Element
name - the tag name
attribute - Attribute name
value - Attribute value
returnFirst - 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. Null if none.

Parameters:
element - the containing Element
name - 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 search
tagName - Node name to look up
Returns:
matching Node (null if none)

getNextNodeByName

public static Node getNextNodeByName(Node currentNode,
                                     String tagName)
Search down the tree for a given node

Parameters:
currentNode - Starting point for our search
tagName - 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

createHtmlDocument

public static HTMLDocument createHtmlDocument()
Start a new HTML Document.

Returns:
the HTMLDocument
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

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

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

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

parseHtmlStream

public static Document parseHtmlStream(InputStream stream)
                                throws DomException
Parse HTML from an InputStream

Parameters:
stream - Input stream
Returns:
DOM Document
Throws:
DomException

parseHtmlReader

public static Document parseHtmlReader(Reader reader)
                                throws DomException
Parse HTML from a Reader

Parameters:
reader - Reader input
Returns:
DOM Document
Throws:
DomException

parseHtmlFromInputSource

public static Document parseHtmlFromInputSource(InputSource in)
                                         throws DomException
Parse HTML from an InputSource

Parameters:
in - InputSource
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

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

parseHtmlFile

public static Document parseHtmlFile(String filename)
                              throws DomException
Parse an HTML file

Parameters:
filename -
Returns:
DOM Document
Throws:
DomException

getXmlFormatter

public static org.apache.xml.serialize.OutputFormat getXmlFormatter()
Get (and configure) an XML formatter.

Returns:
A Xerces OutputFormat object

getHtmlFormatter

public static org.apache.xml.serialize.OutputFormat getHtmlFormatter()
Get (and configure) an HTML formatter.

Returns:
A Xerces OutputFormat object

getFormatter

public static org.apache.xml.serialize.OutputFormat getFormatter(String type,
                                                                 String encoding,
                                                                 boolean doIndent)
Get a DOM formatter.

Returns:
A Xerces OutputFormat object

serializeHtml

public static void serializeHtml(Document document,
                                 OutputStream target)
                          throws DomException
Write formatted HTML text to supplied OutputStream.

Parameters:
document - the Document to write
target - stream to write to
Throws:
DomException

serializeHtml

public static void serializeHtml(Document document,
                                 Writer target)
                          throws DomException
Write formatted HTML text to supplied Writer.

Parameters:
document - the Document to write
target - Output Writer
Throws:
DomException

serializeXml

public static void serializeXml(Document document,
                                OutputStream target)
                         throws DomException
Write formatted XML text to supplied OutputStream.

Parameters:
document - the Document to write
target - stream to write to
Throws:
DomException

serializeXml

public static void serializeXml(Document document,
                                Writer writer)
                         throws DomException
Write formatted XML text to supplied Writer.

Parameters:
document - the Document to write
writer - Writer the document is written to
Throws:
DomException

serializeXml

public static void serializeXml(Document document,
                                String filename)
                         throws DomException
Write formatted XML text to specified file.

Parameters:
document - the Document to write
filename - Filename the document is written to
Throws:
DomException

serialize

public static String serialize(Document document)
                        throws DomException
Write formatted HTML or XML text to a String.

Parameters:
document - the Document to write
Returns:
String containing the formatted document text
Throws:
DomException