org.backsource.utils.xml
Class DocumentUtil

java.lang.Object
  extended byorg.backsource.utils.xml.DocumentUtil

public class DocumentUtil
extends java.lang.Object

Util to handle stuff relating to documents and nodes.

Version:
$Revision: 1.1.1.1 $
Author:
Peter Antman

Constructor Summary
DocumentUtil()
           
 
Method Summary
static org.w3c.dom.Node copyInto(org.w3c.dom.Node fromNode, org.w3c.dom.Node toNode)
          Copy the child nodes and its children from fromNode into toNode.
static org.w3c.dom.Document createDocument(org.w3c.dom.Node node)
          Create a new Document from the node, where node becomes the new root node and all its children the rest of the document.
static org.w3c.dom.Document getDocument(java.io.File file)
          If reading from a file, use this, since it will set the SystemId.
static org.w3c.dom.Document getDocument(org.xml.sax.InputSource is)
          FIXME: Here we could fix our own factory/prop stuff to use alternative builders than system default.
static org.w3c.dom.Document getDocument(java.io.InputStream stream)
           
static org.w3c.dom.Document getDocument(java.io.Reader reader)
           
static org.w3c.dom.Document getDocument(java.lang.String xml)
          Get DOM document from xml string-
static org.w3c.dom.Element getDocumentElement(java.lang.String xml)
          Create a DOM from string and return its root element.
static org.w3c.dom.DocumentFragment getDocumentFragment(org.w3c.dom.Node node)
          Create a document fragment around the node and its children, which are cloned.
static org.w3c.dom.DocumentFragment getDocumentFragment(java.lang.String xml, boolean forceElementTree)
          Create a DocumentFragment from the XML given i string.
static java.lang.String getXml(org.w3c.dom.Node node)
          Get the serialized XML from node.
static java.lang.String getXml(org.w3c.dom.Node node, boolean indent, java.lang.String encoding, boolean omitDecl)
          Return the string XML rep of node.
static org.w3c.dom.Node importNode(org.w3c.dom.Document document, org.w3c.dom.Node toImport)
          Import node into document, make shure deep works (manually).
static void main(java.lang.String[] args)
           
static void replace(org.w3c.dom.Node oldNode, org.w3c.dom.Node newNode)
          Replace oldNode with newNode and its children, importing the new node if needed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DocumentUtil

public DocumentUtil()
Method Detail

getXml

public static java.lang.String getXml(org.w3c.dom.Node node)
                               throws XmlException

Get the serialized XML from node.

Returns:
the serialized XML from node, wich will have content encoding iso-8859-1, it will be indented and have a prolog.
Throws:
XmlException

getXml

public static java.lang.String getXml(org.w3c.dom.Node node,
                                      boolean indent,
                                      java.lang.String encoding,
                                      boolean omitDecl)
                               throws XmlException
Return the string XML rep of node.

Parameters:
node - the node to serialize.
indent - indent output if true.
encoding - the encoding to use, if null iso-8859-1
omitDecl - if true the xml prolog is omitted, which may be handly if the XML is to be used as a fragment.
Throws:
XmlException

replace

public static void replace(org.w3c.dom.Node oldNode,
                           org.w3c.dom.Node newNode)
                    throws XmlException
Replace oldNode with newNode and its children, importing the new node if needed.

Throws:
XmlException

getDocumentFragment

public static org.w3c.dom.DocumentFragment getDocumentFragment(java.lang.String xml,
                                                               boolean forceElementTree)
                                                        throws XmlException

Create a DocumentFragment from the XML given i string.

The fragment may not be wellformed XML and may contain multiple children without a root node. To only allow a document fragment to returned where all the children are elements turn forceElementTree on. Any whitespace - including new line - will be removed before trying.

Parameters:
xml - the string possibly containing and XML fragment. The string is trimed before conversion.
forceElementTree - the fragment is only valid if alla its children are elements, othervise an exception will be raised.
Throws:
XMLException - if the xml string was not possible to convert to a document fragment or if null.
XmlException

getDocumentFragment

public static org.w3c.dom.DocumentFragment getDocumentFragment(org.w3c.dom.Node node)
                                                        throws XmlException
Create a document fragment around the node and its children, which are cloned.

Throws:
XmlException

copyInto

public static org.w3c.dom.Node copyInto(org.w3c.dom.Node fromNode,
                                        org.w3c.dom.Node toNode)
Copy the child nodes and its children from fromNode into toNode.


importNode

public static org.w3c.dom.Node importNode(org.w3c.dom.Document document,
                                          org.w3c.dom.Node toImport)
Import node into document, make shure deep works (manually).


createDocument

public static org.w3c.dom.Document createDocument(org.w3c.dom.Node node)
                                           throws XmlException
Create a new Document from the node, where node becomes the new root node and all its children the rest of the document.

Throws:
XmlException

getDocumentElement

public static org.w3c.dom.Element getDocumentElement(java.lang.String xml)
                                              throws XmlException
Create a DOM from string and return its root element.

Throws:
XmlException

getDocument

public static org.w3c.dom.Document getDocument(java.lang.String xml)
                                        throws XmlException
Get DOM document from xml string-

Throws:
XmlException

getDocument

public static org.w3c.dom.Document getDocument(java.io.File file)
                                        throws XmlException
If reading from a file, use this, since it will set the SystemId.

Throws:
XmlException

getDocument

public static org.w3c.dom.Document getDocument(java.io.Reader reader)
                                        throws XmlException
Throws:
XmlException

getDocument

public static org.w3c.dom.Document getDocument(java.io.InputStream stream)
                                        throws XmlException
Throws:
XmlException

getDocument

public static org.w3c.dom.Document getDocument(org.xml.sax.InputSource is)
                                        throws XmlException
FIXME: Here we could fix our own factory/prop stuff to use alternative builders than system default.

Throws:
XmlException

main

public static void main(java.lang.String[] args)


Copyright © 2000-2004 Backsource. All Rights Reserved.