KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > TransformerHandlerImpl


1 package net.sf.saxon;
2 import net.sf.saxon.event.Builder;
3 import net.sf.saxon.event.Receiver;
4 import net.sf.saxon.event.ReceivingContentHandler;
5 import net.sf.saxon.om.DocumentInfo;
6 import net.sf.saxon.trans.XPathException;
7 import org.xml.sax.SAXException JavaDoc;
8
9 import javax.xml.transform.Result JavaDoc;
10 import javax.xml.transform.Transformer JavaDoc;
11 import javax.xml.transform.TransformerException JavaDoc;
12 import javax.xml.transform.sax.TransformerHandler JavaDoc;
13
14
15 /**
16   * <b>TransformerHandlerImpl</b> implements the javax.xml.transform.sax.TransformerHandler
17   * interface. It acts as a ContentHandler and LexicalHandler which receives a stream of
18   * SAX events representing an input document, and performs a transformation treating this
19   * SAX stream as the source document of the transformation.
20   * @author Michael H. Kay
21   */

22
23 public class TransformerHandlerImpl extends ReceivingContentHandler implements TransformerHandler JavaDoc {
24
25     // TODO: if requested, apply schema validation to the source document
26

27     Controller controller;
28     Builder builder;
29     Result JavaDoc result;
30     String JavaDoc systemId;
31     boolean started = false;
32
33     /**
34     * Create a TransformerHandlerImpl and initialise variables. The constructor is protected, because
35     * the Filter should be created using newTransformerHandler() in the SAXTransformerFactory
36     * class
37     */

38
39     protected TransformerHandlerImpl(Controller controller) {
40         this.controller = controller;
41         setPipelineConfiguration(controller.makePipelineConfiguration());
42         builder = controller.makeBuilder();
43         builder.setPipelineConfiguration(getPipelineConfiguration());
44         Receiver stripper = controller.makeStripper(builder);
45         if (controller.getExecutable().stripsInputTypeAnnotations()) {
46             stripper = controller.getConfiguration().getAnnotationStripper(stripper);
47         }
48         this.setReceiver(stripper);
49     }
50
51     /**
52      * Start of a new document. The TransformerHandler is not serially reusable, so this method
53      * must only be called once.
54      * @throws SAXException only if an overriding subclass throws this exception
55      * @throws UnsupportedOperationException if an attempt is made to reuse the TransformerHandler by calling
56      * startDocument() more than once.
57      */

58
59     public void startDocument () throws SAXException JavaDoc {
60         if (started) {
61             throw new UnsupportedOperationException JavaDoc(
62                     "The TransformerHandler is not serially reusable. The startDocument() method must be called once only.");
63         }
64         started = true;
65         super.startDocument();
66     }
67
68     /**
69     * Get the Transformer used for this transformation
70     */

71
72     public Transformer JavaDoc getTransformer() {
73         return controller;
74     }
75
76     /**
77     * Set the SystemId of the document
78     */

79
80     public void setSystemId(String JavaDoc url) {
81         systemId = url;
82         builder.setSystemId(url);
83     }
84
85     /**
86     * Get the systemId of the document
87     */

88
89     public String JavaDoc getSystemId() {
90         return systemId;
91     }
92
93     /**
94     * Set the output destination of the transformation
95     */

96
97     public void setResult(Result JavaDoc result) {
98         if (result==null) {
99             throw new IllegalArgumentException JavaDoc("Result must not be null");
100         }
101         this.result = result;
102     }
103
104     /**
105     * Get the output destination of the transformation
106     */

107
108     public Result JavaDoc getResult() {
109         return result;
110     }
111
112     /**
113     * Override the behaviour of endDocument() in ReceivingContentHandler, so that it fires off
114     * the transformation of the constructed document
115     */

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