| 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.stax; |
| 9 | |
| 10 | import net.sf.saxon.event.PipelineConfiguration; |
| 11 | import net.sf.saxon.event.Receiver; |
| 12 | import net.sf.saxon.s9api.AbstractDestination; |
| 13 | import net.sf.saxon.s9api.Destination; |
| 14 | import net.sf.saxon.s9api.SaxonApiException; |
| 15 | import net.sf.saxon.serialize.SerializationProperties; |
| 16 | |
| 17 | import javax.xml.stream.XMLStreamException; |
| 18 | import javax.xml.stream.XMLStreamWriter; |
| 19 | |
| 20 | /** |
| 21 | * XMLStreamWriterDestination is a s9api {@link Destination} that writes output to a supplied XMLStreamWriter |
| 22 | */ |
| 23 | public class XMLStreamWriterDestination extends AbstractDestination { |
| 24 | |
| 25 | private XMLStreamWriter writer; |
| 26 | |
| 27 | /** |
| 28 | * Create an XMLStreamWriterDestination based on a supplied XMLStreamWriter |
| 29 | * @param writer the supplied XmlStreamWriter |
| 30 | */ |
| 31 | |
| 32 | public XMLStreamWriterDestination(XMLStreamWriter writer) { |
| 33 | this.writer = writer; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Get the XMLStreamWriter to which this XMLStreamWriterDestination is writing |
| 38 | * @return the XMLStreamWriter that was provided as the destination of this |
| 39 | * XMLStreamWriterDestination |
| 40 | */ |
| 41 | |
| 42 | public XMLStreamWriter getXMLStreamWriter() { |
| 43 | return writer; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Return a Receiver. Saxon calls this method to obtain a Receiver, to which it then sends |
| 48 | * a sequence of events representing the content of an XML document. The method is intended |
| 49 | * primarily for internal use, and may give poor diagnostics if used incorrectly. |
| 50 | * |
| 51 | * @param pipe The Saxon configuration. This is supplied so that the destination can |
| 52 | * use information from the configuration (for example, a reference to the name pool) |
| 53 | * to construct or configure the returned Receiver. |
| 54 | * @param params |
| 55 | * @return the Receiver to which events are to be sent. It is the caller's responsibility to |
| 56 | * initialize this Receiver with a {@link net.sf.saxon.event.PipelineConfiguration} before calling |
| 57 | * its <code>open()</code> method. The caller is also responsible for ensuring that the sequence |
| 58 | * of events sent to the Receiver represents a well-formed document: in particular the event |
| 59 | * stream must include namespace events corresponding exactly to the namespace declarations |
| 60 | * that are required. If the calling code cannot guarantee this, it should insert a |
| 61 | * {@link net.sf.saxon.event.NamespaceReducer} into the pipeline in front of the returned |
| 62 | * Receiver. |
| 63 | * @throws SaxonApiException if the Receiver cannot be created |
| 64 | */ |
| 65 | |
| 66 | public Receiver getReceiver(PipelineConfiguration pipe, SerializationProperties params) throws SaxonApiException { |
| 67 | Receiver r = new ReceiverToXMLStreamWriter(writer); |
| 68 | r.setPipelineConfiguration(pipe); |
| 69 | return r; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Close the destination, allowing resources to be released. Saxon calls this method when |
| 74 | * it has finished writing to the destination. |
| 75 | * <p>The close() method should not cause any adverse effects if it is called more than |
| 76 | * once. If any other method is called after the close() call, the results are undefined. |
| 77 | * This means that a Destination is not, in general, serially reusable.</p> |
| 78 | * |
| 79 | * @throws SaxonApiException if any failure occurs |
| 80 | */ |
| 81 | |
| 82 | |
| 83 | public void close() throws SaxonApiException { |
| 84 | try { |
| 85 | writer.close(); |
| 86 | } catch (XMLStreamException e) { |
| 87 | throw new SaxonApiException(e); |
| 88 | } |
| 89 | } |
| 90 | } |