KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > xml > transform > impl > trax > TRAXTransformerImpl


1 /***************************************************************************
2  * Copyright 2001-2005 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5
6 package org.exoplatform.services.xml.transform.impl.trax;
7
8 import java.io.IOException JavaDoc;
9 import java.util.Properties JavaDoc;
10 import javax.xml.transform.ErrorListener JavaDoc;
11 import javax.xml.transform.URIResolver JavaDoc;
12 import javax.xml.transform.Source JavaDoc;
13 import javax.xml.transform.Result JavaDoc;
14 import javax.xml.transform.Transformer JavaDoc;
15 import javax.xml.transform.Templates JavaDoc;
16 import javax.xml.transform.TransformerException JavaDoc;
17 import javax.xml.transform.TransformerConfigurationException JavaDoc;
18 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
19 import javax.xml.transform.sax.TransformerHandler JavaDoc;
20 import javax.xml.transform.sax.SAXSource JavaDoc;
21 import javax.xml.transform.sax.SAXResult JavaDoc;
22 import javax.xml.transform.stream.StreamSource JavaDoc;
23 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
24 import org.xml.sax.InputSource JavaDoc;
25 import org.xml.sax.XMLReader JavaDoc;
26 import org.xml.sax.SAXException JavaDoc;
27
28 import org.exoplatform.services.xml.transform.trax.TRAXTransformer;
29 import org.exoplatform.services.xml.transform.impl.TransformerBase;
30 import org.exoplatform.services.xml.transform.NotSupportedIOTypeException;
31
32
33 //import org.exoplatform.services.xml.transform.TransformRules;
34

35 /**
36  * Created by The eXo Platform SARL .
37  *
38  * Implementation of Trax Transformer interface
39  * @author <a HREF="mailto:geaz@users.sourceforge.net">Gennady Azarenkov</a>
40  * @author <a HREF="mailto:alex.kravchuk@gmail.com">Alexander Kravchuk</a>
41  * @version $Id: TRAXTransformerImpl.java 566 2005-01-25 12:50:49Z kravchuk $
42  */

43
44 public class TRAXTransformerImpl extends TransformerBase
45           implements TRAXTransformer {
46
47 // private SAXTransformerFactory saxTFactory;
48
protected TransformerHandler JavaDoc tHandler;
49
50     protected Transformer JavaDoc getTransformer() {
51         return tHandler.getTransformer();
52     }
53
54
55     public TRAXTransformerImpl() throws TransformerConfigurationException JavaDoc {
56         SAXTransformerFactory JavaDoc saxTFactory = (SAXTransformerFactory JavaDoc)
57                                             SAXTransformerFactory.newInstance();
58         tHandler = saxTFactory.newTransformerHandler();
59     }
60
61     public TRAXTransformerImpl(Source JavaDoc source) throws
62             TransformerConfigurationException JavaDoc {
63         SAXTransformerFactory JavaDoc saxTFactory = (SAXTransformerFactory JavaDoc)
64                                             SAXTransformerFactory.newInstance();
65         tHandler = saxTFactory.newTransformerHandler(source);
66     }
67
68     public TRAXTransformerImpl(Templates JavaDoc templates) throws
69             TransformerConfigurationException JavaDoc {
70         SAXTransformerFactory JavaDoc saxTFactory = (SAXTransformerFactory JavaDoc)
71                                             SAXTransformerFactory.newInstance();
72         tHandler = saxTFactory.newTransformerHandler(templates);
73     }
74
75     protected void internalTransform(Source JavaDoc src) throws TransformerException JavaDoc,
76             NotSupportedIOTypeException, IllegalStateException JavaDoc {
77
78         XMLReader JavaDoc xmlReader = null;
79
80         try {
81 // xmlReader = XMLReaderFactory.
82
// createXMLReader("org.apache.xerces.parsers.SAXParser");
83
xmlReader = getXMLReader();
84             //set default resolver
85
if (resolvingService != null) {
86                 xmlReader.setEntityResolver(
87                         resolvingService.getEntityResolver());
88                 log.debug("Set entity resolver");
89             }
90         } catch (SAXException JavaDoc ex) {
91             throw new TransformerException JavaDoc(ex);
92         }
93
94
95         xmlReader.setContentHandler(tHandler);
96 // tHandler.setResult(getResult());
97

98         //todo simplify
99
InputSource JavaDoc inputSource = SAXSource.sourceToInputSource(src);
100 // InputSource inputSource = null;
101
// if (src instanceof StreamSource) {
102
// inputSource = new InputSource(((StreamSource) src).getInputStream());
103
// }
104
// else {
105
// inputSource = SAXSource.sourceToInputSource(src);
106
// }
107
//
108
// if (inputSource == null) {
109
// throw new NotSupportedIOTypeException(src);
110
// }
111

112         try {
113             xmlReader.parse(inputSource);
114         } catch (SAXException JavaDoc ex) {
115             throw new TransformerException JavaDoc(ex);
116         } catch (IOException JavaDoc ex) {
117             throw new TransformerException JavaDoc(ex);
118         }
119
120     }
121
122     protected void afterInitResult() {
123         tHandler.setResult(getResult());
124     }
125
126
127     //delegation to Transformer, see getTransformer()
128
public Result JavaDoc getTransformerAsResult() {
129         return new SAXResult JavaDoc(tHandler);
130     }
131
132     public Object JavaDoc getParameter(String JavaDoc param) {
133         return getTransformer().getParameter(param);
134     }
135
136     public void setParameter(String JavaDoc name, Object JavaDoc value) {
137         getTransformer().setParameter(name, value);
138     }
139
140     public void clearParameters() {
141         getTransformer().clearParameters();
142     }
143
144     public String JavaDoc getOutputProperty(String JavaDoc prop) {
145         return getTransformer().getOutputProperty(prop);
146     }
147
148     public void setOutputProperty(String JavaDoc name, String JavaDoc value) {
149         getTransformer().setOutputProperty(name, value);
150     }
151
152     public void setOutputProperties(Properties JavaDoc props) {
153         getTransformer().setOutputProperties(props);
154     }
155
156     public Properties JavaDoc getOutputProperties() {
157         return getTransformer().getOutputProperties();
158     }
159
160     public URIResolver JavaDoc getURIResolver() {
161         return getTransformer().getURIResolver();
162     }
163
164     public void setURIResolver(URIResolver JavaDoc resolver) {
165         getTransformer().setURIResolver(resolver);
166     }
167
168     public ErrorListener JavaDoc getErrorListener() {
169         return getTransformer().getErrorListener();
170     }
171
172     public void setErrorListener(ErrorListener JavaDoc listener) {
173         getTransformer().setErrorListener(listener);
174     }
175
176
177 }
178
Popular Tags