org.jdom
Class Document

java.lang.Object
  |
  +--org.jdom.Document

public class Document
extends java.lang.Object

Document defines behavior for an XML Document, modeled in Java. Methods allow the user to the root element as well as processing instructions and other document-level information.

Version:
1.0
Author:
Brett McLaughlin, Jason Hunter

Field Summary
protected  java.util.List content
          This Document's Comments and the root Element
protected  DocType docType
          The DocType declaration
protected  java.util.Map namespaceMappings
          The namespace prefix/URI mappings for this Document.
protected  java.util.List processingInstructions
          ProcessingInstructions of Document, with order preserved.
protected  Element rootElement
          The root Element of the Document.
 
Constructor Summary
protected Document()
           Default, no-args constructor for implementations to use if needed.
  Document(Element rootElement)
           This will create a new Document, with the supplied Element as the root element, and no DocType declaration.
  Document(Element rootElement, DocType docType)
           This will create a new Document, with the supplied Element as the root element and the supplied DocType declaration.
 
Method Summary
 Document addComment(Comment comment)
           This will add a comment to the Document.
 Document addNamespaceMapping(java.lang.String prefix, java.lang.String mapping)
           Adds a namespace prefix/URI mapping.
 Document addProcessingInstruction(ProcessingInstruction pi)
          
 Document addProcessingInstruction(java.lang.String target, java.util.Map data)
          
 Document addProcessingInstruction(java.lang.String target, java.lang.String data)
          
 java.util.List getContent()
           This will return all content for the Document.
 DocType getDocType()
           This will return the DocType declaration for this Document, or null if none exists.
 java.util.Map getNamespaceMappings()
           This will return all namespace mappings.
 java.lang.String getNamespaceURI(java.lang.String prefix)
           This will return the URI associated with the supplied prefix, or an empty String if no such mapping exists.
 ProcessingInstruction getProcessingInstruction(java.lang.String target)
           This returns the first processing instruction for this Document for the supplied target.
 java.util.List getProcessingInstructions()
           This will return the list of ProcessingInstructions for this Document.
 java.util.List getProcessingInstructions(java.lang.String target)
           This returns the processing instructions for this Document which have the supplied target.
 Element getRootElement()
           This will return the root Element for this Document, or null if there is none.
 boolean removeNamespaceMapping(java.lang.String prefix)
           This removes a namespace mapping for the supplied prefix.
 boolean removeProcessingInstruction(ProcessingInstruction processingInstruction)
           This will remove the specified ProcessingInstruction.
 boolean removeProcessingInstruction(java.lang.String target)
           This will remove the first PI with the specified target.
 boolean removeProcessingInstructions(java.lang.String target)
           This will remove all PIs with the specified target.
 Document setDocType(DocType docType)
           This will set the DocType declaration for this Document.
 Document setProcessingInstructions(java.util.List processingInstructions)
           This sets the PIs for this Document to those in the List
 Document setRootElement(Element rootElement)
           This sets the root Element for the Document.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

processingInstructions

protected java.util.List processingInstructions
ProcessingInstructions of Document, with order preserved. These are stored at the document level, so while order is consistent, placement is not. In other words the resulting Document is the same functionally as when created, but not neccessarily lexically.

content

protected java.util.List content
This Document's Comments and the root Element

namespaceMappings

protected java.util.Map namespaceMappings
The namespace prefix/URI mappings for this Document.

rootElement

protected Element rootElement
The root Element of the Document.

docType

protected DocType docType
The DocType declaration
Constructor Detail

Document

protected Document()

Default, no-args constructor for implementations to use if needed.


Document

public Document(Element rootElement,
                DocType docType)

This will create a new Document, with the supplied Element as the root element and the supplied DocType declaration.

Parameters:
rootElement - Element for document root.
docType - DocType declaration.

Document

public Document(Element rootElement)

This will create a new Document, with the supplied Element as the root element, and no DocType declaration.

Parameters:
rootElement - Element for document root
Method Detail

getRootElement

public Element getRootElement()
                       throws NoSuchElementException

This will return the root Element for this Document, or null if there is none.

Returns:
Element - the document's root element
Throws:
NoSuchElementException - - when no root element exists.

setRootElement

public Document setRootElement(Element rootElement)

This sets the root Element for the Document.

Parameters:
rootElement - Element to be new root.
Returns:
Document - modified Document.

getDocType

public DocType getDocType()

This will return the DocType declaration for this Document, or null if none exists.

Returns:
DocType - the DOCTYPE declaration.

setDocType

public Document setDocType(DocType docType)

This will set the DocType declaration for this Document.

Parameters:
docType - DocType declaration.

getProcessingInstructions

public java.util.List getProcessingInstructions()

This will return the list of ProcessingInstructions for this Document.

Returns:
List - PIs for document.

getProcessingInstructions

public java.util.List getProcessingInstructions(java.lang.String target)

This returns the processing instructions for this Document which have the supplied target.

Parameters:
target - String target of PI to return.
Returns:
List - all PIs with the specified target.

getProcessingInstruction

public ProcessingInstruction getProcessingInstruction(java.lang.String target)
                                               throws NoSuchProcessingInstructionException

This returns the first processing instruction for this Document for the supplied target.

Parameters:
target - String target of PI to return.
Returns:
ProcessingInstruction - the first PI with the specified target.

addProcessingInstruction

public Document addProcessingInstruction(ProcessingInstruction pi)

Parameters:
pi - the PI to add.
Returns:
Document this document modified.

addProcessingInstruction

public Document addProcessingInstruction(java.lang.String target,
                                         java.lang.String data)

Parameters:
target - target of PI to add.
data - raw data portion of PI to add.
Returns:
Document this document modified.

addProcessingInstruction

public Document addProcessingInstruction(java.lang.String target,
                                         java.util.Map data)

Parameters:
target - target of PI to add.
data - map data portion of PI to add.
Returns:
Document this document modified.

setProcessingInstructions

public Document setProcessingInstructions(java.util.List processingInstructions)

This sets the PIs for this Document to those in the List

Parameters:
processingInstructions - List of PIs to use.
Returns:
Document - this Document modified.

removeProcessingInstruction

public boolean removeProcessingInstruction(ProcessingInstruction processingInstruction)

This will remove the specified ProcessingInstruction.

Parameters:
processingInstruction - ProcessingInstruction to remove.
Returns:
boolean - whether the requested PI was removed.

removeProcessingInstruction

public boolean removeProcessingInstruction(java.lang.String target)

This will remove the first PI with the specified target.

Parameters:
target - String target of PI to remove.
Returns:
boolean - whether the requested PI was removed.

removeProcessingInstructions

public boolean removeProcessingInstructions(java.lang.String target)

This will remove all PIs with the specified target.

Parameters:
target - String target of PI to remove.
Returns:
boolean - whether the requested PIs were removed.

addNamespaceMapping

public Document addNamespaceMapping(java.lang.String prefix,
                                    java.lang.String mapping)

Adds a namespace prefix/URI mapping.

Parameters:
prefix - String prefix for mapping.
uri - String URI being mapped to
Returns:
Document - this modified

getNamespaceURI

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

This will return the URI associated with the supplied prefix, or an empty String if no such mapping exists.

Parameters:
String - prefix to locate mapping for.
Returns:
String - URI for supplied prefix.

removeNamespaceMapping

public boolean removeNamespaceMapping(java.lang.String prefix)

This removes a namespace mapping for the supplied prefix.

Parameters:
String - prefix to remove mapping for.
Returns:
boolean - whether the mapping was deleted.

getNamespaceMappings

public java.util.Map getNamespaceMappings()

This will return all namespace mappings.

Returns:
Map - namespace mappings.

addComment

public Document addComment(Comment comment)

This will add a comment to the Document.

Parameters:
comment - Comment to add.
Returns:
Document - this object modified.

getContent

public java.util.List getContent()

This will return all content for the Document.

Returns:
List - all Document content


Copyright © 2000 Brett McLaughlin, Jason Hunter. All Rights Reserved.