Package org.javarosa.core.model
Class FormIndex
java.lang.Object
org.javarosa.core.model.FormIndex
- All Implemented Interfaces:
Serializable
public class FormIndex extends Object implements Serializable
FormIndex is an immutable index which is structured to provide quick access to a specific node in a
FormDef (tree of IFormElement representing a blank form) or a
FormInstance (tree of
TreeElement representing a form instance that can be filled).
It also includes a TreeReference representing the XPath path of that node.
The XML node referred to by a FormIndex can correspond to either a question or a group. The index is constructed as a
linked list of levels.
A FormIndex can refer to:
- a node at the top level of the form (only localIndex is used)
- a node nested in a group hierarchy (localIndex and nextLevel are used)
- a node nested in a repeat (localIndex, nextLevel and instanceIndex are all used)
When a FormIndex list refers to a nested node, that root FormIndex represents the path root and links to subsequent
levels using the nextLevel field. The @{link #getReference} method returns the reference of the FormIndex at
the end of the list.
Consider the following instance from a blank form with friends as a repeat group and school_info as a
non-repeat group:
<instance> <data id="my_form"> <address/> <dob/> <school_info> <favorite_class/> <favorite_teacher/> </school_info> <friends> <firstname/> </friends> </data> </instance> </pre>Thedobquestion's path is/data/dob. The index that refers to it has alocalIndexof 1. Thefriendsgroup's path is/data/friends. The index that refers to it has alocalIndexof 3. Thefavorite_teacherquestion's path is/data/school_info/favorite_teacher. The index that refers to it has alocalIndexof 2 which isschool_info's index at the form root. It also has anextLevelset which itself has alocalIndexof 1 because it is the second node in the school_info group. Thefirstnamequestion's path is/data/friends[N]/firstnamewhere @{N} is a 0-based index representing which of potentially severalfriendsrepeat group instances'firstnameis desired (also referred to as multiplicity). Sincefriendsis a repeat group, the filled instance can contain several instances of thefriendsnode. The index that refers to it always starts with alocalIndexof 3 which isfriends's index at the form root. It also has aninstanceIndexwhich represents which of the potentially severalfriendsto use. To index afirstnamenode, anextLevelis added which always has a @{code localIndex} of 0. To go from aFormIndexto a form element in a blank form,FormDef.getChild(FormIndex)can be used. To go from aFormIndexto a node in a filled form,DataInstance.resolveReference(TreeReference)can be used along withgetReference(). No circularity is allowed. That is, noFormIndex's ancestor can be itself. Datatype Productions: FormIndex = BOF | EOF | CompoundIndex(nextIndex:FormIndex,Location) Location = Empty | Simple(localLevel:int) | WithMult(localLevel:int, multiplicity:int)
- Author:
- Clayton Sims
- See Also:
- Serialized Form
-
Constructor Summary
Constructors Constructor Description FormIndex(int localIndex, int instanceIndex, TreeReference reference)Constructs a @{code FormIndex} for a node that does not have any children but that may have repeat instances in a filled form.FormIndex(int localIndex, TreeReference reference)Constructs a @{code FormIndex} for a node that does not have any children.FormIndex(FormIndex nextLevel, int localIndex, int instanceIndex, TreeReference reference)Constructs a @{code FormIndex} for a node that has children and that may have repeat instances in a filled form.FormIndex(FormIndex nextLevel, int localIndex, TreeReference reference)Constructs a @{code FormIndex} for a node that has children.FormIndex(FormIndex nextLevel, FormIndex currentLevel)Constructs an index which references an element past the level of specificity of the current context, founded by the currentLevel index. -
Method Summary
Modifier and Type Method Description voidassignRefs(FormDef f)intcompareTo(Object o)static FormIndexcreateBeginningOfFormIndex()Returns an index before the start of the formstatic FormIndexcreateEndOfFormIndex()Returns an index after the end of the formFormIndexdiff(FormIndex subIndex)Takes in a form index which is a subset of this index, and returns the total difference between them.booleanequals(Object o)intgetDepth()intgetElementMultiplicity()For the fully qualified element, get the multiplicity of the element's referenceintgetInstanceIndex()intgetLocalIndex()TreeReferencegetLocalReference()FormIndexgetNextLevel()TreeReferencegetReference()FormIndexgetTerminal()inthashCode()booleanisBeginningOfFormIndex()booleanisEndOfFormIndex()booleanisInForm()static booleanisSubElement(FormIndex parent, FormIndex child)static booleanisSubIndex(FormIndex parent, FormIndex child)booleanisTerminal()Identifies whether this is a terminal index, in other words whether this index references with more specificity than the current contextFormIndexsnip()StringtoString()static FormIndextrimNegativeIndices(FormIndex index)Trims any negative indices from the end of the passed in index.
-
Constructor Details
-
FormIndex
Constructs a @{code FormIndex} for a node that does not have any children.- Parameters:
localIndex- An integer index into a flat list of elementsreference- A reference to the instance element identified by this index
-
FormIndex
Constructs a @{code FormIndex} for a node that does not have any children but that may have repeat instances in a filled form. TheinstanceIndexidentifies which of these instances should be referred to.- Parameters:
localIndex- An integer index into a flat list of elementsinstanceIndex- An integer index expressing the multiplicity of the current levelreference- A reference to the instance element identified by this index
-
FormIndex
Constructs a @{code FormIndex} for a node that has children.- Parameters:
nextLevel- An index into the referenced element's indexlocalIndex- An index to an element at the current level, a child element of which will be referenced by the nextLevel index.reference- A reference to the instance element identified by this index;
-
FormIndex
Constructs a @{code FormIndex} for a node that has children and that may have repeat instances in a filled form.- Parameters:
nextLevel- An index into the referenced element's indexlocalIndex- An index to an element at the current level, a child element of which will be referenced by the nextLevel index.instanceIndex- How many times the element referenced has been repeated.reference- A reference to the instance element identified by this index;
-
FormIndex
Constructs an index which references an element past the level of specificity of the current context, founded by the currentLevel index. (currentLevel, (nextLevel...))
-
-
Method Details
-
createBeginningOfFormIndex
Returns an index before the start of the form -
createEndOfFormIndex
Returns an index after the end of the form -
isInForm
public boolean isInForm()- Returns:
- true if the index is neither before the start or after the end of the form
-
getLocalIndex
public int getLocalIndex()- Returns:
- The index of the element in the current context
-
getInstanceIndex
public int getInstanceIndex()- Returns:
- The multiplicity of the current instance of a repeated question or group
-
getElementMultiplicity
public int getElementMultiplicity()For the fully qualified element, get the multiplicity of the element's reference- Returns:
- The terminal element (fully qualified)'s instance index
-
getNextLevel
- Returns:
- An index into the next level of specificity past the current context. An example would be an index into an element that is a child of the element referenced by the local index.
-
getLocalReference
-
getReference
- Returns:
- The TreeReference of the fully qualified element described by this FormIndex.
-
getTerminal
-
isTerminal
public boolean isTerminal()Identifies whether this is a terminal index, in other words whether this index references with more specificity than the current context -
isEndOfFormIndex
public boolean isEndOfFormIndex()- Returns:
- true if we are after the end of the form
-
isBeginningOfFormIndex
public boolean isBeginningOfFormIndex()- Returns:
- true if we are before the start of the form
-
equals
-
hashCode
public int hashCode() -
compareTo
-
snip
- Returns:
- Only the local component of this Form Index.
-
diff
Takes in a form index which is a subset of this index, and returns the total difference between them. This is useful for stepping up the level of index specificty. If the subIndex is not a valid subIndex of this index, null is returned. Since the FormIndex represented by null is always a subset, if null is passed in as a subIndex, the full index is returned For example: Indices a = 1_0,2,1,3 b = 1,3 a.diff(b) = 1_0,2- Parameters:
subIndex-- Returns:
-
toString
-
getDepth
public int getDepth()- Returns:
- the level of this index relative to the top level of the form
-
trimNegativeIndices
Trims any negative indices from the end of the passed in index.- Parameters:
index-- Returns:
-
isSubIndex
-
isSubElement
-
assignRefs
-