| 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.event; |
| 9 | |
| 10 | import net.sf.saxon.expr.parser.Location; |
| 11 | import net.sf.saxon.om.NodeName; |
| 12 | import net.sf.saxon.trans.XPathException; |
| 13 | import net.sf.saxon.type.SchemaType; |
| 14 | |
| 15 | /** |
| 16 | * A Valve is a general-purpose component for use in a pipeline of receivers. It selects an alternative |
| 17 | * destination for the pipeline events based on the namespace of the first startElement event. |
| 18 | * |
| 19 | * There is a primary destination which is selected initially. If the namespace of the first element has |
| 20 | * a given value, then subsequent output is sent to an alternative destination. |
| 21 | * */ |
| 22 | |
| 23 | public class Valve extends ProxyReceiver { |
| 24 | |
| 25 | private boolean started = false; |
| 26 | private String testNamespace; |
| 27 | private Receiver alternativeReceiver; |
| 28 | |
| 29 | /** |
| 30 | * Create a {@code Valve}. Events sent to this {@code Valve} will be forwarded |
| 31 | * to the primary receiver, unless the namespace of the first element node matches |
| 32 | * the test namespace, in which case the events will be forwarded to the secondary receiver |
| 33 | * @param testNamespace the test namespace |
| 34 | * @param primary the primary Receiver |
| 35 | * @param secondary the secondary Receiver |
| 36 | */ |
| 37 | |
| 38 | public Valve(String testNamespace, Receiver primary, Receiver secondary) { |
| 39 | super(primary); |
| 40 | this.testNamespace = testNamespace; |
| 41 | this.alternativeReceiver = secondary; |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public void startElement(NodeName elemName, SchemaType typeCode, Location location, int properties) throws XPathException { |
| 46 | if (!started) { |
| 47 | started = true; |
| 48 | if (elemName.getURI().equals(testNamespace)) { |
| 49 | alternativeReceiver.open(); |
| 50 | alternativeReceiver.startDocument(0); |
| 51 | try { |
| 52 | getNextReceiver().close(); |
| 53 | } catch (XPathException err) { |
| 54 | // ignore the failure |
| 55 | } |
| 56 | setUnderlyingReceiver(alternativeReceiver); |
| 57 | } |
| 58 | } |
| 59 | super.startElement(elemName, typeCode, location, properties); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Ask (after the first element event has been received) whether output was diverted to the |
| 64 | * secondary receiver |
| 65 | * @return true if output was diverted, that is, if the first element node was in the test namespace. |
| 66 | */ |
| 67 | |
| 68 | public boolean wasDiverted() { |
| 69 | return getNextReceiver() == alternativeReceiver; |
| 70 | } |
| 71 | } |
| 72 |