KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > xml > transform > impl > TransformerBase


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

7 package org.exoplatform.services.xml.transform.impl;
8
9 import java.io.InputStream JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.OutputStream JavaDoc;
12
13 import javax.xml.transform.TransformerException JavaDoc;
14 import javax.xml.transform.Result JavaDoc;
15 import javax.xml.transform.Source JavaDoc;
16 import javax.xml.transform.sax.SAXResult JavaDoc;
17 import javax.xml.transform.sax.SAXSource JavaDoc;
18 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
19 import javax.xml.transform.sax.TransformerHandler JavaDoc;
20 import javax.xml.transform.stream.StreamResult JavaDoc;
21 import org.xml.sax.InputSource JavaDoc;
22 import org.xml.sax.XMLReader JavaDoc;
23 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
24 import org.xml.sax.SAXException JavaDoc;
25
26 import org.apache.commons.logging.Log;
27
28 import org.exoplatform.services.xml.transform.AbstractTransformer;
29 import org.exoplatform.services.xml.transform.NotSupportedIOTypeException;
30 import org.exoplatform.services.log.LogService;
31 import org.exoplatform.container.PortalContainer;
32 import org.exoplatform.services.xml.resolving.XMLResolvingService;
33
34
35
36 /**
37  * Created by The eXo Platform SARL .
38  *
39  * @author <a HREF="mailto:alex.kravchuk@gmail.com">Alexander Kravchuk</a>
40  * @version $Id: TransformerBase.java 566 2005-01-25 12:50:49Z kravchuk $
41  */

42 public abstract class TransformerBase implements AbstractTransformer {
43     private Result JavaDoc result = null;
44
45     protected Log log;
46     protected SAXTransformerFactory JavaDoc tSAXFactory;
47
48     public XMLResolvingService resolvingService;
49
50     public TransformerBase() {
51         LogService logService = (LogService) PortalContainer.getInstance().
52                                 getComponentInstanceOfType(LogService.class);
53         log = logService.getLog(this.getClass());
54
55         log.debug("Current javax.xml.parsers.SAXParserFactory sys property [ " +
56                   System.getProperty("javax.xml.parsers.SAXParserFactory",
57                                      "-Not set-") + "]");
58
59         tSAXFactory = (SAXTransformerFactory JavaDoc)
60            SAXTransformerFactory.newInstance();
61     }
62
63     /**
64      * @todo change. Must no use explicit parser class name
65      */

66     static public XMLReader JavaDoc getXMLReader() throws SAXException JavaDoc {
67        return XMLReaderFactory.
68                createXMLReader("org.apache.xerces.parsers.SAXParser");
69     }
70
71     /**
72      * override when need some operation after initialization result
73      * in transformer
74      */

75     protected void afterInitResult(){
76         log.debug("Result is set");
77     }
78
79     final public void initResult(Result JavaDoc result) throws NotSupportedIOTypeException {
80         if (!isResultSupported(result)) {
81             throw new NotSupportedIOTypeException(result);
82         }
83         this.result = result;
84         afterInitResult();
85     }
86
87     protected Result JavaDoc getResult() {
88         return this.result;
89     }
90
91     protected boolean isSourceSupported(Source JavaDoc source) {
92         return true;
93     }
94
95     protected boolean isResultSupported(Result JavaDoc result) {
96         return true;
97     }
98
99     protected abstract void internalTransform(Source JavaDoc src) throws
100             NotSupportedIOTypeException,
101             TransformerException JavaDoc, IllegalStateException JavaDoc;
102
103
104     final public void transform(Source JavaDoc source) throws
105             NotSupportedIOTypeException,
106             TransformerException JavaDoc, IllegalStateException JavaDoc {
107
108         if (!isSourceSupported(source)) {
109             log.error("source of type "+source.getClass().getName()+" not supported");
110             throw new NotSupportedIOTypeException(source);
111         }
112
113         if (this.result == null) {
114             log.error("Result not set");
115             throw new IllegalStateException JavaDoc(
116                     "Result not specified. See initResult(Result)");
117         }
118
119         internalTransform(source);
120     }
121
122     /**
123      * Tranform InputStream to specified result, according to type of result
124      * @param input InputStream
125      * @param result Result
126      * @throws TransformerException
127      */

128
129     public void transformInputStream2Result(InputStream JavaDoc input, Result JavaDoc result) throws
130             TransformerException JavaDoc {
131         log.debug("Transform InputStream to result of type "+result.getClass().getName());
132
133         //StreamResult - write data from InputStream to OutputStream
134
if (result instanceof StreamResult JavaDoc) {
135             OutputStream JavaDoc outputStream =
136             ((StreamResult JavaDoc) result).getOutputStream();
137             try {
138                 int counter = 0;
139                 while (input.available() > 0) {
140                     byte[] byteArray = new byte[input.available()];
141                     int readBytes = input.read(byteArray);
142                     counter += readBytes;
143                     outputStream.write(byteArray, 0, readBytes);
144                 }
145                 log.debug("Write "+counter+" bytes to ouput stream");
146             } catch (IOException JavaDoc ex) {
147                 log.error("Error on read/write ",ex);
148                 throw new TransformerException JavaDoc(ex);
149             }
150         }
151         //not StreamResult
152
else {
153             XMLReader JavaDoc xmlReader = null;
154             try {
155                 xmlReader = getXMLReader();
156                 log.debug("xmlReader class is "+xmlReader.getClass().getName());
157
158                 //set default resolver
159
if (resolvingService != null) {
160                     xmlReader.setEntityResolver(
161                             resolvingService.getEntityResolver());
162                     log.debug("Set entity resolver");
163                 }
164
165                 //SAXResult use XMLReader to parce InputStream to SAXEvents
166
if (result instanceof SAXResult JavaDoc) {
167                     SAXResult JavaDoc saxResult = (SAXResult JavaDoc) result;
168                     xmlReader.setContentHandler(saxResult.getHandler());
169                     log.debug("Parse direct to result");
170                 }
171
172                 //not StreamResult, not SAXResult - create empty transformation
173
else {
174                     log.debug("Create empty transformation");
175                     TransformerHandler JavaDoc transformerHandler =
176                             tSAXFactory.newTransformerHandler();
177                     transformerHandler.setResult(result);
178                     xmlReader.setContentHandler(transformerHandler);
179                     log.debug("Parse to result throw empty transformer");
180                 }
181                 xmlReader.parse(new InputSource JavaDoc(input));
182                 log.debug("Parse complete");
183             } catch (SAXException JavaDoc ex) {
184                 throw new TransformerException JavaDoc(ex);
185             } catch (IOException JavaDoc ex) {
186                 throw new TransformerException JavaDoc(ex);
187             }
188         }
189     }
190
191     /**
192      * Transform javax.xml.transform.Source to java.io.InputStream
193      * if can't transform throw exception
194      * @param source Source
195      * @return InputStream
196      * @throws NotSupportedIOTypeException
197      */

198     protected InputStream JavaDoc sourceAsInputStream(Source JavaDoc source) throws
199             NotSupportedIOTypeException {
200         InputSource JavaDoc inputSource = SAXSource.sourceToInputSource(source);
201         if (inputSource == null) {
202             throw new NotSupportedIOTypeException(source);
203         }
204         return inputSource.getByteStream();
205     }
206
207     /**
208      * @todo REMOVE!!!! For debug only!!!
209      * @deprecated see Warning
210      */

211     protected void writeTofile( byte[] bytes,
212                                 String JavaDoc postfix){
213         String JavaDoc POSTFIX = new java.text.SimpleDateFormat JavaDoc("yy-MM-DD_HH-mm-ss_").
214                           format(new java.util.Date JavaDoc());
215         try {
216
217             java.io.FileOutputStream JavaDoc fileLog =
218                     new java.io.FileOutputStream JavaDoc(
219                             "c:/tmp/transf" + POSTFIX+postfix+".xhtml");
220             fileLog.write(bytes);
221             fileLog.flush();
222             fileLog.close();
223
224         } catch (java.io.FileNotFoundException JavaDoc ex) {
225         } catch (IOException JavaDoc ex) {
226         }
227     }
228
229 }
230
231
232
233
234
Popular Tags