Debian Codegraph @

net/sf/saxon/s9api/Destination.java

raw symbols SCIP
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.s9api;
9
10 import net.sf.saxon.event.PipelineConfiguration;
11 import net.sf.saxon.event.Receiver;
12 import net.sf.saxon.serialize.SerializationProperties;
13
14 import java.net.URI;
15 import java.util.function.Consumer;
16
17 /**
18 * A <tt>Destination</tt> represents a place where XDM values can be sent. It is used, for example,
19 * to define the output of a transformation or query.
20 * <p>
21 * A {@code Destination} is either a tree destination or a raw destination. A <b>tree destination</b>
22 * performs <i>sequence normalization</i> on the stream of events passed to it, to construct
23 * a tree rooted at a single XDM document node, as defined in the W3C serialization specification
24 * (even if the destination is not actually a serializer). A <b>raw destination</b> omits this step.
25 * Examples of tree destinations are those designed to accept XML: {@link DOMDestination},
26 * {@link SAXDestination}, {@link XdmDestination}, {@link SchemaValidator}.
27 * <p>
28 * The {@link Serializer} acts as a tree destination when the output methods XML, HTML, XHTML, or TEXT
29 * are used, but as a raw destination when the output method is JSON or ADAPTIVE.
30 * <p>
31 * The interface {@code Destination} has some similarities with the JAXP
32 * {@link javax.xml.transform.Result} class. It differs, however, in that implementations
33 * of this interface can be written by users or third parties to define new kinds of
34 * destination, and any such implementation can be supplied to the Saxon methods that
35 * take a {@code Destination} as an argument.
36 * <p>
37 * Implementing a new {@code Destination} will generally require access
38 * to implementation-level classes and methods within the Saxon product. The only method that
39 * needs to be supplied is {@link #getReceiver}, which returns an instance of
40 * {@link Receiver}, and unless you use an existing implementation of
41 * {@code Receiver}, you will need to handle internal Saxon concepts such as name codes
42 * and name pools.
43 * <p>
44 * In general a Destination is not thread-safe (cannot be used from more than one thread),
45 * and is not serially reusable. So a Destination should only be used once. A Destination
46 * supplied to Saxon may be modified by Saxon.
47 * <p>
48 * The {@link #close} method is called by the system when
49 * it finishes writing the document, and this should cause all resources held by the Destination
50 * to be released.
51 */
52 public interface Destination {
53
54 /**
55 * Set the base URI of the resource being written to this destination
56 * @param baseURI the base URI to be used
57 */
58
59 void setDestinationBaseURI(URI baseURI);
60
61 /**
62 * Get the base URI of the resource being written to this destination
63 * @return the baseURI, or null if none is known
64 */
65
66 URI getDestinationBaseURI();
67
68 /**
69 * Return a {@link Receiver}. Saxon calls this method to obtain a Receiver, to which it then sends
70 * a sequence of events representing an XDM value. The method is intended
71 * primarily for internal use, and may give poor diagnostics if used incorrectly.
72 * <p>
73 * This method is normally only called once. However, in the case where a stylesheet includes
74 * a call of <code>xsl:result-document</code> with no <code>href</code> attribute (or with an
75 * <code>href</code> attribute that resolves to the base output URI of the transformation), the
76 * method may be called a second time (with a potentially different set of serialization parameters,
77 * and perhaps a different validation request) to return a second {@link Receiver}, which will typically
78 * write to the same destination. The XSLT rules ensure that it is not possible to write principal and
79 * secondary output to the same destination, so only one of these Receivers will actually be used.
80 *
81 * @param pipe The pipeline configuration. This is supplied so that the destination can
82 * use information from the configuration (for example, a reference to the name pool)
83 * to construct or configure the returned Receiver.
84 * @param params Serialization parameters known to the caller of the method; typically,
85 * output properties defined in a stylesheet or query. These will mainly
86 * be of interest if the destination is performing serialization, but some
87 * properties (such as {@code item-separator}) are also used in other
88 * situations. These properties are typically subordinate to any properties
89 * defined on the (serializer) destination itself: for example if {@code indent=yes}
90 * was explicitly specified on a {@code Serializer}, this takes precedence
91 * over {@code indent=no} defined in a query or stylesheet.
92 * <p>
93 * The {@link SerializationProperties} object may also contain a factory object for
94 * generating a validator to add to the output pipeline. The {@link Destination} object
95 * is responsible for instantiating this validator and inserting it into the pipeline.
96 * In most cases this is done by invoking the helper method
97 * {@link SerializationProperties#makeSequenceNormalizer(Receiver)}. Validation can be skipped
98 * in the case of non-XML destinations.
99 * @return the Receiver to which events are to be sent.
100 * <p>It is the caller's responsibility to
101 * initialize this Receiver with a {@link net.sf.saxon.event.PipelineConfiguration} before calling
102 * its {@code open()} method.</p>
103 * <p>The {@code Receiver} is expected to handle a <b>regular event sequence</b> as defined in
104 * {@link net.sf.saxon.event.RegularSequenceChecker}. It is the caller's responsibility to
105 * ensure that the sequence of calls to the {@code Receiver} satisfies these rules, and it
106 * is the responsibility of the implementation to accept any sequence conforming these rules;
107 * the implementation is not expected to check that the sequence is valid, but it can do so
108 * if it wishes by inserting a {@link net.sf.saxon.event.RegularSequenceChecker} into the pipeline.</p>
109 * <p>The sequence of events passed to the {@code Receiver} represents the <b>raw results</b> of
110 * the query or transformation. If the {@code Destination} is to perform sequence normalization,
111 * this is typically done by returning a {@link net.sf.saxon.event.SequenceNormalizer} as the
112 * result of this method.</p>
113 * <p>The returned {@code Receiver} is responsible for ensuring that when its {@link Receiver#close()}
114 * method is called, this results in all registered {@code onClose} actions being invoked.
115 * An implementation returning a {@code SequenceNormalizer} can achieve this by registering
116 * the actions with the {@link net.sf.saxon.event.SequenceNormalizer#onClose} method.</p>
117 * <p>Only a single call on this method will be made during the lifetime of the {@code Destination}
118 * object, with the exception of the case noted above where a secondary result document is written
119 * to the same destination as the principal transformation result.</p>
120 * @throws SaxonApiException if the Receiver cannot be created
121 */
122
123 Receiver getReceiver(PipelineConfiguration pipe, SerializationProperties params) throws SaxonApiException;
124
125 /**
126 * Register a listener to be notified when a {@link Receiver} linked to this destination is closed.
127 * <p>Example: <code>destination.onClose(() -&gt; System.out.println("Finished writing to " + uri)</code></p>
128 * <p>The method must be called before the call on {@link #getReceiver(PipelineConfiguration, SerializationProperties)}; the
129 * effect of calling it after getting a {@code Receiver}, but before closing the
130 * {@link Receiver}, is undefined.</p>
131 *
132 * @param listener an object to be notified when writing to the destination
133 * is successfully completed
134 */
135
136 void onClose(Action listener);
137
138 /**
139 * Close the destination and notify all registered listeners that it has been closed.
140 * This method is intended for internal use by Saxon. The method first calls {@link #close}
141 * to close the destination, then it calls {@link Consumer#accept} on each of the
142 * listeners in turn to notify the fact that it has been closed.
143 * @throws SaxonApiException if the close() method throws {@code SaxonApiException}.
144 */
145
146 void closeAndNotify() throws SaxonApiException;
147
148 /**
149 * Close the destination, allowing resources to be released. Saxon calls this method when
150 * it has finished writing to the destination.
151 * <p>
152 * The {@code close()} method should not cause any adverse effects if it is called more than
153 * once. If any other method is called after the {@code close()} call, the results are undefined.
154 * This means that a Destination is not, in general, serially reusable.
155 * <p>
156 * If an {@link #onClose} action has been associated with the destination,
157 * this will be called after the destination is closed.</p>
158 *
159 * @throws SaxonApiException if any failure occurs
160 */
161
162 void close() throws SaxonApiException;
163
164 }
165