com.hp.hpl.sparta
Class Document

java.lang.Object
  |
  +--com.hp.hpl.sparta.Node
        |
        +--com.hp.hpl.sparta.Document
All Implemented Interfaces:
java.lang.Cloneable

public class Document
extends Node

An XML Document.

Copyright (C) 2002 Hewlett-Packard Company. This file is part of Sparta, an XML Parser, DOM, and XPath library. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library 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.

Version:
$Date: 2002/09/12 23:00:57 $ $Revision: 1.2 $
Author:
Eamonn O'Brien-Strain
See Also:
GNU Lesser General Public License, Document

Inner Class Summary
 class Document.Index
           
static interface Document.Observer
           
 
Constructor Summary
Document()
           
 
Method Summary
 void addObserver(Document.Observer observer)
           
 java.lang.Object clone()
          Deep copy of this document.
 void deleteObserver(Document.Observer observer)
           
 boolean equals(java.lang.Object thatO)
          Two documents are equal IFF their document elements are equal.
 java.lang.Object getAnnotation()
           
 Element getDocumentElement()
           
 java.lang.String getSystemId()
           
 void removeIndices()
           
 void setAnnotation(java.lang.Object annotation)
          Use by client to attach arbitrary data to DOM document.
 void setDocumentElement(Element rootElement)
           
 void setSystemId(java.lang.String systemId)
           
 java.lang.String toString()
          Returns URL of systemID that was parsed or "MEMORY" of createed in memory.
 void toString(java.io.Writer writer)
          Accumlate text nodes hierarchically.
 void toXml(java.io.Writer writer)
           
 boolean xpathEnsure(java.lang.String xpath)
          Just like Node.xpathEnsure, but also handles case of no documentElement.
 Document.Index xpathGetIndex(java.lang.String xpath)
          For faster lookup by XPath return (creating if necessary) an index.
 boolean xpathHasIndex(java.lang.String xpath)
           
 Element xpathSelectElement(java.lang.String xpath)
          Select the first element that matches the absolute XPath expression in this document, or null if there is no match.
 java.util.Enumeration xpathSelectElements(java.lang.String xpath)
          Select all the elements that match the absolute XPath expression in this document.
 java.lang.String xpathSelectString(java.lang.String xpath)
          Select the first element that matches the absolute XPath expression in this document, or null if there is no match.
 java.util.Enumeration xpathSelectStrings(java.lang.String xpath)
          Select all the strings that match the absolute XPath expression in this document.
 
Methods inherited from class com.hp.hpl.sparta.Node
getNextSibling, getOwnerDocument, getParentNode, getPreviousSibling, htmlEncode, toXml, xpathSetStrings
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Document

public Document()
Method Detail

clone

public java.lang.Object clone()
Deep copy of this document. Any annotation is not copied.
Overrides:
clone in class Node

getSystemId

public java.lang.String getSystemId()

setSystemId

public void setSystemId(java.lang.String systemId)

toString

public java.lang.String toString()
Returns URL of systemID that was parsed or "MEMORY" of createed in memory.
Overrides:
toString in class Node

getDocumentElement

public Element getDocumentElement()

setDocumentElement

public void setDocumentElement(Element rootElement)

getAnnotation

public java.lang.Object getAnnotation()
Overrides:
getAnnotation in class Node

setAnnotation

public void setAnnotation(java.lang.Object annotation)
Use by client to attach arbitrary data to DOM document. Do not notify indices or other observers.
Overrides:
setAnnotation in class Node

xpathSelectElements

public java.util.Enumeration xpathSelectElements(java.lang.String xpath)
                                          throws ParseException
Select all the elements that match the absolute XPath expression in this document.
Overrides:
xpathSelectElements in class Node

xpathSelectStrings

public java.util.Enumeration xpathSelectStrings(java.lang.String xpath)
                                         throws ParseException
Select all the strings that match the absolute XPath expression in this document.
Overrides:
xpathSelectStrings in class Node

xpathSelectElement

public Element xpathSelectElement(java.lang.String xpath)
                           throws ParseException
Select the first element that matches the absolute XPath expression in this document, or null if there is no match.
Overrides:
xpathSelectElement in class Node

xpathSelectString

public java.lang.String xpathSelectString(java.lang.String xpath)
                                   throws ParseException
Select the first element that matches the absolute XPath expression in this document, or null if there is no match.
Overrides:
xpathSelectString in class Node

xpathEnsure

public boolean xpathEnsure(java.lang.String xpath)
                    throws ParseException
Just like Node.xpathEnsure, but also handles case of no documentElement. TODO. This needs to be fixed. It currently will get confused by xpaths with slashes anywhere other than between steps. FOr example it will not work work for an xpath "/foo[@bar='a/b']" when the documentElement is null.
Overrides:
xpathEnsure in class Node

xpathHasIndex

public boolean xpathHasIndex(java.lang.String xpath)

xpathGetIndex

public Document.Index xpathGetIndex(java.lang.String xpath)
                             throws ParseException
For faster lookup by XPath return (creating if necessary) an index. The xpath should be of the form "xp[@attrName]" where xp is an xpath, not ending in a "[...]" predicate, that returns a list of elements. Doing a get("foo") on the index is equivalent to doing an xpathSelectElement("xp[@attrName='foo']") on the document except that it is faster ( O(1) as apposed to O(n) ). EXAMPLE:
   Enumeration leaders;
   if( doc.xpathHasIndex( "/Team/Members[@firstName]" ){
     //fast version
     Document.Index index = doc.xpathGetIndex( "/Team/Members[@role]" );
     leaders = index.get("leader");
   }else
     //slow version
     leaders = doc.xpathSelectElement( "/Team/Members[@role='leader']" );

removeIndices

public void removeIndices()

addObserver

public void addObserver(Document.Observer observer)

deleteObserver

public void deleteObserver(Document.Observer observer)

toString

public void toString(java.io.Writer writer)
              throws java.io.IOException
Accumlate text nodes hierarchically.

toXml

public void toXml(java.io.Writer writer)
           throws java.io.IOException

equals

public boolean equals(java.lang.Object thatO)
Two documents are equal IFF their document elements are equal.
Overrides:
equals in class java.lang.Object