001: package net.sf.saxon.om;
002:
003: /**
004: * An iterator over nodes, that prepends a given node to the nodes
005: * returned by another iterator. Used to modify an iterator over axis A
006: * to one that iterates over A-OR-SELF.
007: */
008:
009: public class PrependIterator implements AxisIterator {
010:
011: NodeInfo start;
012: AxisIterator base;
013: int position = 0;
014:
015: public PrependIterator(NodeInfo start, AxisIterator base) {
016: this .start = start;
017: this .base = base;
018: }
019:
020: /**
021: * Get the next item in the sequence. <BR>
022: *
023: * @return the next Item. If there are no more nodes, return null.
024: */
025:
026: public Item next() {
027: if (position == 0) {
028: position = 1;
029: return start;
030: }
031: Item n = base.next();
032: if (n == null) {
033: position = -1;
034: } else {
035: position++;
036: }
037: return n;
038: }
039:
040: /**
041: * Get the current item in the sequence.
042: *
043: * @return the current item, that is, the item most recently returned by
044: * next()
045: */
046:
047: public Item current() {
048: if (position() == 1) {
049: return start;
050: } else if (position < 1) {
051: return null;
052: } else {
053: return base.current();
054: }
055: }
056:
057: /**
058: * Get the current position
059: *
060: * @return the position of the current item (the item most recently
061: * returned by next()), starting at 1 for the first node
062: */
063:
064: public int position() {
065: return position;
066: }
067:
068: /**
069: * Get another iterator over the same sequence of items, positioned at the
070: * start of the sequence
071: *
072: * @return a new iterator over the same sequence
073: */
074:
075: public SequenceIterator getAnother() {
076: return new PrependIterator(start, base);
077: }
078:
079: /**
080: * Get properties of this iterator, as a bit-significant integer.
081: *
082: * @return the properties of this iterator. This will be some combination of
083: * properties such as {@link GROUNDED}, {@link LAST_POSITION_FINDER},
084: * and {@link LOOKAHEAD}. It is always
085: * acceptable to return the value zero, indicating that there are no known special properties.
086: * It is acceptable for the properties of the iterator to change depending on its state.
087: */
088:
089: public int getProperties() {
090: return 0;
091: }
092:
093: }
094:
095: //
096: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
097: // you may not use this file except in compliance with the License. You may obtain a copy of the
098: // License at http://www.mozilla.org/MPL/
099: //
100: // Software distributed under the License is distributed on an "AS IS" basis,
101: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
102: // See the License for the specific language governing rights and limitations under the License.
103: //
104: // The Original Code is: all this file.
105: //
106: // The Initial Developer of the Original Code is Michael H. Kay.
107: //
108: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
109: //
110: // Contributor(s): none.
111: //
|