org.apache.lenya.xml
Class DocumentHelper

java.lang.Object
  extended by org.apache.lenya.xml.DocumentHelper

public class DocumentHelper
extends java.lang.Object

Various utility methods to work with JAXP.


Constructor Summary
DocumentHelper()
           
 
Method Summary
static javax.xml.parsers.DocumentBuilder createBuilder()
          Creates a non-validating and namespace-aware DocumentBuilder.
static org.w3c.dom.Document createDocument(java.lang.String namespaceUri, java.lang.String qualifiedName, org.w3c.dom.DocumentType documentType)
          Creates a document.
 org.w3c.dom.DocumentType createDocumentType(java.lang.String qualifiedName, java.lang.String publicId, java.lang.String systemId)
          Creates a document type.
static org.w3c.dom.Element[] getChildren(org.w3c.dom.Element element)
          Returns all child elements of an element, regardless of the namespace.
static org.w3c.dom.Element[] getChildren(org.w3c.dom.Element element, java.lang.String namespaceUri)
          Returns all child elements of an element that belong to a certain namespace.
static org.w3c.dom.Element[] getChildren(org.w3c.dom.Element element, java.lang.String namespaceUri, java.lang.String localName)
          Returns all child elements of an element that belong to a certain namespace and have a certain local name.
static org.w3c.dom.Element getFirstChild(org.w3c.dom.Element element, java.lang.String namespaceUri)
          Returns the first child element of an element that belong to a certain namespace or null if none exists.
static org.w3c.dom.Element getFirstChild(org.w3c.dom.Element element, java.lang.String namespaceUri, java.lang.String localName)
          Returns the first child element of an element that belongs to a certain namespace and has a certain local name or null if none exists.
static org.w3c.dom.Element[] getNextSiblings(org.w3c.dom.Element element, java.lang.String namespaceUri)
          Returns all following sibling elements of an element that belong to a certain namespace.
static org.w3c.dom.Element[] getNextSiblings(org.w3c.dom.Element element, java.lang.String namespaceUri, java.lang.String localName)
          Returns all following sibling elements of an element that belong to a certain namespace.
static java.lang.String getSimpleElementText(org.w3c.dom.Element element)
          Returns the text inside an element.
protected static javax.xml.transform.Transformer getTransformer(org.w3c.dom.DocumentType documentType)
          Get the tranformer.
static org.w3c.dom.Document readDocument(java.io.File file)
          Reads a document from a file.
static org.w3c.dom.Document readDocument(java.io.InputStream stream)
          Reads a document from an input stream.
static org.w3c.dom.Document readDocument(java.lang.String string)
          Reads a document from a string.
static org.w3c.dom.Document readDocument(java.net.URI uri)
          Reads a document from a URI.
static org.w3c.dom.Document readDocument(java.net.URL url)
          Reads a document from a URL.
static void setSimpleElementText(org.w3c.dom.Element element, java.lang.String text)
          Replaces all child nodes of an element by a single text node.
static void writeDocument(org.w3c.dom.Document document, java.io.File file)
          Writes a document to a file.
static void writeDocument(org.w3c.dom.Document document, java.io.Writer writer)
          Writes a document to a writer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DocumentHelper

public DocumentHelper()
Method Detail

createBuilder

public static javax.xml.parsers.DocumentBuilder createBuilder()
                                                       throws javax.xml.parsers.ParserConfigurationException
Creates a non-validating and namespace-aware DocumentBuilder.

Returns:
A new DocumentBuilder object.
Throws:
javax.xml.parsers.ParserConfigurationException - if an error occurs

createDocument

public static org.w3c.dom.Document createDocument(java.lang.String namespaceUri,
                                                  java.lang.String qualifiedName,
                                                  org.w3c.dom.DocumentType documentType)
                                           throws org.w3c.dom.DOMException,
                                                  javax.xml.parsers.ParserConfigurationException
Creates a document. A xmlns:prefix="namespaceUri" attribute is added to the document element.

Parameters:
namespaceUri - The namespace URL of the root element.
qualifiedName - The qualified name of the root element.
documentType - The type of document to be created or null. When doctype is not null, its Node.ownerDocument attribute is set to the document being created.
Returns:
A new Document object.
Throws:
org.w3c.dom.DOMException - if an error occurs
javax.xml.parsers.ParserConfigurationException - if an error occurs
See Also:
DOMImplementation.createDocument(String, String, DocumentType)

readDocument

public static org.w3c.dom.Document readDocument(java.io.File file)
                                         throws javax.xml.parsers.ParserConfigurationException,
                                                org.xml.sax.SAXException,
                                                java.io.IOException
Reads a document from a file.

Parameters:
file - The file to load the document from.
Returns:
A document.
Throws:
javax.xml.parsers.ParserConfigurationException - if an error occurs
org.xml.sax.SAXException - if an error occurs
java.io.IOException - if an error occurs

readDocument

public static org.w3c.dom.Document readDocument(java.net.URL url)
                                         throws javax.xml.parsers.ParserConfigurationException,
                                                org.xml.sax.SAXException,
                                                java.io.IOException
Reads a document from a URL.

Parameters:
url - The URL to load the document from.
Returns:
A document.
Throws:
javax.xml.parsers.ParserConfigurationException - if an error occurs
org.xml.sax.SAXException - if an error occurs
java.io.IOException - if an error occurs

readDocument

public static org.w3c.dom.Document readDocument(java.net.URI uri)
                                         throws javax.xml.parsers.ParserConfigurationException,
                                                org.xml.sax.SAXException,
                                                java.io.IOException
Reads a document from a URI.

Parameters:
uri - The URI to load the document from.
Returns:
A document.
Throws:
javax.xml.parsers.ParserConfigurationException - if an error occurs
org.xml.sax.SAXException - if an error occurs
java.io.IOException - if an error occurs

readDocument

public static org.w3c.dom.Document readDocument(java.lang.String string)
                                         throws javax.xml.parsers.ParserConfigurationException,
                                                org.xml.sax.SAXException,
                                                java.io.IOException
Reads a document from a string.

Parameters:
string - The string to load the document from.
Returns:
A document.
Throws:
javax.xml.parsers.ParserConfigurationException - if an error occurs
org.xml.sax.SAXException - if an error occurs
java.io.IOException - if an error occurs

readDocument

public static org.w3c.dom.Document readDocument(java.io.InputStream stream)
                                         throws javax.xml.parsers.ParserConfigurationException,
                                                org.xml.sax.SAXException,
                                                java.io.IOException
Reads a document from an input stream.

Parameters:
stream - The input stream to load the document from.
Returns:
A document.
Throws:
javax.xml.parsers.ParserConfigurationException - if an error occurs
org.xml.sax.SAXException - if an error occurs
java.io.IOException - if an error occurs

writeDocument

public static void writeDocument(org.w3c.dom.Document document,
                                 java.io.File file)
                          throws javax.xml.transform.TransformerConfigurationException,
                                 javax.xml.transform.TransformerException,
                                 java.io.IOException
Writes a document to a file. A new file is created if it does not exist.

Parameters:
document - The document to save.
file - The file to save the document to.
Throws:
java.io.IOException - if an error occurs
javax.xml.transform.TransformerConfigurationException - if an error occurs
javax.xml.transform.TransformerException - if an error occurs

writeDocument

public static void writeDocument(org.w3c.dom.Document document,
                                 java.io.Writer writer)
                          throws javax.xml.transform.TransformerConfigurationException,
                                 javax.xml.transform.TransformerException,
                                 java.io.IOException
Writes a document to a writer.

Parameters:
document - The document to write.
writer - The writer to write the document to.
Throws:
java.io.IOException - if an error occurs
javax.xml.transform.TransformerConfigurationException - if an error occurs
javax.xml.transform.TransformerException - if an error occurs

getTransformer

protected static javax.xml.transform.Transformer getTransformer(org.w3c.dom.DocumentType documentType)
                                                         throws javax.xml.transform.TransformerConfigurationException
Get the tranformer.

Parameters:
documentType - the document type
Returns:
a transformer
Throws:
javax.xml.transform.TransformerConfigurationException - if an error occurs

createDocumentType

public org.w3c.dom.DocumentType createDocumentType(java.lang.String qualifiedName,
                                                   java.lang.String publicId,
                                                   java.lang.String systemId)
                                            throws javax.xml.parsers.ParserConfigurationException
Creates a document type.

Parameters:
qualifiedName - The qualified name of the document type.
publicId - The public identifier.
systemId - The system identifier.
Returns:
the document type
Throws:
javax.xml.parsers.ParserConfigurationException - if an error occurs
See Also:
DOMImplementation.createDocumentType(java.lang.String, java.lang.String, java.lang.String)

getFirstChild

public static org.w3c.dom.Element getFirstChild(org.w3c.dom.Element element,
                                                java.lang.String namespaceUri)
Returns the first child element of an element that belong to a certain namespace or null if none exists.

Parameters:
element - The parent element.
namespaceUri - The namespace that the childen must belong to.
Returns:
The first child element or null if none exists.

getFirstChild

public static org.w3c.dom.Element getFirstChild(org.w3c.dom.Element element,
                                                java.lang.String namespaceUri,
                                                java.lang.String localName)
Returns the first child element of an element that belongs to a certain namespace and has a certain local name or null if none exists.

Parameters:
element - The parent element.
namespaceUri - The namespace that the childen must belong to.
localName - The local name of the children.
Returns:
The child element or null if none exists.

getChildren

public static org.w3c.dom.Element[] getChildren(org.w3c.dom.Element element)
Returns all child elements of an element, regardless of the namespace.

Parameters:
element - The parent element.
Returns:
The child elements.

getChildren

public static org.w3c.dom.Element[] getChildren(org.w3c.dom.Element element,
                                                java.lang.String namespaceUri)
Returns all child elements of an element that belong to a certain namespace.

Parameters:
element - The parent element.
namespaceUri - The namespace that the childen must belong to.
Returns:
The child elements.

getChildren

public static org.w3c.dom.Element[] getChildren(org.w3c.dom.Element element,
                                                java.lang.String namespaceUri,
                                                java.lang.String localName)
Returns all child elements of an element that belong to a certain namespace and have a certain local name.

Parameters:
element - The parent element.
namespaceUri - The namespace that the childen must belong to.
localName - The local name of the children.
Returns:
The child elements.

getSimpleElementText

public static java.lang.String getSimpleElementText(org.w3c.dom.Element element)
Returns the text inside an element. Only the child text nodes of this element are collected.

Parameters:
element - The element.
Returns:
The text inside the element.

setSimpleElementText

public static void setSimpleElementText(org.w3c.dom.Element element,
                                        java.lang.String text)
Replaces all child nodes of an element by a single text node.

Parameters:
element - The element.
text - The text to insert.

getNextSiblings

public static org.w3c.dom.Element[] getNextSiblings(org.w3c.dom.Element element,
                                                    java.lang.String namespaceUri)
Returns all following sibling elements of an element that belong to a certain namespace.

Parameters:
element - The parent element.
namespaceUri - The namespace that the childen must belong to.
Returns:
The following sibling elements.

getNextSiblings

public static org.w3c.dom.Element[] getNextSiblings(org.w3c.dom.Element element,
                                                    java.lang.String namespaceUri,
                                                    java.lang.String localName)
Returns all following sibling elements of an element that belong to a certain namespace. and have a certain local name.

Parameters:
element - The parent element.
namespaceUri - The namespace that the childen must belong to.
localName - The local name of the children.
Returns:
The following sibling elements.


Copyright © 1999-2005 Apache Software Foundation. All Rights Reserved.