net.sourceforge.spedit.core
Class XMLElementWrapper

java.lang.Object
  |
  +--net.sourceforge.spedit.core.XMLElementWrapper
All Implemented Interfaces:
Element, TreeNode, XMLNode

public class XMLElementWrapper
extends Object
implements Element, TreeNode, XMLNode

The XMLElementWrapper allows a JDOM Element to appear a Swing Element. This wrapper does not work for JDOM Element but works fine with the XMLElement subclass which has been designed for this purpose.

It is important to note that the XMLElementWrapper automatically notifies the document of a given element when it changes its content.


Field Summary
protected  int contentLength
           
static short CUT_INSIDE
          Indicates that the given block should be cut inside.
static short CUT_LEFT
          Indicates that the given block should be cut left.
static short CUT_RIGHT
          Indicates that the given block should be cut right.
static short DELETE
          Indicates that the given block should be deleted.
protected  XMLElement element
           
static short ELEMENT_AFTER
          Means that the searched element index is located after the current element.
static short ELEMENT_BEFORE
          Means that the searched element index is located before the current element.
static short ELEMENT_THIS
          Means that the searched element index does correspond to the text directly contained in the wrapped XMLElement, and not in a child XMLElement.
protected  Position endPosition
           
protected  XMLInformation information
           
static short LEAVE
          Indicates that the given block should be left as is.
protected  Position startPosition
           
 
Constructor Summary
XMLElementWrapper(XMLElement element)
          Constructs a new XMLElemetWrapper fron the given XMLElement, which is a JDOM Element sublass.
 
Method Summary
 Enumeration children()
           
 boolean contains(int start, int end)
          Tells whether the given range is contained withing the element.
static String cutString(String s, int offset, int start, int end)
          Cuts the given string according to the operation adviced by the getOperation method.
 boolean getAllowsChildren()
           
 AttributeSet getAttributes()
          Fetches the collection of attributes this element contains.
 TreeNode getChildAt(int childIndex)
           
 int getChildCount()
           
 Document getDocument()
          Fetches the document associated with this element.
 Element getElement(int index)
          Returns the Element that is the nth of this Element. Note: The element indexes are different from the wrapped element indexes, as the wrapper only counts elements that are XMLElements.
 Element getElementAt(int offset)
          Searches any descendant element that contains the given offset.
 int getElementCount()
          Gets the number of child elements contained by this element.
 int getElementIndex(int offset)
          Gets the child element index closest to the given offset.
 int getElementIndex(Object object)
          Gets the index of the given object in the child list of this element.
 int getEndOffset()
          Fetches the offset from the beginning of the document that this element ends at.
 int getIndex(TreeNode node)
           
 XMLInformation getInformation()
          Returns the information attached to this element.
 int getLength()
          Returns the length of this element, int terms of text.
 String getName()
          Returns the name of the given text element.
static short getOperation(int offset, int length, int start, int end)
          Tells which operation should be made on the segmen represented by the given offset and the given length, given the fact that the cut will go from start to end exluded.
 TreeNode getParent()
           
 Element getParentElement()
          Returns the parents Element, which can be null if the Element is a root.
 int getStartOffset()
          Fetches the offset from the beginning of the document that this element begins at.
 int getStartOffsetOf(Object object)
          Returns the start offset of the given object.
 String getWholeText()
           
 String getWholeText(int start, int end)
          This is an extension to the JDOM getText() method.
 XMLElement getXMLElement()
          Returns the XMLElement that this wrapper wraps.
 boolean hasInformation()
          Tells wether this elmement has available information.
 void insert(String s, int offset, Position.Bias bias)
          Inserts the given string after or before the given offset.
 void insert(XMLElement elt, int offset, Position.Bias bias)
          Inserts the given element at the given offset in the text.
static String insertString(String source, String insert, int offset, Position.Bias bias)
          Inserts the a string into another at the specified offset.
 boolean isLeaf()
          Returns true if the Element has no children, otherwise returns false.
 Vector remove(int start, int end)
          Removes the text contained between the start and end offsets, the end offset being exclusive.
Elements that are fully contained withing the given range will be deleted.
 void setInformation(XMLInformation info)
          Sets the information attached to this particular element.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ELEMENT_BEFORE

public static final short ELEMENT_BEFORE
Means that the searched element index is located before the current element.

ELEMENT_AFTER

public static final short ELEMENT_AFTER
Means that the searched element index is located after the current element.

ELEMENT_THIS

public static final short ELEMENT_THIS
Means that the searched element index does correspond to the text directly contained in the wrapped XMLElement, and not in a child XMLElement.

CUT_LEFT

public static final short CUT_LEFT
Indicates that the given block should be cut left.

CUT_RIGHT

public static final short CUT_RIGHT
Indicates that the given block should be cut right.

CUT_INSIDE

public static final short CUT_INSIDE
Indicates that the given block should be cut inside.

DELETE

public static final short DELETE
Indicates that the given block should be deleted.

LEAVE

public static final short LEAVE
Indicates that the given block should be left as is.

element

protected XMLElement element

information

protected XMLInformation information

contentLength

protected int contentLength

startPosition

protected Position startPosition

endPosition

protected Position endPosition
Constructor Detail

XMLElementWrapper

public XMLElementWrapper(XMLElement element)
Constructs a new XMLElemetWrapper fron the given XMLElement, which is a JDOM Element sublass.
Method Detail

getInformation

public XMLInformation getInformation()
Returns the information attached to this element. Information includes which views are recommended for this element, but also information about the possible children of this elment.
Specified by:
getInformation in interface XMLNode
Returns:
The associated XMLInformation instance.

setInformation

public void setInformation(XMLInformation info)

Sets the information attached to this particular element. In general elements of the same namespace and name share the same information reference. Using this method will set a specific XML information to this element.

You generally should'nt use this method without good reasons ;).

Specified by:
setInformation in interface XMLNode
Parameters:
info - The new information that will be attached to this element.

hasInformation

public boolean hasInformation()
Tells wether this elmement has available information.
Specified by:
hasInformation in interface XMLNode
Returns:
True if has, false if it has no available information.

children

public Enumeration children()
Specified by:
children in interface TreeNode

getAllowsChildren

public boolean getAllowsChildren()
Specified by:
getAllowsChildren in interface TreeNode

getChildAt

public TreeNode getChildAt(int childIndex)
Specified by:
getChildAt in interface TreeNode

getChildCount

public int getChildCount()
Specified by:
getChildCount in interface TreeNode

getIndex

public int getIndex(TreeNode node)
Specified by:
getIndex in interface TreeNode

getParent

public TreeNode getParent()
Specified by:
getParent in interface TreeNode

getElementCount

public int getElementCount()
Gets the number of child elements contained by this element.
Specified by:
getElementCount in interface Element

getElement

public Element getElement(int index)

Returns the Element that is the nth of this Element.

Note: The element indexes are different from the wrapped element indexes, as the wrapper only counts elements that are XMLElements.

Specified by:
getElement in interface Element
Parameters:
index - The element's index in the wrapper child list.
Returns:
The Element which is the nth child of the wrapper, can be null if not found.

getElementAt

public Element getElementAt(int offset)
Searches any descendant element that contains the given offset. This method is an extension of the getElement method, which only returns the nth element of the XMLElement content.
Parameters:
offset - is the offset of the element in the text. If this offset is lower than getStartOffset() or higher or equal to getEndOffset() then null is returned.
Returns:
The Element or null is offset is out of the XMLElement bounds.

getElementIndex

public int getElementIndex(int offset)
Gets the child element index closest to the given offset. The offset is specified relative to the beginning of the document. Returns -1 if the Element is a leaf, otherwise returns the index of the Element that best represents the given location. Returns 0 if the location is less than the start offset. Returns getElementCount() - 1 if the location is greater than or equal to the end offset.
Specified by:
getElementIndex in interface Element
Parameters:
offset - - the specified offset >= 0
Returns:
Returns: the element >= 0

getElementIndex

public int getElementIndex(Object object)
Gets the index of the given object in the child list of this element.
Returns:
-1 if the element is not a child, otherwise its index.

getStartOffset

public int getStartOffset()
Fetches the offset from the beginning of the document that this element begins at. When the node has no parent or no document, then the returned value is 0
Specified by:
getStartOffset in interface Element
Returns:
The starting offset in the containing document, or 0 in case the element is not attached to a document.

getEndOffset

public int getEndOffset()
Fetches the offset from the beginning of the document that this element ends at.
Specified by:
getEndOffset in interface Element
Returns:
The starting offset in the containing document, or 0 in case the element is not attached to a document.

getLength

public int getLength()
Returns the length of this element, int terms of text. This is simply the difference between getEndOffset() and getStartOffset().

getStartOffsetOf

public int getStartOffsetOf(Object object)
Returns the start offset of the given object. This is used for instance by the XMLText to get its start offset. If the object was not a child of the current element, then -1 is returned.
Parameters:
object - The object from which we want the start offset.
Returns:
The actual offset or -1 if the object is not a child of the current element.

contains

public boolean contains(int start,
                        int end)
Tells whether the given range is contained withing the element.

insert

public void insert(String s,
                   int offset,
                   Position.Bias bias)
Inserts the given string after or before the given offset. In case the offset is out of the bounds of the current element, nothing will be done.
Parameters:
s - is the text that will be inserted.
offset - is the offset at which the string will be inserted. Attention, the offset begins at the getStartOffset() value.
bias - tells wether the text has to be prepended, which is Position.Bias.backward, or appended, which is Position.Bias.forward.
For example, if you insert("X",2,Position.Bias.forward) into "ABCDE", you'll get ABCXDE, you would have get other ABXCDE

insert

public void insert(XMLElement elt,
                   int offset,
                   Position.Bias bias)
Inserts the given element at the given offset in the text. The rules for insertion are exaclty the same as the other insert method.
Parameters:
elt - The XMLElement to be inserted.
offset - The offset of insertion in the current text. Once again the offset is absolute and not relative to the current element.
bias - The bias as specified before.

insertString

public static String insertString(String source,
                                  String insert,
                                  int offset,
                                  Position.Bias bias)
Inserts the a string into another at the specified offset. The bias, as in the other instance method tells if the string has to be appended or pretended. See the documentation of the instance method for more information.
Parameters:
source - is the string in which the other will be inserted.
insert - is the string that will be inserted into the other one.
offset - is the offset of insertion in the offset string.
bias - is backward, this will prepend the string, if forward it will append it.

remove

public Vector remove(int start,
                     int end)
Removes the text contained between the start and end offsets, the end offset being exclusive.
Elements that are fully contained withing the given range will be deleted.
Parameters:
start - The start offset where text deletion will begin.
end - The first charachter that will not be deleted after the start offset.
Returns:
The deleted elements.

getOperation

public static short getOperation(int offset,
                                 int length,
                                 int start,
                                 int end)
Tells which operation should be made on the segmen represented by the given offset and the given length, given the fact that the cut will go from start to end exluded.

cutString

public static String cutString(String s,
                               int offset,
                               int start,
                               int end)
Cuts the given string according to the operation adviced by the getOperation method.

getWholeText

public String getWholeText()
Specified by:
getWholeText in interface XMLNode

getWholeText

public String getWholeText(int start,
                           int end)
This is an extension to the JDOM getText() method. This basically not only gives the depth-1 contained text, but also includes the text of Element children. This is actually the approach the JDOM should have had.
In case the end offset exceeds the right bound of the wrapped element text, then it will be set to the actual right bound.
Specified by:
getWholeText in interface XMLNode
Parameters:
start - is the start offset in the text.
end - is the end offset in the text, and is exclusive, which means if you add getLength() to getStartOffset(), you will be out of the bounds of the current element.

getAttributes

public AttributeSet getAttributes()
Fetches the collection of attributes this element contains.
Specified by:
getAttributes in interface Element

getDocument

public Document getDocument()
Fetches the document associated with this element.
Specified by:
getDocument in interface Element

getName

public String getName()
Returns the name of the given text element.
Specified by:
getName in interface Element

getParentElement

public Element getParentElement()
Returns the parents Element, which can be null if the Element is a root.
Specified by:
getParentElement in interface Element

isLeaf

public boolean isLeaf()
Returns true if the Element has no children, otherwise returns false.
Specified by:
isLeaf in interface Element

getXMLElement

public XMLElement getXMLElement()
Returns the XMLElement that this wrapper wraps.

toString

public String toString()
Overrides:
toString in class Object