| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.event; |
| 9 | |
| 10 | import net.sf.saxon.Controller; |
| 11 | import net.sf.saxon.expr.XPathContext; |
| 12 | import net.sf.saxon.om.Item; |
| 13 | import net.sf.saxon.om.Sequence; |
| 14 | import net.sf.saxon.om.SequenceIterator; |
| 15 | import net.sf.saxon.tree.iter.EmptyIterator; |
| 16 | import net.sf.saxon.tree.iter.ListIterator; |
| 17 | import net.sf.saxon.value.EmptySequence; |
| 18 | import net.sf.saxon.value.SequenceExtent; |
| 19 | |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.List; |
| 22 | |
| 23 | |
| 24 | /** |
| 25 | * This outputter is used when writing a sequence of atomic values and nodes, that |
| 26 | * is, when xsl:variable is used with content and an "as" attribute. The outputter |
| 27 | * builds the sequence and provides access to it. (It isn't really an outputter at all, |
| 28 | * it doesn't pass the events to anyone, it merely constructs the sequence in memory |
| 29 | * and provides access to it). Note that the event sequence can include calls such as |
| 30 | * startElement and endElement that require trees to be built. If nodes such as attributes |
| 31 | * and text nodes are received while an element is being constructed, the nodes are added |
| 32 | * to the tree. Otherwise, "orphan" nodes (nodes with no parent) are created and added |
| 33 | * directly to the sequence. |
| 34 | * <p>This class is not used to build temporary trees. For that, the ComplexContentOutputter |
| 35 | * is used.</p> |
| 36 | * |
| 37 | * @author Michael H. Kay |
| 38 | */ |
| 39 | |
| 40 | public final class SequenceOutputter extends SequenceWriter { |
| 41 | |
| 42 | private List<Item<?>> list; |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * Create a new SequenceOutputter |
| 47 | * |
| 48 | * @param pipe the pipeline configuration |
| 49 | */ |
| 50 | |
| 51 | public SequenceOutputter(PipelineConfiguration pipe) { |
| 52 | this(pipe, 50); |
| 53 | } |
| 54 | |
| 55 | public SequenceOutputter(PipelineConfiguration pipe, int estimatedSize) { |
| 56 | super(pipe); |
| 57 | this.list = new ArrayList<>(estimatedSize); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Allocate a SequenceOutputter. Used from generated bytecode. |
| 62 | * |
| 63 | * @param context dynamic XPath context |
| 64 | * @param hostLang host language (XSLT/XQuery) |
| 65 | * @return the allocated SequenceOutputter |
| 66 | * @see com.saxonica.ee.bytecode.util.CompilerService |
| 67 | */ |
| 68 | |
| 69 | /*@Nullable*/ |
| 70 | public static SequenceOutputter allocateSequenceOutputter(XPathContext context, int hostLang) { |
| 71 | Controller controller = context.getController(); |
| 72 | SequenceOutputter seq = controller.allocateSequenceOutputter(20); |
| 73 | seq.getPipelineConfiguration().setHostLanguage(hostLang); |
| 74 | return seq; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Clear the contents of the SequenceOutputter and make it available for reuse |
| 79 | */ |
| 80 | |
| 81 | public void reset() { |
| 82 | list = new ArrayList<>(Math.min(list.size() + 10, 50)); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Method to be supplied by subclasses: output one item in the sequence. |
| 87 | */ |
| 88 | |
| 89 | public void write(Item item) { |
| 90 | list.add(item); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get the sequence that has been built |
| 95 | * |
| 96 | * @return the value (sequence of items) that have been written to this SequenceOutputter |
| 97 | */ |
| 98 | |
| 99 | public Sequence<?> getSequence() { |
| 100 | switch (list.size()) { |
| 101 | case 0: |
| 102 | return EmptySequence.getInstance(); |
| 103 | case 1: |
| 104 | //noinspection unchecked |
| 105 | return list.get(0); |
| 106 | default: |
| 107 | return new SequenceExtent<>(list); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get an iterator over the sequence of items that has been constructed |
| 113 | * |
| 114 | * @return an iterator over the items that have been written to this SequenceOutputter |
| 115 | */ |
| 116 | |
| 117 | public SequenceIterator<Item<?>> iterate() { |
| 118 | if (list.isEmpty()) { |
| 119 | return EmptyIterator.emptyIterator(); |
| 120 | } else { |
| 121 | return new ListIterator<>(list); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Get the list containing the sequence of items |
| 127 | * |
| 128 | * @return the list of items that have been written to this SequenceOutputter |
| 129 | */ |
| 130 | |
| 131 | public List<Item<?>> getList() { |
| 132 | return list; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Get the first item in the sequence that has been built |
| 137 | * |
| 138 | * @return the first item in the list of items that have been written to this SequenceOutputter; |
| 139 | * or null if the list is empty. |
| 140 | */ |
| 141 | |
| 142 | public Item getFirstItem() { |
| 143 | if (list.isEmpty()) { |
| 144 | return null; |
| 145 | } else { |
| 146 | return list.get(0); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Get the last item in the sequence that has been built, and remove it |
| 152 | * |
| 153 | * @return the last item written |
| 154 | */ |
| 155 | |
| 156 | public Item popLastItem() { |
| 157 | if (list.isEmpty()) { |
| 158 | return null; |
| 159 | } else { |
| 160 | return list.remove(list.size() - 1); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | |
| 165 | } |
| 166 |