Debian Codegraph @

net/sf/saxon/serialize/PrincipalOutputGatekeeper.java

raw symbols SCIP
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.serialize;
9
10 import net.sf.saxon.event.ProxyReceiver;
11 import net.sf.saxon.event.Receiver;
12 import net.sf.saxon.expr.parser.Location;
13 import net.sf.saxon.om.Item;
14 import net.sf.saxon.om.NodeName;
15 import net.sf.saxon.s9api.Destination;
16 import net.sf.saxon.s9api.SaxonApiException;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.trans.XsltController;
19 import net.sf.saxon.type.SchemaType;
20
21 /**
22 * This class is added to the output pipeline for the principal result destination for a transformation
23 * if the transformation uses xsl:result-document instructions. It is there to deal with the possibility
24 * that an xsl:result-document instruction will use the same output URI (or no output URI) in which case
25 * it is defined to write to the principal output destination, but potentially with different serialization
26 * parameters.
27 * <p>If actual output is produced to the principal result destination, then this {@code PrincipalOutputGatekeeper}
28 * will be marked as being "used by primary", which will cause any attempt to open a secondary result destination on the
29 * same URI (either explicitly, or by omitting the {@code href} attribute, or by setting {@code href} to
30 * a zero length string) as an error. Apart from this marker, the {@code PrincipalOutputGatekeeper} acts as
31 * a pass-through filter for the output events.</p>
32 * <p>If a secondary result destination is opened using the principal output URI (either explicitly or implicitly),
33 * then the {@code PrincipalOutputGatekeeper} is marked as being "used by secondary", which will cause any
34 * attempts to write events to the {@code PrincipalOutputGatekeeper} to fail. Secondary result output is not
35 * actually directed through the gatekeeper; the gatekeeper is merely notified of its existence.</p>
36 */
37
38 public class PrincipalOutputGatekeeper extends ProxyReceiver {
39
40 private XsltController controller;
41 private boolean usedAsPrimaryResult = false;
42 private boolean usedAsSecondaryResult = false;
43 private boolean open = false;
44 private boolean closed = false;
45
46
47 public PrincipalOutputGatekeeper(XsltController controller, Receiver next) {
48 super(next);
49 this.controller = controller;
50 }
51
52 public void open() throws XPathException {
53 if (closed) {
54 String uri = getSystemId().equals(XsltController.ANONYMOUS_PRINCIPAL_OUTPUT_URI) ? "(no URI supplied)" : getSystemId();
55 XPathException err = new XPathException(
56 "Cannot write more than one result document to the principal output destination: " + uri);
57 err.setErrorCode("XTDE1490");
58 throw err;
59 }
60 super.open();
61 open = true;
62 }
63
64 public synchronized void startDocument(int properties) throws XPathException {
65 if (!open) {
66 open();
67 }
68 //checkNotClosed();
69 nextReceiver.startDocument(properties);
70 }
71
72 public void startElement(NodeName nameCode, SchemaType typeCode, Location location, int properties) throws XPathException {
73 useAsPrimary();
74 nextReceiver.startElement(nameCode, typeCode, location, properties);
75 }
76
77 public synchronized void characters(CharSequence chars, Location locationId, int properties) throws XPathException {
78 useAsPrimary();
79 nextReceiver.characters(chars, locationId, properties);
80 }
81
82 public void processingInstruction(String target, CharSequence data, Location locationId, int properties) throws XPathException {
83 useAsPrimary();
84 nextReceiver.processingInstruction(target, data, locationId, properties);
85 }
86
87 public void comment(CharSequence chars, Location locationId, int properties) throws XPathException {
88 useAsPrimary();
89 nextReceiver.comment(chars, locationId, properties);
90 }
91
92 @Override
93 public void append(Item item, Location locationId, int copyNamespaces) throws XPathException {
94 useAsPrimary();
95 nextReceiver.append(item, locationId, copyNamespaces);
96 }
97
98 private synchronized void useAsPrimary() throws XPathException {
99 if (closed) {
100 XPathException err = new XPathException(
101 "Cannot write to the principal output destination as it has already been closed: " + identifySystemId());
102 err.setErrorCode("XTDE1490");
103 throw err;
104 }
105 if (usedAsSecondaryResult) {
106 XPathException err = new XPathException(
107 "Cannot write to the principal output destination as it has already been used by xsl:result-document: " + identifySystemId());
108 err.setErrorCode("XTDE1490");
109 throw err;
110 }
111 usedAsPrimaryResult = true;
112 }
113
114 public synchronized void useAsSecondary() throws XPathException {
115 if (usedAsPrimaryResult) {
116 XPathException err = new XPathException(
117 "Cannot use xsl:result-document to write to a destination already used for the principal output: " + identifySystemId());
118 err.setErrorCode("XTDE1490");
119 throw err;
120 }
121 if (usedAsSecondaryResult) {
122 XPathException err = new XPathException(
123 "Cannot write more than one xsl:result-document to the principal output destination: " + identifySystemId());
124 err.setErrorCode("XTDE1490");
125 throw err;
126 }
127 usedAsSecondaryResult = true;
128 }
129
130 public Receiver makeReceiver(SerializationProperties params) {
131 try {
132 Destination dest = controller.getPrincipalDestination();
133 if (dest != null) {
134 return dest.getReceiver(controller.makePipelineConfiguration(), params);
135 }
136 } catch (SaxonApiException e) {
137 return null;
138 }
139 return null;
140 }
141
142 private String identifySystemId() {
143 String uri = getSystemId();
144 return uri==null ? "(no URI supplied)" : uri;
145 }
146
147
148 public void close() throws XPathException {
149 closed = true;
150 if (usedAsPrimaryResult) {
151 nextReceiver.close();
152 }
153 }
154
155 }
156