| 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.ContentHandlerProxy; |
| 11 | import net.sf.saxon.event.PipelineConfiguration; |
| 12 | import net.sf.saxon.event.Receiver; |
| 13 | import net.sf.saxon.serialize.SerializationProperties; |
| 14 | import org.xml.sax.ContentHandler; |
| 15 | |
| 16 | |
| 17 | /** |
| 18 | * This class represents a {@link Destination} (for example, the destination of the output of a transformation) |
| 19 | * in which events representing the XML document are sent to a user-supplied SAX2 {@link ContentHandler}, as |
| 20 | * if the {@code ContentHandler} were receiving the document directly from an XML parser. |
| 21 | * <p>If the supplied {@code ContentHandler} implements the {@link org.xml.sax.ext.LexicalHandler} interface, |
| 22 | * then comments nodes will be notified to the handler; if not, comments will be silently ignored.</p> |
| 23 | * <p>Namespace-related information is reported to the {@code ContentHandler} following the conventions of |
| 24 | * an {@link org.xml.sax.XMLReader} configured with default values for the features |
| 25 | * {@code http://xml.org/sax/features/namespaces} and {@code http://xml.org/sax/features/namespace-prefixes}.</p> |
| 26 | * <p>If and only if the supplied {@code ContentHandler} implements the {@link javax.xml.transform.sax.TransformerHandler} |
| 27 | * interface, then unparsed entities may be notified to the {@link org.xml.sax.DTDHandler#unparsedEntityDecl(String, String, String, String)} |
| 28 | * method.</p> |
| 29 | */ |
| 30 | |
| 31 | public class SAXDestination extends AbstractDestination { |
| 32 | |
| 33 | private ContentHandler contentHandler; |
| 34 | |
| 35 | /** |
| 36 | * Create a SAXDestination, supplying a SAX ContentHandler to which |
| 37 | * events will be routed |
| 38 | * |
| 39 | * @param handler the SAX ContentHandler that is to receive the output. If the |
| 40 | * ContentHandler is also a {@link org.xml.sax.ext.LexicalHandler} then it will also receive |
| 41 | * notification of events such as comments. |
| 42 | */ |
| 43 | |
| 44 | public SAXDestination(ContentHandler handler) { |
| 45 | contentHandler = handler; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Return a Receiver. Saxon calls this method to obtain a Receiver, to which it then sends |
| 50 | * a sequence of events representing the content of an XML document. |
| 51 | * |
| 52 | * @param pipe The Saxon configuration. This is supplied so that the destination can |
| 53 | * use information from the configuration (for example, a reference to the name pool) |
| 54 | * to construct or configure the returned Receiver. |
| 55 | * @param params Seriaization properties. Since this destination is not a serializing destination. |
| 56 | * the main property of interest is {@code item-separator}. |
| 57 | * @return the Receiver to which events are to be sent. |
| 58 | */ |
| 59 | |
| 60 | /*@NotNull*/ |
| 61 | public Receiver getReceiver(PipelineConfiguration pipe, SerializationProperties params) { |
| 62 | ContentHandlerProxy chp = new ContentHandlerProxy(); |
| 63 | chp.setUnderlyingContentHandler(contentHandler); |
| 64 | chp.setPipelineConfiguration(pipe); |
| 65 | return params.makeSequenceNormalizer(chp); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Close the destination, allowing resources to be released. Saxon calls this method when |
| 70 | * it has finished writing to the destination. |
| 71 | */ |
| 72 | |
| 73 | public void close() { |
| 74 | // no action |
| 75 | } |
| 76 | } |
| 77 |