KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > event > SequenceOutputter


1 package net.sf.saxon.event;
2 import net.sf.saxon.om.*;
3 import net.sf.saxon.value.EmptySequence;
4 import net.sf.saxon.value.SequenceExtent;
5
6 import java.util.ArrayList JavaDoc;
7
8
9 /**
10  * This outputter is used when writing a sequence of atomic values and nodes, that
11  * is, when xsl:variable is used with content and an "as" attribute. The outputter
12  * builds the sequence and provides access to it. (It isn't really an outputter at all,
13  * it doesn't pass the events to anyone, it merely constructs the sequence in memory
14  * and provides access to it). Note that the event sequence can include calls such as
15  * startElement and endElement that require trees to be built. If nodes such as attributes
16  * and text nodes are received while an element is being constructed, the nodes are added
17  * to the tree. Otherwise, "orphan" nodes (nodes with no parent) are created and added
18  * directly to the sequence.
19  *
20  * <p>This class is not used to build temporary trees. For that, the ComplexContentOutputter
21  * is used.</p>
22  *
23  *
24  * @author Michael H. Kay
25  */

26
27 public final class SequenceOutputter extends SequenceWriter {
28
29     private ArrayList JavaDoc list;
30
31     /**
32     * Create a new SequenceOutputter
33     */

34
35     public SequenceOutputter() {
36         this.list = new ArrayList JavaDoc(50);
37     }
38
39     public SequenceOutputter(int estimatedSize) {
40         this.list = new ArrayList JavaDoc(estimatedSize);
41     }
42
43     /**
44      * Abstract method to be supplied by subclasses: output one item in the sequence.
45      */

46
47     public void write(Item item) {
48         list.add(item);
49     }
50
51     /**
52     * Get the sequence that has been built
53     */

54
55     public ValueRepresentation getSequence() {
56         switch (list.size()) {
57             case 0:
58                 return EmptySequence.getInstance();
59             case 1:
60                 return (Item)list.get(0);
61             default:
62                 return new SequenceExtent(list);
63         }
64     }
65
66     /**
67      * Get an iterator over the sequence of items that has been constructed
68      */

69
70     public SequenceIterator iterate() {
71         if (list.size() == 0) {
72             return EmptyIterator.getInstance();
73         } else {
74             return new ListIterator(list);
75         }
76     }
77
78     /**
79      * Get the list containing the sequence of items
80      */

81
82     public ArrayList JavaDoc getList() {
83         return list;
84     }
85
86     /**
87      * Get the first item in the sequence that has been built
88      */

89
90     public Item getFirstItem() {
91         if (list.size() == 0) {
92             return null;
93         } else {
94             return (Item)list.get(0);
95         }
96     }
97
98     /**
99      * 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 //
114
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
115
// you may not use this file except in compliance with the License. You may obtain a copy of the
116
// License at http://www.mozilla.org/MPL/
117
//
118
// Software distributed under the License is distributed on an "AS IS" basis,
119
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
120
// See the License for the specific language governing rights and limitations under the License.
121
//
122
// The Original Code is: all this file.
123
//
124
// The Initial Developer of the Original Code is Michael H. Kay
125
//
126
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
127
//
128
// Contributor(s): none.
129
//
130
Popular Tags