KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > event > TransformerReceiver


1 package net.sf.saxon.event;
2 import net.sf.saxon.Controller;
3 import net.sf.saxon.om.DocumentInfo;
4 import net.sf.saxon.trans.DynamicError;
5 import net.sf.saxon.trans.XPathException;
6
7 import javax.xml.transform.Result JavaDoc;
8 import javax.xml.transform.Transformer JavaDoc;
9 import javax.xml.transform.TransformerException JavaDoc;
10
11
12 /**
13   * <b>TransformerReceiver</b> is similar in concept to the JAXP TransformerHandler,
14  * except that it implements Saxon's Receiver interface rather than the standard
15  * SAX2 interface. This means that it allows nodes with type annotations to be
16  * passed down a pipeline from one transformation to another.
17   */

18
19 public class TransformerReceiver extends ProxyReceiver {
20
21     Controller controller;
22     Builder builder;
23     Result JavaDoc result;
24     String JavaDoc systemId;
25
26     /**
27     * Create a TransformerHandlerImpl and initialise variables.
28     */

29
30     public TransformerReceiver(Controller controller) {
31         this.controller = controller;
32     }
33
34     /**
35      * Start of event stream
36      */

37
38     public void open() throws XPathException {
39         setPipelineConfiguration(controller.makePipelineConfiguration());
40         builder = controller.makeBuilder();
41         builder.setPipelineConfiguration(getPipelineConfiguration());
42         builder.setSystemId(systemId);
43         Receiver stripper = controller.makeStripper(builder);
44         if (controller.getExecutable().stripsInputTypeAnnotations()) {
45             stripper = controller.getConfiguration().getAnnotationStripper(stripper);
46         }
47         this.setUnderlyingReceiver(stripper);
48         super.open();
49     }
50
51     /**
52     * Get the Transformer used for this transformation
53     */

54
55     public Transformer JavaDoc getTransformer() {
56         return controller;
57     }
58
59     /**
60     * Set the SystemId of the document
61     */

62
63     public void setSystemId(String JavaDoc url) {
64         systemId = url;
65     }
66
67     /**
68     * Get the systemId of the document
69     */

70
71     public String JavaDoc getSystemId() {
72         return systemId;
73     }
74
75     /**
76      * Notify the start of an element
77      * @param nameCode integer code identifying the name of the element within the name pool.
78      * @param typeCode integer code identifying the element's type within the name pool.
79      * @param properties bit-significant properties of the element node.
80      */

81
82     public void startElement(int nameCode, int typeCode, int locationId, int properties) throws XPathException {
83         super.startElement(nameCode, typeCode, locationId, properties);
84     }
85
86     /**
87     * Set the output destination of the transformation
88     */

89
90     public void setResult(Result JavaDoc result) {
91         if (result==null) {
92             throw new IllegalArgumentException JavaDoc("Result must not be null");
93         }
94         this.result = result;
95     }
96
97     /**
98     * Get the output destination of the transformation
99     */

100
101     public Result JavaDoc getResult() {
102         return result;
103     }
104
105     /**
106     * Override the behaviour of endDocument() in ReceivingContentHandler, so that it fires off
107     * the transformation of the constructed document
108     */

109
110     public void close() throws XPathException {
111         super.close();
112         DocumentInfo doc = (DocumentInfo)builder.getCurrentRoot();
113         if (doc==null) {
114             throw new DynamicError("No source document has been built");
115         }
116         //doc.getNamePool().allocateDocumentNumber(doc);
117
try {
118             controller.transformDocument(doc, result);
119         } catch (TransformerException JavaDoc e) {
120             throw DynamicError.makeDynamicError(e);
121         }
122     }
123
124
125
126
127 }
128
129 //
130
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
131
// you may not use this file except in compliance with the License. You may obtain a copy of the
132
// License at http://www.mozilla.org/MPL/
133
//
134
// Software distributed under the License is distributed on an "AS IS" basis,
135
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
136
// See the License for the specific language governing rights and limitations under the License.
137
//
138
// The Original Code is: all this file.
139
//
140
// The Initial Developer of the Original Code is Michael H. Kay.
141
//
142
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
143
//
144
// Contributor(s): None
145
//
146
Popular Tags