TopoWeb Commons API

org.topoweb.xml
Class XmlUtil

java.lang.Object
  |
  +--org.topoweb.xml.XmlUtil
All Implemented Interfaces:
XmlUtilConstants

public class XmlUtil
extends java.lang.Object
implements XmlUtilConstants

Utility for manipulating XML structures. Provides convenience methods for writing and reading DOM structures to and from streams.

Author:
Babak Farhang

Field Summary
 
Fields inherited from interface org.topoweb.xml.XmlUtilConstants
XML_DECLARATION, XML_DECLARATION_VERSION1_UTF8
 
Method Summary
static java.lang.String getText(org.w3c.dom.Element element)
          Returns the text of the given element.
static org.w3c.dom.Document newDocument()
          Returns a newly created DOM document node.
static org.w3c.dom.Document readDocument(java.io.InputStream is)
          Returns a DOM document node created and read from the given input stream
static org.w3c.dom.Document readDocument(java.io.InputStream is, javax.xml.parsers.DocumentBuilder builder)
          Returns a DOM document node created and read from the given input stream
static org.w3c.dom.Document readDocument(java.io.Reader reader)
          Returns a DOM document node created and read from the given reader.
static org.w3c.dom.Document readDocument(java.io.Reader reader, javax.xml.parsers.DocumentBuilder builder)
          Returns a DOM document node created and read from the given reader.
static org.w3c.dom.Document readDocument(javax.xml.transform.Source src)
          Returns a DOM document node created and read from the given source.
static org.w3c.dom.Document readDocument(javax.xml.transform.Source src, javax.xml.parsers.DocumentBuilder builder)
          Returns a DOM document node created using the given builder and read from the given source.
static org.w3c.dom.Document readDocument(java.lang.String xml, javax.xml.parsers.DocumentBuilder builder)
          Returns a DOM document node created and read from the given xml string.
static void setText(org.w3c.dom.Element element, org.w3c.dom.Document doc, java.lang.String txt)
          Sets text for the given element in the specified DOM document.
static java.lang.String toXmlString(org.w3c.dom.Node node)
          Returns a well-formed string representation of the given DOM node .
static void transform(javax.xml.transform.Source src, javax.xml.transform.Result dtn)
          Transforms the given source to the specified destination.
static void writeDocument(java.io.OutputStream out, org.w3c.dom.Document doc)
          Writes the given document to the specified output stream.
static void writeDocument(java.io.OutputStream out, org.w3c.dom.Document doc, java.lang.String dtd)
          Writes the given document to the specified output stream using the specified inlined DTD declaration.
static void writeDocument(java.io.Writer writer, org.w3c.dom.Document doc)
          Writes the given document to the specified writer.
static void writeDocument(java.io.Writer writer, org.w3c.dom.Document doc, java.lang.String dtd)
          Writes the given document to the specified writer using the specified inlined DTD declaration.
static void writeNode(java.io.OutputStream out, org.w3c.dom.Node node)
          Writes the given DOM node to the specified output stream.
static void writeNode(java.io.Writer writer, org.w3c.dom.Node node)
          Writes the given DOM node to the specified writer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

toXmlString

public static java.lang.String toXmlString(org.w3c.dom.Node node)
                                    throws javax.xml.transform.TransformerException
Returns a well-formed string representation of the given DOM node .

Parameters:
node - the DOM node
Returns:
a string representation of the node
Throws:
javax.xml.transform.TransformerException - if a transform error occurs
See Also:
writeNode(Writer,Node)

writeDocument

public static void writeDocument(java.io.OutputStream out,
                                 org.w3c.dom.Document doc)
                          throws java.io.IOException,
                                 javax.xml.transform.TransformerException
Writes the given document to the specified output stream. Equivalent to invoking writeDocument(out, doc, null).

Parameters:
out - the output stream written to
doc - the DOM document
Throws:
java.io.IOException - if there is an error while writing to out
javax.xml.transform.TransformerException - if a transform error occurs
See Also:
writeDocument(OutputStream,Document,String), writeDocument(Writer,Document)

writeDocument

public static void writeDocument(java.io.OutputStream out,
                                 org.w3c.dom.Document doc,
                                 java.lang.String dtd)
                          throws java.io.IOException,
                                 javax.xml.transform.TransformerException
Writes the given document to the specified output stream using the specified inlined DTD declaration. The dtd, may be null. This method invokes the writeDocument(Writer,Document,String) method.

Warning: No checks are performed on the dtd string. Consequently, the XML written to the stream will not be well-formed if dtd is not.

Parameters:
out - the output stream written to
doc - the DOM document
dtd - the inlined DTD for doc
Throws:
java.io.IOException - if there is an error while writing to out
javax.xml.transform.TransformerException - if a transform error occurs
See Also:
writeDocument(Writer,Document,String)

writeDocument

public static void writeDocument(java.io.Writer writer,
                                 org.w3c.dom.Document doc)
                          throws java.io.IOException,
                                 javax.xml.transform.TransformerException
Writes the given document to the specified writer. Equivalent to invoking writeDocument(out, doc, null).

Parameters:
writer - the Writer written to
doc - the DOM document
Throws:
java.io.IOException - if there is an error while writing to out
javax.xml.transform.TransformerException - if a transform error occurs
See Also:
writeDocument(Writer,Document,String), writeDocument(OutputStream,Document)

writeDocument

public static void writeDocument(java.io.Writer writer,
                                 org.w3c.dom.Document doc,
                                 java.lang.String dtd)
                          throws java.io.IOException,
                                 javax.xml.transform.TransformerException
Writes the given document to the specified writer using the specified inlined DTD declaration. The dtd argument may be null. The method first writes a line containing the XML declaration. If dtd is not null, then it too is written. Finally, the writeNode(Writer,Node) method is invoked.

Warning: No checks are performed on the dtd string. Consequently, the XML written to the stream will not be well-formed if dtd is not.

Parameters:
writer - the Writer written to
doc - the DOM document
dtd - the inlined DTD for doc
Throws:
java.io.IOException - if there is an error while writing to out
javax.xml.transform.TransformerException - if a transform error occurs
See Also:
writeDocument(OutputStream,Document,String), XmlUtilConstants.XML_DECLARATION, writeNode(Writer,Node)

writeNode

public static void writeNode(java.io.Writer writer,
                             org.w3c.dom.Node node)
                      throws javax.xml.transform.TransformerException
Writes the given DOM node to the specified writer. The node is written as a well-formed XML snippet (i.e. w/o header declarations). This method is equivalent to invoking transform(new DOMSource(node), new StreamResult(writer)).

Parameters:
writer - the Writer written to
node - the DOM node
Throws:
javax.xml.transform.TransformerException - if a transform error occurs
See Also:
transform(Source,Result)

writeNode

public static void writeNode(java.io.OutputStream out,
                             org.w3c.dom.Node node)
                      throws javax.xml.transform.TransformerException
Writes the given DOM node to the specified output stream. The node is written as a well-formed XML snippet (i.e. w/o header declarations). This method is equivalent to invoking transform(new DOMSource(node), new StreamResult(out)).

Parameters:
out - the output stream written to
node - the DOM node
Throws:
javax.xml.transform.TransformerException - if a transform error occurs
See Also:
transform(Source,Result)

transform

public static void transform(javax.xml.transform.Source src,
                             javax.xml.transform.Result dtn)
                      throws javax.xml.transform.TransformerException
Transforms the given source to the specified destination. Uses a newly created javax.xml.transform.Transformer with the following output properites:
  • method : xml
  • indent : yes
  • omit-xml-declaration : yes

Parameters:
src - the source (Source)
dtn - the destination (Result)
Throws:
javax.xml.transform.TransformerException - if a transform error occurs

newDocument

public static org.w3c.dom.Document newDocument()
                                        throws javax.xml.parsers.ParserConfigurationException
Returns a newly created DOM document node. Creates the new Document from a newly created DocumentBuilder . This method is useful for building a DOM tree from scratch.

Returns:
an empty, newly created Document
Throws:
javax.xml.parsers.ParserConfigurationException - If the document builder cannot be properly initialized
See Also:
readDocument(Source)

readDocument

public static org.w3c.dom.Document readDocument(javax.xml.transform.Source src)
                                         throws javax.xml.parsers.ParserConfigurationException,
                                                javax.xml.transform.TransformerException
Returns a DOM document node created and read from the given source.

Parameters:
src - the source from which the returned document is constructed
Returns:
the DOM representation of src
Throws:
javax.xml.parsers.ParserConfigurationException - If the document builder cannot be properly initialized
javax.xml.transform.TransformerException - if a transform error occurs
See Also:
newDocument()

readDocument

public static org.w3c.dom.Document readDocument(javax.xml.transform.Source src,
                                                javax.xml.parsers.DocumentBuilder builder)
                                         throws javax.xml.transform.TransformerException
Returns a DOM document node created using the given builder and read from the given source.

Parameters:
src - the source from which the returned document is constructed
builder - the factory used to create the returned document instance
Returns:
the DOM representation of src
Throws:
javax.xml.transform.TransformerException - if a transform error occurs
See Also:
newDocument()

readDocument

public static org.w3c.dom.Document readDocument(java.io.InputStream is)
                                         throws javax.xml.parsers.ParserConfigurationException,
                                                javax.xml.transform.TransformerException
Returns a DOM document node created and read from the given input stream

Parameters:
is - the input stream from which the returned document is constructed
Returns:
the DOM representation of is
Throws:
javax.xml.parsers.ParserConfigurationException - If the document builder cannot be properly initialized
javax.xml.transform.TransformerException - if a transform error occurs

readDocument

public static org.w3c.dom.Document readDocument(java.io.InputStream is,
                                                javax.xml.parsers.DocumentBuilder builder)
                                         throws javax.xml.transform.TransformerException
Returns a DOM document node created and read from the given input stream

Parameters:
is - the input stream from which the returned document is constructed
builder - the factory used to create the returned document instance
Returns:
the DOM representation of is
Throws:
javax.xml.transform.TransformerException - if a transform error occurs

readDocument

public static org.w3c.dom.Document readDocument(java.io.Reader reader)
                                         throws javax.xml.parsers.ParserConfigurationException,
                                                javax.xml.transform.TransformerException
Returns a DOM document node created and read from the given reader.

Parameters:
reader - the Reader from which the returned document is constructed
Returns:
the DOM representation of reader
Throws:
javax.xml.parsers.ParserConfigurationException - If the document builder cannot be properly initialized
javax.xml.transform.TransformerException - if a transform error occurs

readDocument

public static org.w3c.dom.Document readDocument(java.io.Reader reader,
                                                javax.xml.parsers.DocumentBuilder builder)
                                         throws javax.xml.transform.TransformerException
Returns a DOM document node created and read from the given reader.

Parameters:
reader - the Reader from which the returned document is constructed
Returns:
the DOM representation of reader
Throws:
javax.xml.parsers.ParserConfigurationException - If the document builder cannot be properly initialized
javax.xml.transform.TransformerException - if a transform error occurs

readDocument

public static org.w3c.dom.Document readDocument(java.lang.String xml,
                                                javax.xml.parsers.DocumentBuilder builder)
                                         throws javax.xml.transform.TransformerException
Returns a DOM document node created and read from the given xml string.

Parameters:
xml - the XML snippet from which the returned document is constructed
Returns:
the DOM representation of reader
Throws:
javax.xml.parsers.ParserConfigurationException - If the document builder cannot be properly initialized
javax.xml.transform.TransformerException - if a transform error occurs

getText

public static java.lang.String getText(org.w3c.dom.Element element)
Returns the text of the given element. The element is not expected to contain sub-elements. The behaviour of this method is undefined for elements that themselves contain sub-elements.

Parameters:
element - the DOM element
Returns:
the element's text

setText

public static void setText(org.w3c.dom.Element element,
                           org.w3c.dom.Document doc,
                           java.lang.String txt)
Sets text for the given element in the specified DOM document. The behviour is undefined if element does not belong to doc.

Parameters:
element - the element whose text is to be set
doc - the document to which element belongs
txt - the text

TopoWeb Commons API

Copyright (C) 2002 Babak Farhang

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.