KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon;
2 import com.icl.saxon.om.NamePool;
3 import com.icl.saxon.output.Outputter;
4 import com.icl.saxon.output.GeneralOutputter;
5 import com.icl.saxon.output.Emitter;
6
7 import javax.xml.transform.*;
8 import javax.xml.transform.sax.TransformerHandler JavaDoc;
9 import javax.xml.transform.stream.StreamResult JavaDoc;
10
11 import org.xml.sax.*;
12
13 import java.util.Properties JavaDoc;
14
15 /**
16   * <b>IdentityTransformerHandler</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 an identity transformation passing
19   * these events to a Result
20   * @author Michael H. Kay (mhkay@iclway.co.uk)
21   */

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

35
36     protected IdentityTransformerHandler(Controller controller) {
37         this.controller = controller;
38         setNamePool(controller.getNamePool());
39         
40     }
41
42     /**
43     * Get the Transformer used for this transformation
44     */

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

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

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

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

80     
81     public Result JavaDoc getResult() {
82         return result;
83     }
84
85     /**
86     * Override the behaviour of startDocument() in ContentEmitter
87     */

88     
89     public void startDocument() throws SAXException {
90         if (result==null) {
91             result = new StreamResult JavaDoc(System.out);
92         }
93         try {
94             NamePool pool = controller.getNamePool();
95             Properties JavaDoc props = controller.getOutputProperties();
96             outputter = new GeneralOutputter(pool);
97             outputter.setOutputDestination(props, result);
98             Emitter emitter = outputter.getEmitter();
99             setNamePool(pool);
100             setEmitter(emitter);
101         } catch (TransformerException err) {
102             throw new SAXException(err);
103         }
104         super.startDocument();
105     }
106
107     /**
108     * Override the behaviour of endDocument() in ContentEmitter
109     */

110     
111     public void endDocument() throws SAXException {
112         try {
113             outputter.close();
114         } catch (TransformerException err) {
115             throw new SAXException(err);
116         }
117     }
118         
119         
120
121
122 }
123
124 //
125
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
126
// you may not use this file except in compliance with the License. You may obtain a copy of the
127
// License at http://www.mozilla.org/MPL/
128
//
129
// Software distributed under the License is distributed on an "AS IS" basis,
130
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
131
// See the License for the specific language governing rights and limitations under the License.
132
//
133
// The Original Code is: all this file.
134
//
135
// The Initial Developer of the Original Code is
136
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
137
//
138
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
139
//
140
// Contributor(s): None
141
//
142
Popular Tags