| 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.ExplicitLocation; |
| 11 | import net.sf.saxon.expr.parser.Location; |
| 12 | import net.sf.saxon.om.Item; |
| 13 | import net.sf.saxon.om.NamespaceBindingSet; |
| 14 | import net.sf.saxon.om.NodeName; |
| 15 | import net.sf.saxon.trans.XPathException; |
| 16 | import net.sf.saxon.type.SchemaType; |
| 17 | import net.sf.saxon.type.SimpleType; |
| 18 | |
| 19 | import javax.xml.transform.Result; |
| 20 | |
| 21 | /** |
| 22 | * Receiver: This interface represents a recipient of XML tree-walking (push) events. It is |
| 23 | * based on SAX2's ContentHandler, but adapted to handle additional events. Namespaces and |
| 24 | * Attributes are handled by separate events following the startElement event. Schema types |
| 25 | * can be defined for elements and attributes. |
| 26 | * <p>The Receiver interface is an important internal interface within Saxon, and provides a powerful |
| 27 | * mechanism for integrating Saxon with other applications. It has been designed with extensibility |
| 28 | * and stability in mind. However, it should be considered as an interface designed primarily for |
| 29 | * internal use, and not as a completely stable part of the public Saxon API.</p> |
| 30 | * |
| 31 | * @since 8.0. Extended in 9.9 to support additional methods with default implementations. |
| 32 | */ |
| 33 | |
| 34 | public interface Receiver extends Result { |
| 35 | |
| 36 | /** |
| 37 | * Set the pipeline configuration |
| 38 | * |
| 39 | * @param pipe the pipeline configuration |
| 40 | */ |
| 41 | |
| 42 | void setPipelineConfiguration(/*@NotNull*/ PipelineConfiguration pipe); |
| 43 | |
| 44 | /** |
| 45 | * Get the pipeline configuration |
| 46 | * |
| 47 | * @return the pipeline configuration |
| 48 | */ |
| 49 | |
| 50 | /*@NotNull*/ |
| 51 | PipelineConfiguration getPipelineConfiguration(); |
| 52 | |
| 53 | /** |
| 54 | * Set the System ID of the tree represented by this event stream |
| 55 | * |
| 56 | * @param systemId the system ID (which is used as the base URI of the nodes |
| 57 | * if there is no xml:base attribute) |
| 58 | */ |
| 59 | |
| 60 | void setSystemId(String systemId); |
| 61 | |
| 62 | /** |
| 63 | * Notify the start of the event stream |
| 64 | * |
| 65 | * @throws XPathException if an error occurs |
| 66 | */ |
| 67 | |
| 68 | void open() throws XPathException; |
| 69 | |
| 70 | /** |
| 71 | * Notify the start of a document node |
| 72 | * |
| 73 | * @param properties bit-significant integer indicating properties of the document node. |
| 74 | * The definitions of the bits are in class {@link ReceiverOptions} |
| 75 | * @throws XPathException if an error occurs |
| 76 | */ |
| 77 | |
| 78 | void startDocument(int properties) throws XPathException; |
| 79 | |
| 80 | /** |
| 81 | * Notify the end of a document node |
| 82 | * |
| 83 | * @throws XPathException if an error occurs |
| 84 | */ |
| 85 | |
| 86 | void endDocument() throws XPathException; |
| 87 | |
| 88 | /** |
| 89 | * Notify an unparsed entity URI. |
| 90 | * |
| 91 | * @param name The name of the unparsed entity |
| 92 | * @param systemID The system identifier of the unparsed entity |
| 93 | * @param publicID The identifier of the unparsed entity |
| 94 | * @throws XPathException if an error occurs |
| 95 | */ |
| 96 | |
| 97 | void setUnparsedEntity(String name, String systemID, String publicID) throws XPathException; |
| 98 | |
| 99 | /** |
| 100 | * Notify the start of an element |
| 101 | * |
| 102 | * @param elemName the name of the element. |
| 103 | * @param typeCode the type annotation of the element. |
| 104 | * @param location an object providing information about the module, line, and column where the node originated |
| 105 | * @param properties bit-significant properties of the element node. If there are no revelant |
| 106 | * properties, zero is supplied. The definitions of the bits are in class {@link ReceiverOptions} |
| 107 | * @throws XPathException if an error occurs |
| 108 | */ |
| 109 | |
| 110 | void startElement(NodeName elemName, SchemaType typeCode, Location location, int properties) |
| 111 | throws XPathException; |
| 112 | |
| 113 | /** |
| 114 | * Notify one or more namespaces. Namespaces are notified <b>after</b> the startElement event, and before |
| 115 | * any children for the element. The namespaces that are reported are only required |
| 116 | * to include those that are different from the parent element. The events represent namespace |
| 117 | * declarations and undeclarations rather than in-scope namespace nodes: an undeclaration is represented |
| 118 | * by a namespace code of zero. If the sequence of namespace events contains two |
| 119 | * A namespace must not conflict with any namespaces already used for element or attribute names. |
| 120 | * |
| 121 | * @param namespaceBindings contains the namespace prefix and namespace URI |
| 122 | * @param properties The most important property is REJECT_DUPLICATES. If this property is set, the |
| 123 | * namespace declaration will be rejected if it conflicts with a previous declaration of the same |
| 124 | * prefix. If the property is not set, the namespace declaration will be ignored if it conflicts |
| 125 | * with a previous declaration. This reflects the fact that when copying a tree, namespaces for child |
| 126 | * elements are emitted before the namespaces of their parent element. Unfortunately this conflicts |
| 127 | * with the XSLT rule for complex content construction, where the recovery action in the event of |
| 128 | * conflicts is to take the namespace that comes last. XSLT therefore doesn't recover from this error: |
| 129 | * @throws XPathException if an error occurs |
| 130 | * @since 9.8: changed in 9.8 to allow multiple namespace bindings to be supplied in a single call |
| 131 | */ |
| 132 | |
| 133 | void namespace(NamespaceBindingSet namespaceBindings, int properties) throws XPathException; |
| 134 | |
| 135 | /** |
| 136 | * Notify an attribute. Attributes are notified after the startElement event, and before any |
| 137 | * children. Namespaces and attributes may be intermingled. |
| 138 | * |
| 139 | * @param attName The name of the attribute |
| 140 | * @param typeCode The type annotation of the attribute |
| 141 | * @param value the string value of the attribute |
| 142 | * @param location provides information such as line number and system ID. |
| 143 | * @param properties Bit significant value. The following bits are defined: |
| 144 | * <dl> |
| 145 | * <dt>DISABLE_ESCAPING</dt> <dd>Disable escaping for this attribute</dd> |
| 146 | * <dt>NO_SPECIAL_CHARACTERS</dt> <dd>Attribute value contains no special characters</dd> |
| 147 | * </dl> |
| 148 | * @throws IllegalStateException: attempt to output an attribute when there is no open element |
| 149 | * start tag |
| 150 | * @throws XPathException if an error occurs |
| 151 | */ |
| 152 | |
| 153 | void attribute(NodeName attName, SimpleType typeCode, CharSequence value, Location location, int properties) |
| 154 | throws XPathException; |
| 155 | |
| 156 | /** |
| 157 | * Notify the start of the content, that is, the completion of all attributes and namespaces. |
| 158 | * Note that the initial receiver of output from XSLT instructions will not receive this event, |
| 159 | * it has to detect it itself. Note that this event is reported for every element even if it has |
| 160 | * no attributes, no namespaces, and no content. |
| 161 | * |
| 162 | * @throws XPathException if an error occurs |
| 163 | */ |
| 164 | |
| 165 | void startContent() throws XPathException; |
| 166 | |
| 167 | /** |
| 168 | * Notify the end of an element. The receiver must maintain a stack if it needs to know which |
| 169 | * element is ending. |
| 170 | * |
| 171 | * @throws XPathException if an error occurs |
| 172 | */ |
| 173 | |
| 174 | void endElement() throws XPathException; |
| 175 | |
| 176 | /** |
| 177 | * Notify character data. Note that some receivers may require the character data to be |
| 178 | * sent in a single event, but in general this is not a requirement. |
| 179 | * |
| 180 | * @param chars The characters |
| 181 | * @param location provides information such as line number and system ID. |
| 182 | * @param properties Bit significant value. The following bits are defined: |
| 183 | * <dl> |
| 184 | * <dt>DISABLE_ESCAPING</dt> <dd>Disable escaping for this text node</dd> |
| 185 | * <dt>USE_CDATA</dt> <dd>Output as a CDATA section</dd> |
| 186 | * </dl> |
| 187 | * @throws XPathException if an error occurs |
| 188 | */ |
| 189 | |
| 190 | void characters(CharSequence chars, Location location, int properties) |
| 191 | throws XPathException; |
| 192 | |
| 193 | /** |
| 194 | * Output a processing instruction |
| 195 | * |
| 196 | * @param name The PI name. This must be a legal name (it will not be checked). |
| 197 | * @param data The data portion of the processing instruction |
| 198 | * @param location provides information such as line number and system ID. |
| 199 | * @param properties Additional information about the PI. |
| 200 | * @throws IllegalArgumentException: the content is invalid for an XML processing instruction |
| 201 | * @throws XPathException if an error occurs |
| 202 | */ |
| 203 | |
| 204 | void processingInstruction(String name, CharSequence data, Location location, int properties) |
| 205 | throws XPathException; |
| 206 | |
| 207 | /** |
| 208 | * Notify a comment. Comments are only notified if they are outside the DTD. |
| 209 | * |
| 210 | * @param content The content of the comment |
| 211 | * @param location provides information such as line number and system ID. |
| 212 | * @param properties Additional information about the comment. |
| 213 | * @throws IllegalArgumentException: the content is invalid for an XML comment |
| 214 | * @throws XPathException if an error occurs |
| 215 | */ |
| 216 | |
| 217 | void comment(CharSequence content, Location location, int properties) throws XPathException; |
| 218 | |
| 219 | /** |
| 220 | * Append an arbitrary item (node, atomic value, or function) to the output. The default |
| 221 | * implementation throws {@code UnsupportedOperationException}. |
| 222 | * |
| 223 | * @param item the item to be appended |
| 224 | * @param locationId the location of the calling instruction, for diagnostics |
| 225 | * @param properties if the item is an element node, this indicates whether its namespaces |
| 226 | * need to be copied. Values are {@link ReceiverOptions#ALL_NAMESPACES}, |
| 227 | * {@link ReceiverOptions#LOCAL_NAMESPACES}; the default (0) means |
| 228 | * no namespaces |
| 229 | */ |
| 230 | |
| 231 | default void append(Item item, Location locationId, int properties) throws XPathException { |
| 232 | throw new UnsupportedOperationException(); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Append an arbitrary item (node, atomic value, or function) to the output. |
| 237 | * By default, if the item is an element node, it is copied with all namespaces. |
| 238 | * The default implementation calls {@link #append(Item, Location, int)}, whose |
| 239 | * default implementation throws {@code UnsupportedOperationException} |
| 240 | * |
| 241 | * @param item the item to be appended |
| 242 | * @throws net.sf.saxon.trans.XPathException if the operation fails |
| 243 | */ |
| 244 | |
| 245 | default void append(Item item) throws XPathException { |
| 246 | append(item, ExplicitLocation.UNKNOWN_LOCATION, ReceiverOptions.ALL_NAMESPACES); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Notify the end of the event stream |
| 251 | * |
| 252 | * @throws XPathException if an error occurs |
| 253 | */ |
| 254 | |
| 255 | void close() throws XPathException; |
| 256 | |
| 257 | /** |
| 258 | * Ask whether this Receiver (or the downstream pipeline) makes any use of the type annotations |
| 259 | * supplied on element and attribute events. The default implementation of this method |
| 260 | * returns false. |
| 261 | * |
| 262 | * @return true if the Receiver makes any use of this information. If false, the caller |
| 263 | * may supply untyped nodes instead of supplying the type annotation (or conversely, it may |
| 264 | * avoid stripping unwanted type annotations) |
| 265 | */ |
| 266 | |
| 267 | default boolean usesTypeAnnotations() { |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Ask whether this Receiver can handle arbitrary items in its {@link #append} and |
| 273 | * {@link #append(Item, Location, int)} methods. If it cannot, then calling |
| 274 | * these methods will raise an exception (typically but not necessarily an |
| 275 | * {@code UnsupportedOperationException}). The default implementation of this |
| 276 | * method returns false. |
| 277 | * |
| 278 | * @return true if the Receiver is able to handle items supplied to |
| 279 | * its {@link #append} and {@link #append(Item, Location, int)} methods. A |
| 280 | * receiver that returns true may still reject some kinds of item, for example |
| 281 | * it may reject function items. |
| 282 | */ |
| 283 | |
| 284 | default boolean handlesAppend() { |
| 285 | return false; |
| 286 | } |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | } |
| 292 |