KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.xml.sax.*;
8 import org.xml.sax.ext.LexicalHandler JavaDoc;
9
10 import javax.xml.transform.*;
11 import javax.xml.transform.stream.*;
12 import javax.xml.transform.sax.*;
13 import javax.xml.transform.dom.*;
14
15 import java.util.Properties JavaDoc;
16
17 class IdentityTransformer extends Controller {
18     
19     protected IdentityTransformer(TransformerFactoryImpl factory) {
20         super(factory);
21     }
22
23     /**
24     * Perform identify transformation from Source to Result
25     */

26     
27     public void transform(Source source, Result result)
28     throws TransformerException {
29         SAXSource saxsource = getTransformerFactory().getSAXSource(source, false);
30         XMLReader parser = saxsource.getXMLReader();
31         
32         try {
33             parser.setFeature("http://xml.org/sax/features/namespaces", true);
34             parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
35         } catch (SAXNotSupportedException err) { // SAX2 parsers MUST support this feature!
36
throw new TransformerException(
37                 "The SAX2 parser does not recognize a required namespace feature");
38         } catch (SAXNotRecognizedException err) {
39             throw new TransformerException(
40                 "The SAX2 parser does not support a required namespace feature");
41         }
42                 
43         if (result instanceof SAXResult) {
44             
45             // if output is a SAX ContentHandler, we couple this directly
46
// to the SAX Source
47

48             ContentHandler ch = ((SAXResult)result).getHandler();
49             parser.setContentHandler(ch);
50             if (ch instanceof LexicalHandler JavaDoc) {
51                 // then try to preserve the comments
52
try {
53                     parser.setProperty("http://xml.org/sax/properties/lexical-handler", ch);
54                 } catch (SAXNotSupportedException err) { // this just means we won't see the comments
55
} catch (SAXNotRecognizedException err) {
56                 }
57             }
58             try {
59                 parser.parse(saxsource.getInputSource());
60             } catch (Exception JavaDoc err) {
61                 throw new TransformerException(err);
62             }
63
64         } else {
65         
66             // If output is a DOM, a Stream, or an Emitter, we construct
67
// a pipeline consisting of a ContentEmitter (which converts
68
// SAX events to Emitter events), then the appropriate Emitter
69
// for that kind of result, which we can obtain using the services
70
// of the Outputter.
71

72             NamePool pool = getNamePool();
73             Properties JavaDoc props = getOutputProperties();
74             GeneralOutputter out = new GeneralOutputter(pool);
75             out.setOutputDestination(props, result);
76             Emitter emitter = out.getEmitter();
77             ContentHandler ch = new ContentEmitter();
78             ((ContentEmitter)ch).setNamePool(pool);
79             ((ContentEmitter)ch).setEmitter(emitter);
80
81             try {
82                 parser.setContentHandler(ch);
83                 try {
84                     // try to preserve the comments
85
parser.setProperty("http://xml.org/sax/properties/lexical-handler", ch);
86                 } catch (SAXNotSupportedException err) { // this just means we won't see the comments
87
} catch (SAXNotRecognizedException err) {
88                 }
89             
90                 parser.parse(saxsource.getInputSource());
91             } catch (Exception JavaDoc err) {
92                 throw new TransformerException(err);
93             }
94             
95             out.close();
96         }
97     }
98 }
99
Popular Tags