| 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.CloseNotifier; |
| 11 | import net.sf.saxon.event.PipelineConfiguration; |
| 12 | import net.sf.saxon.event.Receiver; |
| 13 | import net.sf.saxon.event.SequenceOutputter; |
| 14 | import net.sf.saxon.serialize.SerializationProperties; |
| 15 | import net.sf.saxon.trans.XPathException; |
| 16 | |
| 17 | /** |
| 18 | * An <code>RawDestination</code> is a {@link Destination} that accepts a sequence output |
| 19 | * by a stylesheet or query and returns it directly as an {@link XdmValue}, without |
| 20 | * constructing an XML tree, and without serialization. It corresponds to the serialization |
| 21 | * option <code>build-tree="no"</code> |
| 22 | */ |
| 23 | |
| 24 | public class RawDestination extends AbstractDestination { |
| 25 | |
| 26 | private SequenceOutputter sequenceOutputter; |
| 27 | private boolean closed = false; |
| 28 | |
| 29 | |
| 30 | public RawDestination() { |
| 31 | |
| 32 | } |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * Return a Receiver. Saxon calls this method to obtain a Receiver, to which it then sends |
| 37 | * a sequence of events representing the content of an XML document. |
| 38 | * |
| 39 | * @param pipe The Saxon configuration. This is supplied so that the destination can |
| 40 | * use information from the configuration (for example, a reference to the name pool) |
| 41 | * to construct or configure the returned Receiver. |
| 42 | * @param params |
| 43 | * @return the Receiver to which events are to be sent. |
| 44 | */ |
| 45 | |
| 46 | public Receiver getReceiver(PipelineConfiguration pipe, SerializationProperties params) { |
| 47 | // The Receiver returned by this method is a SequenceOutputter. The SequenceOutputter |
| 48 | // builds a list of all top-level items passed to it. A top-level document or element |
| 49 | // node can be passed as a sequence of events, in which case a ComplexContentOutputter |
| 50 | // is created to build the tree represented by these events; the root document or element |
| 51 | // node in this tree is then added to the same list as a composed items. On completion |
| 52 | // the sequence represented by the list of items is available by calling the getXmlValue() |
| 53 | // method. |
| 54 | sequenceOutputter = new SequenceOutputter(pipe); |
| 55 | closed = false; |
| 56 | helper.onClose(() -> closed=true); |
| 57 | return new CloseNotifier(sequenceOutputter, helper.getListeners()); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Close the destination, allowing resources to be released. Saxon calls this method when |
| 62 | * it has finished writing to the destination. |
| 63 | */ |
| 64 | |
| 65 | public void close() throws SaxonApiException { |
| 66 | try { |
| 67 | sequenceOutputter.close(); |
| 68 | closed = true; |
| 69 | } catch (XPathException e) { |
| 70 | throw new SaxonApiException(e); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Return the result sequence, after it has been constructed. |
| 76 | * <p>This method should not be called until the destination has been closed.</p> |
| 77 | * |
| 78 | * @return the the result sequence |
| 79 | * @throws IllegalStateException if called during the execution of the process that |
| 80 | * is writing the tree. |
| 81 | */ |
| 82 | |
| 83 | public XdmValue getXdmValue() { |
| 84 | if (!closed) { |
| 85 | throw new IllegalStateException("The result sequence has not yet been closed"); |
| 86 | } |
| 87 | return XdmValue.wrap(sequenceOutputter.getSequence()); |
| 88 | } |
| 89 | |
| 90 | } |
| 91 |