net.sf.vex.dom
Class Document

java.lang.Object
  |
  +--net.sf.vex.dom.Document

public class Document
extends java.lang.Object

Represents an XML document.


Constructor Summary
Document(RootElement rootElement)
          Class constructor.
 
Method Summary
 void addDocumentListener(DocumentListener listener)
          Adds a document listener to the list of listeners to be notified of document changes.
 boolean canInsertFragment(int offset, DocumentFragment fragment)
          Returns true if the given document fragment can be inserted at the given offset.
 boolean canInsertText(int offset)
          Returns true if text can be inserted at the given offset.
 Position createPosition(int offset)
          Creates a Position object at the given character offset.
 javax.swing.undo.UndoableEdit delete(int startOffset, int endOffset)
          Deletes a portion of the document.
 Element findCommonElement(int offset1, int offset2)
          Finds the lowest element that contains both of the given offsets.
 char getCharacterAt(int offset)
          Returns the character at the given offset.
 Element getElementAt(int offset)
          Returns the element at the given offset.
 java.lang.String getEncoding()
          Returns the encoding used for this document, or null if no encoding has been declared.
 DocumentFragment getFragment(int startOffset, int endOffset)
          Create a DocumentFragment representing the given range of offsets.
 int getLength()
          Returns the length of the document in characters, including the null characters that delimit each element.
 java.lang.String[] getNodeNames(int startOffset, int endOffset)
          Returns an array of element names and Validator.PCDATA representing the content between the given offsets.
 java.lang.String getPublicID()
          Returns the public ID of the document type.
 java.lang.String getRawText(int startOffset, int endOffset)
          Returns the text between the two given offsets.
 Element getRootElement()
          Returns the root element of this document.
 java.lang.String getSystemID()
          Returns the system ID of the document type.
 java.lang.String getText(int startOffset, int endOffset)
          Returns the text between the two given offsets.
 Validator getValidator()
          Returns the validator used to validate the document, or null if a validator has not been set.
 javax.swing.undo.UndoableEdit insertElement(int offset, Element element)
          Inserts an element at the given position.
 javax.swing.undo.UndoableEdit insertFragment(int offset, DocumentFragment fragment)
          Inserts a document fragment at the given position.
 javax.swing.undo.UndoableEdit insertText(int offset, java.lang.String text)
          Inserts text at the given position.
 void removeDocumentListener(DocumentListener listener)
          Removes a document listener from the list of listeners so that it is no longer notified of document changes.
 void setPublicID(java.lang.String publicID)
          Sets the public ID for the document's document type.
 void setSystemID(java.lang.String systemID)
          Sets the system ID for the document's document type.
 void setValidator(Validator validator)
          Sets the validator to use for this document.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Document

public Document(RootElement rootElement)
Class constructor.

Parameters:
rootElement - root element of the document. The document property of this RootElement is set by this constructor.
Method Detail

addDocumentListener

public void addDocumentListener(DocumentListener listener)
Adds a document listener to the list of listeners to be notified of document changes.

Parameters:
listener - DocumentListener to add.

canInsertFragment

public boolean canInsertFragment(int offset,
                                 DocumentFragment fragment)
Returns true if the given document fragment can be inserted at the given offset.

Parameters:
offset - offset where the insertion is to occur
fragment - fragment to be inserted

canInsertText

public boolean canInsertText(int offset)
Returns true if text can be inserted at the given offset.

Parameters:
offset - offset where the insertion is to occur

createPosition

public Position createPosition(int offset)
Creates a Position object at the given character offset.

Parameters:
offset - initial character offset of the position

delete

public javax.swing.undo.UndoableEdit delete(int startOffset,
                                            int endOffset)
                                     throws DocumentValidationException
Deletes a portion of the document. No element may straddle the deletion span.

Parameters:
startOffset - start of the range to delete
endOffset - end of the range to delete
Returns:
UndoableEdit that can be used to undo the deletion
Throws:
DocumentValidationException - if the change would result in an invalid document.

findCommonElement

public Element findCommonElement(int offset1,
                                 int offset2)
Finds the lowest element that contains both of the given offsets.

Parameters:
offset1 - the first offset
offset2 - the second offset

getCharacterAt

public char getCharacterAt(int offset)
Returns the character at the given offset.


getElementAt

public Element getElementAt(int offset)
Returns the element at the given offset. The given offset must be greater or equal to 1 and less than the current document length.


getEncoding

public java.lang.String getEncoding()
Returns the encoding used for this document, or null if no encoding has been declared.


getFragment

public DocumentFragment getFragment(int startOffset,
                                    int endOffset)
Create a DocumentFragment representing the given range of offsets.

Returns:

getLength

public int getLength()
Returns the length of the document in characters, including the null characters that delimit each element.


getNodeNames

public java.lang.String[] getNodeNames(int startOffset,
                                       int endOffset)
Returns an array of element names and Validator.PCDATA representing the content between the given offsets. The given offsets must both be directly in the same element.

Parameters:
startOffset - the offset at which the sequence begins
endOffset - the offset at which the sequence ends

getPublicID

public java.lang.String getPublicID()
Returns the public ID of the document type.


getRawText

public java.lang.String getRawText(int startOffset,
                                   int endOffset)
Returns the text between the two given offsets. Unlike getText, sentinel characters are not removed.

Parameters:
startOffset - character offset of the start of the text
endOffset - character offset of the end of the text

getRootElement

public Element getRootElement()
Returns the root element of this document.


getSystemID

public java.lang.String getSystemID()
Returns the system ID of the document type.


getText

public java.lang.String getText(int startOffset,
                                int endOffset)
Returns the text between the two given offsets. Sentinal characters are removed.

Parameters:
startOffset - character offset of the start of the text
endOffset - character offset of the end of the text

getValidator

public Validator getValidator()
Returns the validator used to validate the document, or null if a validator has not been set. Note that the DocumentFactory does not automatically create a validator.


insertElement

public javax.swing.undo.UndoableEdit insertElement(int offset,
                                                   Element element)
                                            throws DocumentValidationException
Inserts an element at the given position.

Parameters:
offset - character offset at which the element is to be inserted. Must be greater or equal to 1 and less than the current length of the document, i.e. it must be within the range of the root element.
element - element to insert
Throws:
DocumentValidationException - if the change would result in an invalid document.

insertFragment

public javax.swing.undo.UndoableEdit insertFragment(int offset,
                                                    DocumentFragment fragment)
                                             throws DocumentValidationException
Inserts a document fragment at the given position.

Parameters:
offset - character offset at which the element is to be inserted. Must be greater or equal to 1 and less than the current length of the document, i.e. it must be within the range of the root element.
fragment - fragment to insert
Throws:
DocumentValidationException - if the change would result in an invalid document.

insertText

public javax.swing.undo.UndoableEdit insertText(int offset,
                                                java.lang.String text)
                                         throws DocumentValidationException
Inserts text at the given position.

Parameters:
offset - character offset at which the text is to be inserted. Must be greater or equal to 1 and less than the current length of the document, i.e. it must be within the range of the root element.
text - text to insert
Returns:
UndoableEdit that can be used to undo the deletion
Throws:
DocumentValidationException - if the change would result in an invalid document.

removeDocumentListener

public void removeDocumentListener(DocumentListener listener)
Removes a document listener from the list of listeners so that it is no longer notified of document changes.

Parameters:
listener - DocumentListener to remove.

setPublicID

public void setPublicID(java.lang.String publicID)
Sets the public ID for the document's document type.

Parameters:
publicID - New value for the public ID.

setSystemID

public void setSystemID(java.lang.String systemID)
Sets the system ID for the document's document type.

Parameters:
systemID - New value for the system ID.

setValidator

public void setValidator(Validator validator)
Sets the validator to use for this document.

Parameters:
validator - Validator to use for this document.