KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > TransformerHandlerImpl


1 package com.icl.saxon;
2 import com.icl.saxon.om.Builder;
3 import com.icl.saxon.om.NamePool;
4 import com.icl.saxon.om.DocumentInfo;
5
6 import javax.xml.transform.*;
7 import javax.xml.transform.sax.TransformerHandler JavaDoc;
8
9 import org.xml.sax.*;
10
11
12 /**
13   * <b>TransformerHandlerImpl</b> implements the javax.xml.transform.sax.TransformerHandler
14   * interface. It acts as a ContentHandler and LexicalHandler which receives a stream of
15   * SAX events representing an input document, and performs a transformation treating this
16   * SAX stream as the source document of the transformation.
17   * @author Michael H. Kay (mhkay@iclway.co.uk)
18   */

19   
20 public class TransformerHandlerImpl extends ContentEmitter implements TransformerHandler JavaDoc {
21
22     Controller controller;
23     Builder builder;
24     Result result;
25     String JavaDoc systemId;
26  
27     /**
28     * Create a TransformerHandlerImpl and initialise variables. The constructor is protected, because
29     * the Filter should be created using newTransformerHandler() in the SAXTransformerFactory
30     * class
31     */

32
33     protected TransformerHandlerImpl(Controller controller) {
34         this.controller = controller;
35         setNamePool(controller.getNamePool());
36         builder = controller.makeBuilder();
37         builder.setNamePool(controller.getNamePool());
38         this.setEmitter(builder);
39         // TODO: add a stripper to the pipeline
40

41     }
42
43     /**
44     * Get the Transformer used for this transformation
45     */

46     
47     public Transformer getTransformer() {
48         return controller;
49     }
50
51     /**
52     * Set the SystemId of the document
53     */

54     
55     public void setSystemId(String JavaDoc url) {
56         systemId = url;
57         builder.setSystemId(url);
58     }
59     
60     /**
61     * Get the systemId of the document
62     */

63     
64     public String JavaDoc getSystemId() {
65         return systemId;
66     }
67     
68     /**
69     * Set the output destination of the transformation
70     */

71     
72     public void setResult(Result result) {
73         if (result==null) {
74             throw new IllegalArgumentException JavaDoc("Result must not be null");
75         }
76         this.result = result;
77     }
78     
79     /**
80     * Get the output destination of the transformation
81     */

82     
83     public Result getResult() {
84         return result;
85     }
86
87     /**
88     * Override the behaviour of endDocument() in ContentEmitter, so that it fires off
89     * the transformation of the constructed document
90     */

91     
92     public void endDocument() throws SAXException {
93         // System.err.println("TransformerHandlerImpl " + this + " endDocument()");
94
super.endDocument();
95         DocumentInfo doc = builder.getCurrentDocument();
96         if (doc==null) {
97             throw new SAXException("No source document has been built");
98         }
99         controller.getDocumentPool().add(doc, null);
100         try {
101             controller.transformDocument(doc, result);
102         } catch (TransformerException err) {
103             //err.printStackTrace();
104
throw new SAXException(err);
105         }
106     }
107         
108         
109
110
111 }
112
113 //
114
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
115
// you may not use this file except in compliance with the License. You may obtain a copy of the
116
// License at http://www.mozilla.org/MPL/
117
//
118
// Software distributed under the License is distributed on an "AS IS" basis,
119
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
120
// See the License for the specific language governing rights and limitations under the License.
121
//
122
// The Original Code is: all this file.
123
//
124
// The Initial Developer of the Original Code is
125
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
126
//
127
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
128
//
129
// Contributor(s): None
130
//
131
Popular Tags