org.odftoolkit.odfdom.pkg
Class OdfFileDom

java.lang.Object
  extended by org.apache.xerces.dom.NodeImpl
      extended by org.apache.xerces.dom.ChildNode
          extended by org.apache.xerces.dom.ParentNode
              extended by org.apache.xerces.dom.CoreDocumentImpl
                  extended by org.apache.xerces.dom.DocumentImpl
                      extended by org.odftoolkit.odfdom.pkg.OdfFileDom
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, javax.xml.namespace.NamespaceContext, Document, DocumentEvent, EventTarget, Node, NodeList, DocumentRange, DocumentTraversal
Direct Known Subclasses:
OdfContentDom, OdfMetaDom, OdfSettingsDom, OdfStylesDom

public class OdfFileDom
extends DocumentImpl
implements javax.xml.namespace.NamespaceContext

The DOM repesentation of an XML file within the ODF document.

See Also:
Serialized Form

Field Summary
protected  java.util.Map<java.lang.String,java.util.Set<java.lang.String>> mDuplicatePrefixesByUri
          Contains only the duplicate prefix.
protected  OdfPackage mPackage
           
protected  OdfPackageDocument mPackageDocument
           
protected  java.lang.String mPackagePath
           
protected  java.util.Map<java.lang.String,java.lang.String> mPrefixByUri
           
protected  java.util.Map<java.lang.String,java.lang.String> mUriByPrefix
           
protected  javax.xml.xpath.XPath mXPath
           
 
Fields inherited from class org.apache.xerces.dom.DocumentImpl
eventListeners, iterators, mutationEvents, ranges
 
Fields inherited from class org.apache.xerces.dom.CoreDocumentImpl
actualEncoding, allowGrammarAccess, changes, docElement, docType, encoding, errorChecking, fDocumentURI, identifiers, standalone, userData, version, xmlVersionChanged
 
Fields inherited from class org.apache.xerces.dom.ParentNode
firstChild, fNodeListCache, ownerDocument
 
Fields inherited from class org.apache.xerces.dom.ChildNode
nextSibling, previousSibling
 
Fields inherited from class org.apache.xerces.dom.NodeImpl
DOCUMENT_POSITION_CONTAINS, DOCUMENT_POSITION_DISCONNECTED, DOCUMENT_POSITION_FOLLOWING, DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, DOCUMENT_POSITION_IS_CONTAINED, DOCUMENT_POSITION_PRECEDING, ELEMENT_DEFINITION_NODE, FIRSTCHILD, flags, HASSTRING, ID, IGNORABLEWS, NORMALIZED, OWNED, ownerNode, READONLY, SPECIFIED, SYNCCHILDREN, SYNCDATA, TREE_POSITION_ANCESTOR, TREE_POSITION_DESCENDANT, TREE_POSITION_DISCONNECTED, TREE_POSITION_EQUIVALENT, TREE_POSITION_FOLLOWING, TREE_POSITION_PRECEDING, TREE_POSITION_SAME_NODE
 
Fields inherited from interface org.w3c.dom.Node
ATTRIBUTE_NODE, CDATA_SECTION_NODE, COMMENT_NODE, DOCUMENT_FRAGMENT_NODE, DOCUMENT_NODE, DOCUMENT_POSITION_CONTAINED_BY, DOCUMENT_POSITION_CONTAINS, DOCUMENT_POSITION_DISCONNECTED, DOCUMENT_POSITION_FOLLOWING, DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, DOCUMENT_POSITION_PRECEDING, DOCUMENT_TYPE_NODE, ELEMENT_NODE, ENTITY_NODE, ENTITY_REFERENCE_NODE, NOTATION_NODE, PROCESSING_INSTRUCTION_NODE, TEXT_NODE
 
Constructor Summary
protected OdfFileDom(OdfPackageDocument packageDocument, java.lang.String packagePath)
          Creates the DOM representation of an XML file of an Odf document.
 
Method Summary
 OdfAttribute createAttribute(java.lang.String name)
          Create the ODF attribute with its name
 OdfAttribute createAttributeNS(OdfName name)
          Create the ODF attribute with ODF name
 OdfAttribute createAttributeNS(java.lang.String nsuri, java.lang.String qname)
          Create the ODF attribute with namespace uri and qname
 OdfElement createElement(java.lang.String name)
          Create ODF element with namespace uri and qname
 OdfElement createElementNS(OdfName name)
          Create ODF element with ODF name
 OdfElement createElementNS(java.lang.String nsuri, java.lang.String qname)
          Create ODF element with namespace uri and qname
 OdfPackageDocument getDocument()
          Retrieves the OdfPackageDocument of the XML file.
 java.lang.String getNamespaceURI(java.lang.String prefix)
          Get Namespace URI bound to a prefix in the current scope (the XML file).
 java.lang.String getPackagePath()
          Retrieves the String of Package Path
 java.lang.String getPrefix(java.lang.String namespaceURI)
          Get prefix bound to Namespace URI in the current scope (the XML file).
 java.util.Iterator<java.lang.String> getPrefixes(java.lang.String namespaceURI)
          Get all prefixes bound to a Namespace URI in the current scope.
 OdfElement getRootElement()
          Retrieves the ODF root element.
 javax.xml.xpath.XPath getXPath()
          Create an XPath instance to select one or more nodes from an ODF document.
protected  void initialize()
           
static OdfFileDom newFileDom(OdfPackageDocument packageDocument, java.lang.String packagePath)
           
<T extends OdfElement>
T
newOdfElement(java.lang.Class<T> clazz)
           
 OdfNamespace setNamespace(NamespaceName name)
          Adds a new Namespace to the DOM.
 OdfNamespace setNamespace(java.lang.String prefix, java.lang.String uri)
          Adds a new Namespace to the DOM.
 java.lang.String toString()
           
 
Methods inherited from class org.apache.xerces.dom.DocumentImpl
addEventListener, cloneNode, copyEventListeners, createEvent, createNodeIterator, createNodeIterator, createRange, createTreeWalker, createTreeWalker, dispatchAggregateEvents, dispatchAggregateEvents, dispatchEvent, dispatchEventToSubtree, dispatchingEventToSubtree, getEventListeners, getImplementation, removeEventListener, saveEnclosingAttr, setEventListeners
 
Methods inherited from class org.apache.xerces.dom.CoreDocumentImpl
abort, adoptNode, callUserDataHandlers, changed, changes, checkDOMNSErr, checkNamespaceWF, checkQName, clearIdentifiers, clone, cloneNode, createAttributeNS, createCDATASection, createComment, createDocumentFragment, createDocumentType, createElementDefinition, createElementNS, createEntity, createEntityReference, createNotation, createProcessingInstruction, createTextNode, getAsync, getBaseURI, getDoctype, getDocumentElement, getDocumentURI, getDomConfig, getElementById, getElementsByTagName, getElementsByTagNameNS, getEncoding, getErrorChecking, getFeature, getIdentifier, getIdentifiers, getInputEncoding, getNodeName, getNodeNumber, getNodeNumber, getNodeType, getOwnerDocument, getStandalone, getStrictErrorChecking, getTextContent, getUserData, getUserData, getUserDataRecord, getVersion, getXmlEncoding, getXmlStandalone, getXmlVersion, importNode, insertBefore, isKidOK, isValidQName, isXMLName, load, loadXML, normalizeDocument, putIdentifier, removeChild, removeIdentifier, renameNode, replaceChild, saveXML, setAsync, setDocumentURI, setEncoding, setErrorChecking, setInputEncoding, setStandalone, setStrictErrorChecking, setTextContent, setUserData, setUserData, setVersion, setXmlEncoding, setXmlStandalone, setXmlVersion, undeferChildren
 
Methods inherited from class org.apache.xerces.dom.ParentNode
getChildNodes, getChildNodesUnoptimized, getFirstChild, getLastChild, getLength, hasChildNodes, isEqualNode, item, normalize, setOwnerDocument, setReadOnly, synchronizeChildren
 
Methods inherited from class org.apache.xerces.dom.ChildNode
getNextSibling, getParentNode, getPreviousSibling
 
Methods inherited from class org.apache.xerces.dom.NodeImpl
addEventListener, appendChild, compareDocumentPosition, compareTreePosition, dispatchEvent, getAttributes, getContainer, getLocalName, getNamespaceURI, getNodeValue, getPrefix, getReadOnly, getUserData, getUserData, getUserDataRecord, hasAttributes, isDefaultNamespace, isSameNode, isSupported, lookupNamespaceURI, lookupPrefix, needsSyncChildren, removeEventListener, setNodeValue, setPrefix, setUserData, setUserData, synchronizeData
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.w3c.dom.Node
appendChild, compareDocumentPosition, getAttributes, getChildNodes, getFirstChild, getLastChild, getLocalName, getNamespaceURI, getNextSibling, getNodeValue, getParentNode, getPrefix, getPreviousSibling, getUserData, hasAttributes, hasChildNodes, isDefaultNamespace, isEqualNode, isSameNode, isSupported, lookupNamespaceURI, lookupPrefix, normalize, setNodeValue, setPrefix, setUserData
 

Field Detail

mPackagePath

protected java.lang.String mPackagePath

mPackageDocument

protected OdfPackageDocument mPackageDocument

mPackage

protected OdfPackage mPackage

mXPath

protected javax.xml.xpath.XPath mXPath

mUriByPrefix

protected java.util.Map<java.lang.String,java.lang.String> mUriByPrefix

mPrefixByUri

protected java.util.Map<java.lang.String,java.lang.String> mPrefixByUri

mDuplicatePrefixesByUri

protected java.util.Map<java.lang.String,java.util.Set<java.lang.String>> mDuplicatePrefixesByUri
Contains only the duplicate prefix. The primary hold by mPrefixByUri still have to be added

Constructor Detail

OdfFileDom

protected OdfFileDom(OdfPackageDocument packageDocument,
                     java.lang.String packagePath)
Creates the DOM representation of an XML file of an Odf document.

Parameters:
packageDocument - the document the XML files belongs to
packagePath - the internal package path to the XML file
Method Detail

newFileDom

public static OdfFileDom newFileDom(OdfPackageDocument packageDocument,
                                    java.lang.String packagePath)

initialize

protected void initialize()

getDocument

public OdfPackageDocument getDocument()
Retrieves the OdfPackageDocument of the XML file. A package document is usually represented as a directory with a mediatype.

Returns:
The document holding the XML file.

getPackagePath

public java.lang.String getPackagePath()
Retrieves the String of Package Path

Returns:
The path of the XML file relative to the package root

getRootElement

public OdfElement getRootElement()
Retrieves the ODF root element.

Returns:
The OdfElement being the root of the document.

createElement

public OdfElement createElement(java.lang.String name)
                         throws DOMException
Create ODF element with namespace uri and qname

Specified by:
createElement in interface Document
Overrides:
createElement in class CoreDocumentImpl
Parameters:
name - The element name
Throws:
DOMException

createElementNS

public OdfElement createElementNS(java.lang.String nsuri,
                                  java.lang.String qname)
                           throws DOMException
Create ODF element with namespace uri and qname

Specified by:
createElementNS in interface Document
Overrides:
createElementNS in class CoreDocumentImpl
Parameters:
nsuri - The namespace uri
qname - The element qname
Throws:
DOMException

createElementNS

public OdfElement createElementNS(OdfName name)
                           throws DOMException
Create ODF element with ODF name

Parameters:
name - The OdfName
Returns:
The OdfElement
Throws:
DOMException

createAttribute

public OdfAttribute createAttribute(java.lang.String name)
                             throws DOMException
Create the ODF attribute with its name

Specified by:
createAttribute in interface Document
Overrides:
createAttribute in class CoreDocumentImpl
Parameters:
name - the attribute qname
Returns:
The OdfAttribute
Throws:
DOMException

createAttributeNS

public OdfAttribute createAttributeNS(java.lang.String nsuri,
                                      java.lang.String qname)
                               throws DOMException
Create the ODF attribute with namespace uri and qname

Specified by:
createAttributeNS in interface Document
Overrides:
createAttributeNS in class CoreDocumentImpl
Parameters:
nsuri - The namespace uri
qname - the attribute qname
Returns:
The OdfAttribute
Throws:
DOMException

createAttributeNS

public OdfAttribute createAttributeNS(OdfName name)
                               throws DOMException
Create the ODF attribute with ODF name

Parameters:
name - The OdfName
Returns:
The OdfAttribute
Throws:
DOMException

newOdfElement

public <T extends OdfElement> T newOdfElement(java.lang.Class<T> clazz)

toString

public java.lang.String toString()
Overrides:
toString in class NodeImpl

getXPath

public javax.xml.xpath.XPath getXPath()
Create an XPath instance to select one or more nodes from an ODF document. Therefore the namespace context is set to the OdfNamespace

Returns:
an XPath instance with namespace context set to include the standard ODFDOM prefixes.

getNamespaceURI

public java.lang.String getNamespaceURI(java.lang.String prefix)

Get Namespace URI bound to a prefix in the current scope (the XML file).

When requesting a Namespace URI by prefix, the following table describes the returned Namespace URI value for all possible prefix values:

getNamespaceURI(prefix) return value for specified prefixes
prefix parameter Namespace URI return value
DEFAULT_NS_PREFIX ("") default Namespace URI in the current scope or XMLConstants.NULL_NS_URI("") when there is no default Namespace URI in the current scope
bound prefix Namespace URI bound to prefix in current scope
unbound prefix XMLConstants.NULL_NS_URI("")
XMLConstants.XML_NS_PREFIX ("xml") XMLConstants.XML_NS_URI ("http://www.w3.org/XML/1998/namespace")
XMLConstants.XMLNS_ATTRIBUTE ("xmlns") XMLConstants.XMLNS_ATTRIBUTE_NS_URI ("http://www.w3.org/2000/xmlns/")
null IllegalArgumentException is thrown

Specified by:
getNamespaceURI in interface javax.xml.namespace.NamespaceContext
Parameters:
prefix - prefix to look up
Returns:
Namespace URI bound to prefix in the current scope
Throws:
java.lang.IllegalArgumentException - When prefix is null

getPrefix

public java.lang.String getPrefix(java.lang.String namespaceURI)

Get prefix bound to Namespace URI in the current scope (the XML file).

Multiple prefixes bound to Namespace URI will be normalized to the first prefix defined.

When requesting a prefix by Namespace URI, the following table describes the returned prefix value for all Namespace URI values:

getPrefix(namespaceURI) return value for specified Namespace URIs
Namespace URI parameter prefix value returned
<default Namespace URI> XMLConstants.DEFAULT_NS_PREFIX ("")
bound Namespace URI prefix bound to Namespace URI in the current scope, if multiple prefixes are bound to the Namespace URI in the current scope, a single arbitrary prefix, whose choice is implementation dependent, is returned
unbound Namespace URI null
XMLConstants.XML_NS_URI ("http://www.w3.org/XML/1998/namespace") XMLConstants.XML_NS_PREFIX ("xml")
XMLConstants.XMLNS_ATTRIBUTE_NS_URI ("http://www.w3.org/2000/xmlns/") XMLConstants.XMLNS_ATTRIBUTE ("xmlns")
null IllegalArgumentException is thrown

Specified by:
getPrefix in interface javax.xml.namespace.NamespaceContext
Parameters:
namespaceURI - URI of Namespace to lookup
Returns:
prefix bound to Namespace URI in current context
Throws:
java.lang.IllegalArgumentException - When namespaceURI is null

getPrefixes

public java.util.Iterator<java.lang.String> getPrefixes(java.lang.String namespaceURI)

Get all prefixes bound to a Namespace URI in the current scope. (the XML file)

NOTE: Multiple prefixes bound to a similar Namespace URI will be normalized to the first prefix defined. Still the namespace attributes exist in the XML as inner value prefixes might be used.

The Iterator is not modifiable. e.g. the remove() method will throw UnsupportedOperationException.

When requesting prefixes by Namespace URI, the following table describes the returned prefixes value for all Namespace URI values:

getPrefixes(namespaceURI) return value for specified Namespace URIs
Namespace URI parameter prefixes value returned
bound Namespace URI, including the <default Namespace URI> Iterator over prefixes bound to Namespace URI in the current scope in an arbitrary, implementation dependent, order
unbound Namespace URI empty Iterator
XMLConstants.XML_NS_URI ("http://www.w3.org/XML/1998/namespace") Iterator with one element set to XMLConstants.XML_NS_PREFIX ("xml")
XMLConstants.XMLNS_ATTRIBUTE_NS_URI ("http://www.w3.org/2000/xmlns/") Iterator with one element set to XMLConstants.XMLNS_ATTRIBUTE ("xmlns")
null IllegalArgumentException is thrown

Specified by:
getPrefixes in interface javax.xml.namespace.NamespaceContext
Parameters:
namespaceURI - URI of Namespace to lookup
Returns:
Iterator for all prefixes bound to the Namespace URI in the current scope
Throws:
java.lang.IllegalArgumentException - When namespaceURI is null

setNamespace

public OdfNamespace setNamespace(java.lang.String prefix,
                                 java.lang.String uri)
Adds a new Namespace to the DOM. Making the prefix usable with JDK XPath. All namespace attributes will be written to the root element during later serialization of the DOM by the OdfPackage.

Parameters:
prefix - of the namespace to be set to this DOM
uri - of the namespace to be set to this DOM
Returns:
the namespace that was set. If an URI was registered before to the DOM, the previous prefix will be taken. In case of a given prefix that was already registered, but related to a new URI, the prefix will be adapted. The new prefix receives the suffix '__' plus integer, e.g. "__1" for the first duplicate and "__2" for the second.

setNamespace

public OdfNamespace setNamespace(NamespaceName name)
Adds a new Namespace to the DOM. Making the prefix usable with JDK XPath. All namespace attributes will be written to the root element during later serialization of the DOM by the OdfPackage.

Parameters:
name - the namespace to be set
Returns:
the namespace that was set. If an URI was registered before to the DOM, the previous prefix will be taken. In case of a given prefix that was already registered, but related to a new URI, the prefix will be adapted. The new prefix receives the suffix '__' plus integer, e.g. "__1" for the first duplicate and "__2" for the second.


Copyright © 2008-2012 The Apache Software Foundation. All Rights Reserved.