KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon;
2 import net.sf.saxon.event.PipelineConfiguration;
3 import net.sf.saxon.event.ReceivingContentHandler;
4 import net.sf.saxon.event.ResultWrapper;
5 import net.sf.saxon.trans.XPathException;
6 import org.xml.sax.SAXException JavaDoc;
7
8 import javax.xml.transform.Result JavaDoc;
9 import javax.xml.transform.Transformer JavaDoc;
10 import javax.xml.transform.sax.TransformerHandler JavaDoc;
11 import javax.xml.transform.stream.StreamResult JavaDoc;
12 import java.util.Properties JavaDoc;
13
14 /**
15   * <b>IdentityTransformerHandler</b> implements the javax.xml.transform.sax.TransformerHandler
16   * interface. It acts as a ContentHandler and LexicalHandler which receives a stream of
17   * SAX events representing an input document, and performs an identity transformation passing
18   * these events to a Result
19   * @author Michael H. Kay
20   */

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

33
34     protected IdentityTransformerHandler(Controller controller) {
35         this.controller = controller;
36         setPipelineConfiguration(controller.makePipelineConfiguration());
37
38     }
39
40     /**
41     * Get the Transformer used for this transformation
42     */

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

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

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

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

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

86
87     public void startDocument() throws SAXException JavaDoc {
88         if (result==null) {
89             result = new StreamResult JavaDoc(System.out);
90         }
91         try {
92             Properties JavaDoc props = controller.getOutputProperties();
93             PipelineConfiguration pipe = controller.makePipelineConfiguration();
94             setReceiver(ResultWrapper.getReceiver(result, pipe, props));
95             setPipelineConfiguration(pipe);
96         } catch (XPathException err) {
97             throw new SAXException JavaDoc(err);
98         }
99         super.startDocument();
100     }
101
102 }
103
104 //
105
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
106
// you may not use this file except in compliance with the License. You may obtain a copy of the
107
// License at http://www.mozilla.org/MPL/
108
//
109
// Software distributed under the License is distributed on an "AS IS" basis,
110
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
111
// See the License for the specific language governing rights and limitations under the License.
112
//
113
// The Original Code is: all this file.
114
//
115
// The Initial Developer of the Original Code is Michael H. Kay.
116
//
117
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
118
//
119
// Contributor(s): None
120
//
121
Popular Tags