KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > trax > TransformerHandlerImpl


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: TransformerHandlerImpl.java,v 1.18 2004/02/16 22:57:21 minchau Exp $
18  */

19
20 package org.apache.xalan.xsltc.trax;
21
22 import javax.xml.transform.Result JavaDoc;
23 import javax.xml.transform.Transformer JavaDoc;
24 import javax.xml.transform.TransformerException JavaDoc;
25 import javax.xml.transform.sax.TransformerHandler JavaDoc;
26
27 import org.apache.xalan.xsltc.StripFilter;
28 import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
29 import org.apache.xalan.xsltc.dom.DOMWSFilter;
30 import org.apache.xalan.xsltc.dom.SAXImpl;
31 import org.apache.xalan.xsltc.dom.XSLTCDTMManager;
32 import org.apache.xalan.xsltc.runtime.AbstractTranslet;
33 import org.apache.xml.dtm.DTMWSFilter;
34 import org.apache.xml.serializer.SerializationHandler;
35
36 import org.xml.sax.Attributes JavaDoc;
37 import org.xml.sax.ContentHandler JavaDoc;
38 import org.xml.sax.DTDHandler JavaDoc;
39 import org.xml.sax.Locator JavaDoc;
40 import org.xml.sax.SAXException JavaDoc;
41 import org.xml.sax.ext.DeclHandler JavaDoc;
42 import org.xml.sax.ext.LexicalHandler JavaDoc;
43 import org.xml.sax.helpers.DefaultHandler JavaDoc;
44
45 /**
46  * Implementation of a JAXP1.1 TransformerHandler
47  * @author Morten Jorgensen
48  */

49 public class TransformerHandlerImpl implements TransformerHandler JavaDoc, DeclHandler JavaDoc {
50
51     private TransformerImpl _transformer;
52     private AbstractTranslet _translet = null;
53     private String JavaDoc _systemId;
54     private SAXImpl _dom = null;
55     private ContentHandler JavaDoc _handler = null;
56     private LexicalHandler JavaDoc _lexHandler = null;
57     private DTDHandler JavaDoc _dtdHandler = null;
58     private DeclHandler JavaDoc _declHandler = null;
59     private Result JavaDoc _result = null;
60     private Locator JavaDoc _locator = null;
61
62     private boolean _done = false; // Set in endDocument()
63

64     /**
65      * A flag indicating whether this transformer handler implements the
66      * identity transform.
67      */

68     private boolean _isIdentity = false;
69
70     /**
71      * Cosntructor - pass in reference to a TransformerImpl object
72      */

73     public TransformerHandlerImpl(TransformerImpl transformer) {
74     // Save the reference to the transformer
75
_transformer = transformer;
76
77     if (transformer.isIdentity()) {
78         // Set initial handler to the empty handler
79
_handler = new DefaultHandler JavaDoc();
80         _isIdentity = true;
81     }
82     else {
83         // Get a reference to the translet wrapped inside the transformer
84
_translet = _transformer.getTranslet();
85     }
86     }
87
88     /**
89      * Implements javax.xml.transform.sax.TransformerHandler.getSystemId()
90      * Get the base ID (URI or system ID) from where relative URLs will be
91      * resolved.
92      * @return The systemID that was set with setSystemId(String id)
93      */

94     public String JavaDoc getSystemId() {
95     return _systemId;
96     }
97
98     /**
99      * Implements javax.xml.transform.sax.TransformerHandler.setSystemId()
100      * Get the base ID (URI or system ID) from where relative URLs will be
101      * resolved.
102      * @param id Base URI for this stylesheet
103      */

104     public void setSystemId(String JavaDoc id) {
105     _systemId = id;
106     }
107
108     /**
109      * Implements javax.xml.transform.sax.TransformerHandler.getTransformer()
110      * Get the Transformer associated with this handler, which is needed in
111      * order to set parameters and output properties.
112      * @return The Transformer object
113      */

114     public Transformer JavaDoc getTransformer() {
115     return _transformer;
116     }
117
118     /**
119      * Implements javax.xml.transform.sax.TransformerHandler.setResult()
120      * Enables the user of the TransformerHandler to set the to set the Result
121      * for the transformation.
122      * @param result A Result instance, should not be null
123      * @throws IllegalArgumentException if result is invalid for some reason
124      */

125     public void setResult(Result JavaDoc result) throws IllegalArgumentException JavaDoc {
126     _result = result;
127
128     if (null == result) {
129        ErrorMsg err = new ErrorMsg(ErrorMsg.ER_RESULT_NULL);
130        throw new IllegalArgumentException JavaDoc(err.toString()); //"result should not be null");
131
}
132        
133     if (_isIdentity) {
134         try {
135         // Connect this object with output system directly
136
SerializationHandler outputHandler =
137             _transformer.getOutputHandler(result);
138         _transformer.transferOutputProperties(outputHandler);
139
140         _handler = outputHandler;
141         _lexHandler = outputHandler;
142         }
143         catch (TransformerException JavaDoc e) {
144         _result = null;
145         }
146     }
147     else if (_done) {
148         // Run the transformation now, if not already done
149
try {
150         _transformer.setDOM(_dom);
151         _transformer.transform(null, _result);
152         }
153         catch (TransformerException JavaDoc e) {
154         // What the hell are we supposed to do with this???
155
throw new IllegalArgumentException JavaDoc(e.getMessage());
156         }
157     }
158     }
159
160     /**
161      * Implements org.xml.sax.ContentHandler.characters()
162      * Receive notification of character data.
163      */

164     public void characters(char[] ch, int start, int length)
165     throws SAXException JavaDoc
166     {
167     _handler.characters(ch, start, length);
168     }
169
170     /**
171      * Implements org.xml.sax.ContentHandler.startDocument()
172      * Receive notification of the beginning of a document.
173      */

174     public void startDocument() throws SAXException JavaDoc {
175     // Make sure setResult() was called before the first SAX event
176
if (_result == null) {
177         ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_SET_RESULT_ERR);
178         throw new SAXException JavaDoc(err.toString());
179     }
180
181         if (!_isIdentity) {
182             boolean hasIdCall = (_translet != null) ? _translet.hasIdCall() : false;
183             XSLTCDTMManager dtmManager = null;
184             
185             // Create an internal DOM (not W3C) and get SAX2 input handler
186
try {
187                 dtmManager =
188                     (XSLTCDTMManager)_transformer.getTransformerFactory()
189                                                  .getDTMManagerClass()
190                                                  .newInstance();
191             } catch (Exception JavaDoc e) {
192                 throw new SAXException JavaDoc(e);
193             }
194
195             DTMWSFilter wsFilter;
196             if (_translet != null && _translet instanceof StripFilter) {
197                 wsFilter = new DOMWSFilter(_translet);
198             } else {
199                 wsFilter = null;
200             }
201           
202             // Construct the DTM using the SAX events that come through
203
_dom = (SAXImpl)dtmManager.getDTM(null, false, wsFilter, true,
204                                               false, hasIdCall);
205
206             _handler = _dom.getBuilder();
207             _lexHandler = (LexicalHandler JavaDoc) _handler;
208             _dtdHandler = (DTDHandler JavaDoc) _handler;
209             _declHandler = (DeclHandler JavaDoc) _handler;
210             
211             
212             // Set document URI
213
_dom.setDocumentURI(_systemId);
214             
215             if (_locator != null) {
216                 _handler.setDocumentLocator(_locator);
217             }
218         }
219
220     // Proxy call
221
_handler.startDocument();
222     }
223
224     /**
225      * Implements org.xml.sax.ContentHandler.endDocument()
226      * Receive notification of the end of a document.
227      */

228     public void endDocument() throws SAXException JavaDoc {
229     // Signal to the DOMBuilder that the document is complete
230
_handler.endDocument();
231
232     if (!_isIdentity) {
233         // Run the transformation now if we have a reference to a Result object
234
if (_result != null) {
235         try {
236             _transformer.setDOM(_dom);
237             _transformer.transform(null, _result);
238         }
239         catch (TransformerException JavaDoc e) {
240             throw new SAXException JavaDoc(e);
241         }
242         }
243         // Signal that the internal DOM is built (see 'setResult()').
244
_done = true;
245
246         // Set this DOM as the transformer's DOM
247
_transformer.setDOM(_dom);
248     }
249     }
250     
251     /**
252      * Implements org.xml.sax.ContentHandler.startElement()
253      * Receive notification of the beginning of an element.
254      */

255     public void startElement(String JavaDoc uri, String JavaDoc localName,
256                  String JavaDoc qname, Attributes JavaDoc attributes)
257     throws SAXException JavaDoc
258     {
259     _handler.startElement(uri, localName, qname, attributes);
260     }
261     
262     /**
263      * Implements org.xml.sax.ContentHandler.endElement()
264      * Receive notification of the end of an element.
265      */

266     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qname)
267     throws SAXException JavaDoc
268     {
269     _handler.endElement(namespaceURI, localName, qname);
270     }
271
272     /**
273      * Implements org.xml.sax.ContentHandler.processingInstruction()
274      * Receive notification of a processing instruction.
275      */

276     public void processingInstruction(String JavaDoc target, String JavaDoc data)
277     throws SAXException JavaDoc
278     {
279     _handler.processingInstruction(target, data);
280     }
281
282     /**
283      * Implements org.xml.sax.ext.LexicalHandler.startCDATA()
284      */

285     public void startCDATA() throws SAXException JavaDoc {
286     if (_lexHandler != null) {
287         _lexHandler.startCDATA();
288     }
289     }
290
291     /**
292      * Implements org.xml.sax.ext.LexicalHandler.endCDATA()
293      */

294     public void endCDATA() throws SAXException JavaDoc {
295     if (_lexHandler != null) {
296         _lexHandler.endCDATA();
297     }
298     }
299
300     /**
301      * Implements org.xml.sax.ext.LexicalHandler.comment()
302      * Receieve notification of a comment
303      */

304     public void comment(char[] ch, int start, int length)
305     throws SAXException JavaDoc
306     {
307     if (_lexHandler != null) {
308         _lexHandler.comment(ch, start, length);
309     }
310     }
311
312     /**
313      * Implements org.xml.sax.ContentHandler.ignorableWhitespace()
314      * Receive notification of ignorable whitespace in element
315      * content. Similar to characters(char[], int, int).
316      */

317     public void ignorableWhitespace(char[] ch, int start, int length)
318     throws SAXException JavaDoc
319     {
320     _handler.ignorableWhitespace(ch, start, length);
321     }
322
323     /**
324      * Implements org.xml.sax.ContentHandler.setDocumentLocator()
325      * Receive an object for locating the origin of SAX document events.
326      */

327     public void setDocumentLocator(Locator JavaDoc locator) {
328         _locator = locator;
329
330         if (_handler != null) {
331             _handler.setDocumentLocator(locator);
332         }
333     }
334
335     /**
336      * Implements org.xml.sax.ContentHandler.skippedEntity()
337      * Receive notification of a skipped entity.
338      */

339     public void skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
340     _handler.skippedEntity(name);
341     }
342
343     /**
344      * Implements org.xml.sax.ContentHandler.startPrefixMapping()
345      * Begin the scope of a prefix-URI Namespace mapping.
346      */

347     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
348     throws SAXException JavaDoc {
349     _handler.startPrefixMapping(prefix, uri);
350     }
351
352     /**
353      * Implements org.xml.sax.ContentHandler.endPrefixMapping()
354      * End the scope of a prefix-URI Namespace mapping.
355      */

356     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
357     _handler.endPrefixMapping(prefix);
358     }
359
360     /**
361      * Implements org.xml.sax.ext.LexicalHandler.startDTD()
362      */

363     public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
364     throws SAXException JavaDoc
365     {
366     if (_lexHandler != null) {
367         _lexHandler.startDTD(name, publicId, systemId);
368     }
369     }
370
371     /**
372      * Implements org.xml.sax.ext.LexicalHandler.endDTD()
373      */

374     public void endDTD() throws SAXException JavaDoc {
375     if (_lexHandler != null) {
376         _lexHandler.endDTD();
377     }
378     }
379
380     /**
381      * Implements org.xml.sax.ext.LexicalHandler.startEntity()
382      */

383     public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
384     if (_lexHandler != null) {
385         _lexHandler.startEntity(name);
386     }
387     }
388
389     /**
390      * Implements org.xml.sax.ext.LexicalHandler.endEntity()
391      */

392     public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
393     if (_lexHandler != null) {
394         _lexHandler.endEntity(name);
395     }
396     }
397
398     /**
399      * Implements org.xml.sax.DTDHandler.unparsedEntityDecl()
400      */

401     public void unparsedEntityDecl(String JavaDoc name, String JavaDoc publicId,
402     String JavaDoc systemId, String JavaDoc notationName) throws SAXException JavaDoc
403     {
404         if (_dtdHandler != null) {
405         _dtdHandler.unparsedEntityDecl(name, publicId, systemId,
406                                            notationName);
407         }
408     }
409
410     /**
411      * Implements org.xml.sax.DTDHandler.notationDecl()
412      */

413     public void notationDecl(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
414     throws SAXException JavaDoc
415     {
416         if (_dtdHandler != null) {
417         _dtdHandler.notationDecl(name, publicId, systemId);
418         }
419     }
420
421     /**
422      * Implements org.xml.sax.ext.DeclHandler.attributeDecl()
423      */

424     public void attributeDecl(String JavaDoc eName, String JavaDoc aName, String JavaDoc type,
425     String JavaDoc valueDefault, String JavaDoc value) throws SAXException JavaDoc
426     {
427         if (_declHandler != null) {
428         _declHandler.attributeDecl(eName, aName, type, valueDefault, value);
429         }
430     }
431
432     /**
433      * Implements org.xml.sax.ext.DeclHandler.elementDecl()
434      */

435     public void elementDecl(String JavaDoc name, String JavaDoc model)
436     throws SAXException JavaDoc
437     {
438         if (_declHandler != null) {
439         _declHandler.elementDecl(name, model);
440         }
441     }
442
443     /**
444      * Implements org.xml.sax.ext.DeclHandler.externalEntityDecl()
445      */

446     public void externalEntityDecl(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
447     throws SAXException JavaDoc
448     {
449         if (_declHandler != null) {
450         _declHandler.externalEntityDecl(name, publicId, systemId);
451         }
452     }
453
454     /**
455      * Implements org.xml.sax.ext.DeclHandler.externalEntityDecl()
456      */

457     public void internalEntityDecl(String JavaDoc name, String JavaDoc value)
458     throws SAXException JavaDoc
459     {
460         if (_declHandler != null) {
461         _declHandler.internalEntityDecl(name, value);
462         }
463     }
464 }
465
Popular Tags