Class SMInputCursor

java.lang.Object
org.codehaus.staxmate.in.CursorBase
org.codehaus.staxmate.in.SMInputCursor
Direct Known Subclasses:
SMFlatteningCursor, SMHierarchicCursor

public abstract class SMInputCursor extends CursorBase
Base class for reader-side cursors that form the main input side abstraction offered by StaxMate. This class offers the main API for applications to use: sub-classes generally just implement abstract methods or override default behavior, but do not offer new public methods. As a result, up-casts are usually not necessary.

Implementation Note: as cursors are thin wrappers around XMLStreamReader2, and since not all Stax implementations implement XMLStreamReader2, some wrapping may be needed to expose basic Stax 1.0 XMLStreamReaders as Stax2 XMLStreamReader2 readers. But without native support, not all stax2 features may be available for such stream readers. This should usuall not be a problem with cursor functionality, as cursors will try to limit their usage to known working subset, but care must be taken if application code wants to directly access underlying stream reader.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    Different tracking behaviors available for cursors.

    Nested classes/interfaces inherited from class org.codehaus.staxmate.in.CursorBase

    CursorBase.State
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected Object
    Non-typesafe payload data that applications can use, to pass an extra argument along with cursors.
    Optional factory instance that is used to create element info objects if element tracking is enabled.
    Whether element information is to be tracked or not, and if it is, how much of it will be stored.
    protected SMFilter
    Optional filter object that can be used to filter out events of types caller is not interested in.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    private final void
    This method can be called to ensure that this cursor gets to point to END_ELEMENT that closes typed element that has been read; usually this works as expected, but during type conversion exceptions handling may not work as expected
    protected void
    Method to call to notify that textual contents of this cursor have been read; typically when a call has been made that will traverse contents and make stream point to matching END_ELEMENT.
    private final void
    Helper method called by getElemXxxValue methods to ensure that the state is appropriate for the call
    Method that does what getNext() does, but instead of returning resulting event type, returns this cursor.
    private static void
    appendPathDesc(StringBuilder sb, SMElementInfo info, boolean recursive)
     
    Method for accessing information regarding event this cursor points to, as an instance of XMLEvent.
    Method that will create a new nested cursor for iterating over all (immediate) child nodes of the start element this cursor currently points to.
    Method that will create a new nested cursor for iterating over all (immediate) child nodes of the start element this cursor currently points to that are passed by the specified filter.
    Convenience method; equivalent to childCursor(SMFilterFactory.getElementOnlyFilter());
    childElementCursor(String elemLocalName)
    Convenience method; equivalent to childCursor(SMFilterFactory.getElementOnlyFilter(elemName)); Will only return START_ELEMENT and END_ELEMENT events, whose element's local name matches given local name, and that does not belong to a namespace.
    Convenience method; equivalent to childCursor(SMFilterFactory.getElementOnlyFilter(elemName)); Will only return START_ELEMENT and END_ELEMENT events, whose element name matches given qname.
    Convenience method; equivalent to childCursor(SMFilterFactory.getMixedFilter());
    final String
    Same as calling collectDescendantText(boolean) with 'false': that is, do not include ignorable white space (as determined by DTD or Schema) in the result text.
    collectDescendantText(boolean includeIgnorable)
    Method that can collect all text contained within START_ELEMENT currently pointed by this cursor.
    protected abstract SMInputCursor
    Abstract method that concrete sub-classes implement, and is used for all instantiation of child cursors by this cursor instance.
    protected abstract SMInputCursor
    Abstract method that concrete sub-classes implement, and is used for all instantiation of descendant cursors by this cursor instance.
    protected SMElementInfo
    Method cursor calls when it needs to track element state information; if so, it calls this method to take a snapshot of the element.
    Method for constructing stream exception with given message, and location that matches that of the underlying stream regardless of whether this cursor is valid (that is, will indicate location of the stream which may differ from where this cursor was last valid)
    Method that will create a new nested cursor for iterating over all the descendant (children and grandchildren) nodes of the start element this cursor currently points to.
    Method that will create a new nested cursor for iterating over all the descendant (children and grandchildren) nodes of the start element this cursor currently points to that are accepted by the specified filter.
    Convenience method; equivalent to descendantCursor(SMFilterFactory.getElementOnlyFilter());
    Convenience method; equivalent to descendantCursor(SMFilterFactory.getElementOnlyFilter(elemLocalName));.
    Convenience method; equivalent to descendantCursor(SMFilterFactory.getElementOnlyFilter(elemName)); Will only return START_ELEMENT and END_ELEMENT events, whose element name matches given qname.
    Convenience method; equivalent to descendantCursor(SMFilterFactory.getMixedFilter());
    int
    findAttrIndex(String uri, String localName)
    Method that can be called when this cursor points to START_ELEMENT, and which will return index of specified attribute, if it exists for this element.
    byte[]
    getAttrBinaryValue(int index)
    Method for accessing value of a base64-encoded attribute, using default base64 encoding variant (as per Base64Variants.getDefaultVariant().
    byte[]
    getAttrBinaryValue(int index, org.codehaus.stax2.typed.Base64Variant variant)
    Method for accessing value of a base64-encoded attribute, using specified base64 encoding variant.
    boolean
    Method for accessing value of specified attribute as boolean.
    boolean
    getAttrBooleanValue(int index, boolean defValue)
    Method for accessing value of specified attribute as boolean.
    int
    Method that can be called when this cursor points to START_ELEMENT, and which will return number of attributes with values for the start element.
    double
    getAttrDoubleValue(int index)
    Method for accessing value of specified attribute as double.
    double
    getAttrDoubleValue(int index, double defValue)
    Method for accessing value of specified attribute as double.
    <T extends Enum<T>>
    T
    getAttrEnumValue(int index, Class<T> enumType)
    Method for accessing value of specified attribute as an Enum value of specified type, if content non-empty.
    int
    getAttrIntValue(int index)
    Method for accessing value of specified attribute as integer.
    int
    getAttrIntValue(int index, int defValue)
    Method for accessing value of specified attribute as integer.
    int
    getAttrIntValue(String uri, String localName)
    Deprecated.
    int
    getAttrIntValue(String uri, String localName, int defValue)
    getAttrLocalName(int index)
    Method that can be called when this cursor points to START_ELEMENT, and returns local name of the attribute at specified index.
    long
    getAttrLongValue(int index)
    Method for accessing value of specified attribute as long.
    long
    getAttrLongValue(int index, long defValue)
    Method for accessing value of specified attribute as long.
    getAttrName(int index)
    Method that can be called when this cursor points to START_ELEMENT, and returns fully qualified name of the attribute at specified index.
    getAttrNsUri(int index)
    Method that can be called when this cursor points to START_ELEMENT, and returns namespace URI of the attribute at specified index (non-empty String if it has one, and empty String if attribute does not belong to a namespace) Index has to be between [0, getAttrCount()[; otherwise IllegalArgumentException will be thrown.
    getAttrPrefix(int index)
    Method that can be called when this cursor points to START_ELEMENT, and returns namespace prefix of the attribute at specified index (if it has any), or empty String if attribute has no prefix (does not belong to a namespace).
    getAttrValue(int index)
    Method that can be called when this cursor points to START_ELEMENT, and returns unmodified textual value of the attribute at specified index (non-empty String if it has one, and empty String if attribute does not belong to a namespace) Index has to be between [0, getAttrCount()[; otherwise IllegalArgumentException will be thrown.
    getAttrValue(String localName)
    Convenience accessor method to access an attribute that is not in a namespace (has no prefix).
    getAttrValue(String namespaceURI, String localName)
    Method that can be called when this cursor points to START_ELEMENT, and returns unmodified textual value of the specified attribute (if element has it), or null if element has no value for such attribute.
    Returns the type of event this cursor either currently points to (if in valid state), or pointed to (if ever iterated forward), or null if just created.
    int
    Convenience method for accessing type of the current event (as would be returned by getCurrEvent()) as one of event types defined in XMLStreamConstants (like XMLStreamConstants.START_ELEMENT).
    protected String
     
    Method to access starting Location of event (as defined by Stax specification) that this cursor points to.
    Method for accessing application-provided data set previously by a setData(java.lang.Object) call.
    byte[]
    Method for accessing value current element, which isbase64-encoded binary data, by decoding contents using the default base64 variant
    byte[]
    getElemBinaryValue(org.codehaus.stax2.typed.Base64Variant variant)
    Method for accessing value current element, which isbase64-encoded binary data, by decoding contents using specified variant.
    boolean
    Method that can collect text directly contained within START_ELEMENT currently pointed by this cursor and convert it to a boolean value.
    boolean
    getElemBooleanValue(boolean defValue)
    Similar to getElemBooleanValue(), but instead of failing on invalid value, returns given default value.
    double
    Method that can collect text directly contained within START_ELEMENT currently pointed by this cursor and convert it to a double value.
    double
    getElemDoubleValue(double defValue)
    Similar to getElemDoubleValue(), but instead of failing on invalid value, returns given default value.
    int
    Method to access number of start elements cursor has traversed (including ones that were filtered out, if any).
     
     
    <T extends Enum<T>>
    T
    getElemEnumValue(Class<T> enumType)
    Method that can collect text directly contained within START_ELEMENT currently pointed by this cursor and convert it to one of enumerated values of given type, if textual value non-type, and otherwise to null.
    int
    Method that can collect text directly contained within START_ELEMENT currently pointed by this cursor and convert it to a int value.
    int
    getElemIntValue(int defValue)
    Similar to getElemIntValue(), but instead of failing on invalid value, returns given default value.
    long
    Method that can collect text directly contained within START_ELEMENT currently pointed by this cursor and convert it to a long value.
    long
    getElemLongValue(long defValue)
    Similar to getElemLongValue(), but instead of failing on invalid value, returns given default value.
    Method that can collect text directly contained within START_ELEMENT currently pointed by this cursor.
    For events with fully qualified names (START_ELEMENT, END_ELEMENT, ATTRIBUTE, NAMESPACE) returns the local component of the full name; for events with only non-qualified name (PROCESSING_INSTRUCTION, entity and notation declarations, references) returns the name, and for other events, returns null.
    Deprecated. 
    abstract SMEvent
    Main iterating method.
    int
    Method to access number of nodes cursor has traversed (including ones that were filtered out, if any).
    Method for accessing namespace URI of the START_ELEMENT this cursor points to.
    abstract int
    Number of parent elements that the token/event cursor points to has, if it points to one.
     
    Method that generates developer-readable description of the logical path of the event this cursor points to, assuming that element tracking is enabled.
    Method for accessing namespace prefix of the START_ELEMENT this cursor points to.
    Returns a String representation of either the fully-qualified name (if the event has full name) or the local name (if event does not have full name but has local name); or if no name available, throws stream exception.
     
    Method to access Location that the underlying stream reader points to.
    final org.codehaus.stax2.XMLStreamReader2
    Method that can be used to get direct access to the underlying stream reader.
    Method that can be used when this cursor points to a textual event; something for which XMLStreamReader.getText() can be called.
     
    boolean
    Method for verifying whether current named event (one for which getLocalName() can be called) has the specified local name or not.
    boolean
    hasName(String expNsURI, String expLN)
    Method for verifying whether current named event (one for which getLocalName() can be called) has the specified fully-qualified name or not.
    boolean
    hasName(QName qname)
    Equivalent to calling hasName(String, String) with namespace URI and local name contained in the argument QName
    final boolean
    Method for determining whether this cursor iterates over root level of the underlying stream reader
    void
    processDescendantText(Writer w, boolean includeIgnorable)
    Method similar to collectDescendantText(boolean), but will write the text to specified Writer instead of collecting it into a String.
    final boolean
    Method that can be used to check whether this cursor is currently valid; that is, it is the cursor that points to the event underlying stream is at.
    void
    Method for assigning per-cursor application-managed data, readable using getData().
    final void
    Set element info factory used for constructing SMElementInfo instances during traversal for this cursor, as well as all of its children.
    final void
    Changes tracking mode of this cursor to the new specified mode.
    final void
    Method for setting filter used for selecting which events are to be returned to the caller when getNext() is called.
    void
    Method for constructing and throwing stream exception with given message.
    Overridden implementation will just display description of the event this cursor points to (or last pointed to, if not valid)

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • mFilter

      protected SMFilter mFilter
      Optional filter object that can be used to filter out events of types caller is not interested in.
    • mElemTracking

      protected SMInputCursor.Tracking mElemTracking
      Whether element information is to be tracked or not, and if it is, how much of it will be stored. See SMInputCursor.Tracking for details.
    • mElemInfoFactory

      protected ElementInfoFactory mElemInfoFactory
      Optional factory instance that is used to create element info objects if element tracking is enabled. If null, will use default generation mechanism, implemented by SMInputCursor itself.

      Note that by default, this factory will be passed down to child and descendant cursors this cursor creates, so usually one only needs to set the factory of the root cursor.

    • mData

      protected Object mData
      Non-typesafe payload data that applications can use, to pass an extra argument along with cursors. Not used by the framework itself for anything.
  • Constructor Details

  • Method Details

    • setFilter

      public final void setFilter(SMFilter f)
      Method for setting filter used for selecting which events are to be returned to the caller when getNext() is called.
    • setElementTracking

      public final void setElementTracking(SMInputCursor.Tracking tracking)
      Changes tracking mode of this cursor to the new specified mode. Default mode for cursors is the one their parent uses; SMInputCursor.Tracking.NONE for root cursors with no parent.

      See also getPathDesc() for information on how to display tracked path/element information.

    • getElementTracking

      public final SMInputCursor.Tracking getElementTracking()
    • setElementInfoFactory

      public final void setElementInfoFactory(ElementInfoFactory f)
      Set element info factory used for constructing SMElementInfo instances during traversal for this cursor, as well as all of its children.
    • getElementInfoFactory

      public final ElementInfoFactory getElementInfoFactory()
    • getNodeCount

      public int getNodeCount()
      Method to access number of nodes cursor has traversed (including ones that were filtered out, if any). Starts with 0, and is incremented each time underlying stream reader's XMLStreamReader.next() method is called, but not counting child cursors' node counts. Whether END_ELEMENTs (end tags) are included depends on type of cursor: for nested (which do not return events for end tags) they are not counted, but for flattened one (that do return) they are counted as nodes.
      Returns:
      Number of nodes (events) cursor has traversed
    • getElementCount

      public int getElementCount()
      Method to access number of start elements cursor has traversed (including ones that were filtered out, if any). Starts with 0, and is incremented each time underlying stream reader's XMLStreamReader.next() method is called and has moved over a start element, but not counting child cursors' element counts.
      Returns:
      Number of start elements cursor has traversed
    • getParentCount

      public abstract int getParentCount()
      Number of parent elements that the token/event cursor points to has, if it points to one. If not, either most recent valid parent count (if cursor is closed), or the depth that it will have once is is advanced. One practical result is that a nested cursor instance will always have a single constant value it returns, whereas flattening cursors can return different values during traversal. Another thing to notice that matching START_ELEMENT and END_ELEMENT will always correspond to the same parent count.

      For example, here are expected return values for an example XML document:

        <!-- Comment outside tree --> [0]
        <root> [0]
          Text [1]
          <branch> [1]
            Inner text [2]
            <child /> [2]/[2]
          </branch> [1]
        </root> [0]
      
      Numbers in bracket are depths that would be returned when the cursor points to the node.

      Note: depths are different from what many other xml processing APIs (such as Stax and XmlPull)return.

      Returns:
      Number of enclosing nesting levels, ie. number of parent start elements for the node that cursor currently points to (or, in case of initial state, that it will point to if scope has node(s)).
    • getCurrEvent

      public SMEvent getCurrEvent()
      Returns the type of event this cursor either currently points to (if in valid state), or pointed to (if ever iterated forward), or null if just created.
      Returns:
      Type of event this cursor points to, if it currently points to one, or last one it pointed to otherwise (if ever pointed to a valid event), or null if neither.
    • getCurrEventCode

      public int getCurrEventCode()
      Convenience method for accessing type of the current event (as would be returned by getCurrEvent()) as one of event types defined in XMLStreamConstants (like XMLStreamConstants.START_ELEMENT).
    • isRootCursor

      public final boolean isRootCursor()
      Method for determining whether this cursor iterates over root level of the underlying stream reader
      Specified by:
      isRootCursor in class CursorBase
      Returns:
      True if this cursor iterates over root level of the underlying stream reader
    • getTrackedElement

      public SMElementInfo getTrackedElement()
      Returns:
      Information about last "tracked" element; element we have last iterated over when tracking has been enabled.
    • getParentTrackedElement

      public SMElementInfo getParentTrackedElement()
      Returns:
      Information about the tracked element the parent cursor had, if parent cursor existed and was tracking element information.
    • readerAccessible

      public final boolean readerAccessible()
      Method that can be used to check whether this cursor is currently valid; that is, it is the cursor that points to the event underlying stream is at. Only one cursor at any given time is valid in this sense, although other cursors may be made valid by advancing them (and by process invalidating the cursor that was valid at that point). It is also possible that none of cursors is valid at some point: this is the case when formerly valid cursor reached end of its content (END_ELEMENT).
      Returns:
      True if the cursor is currently valid; false if not
    • getStreamReader

      public final org.codehaus.stax2.XMLStreamReader2 getStreamReader()
      Method that can be used to get direct access to the underlying stream reader. Custom sub-classed versions (which can be constructed by overriding this classes factory methods) can choose to block such access, but the default implementation does allow access to it.

      Note that this method should not be needed (or extensively used) for regular StaxMate usage, because direct access to the stream may cause cursor's understanding of stream reader state to be incompatible with its actual state.

      Returns:
      Stream reader the cursor uses for getting XML events
    • getCursorLocation

      public Location getCursorLocation() throws XMLStreamException
      Method to access starting Location of event (as defined by Stax specification) that this cursor points to. Method can only be called if the cursor is valid (as per readerAccessible()); if not, an exception is thrown
      Returns:
      Location of the event this cursor points to
      Throws:
      XMLStreamException
    • getStreamLocation

      public Location getStreamLocation()
      Method to access Location that the underlying stream reader points to.
      Returns:
      Location of the event the underlying stream reader points to (independent of whether this cursor points to that event)
    • getLocation

      @Deprecated public Location getLocation() throws XMLStreamException
      Deprecated.
      Same as calling getCursorLocation()
      Throws:
      XMLStreamException
    • getText

      public String getText() throws XMLStreamException
      Method that can be used when this cursor points to a textual event; something for which XMLStreamReader.getText() can be called. Note that it does not advance the cursor, or combine multiple textual events.
      Returns:
      Textual content of the current event that this cursor points to, if any
      Throws:
      XMLStreamException - if either the underlying parser has problems (possibly including event type not being of textual type, see Stax 1.0 specs for details); or if this cursor does not currently point to an event.
    • collectDescendantText

      public String collectDescendantText(boolean includeIgnorable) throws XMLStreamException
      Method that can collect all text contained within START_ELEMENT currently pointed by this cursor. Collection is done recursively through all descendant text (CHARACTER, CDATA; optionally SPACE) nodes, ignoring nodes of other types. After collecting text, cursor will be positioned at the END_ELEMENT matching initial START_ELEMENT and thus needs to be advanced to access the next sibling event.
      Parameters:
      includeIgnorable - Whether text for events of type SPACE should be ignored in the results or not. If false, SPACE events will be skipped; if true, white space will be included in results.
      Throws:
      XMLStreamException
    • collectDescendantText

      public final String collectDescendantText() throws XMLStreamException
      Same as calling collectDescendantText(boolean) with 'false': that is, do not include ignorable white space (as determined by DTD or Schema) in the result text.

      Note: it is not common to have have ignorable white space; it usually results from indentation, but its detection requires DTD/schema-aware processing

      Throws:
      XMLStreamException
      Since:
      2.0.0
    • processDescendantText

      public void processDescendantText(Writer w, boolean includeIgnorable) throws IOException, XMLStreamException
      Method similar to collectDescendantText(boolean), but will write the text to specified Writer instead of collecting it into a String.
      Parameters:
      w - Writer to use for outputting text found
      includeIgnorable - Whether text for events of type SPACE should be ignored in the results or not. If false, SPACE events will be skipped; if true, white space will be included in results.
      Throws:
      IOException
      XMLStreamException
    • getQName

      public QName getQName() throws XMLStreamException
      Throws:
      XMLStreamException
    • getLocalName

      public String getLocalName() throws XMLStreamException
      For events with fully qualified names (START_ELEMENT, END_ELEMENT, ATTRIBUTE, NAMESPACE) returns the local component of the full name; for events with only non-qualified name (PROCESSING_INSTRUCTION, entity and notation declarations, references) returns the name, and for other events, returns null.
      Returns:
      Local component of the name
      Throws:
      XMLStreamException
    • getPrefix

      public String getPrefix() throws XMLStreamException
      Method for accessing namespace prefix of the START_ELEMENT this cursor points to.
      Returns:
      Prefix of currently pointed-to START_ELEMENT, if it has one; "" if none
      Throws:
      XMLStreamException - if cursor does not point to START_ELEMENT
    • getNsUri

      public String getNsUri() throws XMLStreamException
      Method for accessing namespace URI of the START_ELEMENT this cursor points to.
      Returns:
      Namespace URI of currently pointed-to START_ELEMENT, if it has one; "" if none
      Throws:
      XMLStreamException - if cursor does not point to START_ELEMENT
    • getPrefixedName

      public String getPrefixedName() throws XMLStreamException
      Returns a String representation of either the fully-qualified name (if the event has full name) or the local name (if event does not have full name but has local name); or if no name available, throws stream exception.
      Throws:
      XMLStreamException
    • hasLocalName

      public boolean hasLocalName(String expName) throws XMLStreamException
      Method for verifying whether current named event (one for which getLocalName() can be called) has the specified local name or not.
      Returns:
      True if the local name associated with the event is as expected
      Throws:
      XMLStreamException
    • hasName

      public boolean hasName(String expNsURI, String expLN) throws XMLStreamException
      Method for verifying whether current named event (one for which getLocalName() can be called) has the specified fully-qualified name or not. Both namespace URI and local name must match for the result to be true.
      Returns:
      True if the fully-qualified name associated with the event is as expected
      Throws:
      XMLStreamException
    • hasName

      public boolean hasName(QName qname) throws XMLStreamException
      Equivalent to calling hasName(String, String) with namespace URI and local name contained in the argument QName
      Parameters:
      qname - Name to compare name of current event (if any) against.
      Throws:
      XMLStreamException
      Since:
      2.0
    • asEvent

      public XMLEvent asEvent() throws XMLStreamException
      Method for accessing information regarding event this cursor points to, as an instance of XMLEvent. Depending on underlying input source this may be constructed from scratch (for example, if the input cursor was not constructed from XMLEventReader), or accessed from the underlying event reader.

      Calling this method does not advance the underlying stream or cursor itself.

      Note, too, that it is ok to call this method at any time: if the cursor is not in valid state for accessing information null will be returned.

      Returns:
      Information about currently pointed-to input stream event, if we are pointing to one; null otherwise
      Throws:
      XMLStreamException
      Since:
      2.0.0
    • getAttrCount

      public int getAttrCount() throws XMLStreamException
      Method that can be called when this cursor points to START_ELEMENT, and which will return number of attributes with values for the start element. This includes both explicit attribute values and possible implied default values (when DTD support is enabled by the underlying stream reader).
      Throws:
      XMLStreamException - if either the underlying parser has problems (cursor not valid or not pointing to START_ELEMENT)
    • findAttrIndex

      public int findAttrIndex(String uri, String localName) throws XMLStreamException
      Method that can be called when this cursor points to START_ELEMENT, and which will return index of specified attribute, if it exists for this element. If not, -1 is returned to denote "not found".
      Throws:
      XMLStreamException - if either the underlying parser has problems (cursor not valid or not pointing to START_ELEMENT)
    • getAttrName

      public QName getAttrName(int index) throws XMLStreamException
      Method that can be called when this cursor points to START_ELEMENT, and returns fully qualified name of the attribute at specified index. Index has to be between [0, getAttrCount()[; otherwise IllegalArgumentException will be thrown.
      Parameters:
      index - Index of the attribute
      Throws:
      XMLStreamException - if either the underlying parser has problems (cursor not valid or not pointing to START_ELEMENT), or if invalid attribute
      IllegalArgumentException - if attribute index is invalid (less than 0 or greater than the last valid index [getAttributeCount()-1])
    • getAttrLocalName

      public String getAttrLocalName(int index) throws XMLStreamException
      Method that can be called when this cursor points to START_ELEMENT, and returns local name of the attribute at specified index. Index has to be between [0, getAttrCount()[; otherwise IllegalArgumentException will be thrown.
      Parameters:
      index - Index of the attribute
      Throws:
      XMLStreamException - if either the underlying parser has problems (cursor not valid or not pointing to START_ELEMENT), or if invalid attribute
      IllegalArgumentException - if attribute index is invalid (less than 0 or greater than the last valid index [getAttributeCount()-1])
    • getAttrPrefix

      public String getAttrPrefix(int index) throws XMLStreamException
      Method that can be called when this cursor points to START_ELEMENT, and returns namespace prefix of the attribute at specified index (if it has any), or empty String if attribute has no prefix (does not belong to a namespace). Index has to be between [0, getAttrCount()[; otherwise IllegalArgumentException will be thrown.
      Parameters:
      index - Index of the attribute
      Throws:
      XMLStreamException - if either the underlying parser has problems (cursor not valid or not pointing to START_ELEMENT), or if invalid attribute
      IllegalArgumentException - if attribute index is invalid (less than 0 or greater than the last valid index [getAttributeCount()-1])
    • getAttrNsUri

      public String getAttrNsUri(int index) throws XMLStreamException
      Method that can be called when this cursor points to START_ELEMENT, and returns namespace URI of the attribute at specified index (non-empty String if it has one, and empty String if attribute does not belong to a namespace) Index has to be between [0, getAttrCount()[; otherwise IllegalArgumentException will be thrown.
      Parameters:
      index - Index of the attribute
      Throws:
      XMLStreamException - if either the underlying parser has problems (cursor not valid or not pointing to START_ELEMENT), or if invalid attribute
      IllegalArgumentException - if attribute index is invalid (less than 0 or greater than the last valid index [getAttributeCount()-1])
    • getAttrValue

      public String getAttrValue(int index) throws XMLStreamException
      Method that can be called when this cursor points to START_ELEMENT, and returns unmodified textual value of the attribute at specified index (non-empty String if it has one, and empty String if attribute does not belong to a namespace) Index has to be between [0, getAttrCount()[; otherwise IllegalArgumentException will be thrown.
      Parameters:
      index - Index of the attribute
      Throws:
      XMLStreamException - if either the underlying parser has problems (cursor not valid or not pointing to START_ELEMENT), or if invalid attribute
      IllegalArgumentException - if attribute index is invalid (less than 0 or greater than the last valid index [getAttributeCount()-1])
    • getAttrValue

      public String getAttrValue(String localName) throws XMLStreamException
      Convenience accessor method to access an attribute that is not in a namespace (has no prefix). Equivalent to calling getAttrValue(String,String) with 'null' for 'namespace URI' argument
      Throws:
      XMLStreamException
    • getAttrValue

      public String getAttrValue(String namespaceURI, String localName) throws XMLStreamException
      Method that can be called when this cursor points to START_ELEMENT, and returns unmodified textual value of the specified attribute (if element has it), or null if element has no value for such attribute.
      Parameters:
      namespaceURI - Namespace URI for the attribute, if any; empty String or null if none.
      localName - Local name of the attribute to access (in namespace-aware mode: in non-namespace-aware mode, needs to be the full name)
      Throws:
      XMLStreamException - if either the underlying parser has problems (cursor not valid or not pointing to START_ELEMENT), or if invalid attribute
    • getAttrBooleanValue

      public boolean getAttrBooleanValue(int index) throws XMLStreamException
      Method for accessing value of specified attribute as boolean. Method will only succeed if the attribute value is a valid boolean, as specified by XML Schema specification (and hence is accessible via Stax2 Typed Access API).
      Parameters:
      index - Index of attribute to access
      Throws:
      XMLStreamException - If specified attribute can not be accessed (due to cursor state), or if attribute value is not a valid textual representation of boolean
      IllegalArgumentException - If given attribute index is invalid
    • getAttrBooleanValue

      public boolean getAttrBooleanValue(int index, boolean defValue) throws XMLStreamException
      Method for accessing value of specified attribute as boolean. If attribute value is not a valid boolean (as specified by XML Schema specification), will instead return specified "default value".
      Parameters:
      index - Index of attribute to access
      defValue - Value to return if attribute value exists but is not a valid boolean value
      Throws:
      XMLStreamException - If specified attribute can not be accessed (due to cursor state), or if attribute value is not a valid textual representation of boolean.
      IllegalArgumentException - If given attribute index is invalid
    • getAttrIntValue

      public int getAttrIntValue(int index) throws XMLStreamException
      Method for accessing value of specified attribute as integer. Method will only succeed if the attribute value is a valid integer, as specified by XML Schema specification (and hence is accessible via Stax2 Typed Access API).
      Parameters:
      index - Index of attribute to access
      Throws:
      XMLStreamException - If specified attribute can not be accessed (due to cursor state), or if attribute value is not a valid textual representation of integer.
      IllegalArgumentException - If given attribute index is invalid
    • getAttrIntValue

      public int getAttrIntValue(int index, int defValue) throws XMLStreamException
      Method for accessing value of specified attribute as integer. If attribute value is not a valid integer (as specified by XML Schema specification), will instead return specified "default value".
      Parameters:
      index - Index of attribute to access
      defValue - Value to return if attribute value exists but is not a valid integer value
      Throws:
      XMLStreamException - If specified attribute can not be accessed (due to cursor state), or if attribute value is not a valid textual representation of integer.
      IllegalArgumentException - If given attribute index is invalid
    • getAttrLongValue

      public long getAttrLongValue(int index) throws XMLStreamException
      Method for accessing value of specified attribute as long. Method will only succeed if the attribute value is a valid long, as specified by XML Schema specification (and hence is accessible via Stax2 Typed Access API).
      Parameters:
      index - Index of attribute to access
      Throws:
      XMLStreamException - If specified attribute can not be accessed (due to cursor state), or if attribute value is not a valid textual representation of long.
      IllegalArgumentException - If given attribute index is invalid
    • getAttrLongValue

      public long getAttrLongValue(int index, long defValue) throws XMLStreamException
      Method for accessing value of specified attribute as long. If attribute value is not a valid long (as specified by XML Schema specification), will instead return specified "default value".
      Parameters:
      index - Index of attribute to access
      defValue - Value to return if attribute value exists but is not a valid long value
      Throws:
      XMLStreamException - If specified attribute can not be accessed (due to cursor state), or if attribute value is not a valid textual representation of long.
      IllegalArgumentException - If given attribute index is invalid
    • getAttrDoubleValue

      public double getAttrDoubleValue(int index) throws XMLStreamException
      Method for accessing value of specified attribute as double. Method will only succeed if the attribute value is a valid double, as specified by XML Schema specification (and hence is accessible via Stax2 Typed Access API).
      Parameters:
      index - Index of attribute to access
      Throws:
      XMLStreamException - If specified attribute can not be accessed (due to cursor state), or if attribute value is not a valid textual representation of double.
      IllegalArgumentException - If given attribute index is invalid
    • getAttrDoubleValue

      public double getAttrDoubleValue(int index, double defValue) throws XMLStreamException
      Method for accessing value of specified attribute as double. If attribute value is not a valid double (as specified by XML Schema specification), will instead return specified "default value".
      Parameters:
      index - Index of attribute to access
      defValue - Value to return if attribute value exists but is not a valid double value
      Throws:
      XMLStreamException - If specified attribute can not be accessed (due to cursor state), or if attribute value is not a valid textual representation of double.
      IllegalArgumentException - If given attribute index is invalid
    • getAttrEnumValue

      public <T extends Enum<T>> T getAttrEnumValue(int index, Class<T> enumType) throws XMLStreamException
      Method for accessing value of specified attribute as an Enum value of specified type, if content non-empty. If it is empty, will return null. And if non-empty value is not equal to name() of one of Enum values, will throw a TypedXMLStreamException to indicate the problem.
      Parameters:
      index - Index of attribute to access
      Throws:
      XMLStreamException - If specified attribute can not be accessed (due to cursor state), or if attribute value is not a valid textual representation of double.
      IllegalArgumentException - If given attribute index is invalid
    • getAttrBinaryValue

      public byte[] getAttrBinaryValue(int index, org.codehaus.stax2.typed.Base64Variant variant) throws XMLStreamException
      Method for accessing value of a base64-encoded attribute, using specified base64 encoding variant.
      Throws:
      XMLStreamException
      Since:
      2.2
    • getAttrBinaryValue

      public byte[] getAttrBinaryValue(int index) throws XMLStreamException
      Method for accessing value of a base64-encoded attribute, using default base64 encoding variant (as per Base64Variants.getDefaultVariant().
      Throws:
      XMLStreamException
      Since:
      2.2
    • getAttrIntValue

      @Deprecated public int getAttrIntValue(String uri, String localName) throws XMLStreamException
      Deprecated.
      Throws:
      XMLStreamException
    • getAttrIntValue

      @Deprecated public int getAttrIntValue(String uri, String localName, int defValue) throws XMLStreamException
      Throws:
      XMLStreamException
    • getElemStringValue

      public String getElemStringValue() throws XMLStreamException
      Method that can collect text directly contained within START_ELEMENT currently pointed by this cursor. This is different from collectDescendantText(boolean) in that it does NOT work for mixed content (child elements are not allowed: comments and processing instructions are allowed and ignored if encountered). If any ignorable white space (as per schema, dtd or so) is encountered, it will be ignored.

      The main technical difference to collectDescendantText(boolean) is that this method tries to make use of Stax2 v3.0 Typed Access API, if available, and can thus be more efficient.

      Throws:
      XMLStreamException - if content is not accessible; may also be thrown if child elements are encountered.
    • getElemBooleanValue

      public boolean getElemBooleanValue() throws XMLStreamException
      Method that can collect text directly contained within START_ELEMENT currently pointed by this cursor and convert it to a boolean value. For method to work, the value must be legal textual representation of boolean data type as specified by W3C Schema (as well as Stax2 Typed Access API). Element also can not contain mixed content (child elements; comments and processing instructions are allowed and ignored if encountered).
      Throws:
      XMLStreamException - if content is not accessible or convertible to required return type
    • getElemBooleanValue

      public boolean getElemBooleanValue(boolean defValue) throws XMLStreamException
      Similar to getElemBooleanValue(), but instead of failing on invalid value, returns given default value.
      Throws:
      XMLStreamException
    • getElemIntValue

      public int getElemIntValue() throws XMLStreamException
      Method that can collect text directly contained within START_ELEMENT currently pointed by this cursor and convert it to a int value. For method to work, the value must be legal textual representation of int data type as specified by W3C Schema (as well as Stax2 Typed Access API). Element also can not contain mixed content (child elements; comments and processing instructions are allowed and ignored if encountered).
      Throws:
      XMLStreamException - if content is not accessible or convertible to required return type
    • getElemIntValue

      public int getElemIntValue(int defValue) throws XMLStreamException
      Similar to getElemIntValue(), but instead of failing on invalid value, returns given default value.
      Throws:
      XMLStreamException
    • getElemLongValue

      public long getElemLongValue() throws XMLStreamException
      Method that can collect text directly contained within START_ELEMENT currently pointed by this cursor and convert it to a long value. For method to work, the value must be legal textual representation of long data type as specified by W3C Schema (as well as Stax2 Typed Access API). Element also can not contain mixed content (child elements; comments and processing instructions are allowed and ignored if encountered).
      Throws:
      XMLStreamException - if content is not accessible or convertible to required return type
    • getElemLongValue

      public long getElemLongValue(long defValue) throws XMLStreamException
      Similar to getElemLongValue(), but instead of failing on invalid value, returns given default value.
      Throws:
      XMLStreamException
    • getElemDoubleValue

      public double getElemDoubleValue() throws XMLStreamException
      Method that can collect text directly contained within START_ELEMENT currently pointed by this cursor and convert it to a double value. For method to work, the value must be legal textual representation of double data type as specified by W3C Schema (as well as Stax2 Typed Access API). Element also can not contain mixed content (child elements; comments and processing instructions are allowed and ignored if encountered).
      Throws:
      XMLStreamException - if content is not accessible or convertible to required return type
    • getElemDoubleValue

      public double getElemDoubleValue(double defValue) throws XMLStreamException
      Similar to getElemDoubleValue(), but instead of failing on invalid value, returns given default value.
      Throws:
      XMLStreamException
    • getElemEnumValue

      public <T extends Enum<T>> T getElemEnumValue(Class<T> enumType) throws XMLStreamException
      Method that can collect text directly contained within START_ELEMENT currently pointed by this cursor and convert it to one of enumerated values of given type, if textual value non-type, and otherwise to null. If a non-empty value that is not one of legal enumerated values is encountered, a TypedXMLStreamException is thrown.

      Element also can not contain mixed content (child elements; comments and processing instructions are allowed and ignored if encountered).

      Throws:
      XMLStreamException - if content is not accessible or convertible to required return type
      org.codehaus.stax2.typed.TypedXMLStreamException - if element value is non-empty and not one of allowed values for the enumeration type
    • getElemBinaryValue

      public byte[] getElemBinaryValue(org.codehaus.stax2.typed.Base64Variant variant) throws XMLStreamException
      Method for accessing value current element, which isbase64-encoded binary data, by decoding contents using specified variant.
      Throws:
      XMLStreamException
      Since:
      2.2
    • getElemBinaryValue

      public byte[] getElemBinaryValue() throws XMLStreamException
      Method for accessing value current element, which isbase64-encoded binary data, by decoding contents using the default base64 variant
      Throws:
      XMLStreamException
      Since:
      2.2
    • _verifyElemAccess

      private final void _verifyElemAccess(String method) throws XMLStreamException
      Helper method called by getElemXxxValue methods to ensure that the state is appropriate for the call
      Throws:
      XMLStreamException
    • _ensureEndElement

      private final void _ensureEndElement() throws XMLStreamException
      This method can be called to ensure that this cursor gets to point to END_ELEMENT that closes typed element that has been read; usually this works as expected, but during type conversion exceptions handling may not work as expected
      Throws:
      XMLStreamException
    • getData

      public Object getData()
      Method for accessing application-provided data set previously by a setData(java.lang.Object) call.
    • setData

      public void setData(Object o)
      Method for assigning per-cursor application-managed data, readable using getData().
    • getNext

      public abstract SMEvent getNext() throws XMLStreamException
      Main iterating method. Will try to advance the cursor to the next visible event (visibility defined by the filter cursor is configured with, if any), and return event type. If no such events are available, will return null.

      Note that one side-effect of calling this method is to invalidate the child cursor, if one was active. This is done by iterating over any events child cursor (and its descendants if any) might expose.

      Returns:
      Type of event (from XMLStreamConstants, such as XMLStreamConstants.START_ELEMENT, if a new node was iterated over; null when there are no more nodes this cursor can iterate over.
      Throws:
      XMLStreamException - If there are underlying parsing problems.
    • advance

      public final SMInputCursor advance() throws XMLStreamException
      Method that does what getNext() does, but instead of returning resulting event type, returns this cursor. The main purpose of this method is to allow for chaining calls. This is especially useful right after constructing a root level cursor, and needing to advance it to point to the root element. As such, a common idiom is:
       SMInputCursor positionedRoot = smFactory.rootElementCursor(file).advance();
      
      which both constructs the root element cursor, and positions it over the root element. Can be similarly used with other kinds of cursors as well, of course
      Throws:
      XMLStreamException
      Since:
      2.0
    • childCursor

      public SMInputCursor childCursor(SMFilter f) throws XMLStreamException
      Method that will create a new nested cursor for iterating over all (immediate) child nodes of the start element this cursor currently points to that are passed by the specified filter. If cursor does not point to a start element, it will throw IllegalStateException; if it does not support concept of child cursors, it will throw UnsupportedOperationException
      Parameters:
      f - Filter child cursor is to use for filtering out 'unwanted' nodes; may be null if no filtering is to be done
      Throws:
      IllegalStateException - If cursor can not be created due to the state cursor is in.
      UnsupportedOperationException - If cursor does not allow creation of child cursors.
      XMLStreamException
    • childCursor

      public final SMInputCursor childCursor() throws XMLStreamException
      Method that will create a new nested cursor for iterating over all (immediate) child nodes of the start element this cursor currently points to. If cursor does not point to a start element, it will throw IllegalStateException; if it does not support concept of child cursors, it will throw UnsupportedOperationException
      Throws:
      IllegalStateException - If cursor can not be created due to the state cursor is in.
      UnsupportedOperationException - If cursor does not allow creation of child cursors.
      XMLStreamException
    • descendantCursor

      public SMInputCursor descendantCursor(SMFilter f) throws XMLStreamException
      Method that will create a new nested cursor for iterating over all the descendant (children and grandchildren) nodes of the start element this cursor currently points to that are accepted by the specified filter. If cursor does not point to a start element, it will throw IllegalStateException; if it does not support concept of descendant cursors, it will throw UnsupportedOperationException
      Parameters:
      f - Filter child cursor is to use for filtering out 'unwanted' nodes; may be null if no filtering is to be done
      Throws:
      IllegalStateException - If cursor can not be created due to the state cursor is in (or for some cursors, if they never allow creating such cursors)
      UnsupportedOperationException - If cursor does not allow creation of descendant cursors.
      XMLStreamException
    • descendantCursor

      public final SMInputCursor descendantCursor() throws XMLStreamException
      Method that will create a new nested cursor for iterating over all the descendant (children and grandchildren) nodes of the start element this cursor currently points to. If cursor does not point to a start element, it will throw IllegalStateException; if it does not support concept of descendant cursors, it will throw UnsupportedOperationException
      Throws:
      IllegalStateException - If cursor can not be created due to the state cursor is in (or for some cursors, if they never allow creating such cursors)
      UnsupportedOperationException - If cursor does not allow creation of descendant cursors.
      XMLStreamException
    • childElementCursor

      public final SMInputCursor childElementCursor() throws XMLStreamException
      Convenience method; equivalent to childCursor(SMFilterFactory.getElementOnlyFilter());
      Throws:
      XMLStreamException
    • childElementCursor

      public final SMInputCursor childElementCursor(QName elemName) throws XMLStreamException
      Convenience method; equivalent to childCursor(SMFilterFactory.getElementOnlyFilter(elemName)); Will only return START_ELEMENT and END_ELEMENT events, whose element name matches given qname.
      Throws:
      XMLStreamException
    • childElementCursor

      public final SMInputCursor childElementCursor(String elemLocalName) throws XMLStreamException
      Convenience method; equivalent to childCursor(SMFilterFactory.getElementOnlyFilter(elemName)); Will only return START_ELEMENT and END_ELEMENT events, whose element's local name matches given local name, and that does not belong to a namespace.
      Throws:
      XMLStreamException
    • descendantElementCursor

      public final SMInputCursor descendantElementCursor() throws XMLStreamException
      Convenience method; equivalent to descendantCursor(SMFilterFactory.getElementOnlyFilter());
      Throws:
      XMLStreamException
    • descendantElementCursor

      public final SMInputCursor descendantElementCursor(QName elemName) throws XMLStreamException
      Convenience method; equivalent to descendantCursor(SMFilterFactory.getElementOnlyFilter(elemName)); Will only return START_ELEMENT and END_ELEMENT events, whose element name matches given qname.
      Throws:
      XMLStreamException
    • descendantElementCursor

      public final SMInputCursor descendantElementCursor(String elemLocalName) throws XMLStreamException
      Convenience method; equivalent to descendantCursor(SMFilterFactory.getElementOnlyFilter(elemLocalName));. Will only return START_ELEMENT and END_ELEMENT events, whose element local name matches given local name, and that do not belong to a namespace
      Throws:
      XMLStreamException
    • childMixedCursor

      public final SMInputCursor childMixedCursor() throws XMLStreamException
      Convenience method; equivalent to childCursor(SMFilterFactory.getMixedFilter());
      Throws:
      XMLStreamException
    • descendantMixedCursor

      public final SMInputCursor descendantMixedCursor() throws XMLStreamException
      Convenience method; equivalent to descendantCursor(SMFilterFactory.getMixedFilter());
      Throws:
      XMLStreamException
    • constructStreamException

      public XMLStreamException constructStreamException(String msg)
      Method for constructing stream exception with given message, and location that matches that of the underlying stream regardless of whether this cursor is valid (that is, will indicate location of the stream which may differ from where this cursor was last valid)
      Specified by:
      constructStreamException in class CursorBase
    • throwStreamException

      public void throwStreamException(String msg) throws XMLStreamException
      Method for constructing and throwing stream exception with given message. Equivalent to throwing exception that constructStreamException(java.lang.String) constructs and returns.
      Specified by:
      throwStreamException in class CursorBase
      Throws:
      XMLStreamException
    • getPathDesc

      public String getPathDesc()
      Method that generates developer-readable description of the logical path of the event this cursor points to, assuming that element tracking is enabled. If it is, a path description will be constructed; if not, result will be "." ("unspecified current location").

      Note: while results look similar to XPath expressions, they are not accurate (or even valid) XPath. This is partly because of filtering, and partly because of differences between element/node index calculation. The idea is to make it easier to get reasonable idea of logical location, in addition to physical input location.

    • appendPathDesc

      private static void appendPathDesc(StringBuilder sb, SMElementInfo info, boolean recursive)
    • getCurrEventDesc

      protected String getCurrEventDesc()
      Specified by:
      getCurrEventDesc in class CursorBase
      Returns:
      Developer-readable description of the event this cursor currently points to.
    • toString

      public String toString()
      Overridden implementation will just display description of the event this cursor points to (or last pointed to, if not valid)
      Overrides:
      toString in class CursorBase
    • constructElementInfo

      protected SMElementInfo constructElementInfo(SMElementInfo parent, SMElementInfo prevSibling) throws XMLStreamException
      Method cursor calls when it needs to track element state information; if so, it calls this method to take a snapshot of the element.

      Note caller already suppresses calls so that this method is only called when information needs to be preserved. Further, previous element is only passed if such linkage is to be preserved (reason for not always doing it is the increased memory usage).

      Finally, note that this method does NOT implement ElementInfoFactory, as its signature does not include the cursor argument, as that's passed as this pointer already.

      Throws:
      XMLStreamException
    • constructChildCursor

      protected abstract SMInputCursor constructChildCursor(SMFilter f) throws XMLStreamException
      Abstract method that concrete sub-classes implement, and is used for all instantiation of child cursors by this cursor instance.

      Note that custom cursor implementations can be used by overriding this method.

      Throws:
      XMLStreamException
    • constructDescendantCursor

      protected abstract SMInputCursor constructDescendantCursor(SMFilter f) throws XMLStreamException
      Abstract method that concrete sub-classes implement, and is used for all instantiation of descendant cursors by this cursor instance.

      Note that custom cursor implementations can be used by overriding this method.

      Throws:
      XMLStreamException
    • _markConsumed

      protected void _markConsumed()
      Method to call to notify that textual contents of this cursor have been read; typically when a call has been made that will traverse contents and make stream point to matching END_ELEMENT.
      Since:
      2.2