001: package net.sf.saxon.event;
002:
003: import net.sf.saxon.om.*;
004: import net.sf.saxon.value.EmptySequence;
005: import net.sf.saxon.value.SequenceExtent;
006:
007: import java.util.ArrayList;
008:
009: /**
010: * This outputter is used when writing a sequence of atomic values and nodes, that
011: * is, when xsl:variable is used with content and an "as" attribute. The outputter
012: * builds the sequence and provides access to it. (It isn't really an outputter at all,
013: * it doesn't pass the events to anyone, it merely constructs the sequence in memory
014: * and provides access to it). Note that the event sequence can include calls such as
015: * startElement and endElement that require trees to be built. If nodes such as attributes
016: * and text nodes are received while an element is being constructed, the nodes are added
017: * to the tree. Otherwise, "orphan" nodes (nodes with no parent) are created and added
018: * directly to the sequence.
019: *
020: * <p>This class is not used to build temporary trees. For that, the ComplexContentOutputter
021: * is used.</p>
022: *
023: *
024: * @author Michael H. Kay
025: */
026:
027: public final class SequenceOutputter extends SequenceWriter {
028:
029: private ArrayList list;
030:
031: /**
032: * Create a new SequenceOutputter
033: */
034:
035: public SequenceOutputter() {
036: this .list = new ArrayList(50);
037: }
038:
039: public SequenceOutputter(int estimatedSize) {
040: this .list = new ArrayList(estimatedSize);
041: }
042:
043: /**
044: * Abstract method to be supplied by subclasses: output one item in the sequence.
045: */
046:
047: public void write(Item item) {
048: list.add(item);
049: }
050:
051: /**
052: * Get the sequence that has been built
053: */
054:
055: public ValueRepresentation getSequence() {
056: switch (list.size()) {
057: case 0:
058: return EmptySequence.getInstance();
059: case 1:
060: return (Item) list.get(0);
061: default:
062: return new SequenceExtent(list);
063: }
064: }
065:
066: /**
067: * Get an iterator over the sequence of items that has been constructed
068: */
069:
070: public SequenceIterator iterate() {
071: if (list.size() == 0) {
072: return EmptyIterator.getInstance();
073: } else {
074: return new ListIterator(list);
075: }
076: }
077:
078: /**
079: * Get the list containing the sequence of items
080: */
081:
082: public ArrayList getList() {
083: return list;
084: }
085:
086: /**
087: * Get the first item in the sequence that has been built
088: */
089:
090: public Item getFirstItem() {
091: if (list.size() == 0) {
092: return null;
093: } else {
094: return (Item) list.get(0);
095: }
096: }
097:
098: /**
099: * Get the last item in the sequence that has been built, and remove it
100: */
101:
102: public Item popLastItem() {
103: if (list.size() == 0) {
104: return null;
105: } else {
106: return (Item) list.remove(list.size() - 1);
107: }
108: }
109:
110: }
111:
112: //
113: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
114: // you may not use this file except in compliance with the License. You may obtain a copy of the
115: // License at http://www.mozilla.org/MPL/
116: //
117: // Software distributed under the License is distributed on an "AS IS" basis,
118: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
119: // See the License for the specific language governing rights and limitations under the License.
120: //
121: // The Original Code is: all this file.
122: //
123: // The Initial Developer of the Original Code is Michael H. Kay
124: //
125: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
126: //
127: // Contributor(s): none.
128: //
|