Project JXTA

net.jxta.impl.document
Class LiteXMLElement

java.lang.Object
  |
  +--net.jxta.impl.document.TextElementCommon
        |
        +--net.jxta.impl.document.LiteXMLElement
Direct Known Subclasses:
LiteXMLDocument

public class LiteXMLElement
extends TextElementCommon
implements net.jxta.document.Attributable

An element of a StructuredDocument. StructuredDocuments are made up of hierarchies of elements. LiteXMLElement is part of an implementation while makes use of XML-style document conventions, but without the overhead of a full parser.

Since:
Jxta 0.1

Inner Class Summary
protected static class LiteXMLElement.charRange
          A charRange defines a range of characters, probably within a string.
protected static class LiteXMLElement.tagRange
          A tagRange is a collection of char ranges useful for describing XML structures.
 
Field Summary
protected  LiteXMLDocument doc
          The document associated with this Element.
protected  LiteXMLElement.tagRange loc
          The portion of the source XML associated with this node
protected  net.jxta.document.Element parent
          Identifies the element which is the parent of this element.
 
Constructor Summary
protected LiteXMLElement(LiteXMLDocument doc, LiteXMLElement.tagRange loc)
          Creates new LiteXMLElement
protected LiteXMLElement(LiteXMLDocument doc, java.lang.String name)
          Creates new LiteElement
protected LiteXMLElement(LiteXMLDocument doc, java.lang.String name, java.lang.String val)
          Creates new LiteElement
 
Method Summary
 java.lang.String addAttribute(net.jxta.document.Attribute newAttrib)
          Adds an attribute with the given name and value.
 java.lang.String addAttribute(java.lang.String name, java.lang.String value)
          Adds an attribute with the given name and value.
protected  void addChildTags(LiteXMLElement.charRange scanRange, LiteXMLElement addTo)
          Parse a charRange and add any tags found as content as children of a specified element.
protected  void adjustLocations(int beginningAt, int by)
          For this element and all its children adjust the location of its ranges by the amount specified.
 void appendChild(net.jxta.document.TextElement element)
          Add a child element to this element
protected  java.lang.StringBuffer decodeEscaped(java.lang.StringBuffer target)
          Given a StringBuffer find all occurances of escaped characters which must be decoded and convert them back to their non-escaped equivalents.
protected  void encodeEscaped(java.lang.StringBuffer target)
          Given a StringBuffer find all occurances of characters which must be escaped and convert them to their escaped equivalents.
 boolean equals(java.lang.Object element)
          Tests two elements for equality.
protected  void finalize()
           
 net.jxta.document.Attribute getAttribute(java.lang.String name)
          Returns a single attribute which matches the name provided.
 java.util.Enumeration getAttributes()
          Returns an enumerations of the attributes assosicated with this object.
 java.util.Enumeration getChildren()
          Returns an enumeration of the immediate children of this element
 java.util.Enumeration getChildren(java.lang.String name)
          Returns an enumeration of the immediate children of this element whose name match the specified string.
 java.lang.String getName()
          Returns the name associated with this element.
 net.jxta.document.Element getParent()
          Get the parent of this element.
 net.jxta.document.StructuredDocument getRoot()
          Get the root element of the hierarchy this element belongs to.
protected  LiteXMLElement.tagRange getTagRanges(java.lang.String source, java.lang.String tag, LiteXMLElement.charRange range)
          Given a source string, an optional tag and a range with in the source find either the tag specified or the next tag.
 java.lang.String getTextValue()
          Get the value (if any) associated with an element.
protected  java.lang.String getTextValue(boolean getEncoded)
          Get the value (if any) associated with an element.
protected  void printNice(java.io.Writer into, int indent, boolean recurse)
          Write the contents of this element and optionally its children.
 
Methods inherited from class net.jxta.impl.document.TextElementCommon
appendChild, getChildren, getKey, getValue
 
Methods inherited from class java.lang.Object
clone, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

doc

protected LiteXMLDocument doc
The document associated with this Element.

parent

protected net.jxta.document.Element parent
Identifies the element which is the parent of this element. If this.parent == this then this element is the root of the document. If null == parent then this element has not yet been inserted into the document.

loc

protected LiteXMLElement.tagRange loc
The portion of the source XML associated with this node
Constructor Detail

LiteXMLElement

protected LiteXMLElement(LiteXMLDocument doc,
                         java.lang.String name)
Creates new LiteElement
Parameters:
doc - The LiteXMLDocument which is associated with this element.
name - The name of the element being created.

LiteXMLElement

protected LiteXMLElement(LiteXMLDocument doc,
                         java.lang.String name,
                         java.lang.String val)
Creates new LiteElement
Parameters:
doc - The LiteXMLDocument which is associated with this element.
name - The name of the element being created.
val - The value of the element being created.

LiteXMLElement

protected LiteXMLElement(LiteXMLDocument doc,
                         LiteXMLElement.tagRange loc)
Creates new LiteXMLElement
Parameters:
loc - The location of the element within the document.
doc - The LiteXMLDocument which is associated with this element.
Method Detail

equals

public boolean equals(java.lang.Object element)
Tests two elements for equality.
Overrides:
equals in class java.lang.Object
Parameters:
element - the element to be compared against.
Returns:
true if the elements are equal (by whatever definition of equality is appropriate) otherwise false

finalize

protected void finalize()
                 throws java.lang.Throwable
Overrides:
finalize in class java.lang.Object

getRoot

public net.jxta.document.StructuredDocument getRoot()
Get the root element of the hierarchy this element belongs to.
Returns:
StructuredDocument root of this element's hierarchy.
Since:
JXTA 1.0

getParent

public net.jxta.document.Element getParent()
Get the parent of this element. If the element has not been inserted into the Document then null is returned. If this element is the root of the Document then it returns itself.
Returns:
Element parent of this element
Since:
JXTA 1.0

getChildren

public java.util.Enumeration getChildren()
Returns an enumeration of the immediate children of this element
Returns:
Enumeration An enumeration containing all of the children of this element.
Since:
JXTA 1.0

getName

public java.lang.String getName()
Returns the name associated with this element.
Returns:
the name associated with this Element

getTextValue

public java.lang.String getTextValue()
Get the value (if any) associated with an element.
Returns:
A string containing the value of this element, if any, otherwise null.

appendChild

public void appendChild(net.jxta.document.TextElement element)
Add a child element to this element
Parameters:
element - the element to be added as a child

getChildren

public java.util.Enumeration getChildren(java.lang.String name)
Returns an enumeration of the immediate children of this element whose name match the specified string.
Parameters:
name - The name which will be matched against.
Returns:
An enumeration containing all of the children of this element.

getTextValue

protected java.lang.String getTextValue(boolean getEncoded)
Get the value (if any) associated with an element.
Parameters:
getEncoded - if true then the contents will be encoded such that the contents will not be interpreted as XML. see ie. < -> < & -> &
Returns:
A string containing the value of this element, if any, otherwise null.

printNice

protected void printNice(java.io.Writer into,
                         int indent,
                         boolean recurse)
Write the contents of this element and optionally its children. The writing is done to a provided java.io.Writer. The writing can optionally be indented
Parameters:
into - The java.io.Writer that the output will be sent to.
indent - the number of tabs which will be inserted before each line.
recurse - if true then also print the children of this element.

getTagRanges

protected LiteXMLElement.tagRange getTagRanges(java.lang.String source,
                                               java.lang.String tag,
                                               LiteXMLElement.charRange range)
Given a source string, an optional tag and a range with in the source find either the tag specified or the next tag. The search consists of 4 phases : 0. If no tag was specified, determine if a tag can be found and learn its name. 1. Search for the start of the named tag. 2. Search for the end tag. Each time we think we have found a tag which might be the end tag we make sure it is not the end tag of another element with the same name as our tag. 3. Calculate the position of the body of the tag given the locations of the start and end.
Parameters:
source - the string to search
tag - the tag to search for in the source string. If this tag is empty or null then we will search for the next tag.
range - describes the range of character locations in the source string to which the search will be limited.
Returns:
tagRange containing the ranges of the found tag.

addChildTags

protected void addChildTags(LiteXMLElement.charRange scanRange,
                            LiteXMLElement addTo)
Parse a charRange and add any tags found as content as children of a specified element. This process is repeated recursivly.
Parameters:
scanRange - the range to be parsed for sub-tags
addTo - the element to add any discovered children to.

adjustLocations

protected void adjustLocations(int beginningAt,
                               int by)
For this element and all its children adjust the location of its ranges by the amount specified.
Parameters:
beginningAt - adjust all locations which are at or past this location.
by - amount to adjust all matching locations.

decodeEscaped

protected java.lang.StringBuffer decodeEscaped(java.lang.StringBuffer target)
Given a StringBuffer find all occurances of escaped characters which must be decoded and convert them back to their non-escaped equivalents.
Parameters:
target - The stringbuffer which will be decoded.
Returns:
The decoded version of the stringbuffer.

encodeEscaped

protected void encodeEscaped(java.lang.StringBuffer target)
Given a StringBuffer find all occurances of characters which must be escaped and convert them to their escaped equivalents.
Parameters:
target - The stringbuffer which will be encoded in place.

getAttributes

public java.util.Enumeration getAttributes()
Returns an enumerations of the attributes assosicated with this object. Each element is of type Attribute.
Specified by:
getAttributes in interface net.jxta.document.Attributable
Returns:
Enumeration the attributes associated with this object.

addAttribute

public java.lang.String addAttribute(java.lang.String name,
                                     java.lang.String value)
Adds an attribute with the given name and value. Some implementations may support only a single value for each distinct name. Others may support multiple values for each name. If the value being provided replaces some other value then that value is returned otherwise null is returned.
Specified by:
addAttribute in interface net.jxta.document.Attributable
Parameters:
name - name of the attribute.
value - value for the attribute.
Returns:
String containing previous value for this name if the value is being replaced otherwise null.

addAttribute

public java.lang.String addAttribute(net.jxta.document.Attribute newAttrib)
Adds an attribute with the given name and value. Some implementations may support only a single value for each distinct name. Others may support multiple values for each name. If the value being provided replaces some other value then that value is returned otherwise null is returned.
Specified by:
addAttribute in interface net.jxta.document.Attributable
Parameters:
newAttrib - new attribute.
Returns:
String containing previous value for this name if the value is being replaced otherwise null.

getAttribute

public net.jxta.document.Attribute getAttribute(java.lang.String name)
Returns a single attribute which matches the name provided. If no such named attribute exists then null is returned. For impelementations of this interface which support multiple values for each name only the first value will be returned. To access all values for a name you must use getAttributes.
Specified by:
getAttribute in interface net.jxta.document.Attributable
Returns:
Attribute the attributes matching the given name.

Project JXTA