sk.uniba.euromath.document.interval
Class DOMInterval

java.lang.Object
  extended by sk.uniba.euromath.document.interval.DOMInterval
All Implemented Interfaces:
Comparable<DOMInterval>

public final class DOMInterval
extends Object
implements Comparable<DOMInterval>

A simple selection implementation. Selects all nodes between two DOM pointers. References live instance of the document - the interval may become invalid when the document is changed. Immutable.

DOMInterval A is less (greater) than DOMInterval B if and only if: A.from is less (greater) than B.from, or A.to is less (greater) than B.to (if A.from.equals(B.from)).

Two DOMIntervals are equal when they cover the same contents: the same nodes and same amount of text. This does not however imply that first interval's from pointer is equal to the second interval's from.

The object's compareTo(DOMInterval) operates on DomPointers and is thus inconsistent with equals(Object).

Author:
Martin Vysny

Field Summary
 DomPointer from
          Select nodes from this pointer.
 DomPointer to
          Select nodes up to this pointer.
 
Constructor Summary
DOMInterval(DomPointer from, DomPointer to, DomCore doc)
          Constructor.
 
Method Summary
 int compareTo(DOMInterval o)
           
 boolean contains(DOMInterval other)
          Checks if given interval is fully covered by this interval.
 boolean contains(DomPointer pointer)
          Checks if given pointer points into contents of this interval.
 boolean contains(Node node)
          Checks if this interval contains given node (or a part of given node).
 boolean endsOnNodeBoundary()
          Checks if the interval fully covers the last node.
 boolean endsWithText()
          Checks if last node is a text node.
 boolean equals(Object obj)
           
 DocumentFragment extractContents(boolean replaceEntities)
           Extracts nodes from given interval and copies them into a document fragment.
 List<Attr> getAttributes(boolean descendants)
          Returns all attributes from all elements from the content node list.
 Set<String> getContentIds()
          Returns set of identifiers for all content nodes.
 List<Node> getContents()
           Retrieves contents of this interval.
 int getEndingIndex()
          Checks if last node belongs only partially to the interval.
 DOMInterval getIntersection(DOMInterval other)
          Checks if two intervals shares at least one common element / common letters from same text/cdata nodes, and returns interval containing this intersection.
 List<IDSelectionAction> getSelectionActions(boolean select)
          Converts this interval into a list of selection actions.
 int getStartingIndex()
          Checks if first node belongs only partially to the interval.
 String getTextContents()
          Returns contents of all textual nodes contained in the contents.
 int hashCode()
           
 boolean isAdjacentTo(DOMInterval other)
          Checks if two intervals do not overlap and union(DOMInterval) would return a valid interval.
 boolean isDeletable()
          Checks if nodes between given interval can be safely cut out.
 boolean isEmpty()
          Checks if this interval contains any content nodes.
 DOMInterval skipFromEntities()
          For both interval ends tries to skip out from entities.
 boolean startsOnNodeBoundary()
          Checks if the interval fully covers the first node.
 boolean startsWithText()
          Checks if first interval node is a text node.
 String toString()
           
 DOMInterval union(DOMInterval other)
          Computes and returns union of two intervals if the result would still be a single interval.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

from

public final DomPointer from
Select nodes from this pointer. Must not be null.


to

public final DomPointer to
Select nodes up to this pointer. Must not be null.

Constructor Detail

DOMInterval

DOMInterval(DomPointer from,
            DomPointer to,
            DomCore doc)
Constructor. Not intended to be called by clients - please use the factory, obtainable via the XMLAccess.getIntervalFactory() method.

Parameters:
from - select nodes from this pointer. Must not be null.
to - select nodes up to this pointer. Must not be null.
doc - owner document.
Method Detail

getContents

public List<Node> getContents()

Retrieves contents of this interval.

If the from and/or to points into a text/cdata node(s), these nodes are included in the returned list. However, elements are listed only when all child nodes fully belong to the interval, and from neither to descends from the element. Attribute nodes are not part of the document tree and are not listed in the result list.

If an element is listed in the list, none of its children and/or descendants are listed.

Nodes are sorted in the document ordering (parent first, then its children).

Nodes being returned may not have same parent. If you need this functionality, please see the extractContents(boolean) method.

Returns:
list of nodes that belongs to this interval. Never null. May be empty. Result is cached.

isDeletable

public boolean isDeletable()
Checks if nodes between given interval can be safely cut out.

Returns:
true if both pointers point outside an entity.

skipFromEntities

public DOMInterval skipFromEntities()
For both interval ends tries to skip out from entities. If succesfull, the result may become deletable. This method does not modify the contents of this interval.

Returns:
new interval, never null, may return itself.

isEmpty

public boolean isEmpty()
Checks if this interval contains any content nodes.

Returns:
true if this interval is empty, false if getContents() will return non-empty list.

getTextContents

public String getTextContents()
Returns contents of all textual nodes contained in the contents.

Returns:
textual value of all nodes from the getContents() method, or their descendants. Never null, may be empty. Result is cached.

getStartingIndex

public int getStartingIndex()
Checks if first node belongs only partially to the interval. This happens when the from pointer points into a textual node.

Returns:
index of first character that belongs to the interval, or -1 if the pointer does not point into a text node.

startsWithText

public boolean startsWithText()
Checks if first interval node is a text node.

Returns:
false if getStartingIndex() would return -1, true otherwise.

startsOnNodeBoundary

public boolean startsOnNodeBoundary()
Checks if the interval fully covers the first node.

Returns:
true if first node is fully covered by the interval, false otherwise.

getEndingIndex

public int getEndingIndex()
Checks if last node belongs only partially to the interval. This happens when the to pointer points into a textual node.

Returns:
index of first character that does not belong to the interval, or -1 if the pointer does not point into a text node.

endsOnNodeBoundary

public boolean endsOnNodeBoundary()
Checks if the interval fully covers the last node.

Returns:
true if last node is fully covered by the interval, false otherwise.

endsWithText

public boolean endsWithText()
Checks if last node is a text node.

Returns:
false if getEndingIndex() would return -1, true otherwise.

getAttributes

public List<Attr> getAttributes(boolean descendants)
Returns all attributes from all elements from the content node list.

Parameters:
descendants - if true then all elements (descendants of the content nodes) are considered.
Returns:
list of attributes belonging to nodes from given interval. Never null, may be empty. Result is cached.

extractContents

public DocumentFragment extractContents(boolean replaceEntities)

Extracts nodes from given interval and copies them into a document fragment. Original nodes are left intact. The structure of the original tree is kept, thus the fragment may contain nodes not listed in the interval contents.

You may get identical result if you follow the following algorithm:

Note: the result is not cached. Subsequent calls to this method can degrade the application performance.

Parameters:
replaceEntities - if true then EntityReference nodes are replaced by their contents. If isDeletable() returns false then this parameter must be true.
Returns:
a document fragment containing the selection structure. Never null. Will contain no nodes if the interval is empty.

equals

public boolean equals(Object obj)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object

compareTo

public int compareTo(DOMInterval o)
Specified by:
compareTo in interface Comparable<DOMInterval>

getIntersection

public DOMInterval getIntersection(DOMInterval other)
Checks if two intervals shares at least one common element / common letters from same text/cdata nodes, and returns interval containing this intersection.

Parameters:
other - the other interval to check.
Returns:
an interval instance containing all common nodes from both intervals, null otherwise. Will never return empty interval.

getContentIds

public Set<String> getContentIds()
Returns set of identifiers for all content nodes.

Returns:
unmodifiable set of string ids.

contains

public boolean contains(DOMInterval other)
Checks if given interval is fully covered by this interval.

Parameters:
other - the other interval
Returns:
true if given interval does not contain other content than the one contained in this interval.

union

public DOMInterval union(DOMInterval other)
Computes and returns union of two intervals if the result would still be a single interval.

Parameters:
other - the other interval.
Returns:
interval that covers both intervals, or null if such an interval would cover content not present in both intervals.

isAdjacentTo

public boolean isAdjacentTo(DOMInterval other)
Checks if two intervals do not overlap and union(DOMInterval) would return a valid interval.

Parameters:
other - the other interval.
Returns:
true if two intervals are adjacent, false otherwise.

contains

public boolean contains(Node node)
Checks if this interval contains given node (or a part of given node).

Parameters:
node - node to check.
Returns:
true if this interval covers (partially at least) given node.

contains

public boolean contains(DomPointer pointer)
Checks if given pointer points into contents of this interval.

Parameters:
pointer - pointer to check
Returns:
true if given pointer lies inbetween from and to.

getSelectionActions

public List<IDSelectionAction> getSelectionActions(boolean select)
Converts this interval into a list of selection actions.

Parameters:
select - the value of the IDSelectionAction.select attribute for all returned instances.
Returns:
list of selection actions.


Copyright 2003-2003-2006 null. All Rights Reserved.