| 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.expr.parser.Location; |
| 11 | import net.sf.saxon.om.*; |
| 12 | import net.sf.saxon.trans.XPathException; |
| 13 | import net.sf.saxon.type.SchemaType; |
| 14 | import net.sf.saxon.type.SimpleType; |
| 15 | |
| 16 | /** |
| 17 | * TeeOutputter: a SequenceReceiver that duplicates received events to two different destinations |
| 18 | */ |
| 19 | |
| 20 | public class TeeOutputter extends SequenceReceiver { |
| 21 | |
| 22 | Receiver seq1; |
| 23 | Receiver seq2; |
| 24 | |
| 25 | public TeeOutputter(Receiver seq1, Receiver seq2) { |
| 26 | super(seq1.getPipelineConfiguration()); |
| 27 | this.seq1 = seq1; |
| 28 | this.seq2 = seq2; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Set the first destination |
| 33 | * |
| 34 | * @param seq1 the first output destination |
| 35 | */ |
| 36 | |
| 37 | protected void setFirstDestination(Receiver seq1) { |
| 38 | this.seq1 = seq1; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Set the second destination |
| 43 | * |
| 44 | * @param seq2 the second output destination |
| 45 | */ |
| 46 | |
| 47 | protected void setSecondDestination(Receiver seq2) { |
| 48 | this.seq2 = seq2; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get the first destination |
| 53 | * |
| 54 | * @return the first output destination |
| 55 | */ |
| 56 | |
| 57 | protected Receiver getFirstDestination() { |
| 58 | return seq1; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get the second destination |
| 63 | * |
| 64 | * @return the second output destination |
| 65 | */ |
| 66 | |
| 67 | protected Receiver getSecondDestination() { |
| 68 | return seq2; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Pass on information about unparsed entities |
| 73 | * @param name The name of the unparsed entity |
| 74 | * @param systemID The system identifier of the unparsed entity |
| 75 | * @param publicID The public identifier of the unparsed entity |
| 76 | * @throws XPathException in the event of an error |
| 77 | */ |
| 78 | |
| 79 | @Override |
| 80 | public void setUnparsedEntity(String name, String systemID, String publicID) throws XPathException { |
| 81 | seq1.setUnparsedEntity(name, systemID, publicID); |
| 82 | seq2.setUnparsedEntity(name, systemID, publicID); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Output an item (atomic value or node) to the sequence |
| 87 | */ |
| 88 | |
| 89 | public void append(Item item, Location locationId, int properties) throws XPathException { |
| 90 | seq1.append(item, locationId, properties); |
| 91 | seq2.append(item, locationId, properties); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void open() throws XPathException { |
| 96 | super.open(); |
| 97 | seq1.open(); |
| 98 | seq2.open(); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Notify the start of a document node |
| 103 | */ |
| 104 | |
| 105 | public void startDocument(int properties) throws XPathException { |
| 106 | seq1.startDocument(properties); |
| 107 | seq2.startDocument(properties); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Notify the end of a document node |
| 112 | */ |
| 113 | |
| 114 | public void endDocument() throws XPathException { |
| 115 | seq1.endDocument(); |
| 116 | seq2.endDocument(); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Notify the start of an element |
| 121 | * @param nameCode integer code identifying the name of the element within the name pool. |
| 122 | * @param typeCode integer code identifying the element's type within the name pool. The value -1 |
| 123 | * indicates the default type, xs:untyped. |
| 124 | * @param location |
| 125 | * @param properties bit-significant properties of the element node. If there are no revelant |
| 126 | */ |
| 127 | |
| 128 | public void startElement(NodeName nameCode, SchemaType typeCode, Location location, int properties) throws XPathException { |
| 129 | seq1.startElement(nameCode, typeCode, location, properties); |
| 130 | seq2.startElement(nameCode, typeCode, location, properties); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Notify a namespace. Namespaces are notified <b>after</b> the startElement event, and before |
| 135 | * any children for the element. The namespaces that are reported are only required |
| 136 | * to include those that are different from the parent element; however, duplicates may be reported. |
| 137 | * A namespace must not conflict with any namespaces already used for element or attribute names. |
| 138 | * |
| 139 | * @param namespaceBindings the prefix/uri pair |
| 140 | * @throws IllegalStateException: attempt to output a namespace when there is no open element |
| 141 | * start tag |
| 142 | */ |
| 143 | |
| 144 | public void namespace(NamespaceBindingSet namespaceBindings, int properties) throws XPathException { |
| 145 | seq1.namespace(namespaceBindings, properties); |
| 146 | seq2.namespace(namespaceBindings, properties); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Notify an attribute. Attributes are notified after the startElement event, and before any |
| 151 | * children. Namespaces and attributes may be intermingled. |
| 152 | * |
| 153 | * @param nameCode The name of the attribute |
| 154 | * @param typeCode The type of the attribute |
| 155 | * @param locationId an integer which can be interpreted using a LocationMap to return |
| 156 | * information such as line number and system ID. If no location information is available, |
| 157 | * the value zero is supplied. |
| 158 | * @param properties Bit significant value. The following bits are defined: |
| 159 | * <dl> |
| 160 | * <dt>DISABLE_ESCAPING</dt> <dd>Disable escaping for this attribute</dd> |
| 161 | * <dt>NO_SPECIAL_CHARACTERS</dt> <dd>Attribute value contains no special characters</dd> |
| 162 | * </dl> |
| 163 | * @throws IllegalStateException: attempt to output an attribute when there is no open element |
| 164 | * start tag |
| 165 | */ |
| 166 | |
| 167 | public void attribute(NodeName nameCode, SimpleType typeCode, CharSequence value, Location locationId, int properties) throws XPathException { |
| 168 | seq1.attribute(nameCode, typeCode, value, locationId, properties); |
| 169 | seq2.attribute(nameCode, typeCode, value, locationId, properties); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Notify the start of the content, that is, the completion of all attributes and namespaces. |
| 174 | * Note that the initial receiver of output from XSLT instructions will not receive this event, |
| 175 | * it has to detect it itself. Note that this event is reported for every element even if it has |
| 176 | * no attributes, no namespaces, and no content. |
| 177 | */ |
| 178 | |
| 179 | public void startContent() throws XPathException { |
| 180 | seq1.startContent(); |
| 181 | seq2.startContent(); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Notify the end of an element. The receiver must maintain a stack if it needs to know which |
| 186 | * element is ending. |
| 187 | */ |
| 188 | |
| 189 | public void endElement() throws XPathException { |
| 190 | seq1.endElement(); |
| 191 | seq2.endElement(); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Notify character data. Note that some receivers may require the character data to be |
| 196 | * sent in a single event, but in general this is not a requirement. |
| 197 | * @param chars The characters |
| 198 | * @param locationId an integer which can be interpreted using a LocationMap to return |
| 199 | * information such as line number and system ID. If no location information is available, |
| 200 | * the value zero is supplied. |
| 201 | * @param properties Bit significant value. The following bits are defined: |
| 202 | * <dl> |
| 203 | * <dt>DISABLE_ESCAPING</dt> <dd>Disable escaping for this text node</dd> |
| 204 | * </dl> |
| 205 | */ |
| 206 | |
| 207 | public void characters(CharSequence chars, Location locationId, int properties) throws XPathException { |
| 208 | seq1.characters(chars, locationId, properties); |
| 209 | seq2.characters(chars, locationId, properties); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Output a processing instruction |
| 214 | * |
| 215 | * @param name The PI name. This must be a legal name (it will not be checked). |
| 216 | * @param data The data portion of the processing instruction |
| 217 | * @param locationId an integer which can be interpreted using a LocationMap to return |
| 218 | * information such as line number and system ID. If no location information is available, |
| 219 | * the value zero is supplied. |
| 220 | * @param properties Additional information about the PI. The following bits are |
| 221 | * defined: |
| 222 | * <dl> |
| 223 | * <dt>CHECKED</dt> <dd>Data is known to be legal (e.g. doesn't contain "?>")</dd> |
| 224 | * </dl> |
| 225 | * @throws IllegalArgumentException: the content is invalid for an XML processing instruction |
| 226 | */ |
| 227 | |
| 228 | public void processingInstruction(String name, CharSequence data, Location locationId, int properties) throws XPathException { |
| 229 | seq1.processingInstruction(name, data, locationId, properties); |
| 230 | seq2.processingInstruction(name, data, locationId, properties); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Notify a comment. Comments are only notified if they are outside the DTD. |
| 235 | * |
| 236 | * @param content The content of the comment |
| 237 | * @param locationId an integer which can be interpreted using a LocationMap to return |
| 238 | * information such as line number and system ID. If no location information is available, |
| 239 | * the value zero is supplied. |
| 240 | * @param properties Additional information about the comment. The following bits are |
| 241 | * defined: |
| 242 | * <dl> |
| 243 | * <dt>CHECKED</dt> <dd>Comment is known to be legal (e.g. doesn't contain "--")</dd> |
| 244 | * </dl> |
| 245 | * @throws IllegalArgumentException: the content is invalid for an XML comment |
| 246 | */ |
| 247 | |
| 248 | public void comment(CharSequence content, Location locationId, int properties) throws XPathException { |
| 249 | seq1.comment(content, locationId, properties); |
| 250 | seq2.comment(content, locationId, properties); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Notify the end of the event stream |
| 255 | */ |
| 256 | |
| 257 | public void close() throws XPathException { |
| 258 | seq1.close(); |
| 259 | seq2.close(); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Ask whether this Receiver (or the downstream pipeline) makes any use of the type annotations |
| 264 | * supplied on element and attribute events |
| 265 | * |
| 266 | * @return true if the Receiver makes any use of this information. If false, the caller |
| 267 | * may supply untyped nodes instead of supplying the type annotation |
| 268 | */ |
| 269 | |
| 270 | public boolean usesTypeAnnotations() { |
| 271 | return seq1.usesTypeAnnotations() || seq2.usesTypeAnnotations(); |
| 272 | } |
| 273 | } |
| 274 |