| 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.jaxp; |
| 9 | |
| 10 | import net.sf.saxon.event.PipelineConfiguration; |
| 11 | import net.sf.saxon.event.Receiver; |
| 12 | import net.sf.saxon.event.ReceiverWithOutputProperties; |
| 13 | import net.sf.saxon.event.SequenceNormalizer; |
| 14 | import net.sf.saxon.lib.SaxonOutputKeys; |
| 15 | import net.sf.saxon.s9api.AbstractDestination; |
| 16 | import net.sf.saxon.s9api.SaxonApiException; |
| 17 | import net.sf.saxon.serialize.SerializationProperties; |
| 18 | |
| 19 | /** |
| 20 | * An implementation of {@code Destination} that simply wraps a supplied {@code Receiver} |
| 21 | * <p>The supplied {@code Receiver} will be wrapped in a sequence normalizer unless requested |
| 22 | * otherwise. Specifically, it is wrapped unless the method {@link #acceptsRawOutput()} |
| 23 | * returns true.</p> |
| 24 | */ |
| 25 | |
| 26 | public class ReceivingDestination extends AbstractDestination { |
| 27 | |
| 28 | private Receiver outputTarget; |
| 29 | |
| 30 | /** |
| 31 | * Create a {@code ReceivingDestination} that wraps a supplied {@code Receiver} |
| 32 | * @param target the supplied {@code Receiver}. This must accept a <b>regular event sequence</b> |
| 33 | * as defined in {@link net.sf.saxon.event.RegularSequenceChecker} |
| 34 | */ |
| 35 | |
| 36 | public ReceivingDestination(Receiver target) { |
| 37 | this.outputTarget = target; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Get a {@code Receiver} to which events may be sent. For this implementation, this |
| 42 | * will be the wrapped {@code Receiver}. |
| 43 | * @param pipe The Saxon configuration. Not used in this implementation. |
| 44 | * @param properties The required serialization properties (not used) |
| 45 | * @return the wrapped {@code Receiver} |
| 46 | */ |
| 47 | |
| 48 | public Receiver getReceiver(PipelineConfiguration pipe, SerializationProperties properties) { |
| 49 | if (acceptsRawOutput()) { |
| 50 | return outputTarget; |
| 51 | } else { |
| 52 | return properties.makeSequenceNormalizer(outputTarget); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Ask whether this receiver accepts raw output, that is, an arbitrary sequence of items |
| 58 | * not necessarily forming an XML document or element tree. May be overridden in a subclass. |
| 59 | * @return false unless one of the following conditions is true: |
| 60 | * <ol> |
| 61 | * <li>The {@code Receiver} is an instance of {@link SequenceNormalizer}</li> |
| 62 | * <li>The {@code Receiver} implements {@link net.sf.saxon.event.ReceiverWithOutputProperties}, |
| 63 | * and a call on {@code getOutputProperties(SaxonOutputPropertes.REQUIRE_WELL_FORMED)} returns "no"</li> |
| 64 | * <li>The method {#acceptRawOutput()} is implemented in a subclass of {@code ReceivingDestination}, |
| 65 | * and returns false.</li> |
| 66 | * </ol> |
| 67 | */ |
| 68 | |
| 69 | public boolean acceptsRawOutput() { |
| 70 | if (outputTarget instanceof SequenceNormalizer) { |
| 71 | return true; |
| 72 | } |
| 73 | if (outputTarget instanceof ReceiverWithOutputProperties) { |
| 74 | return "no".equals(((ReceiverWithOutputProperties)outputTarget) |
| 75 | .getOutputProperties().getProperty(SaxonOutputKeys.REQUIRE_WELL_FORMED)); |
| 76 | } |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | public void close() throws SaxonApiException { |
| 81 | // try { |
| 82 | // outputTarget.close(); |
| 83 | // } catch (XPathException e) { |
| 84 | // throw new SaxonApiException(e); |
| 85 | // } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 |