Debian Codegraph @

net/sf/saxon/event/TransformerReceiver.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.event;
9
10 import net.sf.saxon.Controller;
11 import net.sf.saxon.om.NodeInfo;
12 import net.sf.saxon.trans.XPathException;
13 import net.sf.saxon.trans.XsltController;
14
15 import javax.xml.transform.TransformerException;
16
17
18 /**
19 * <b>TransformerReceiver</b> is similar in concept to the JAXP TransformerHandler,
20 * except that it implements Saxon's Receiver interface rather than the standard
21 * SAX2 interface. This means that it allows nodes with type annotations to be
22 * passed down a pipeline from one transformation to another.
23 */
24
25 public class TransformerReceiver extends ProxyReceiver {
26
27 private XsltController controller;
28 private Builder builder;
29 private Receiver destination;
30
31 /**
32 * Create a TransformerReceiver and initialise variables.
33 *
34 * @param controller the Controller
35 */
36
37 public TransformerReceiver(XsltController controller) {
38 super(controller.makeBuilder());
39 this.controller = controller;
40 this.builder = (Builder) getNextReceiver();
41 this.builder.setUseEventLocation(false);
42 }
43
44 /**
45 * Start of event stream
46 */
47
48 public void open() throws XPathException {
49 builder.setSystemId(systemId);
50 Receiver stripper = controller.makeStripper(builder);
51 if (controller.isStylesheetStrippingTypeAnnotations()) {
52 stripper = controller.getConfiguration().getAnnotationStripper(stripper);
53 }
54 setUnderlyingReceiver(stripper);
55 nextReceiver.open();
56 }
57
58 /**
59 * Get the Controller used for this transformation
60 *
61 * @return the controller
62 */
63
64 public Controller getController() {
65 return controller;
66 }
67
68 /**
69 * Set the SystemId of the document
70 */
71
72 public void setSystemId(String systemId) {
73 super.setSystemId(systemId);
74 controller.setBaseOutputURI(systemId);
75 }
76
77 /**
78 * Set the output destination of the transformation. This method must be called before
79 * the transformation can proceed.
80 *
81 * @param destination the destination to which the transformation output will be written
82 */
83
84 public void setDestination(Receiver destination) {
85 this.destination = destination;
86 }
87
88 /**
89 * Get the output destination of the transformation
90 *
91 * @return the output destination. May be null if no destination has been set.
92 */
93
94 /*@Nullable*/
95 public Receiver getDestination() {
96 return destination;
97 }
98
99 /**
100 * Override the behaviour of close() in ProxyReceiver, so that it fires off
101 * the transformation of the constructed document
102 */
103
104 public void close() throws XPathException {
105 if (builder == null) {
106 // already closed
107 } else {
108 nextReceiver.close();
109 NodeInfo doc = builder.getCurrentRoot();
110 builder.reset();
111 builder = null;
112 if (doc == null) {
113 throw new XPathException("No source document has been built");
114 }
115 doc.getTreeInfo().setSpaceStrippingRule(controller.getSpaceStrippingRule());
116 if (destination == null) {
117 throw new XPathException("No output destination has been supplied");
118 }
119 try {
120 controller.setGlobalContextItem(doc);
121 controller.applyTemplates(doc, destination);
122 } catch (TransformerException e) {
123 throw XPathException.makeXPathException(e);
124 }
125 }
126 }
127
128 }
129