KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > 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 com.sun.org.apache.xalan.internal.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.dom.DOMResult JavaDoc;
26 import javax.xml.transform.sax.TransformerHandler JavaDoc;
27
28 import com.sun.org.apache.xalan.internal.xsltc.StripFilter;
29 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
30 import com.sun.org.apache.xalan.internal.xsltc.dom.DOMWSFilter;
31 import com.sun.org.apache.xalan.internal.xsltc.dom.SAXImpl;
32 import com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager;
33 import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet;
34 import com.sun.org.apache.xml.internal.dtm.DTMWSFilter;
35 import com.sun.org.apache.xml.internal.serializer.SerializationHandler;
36
37 import org.xml.sax.Attributes JavaDoc;
38 import org.xml.sax.ContentHandler JavaDoc;
39 import org.xml.sax.DTDHandler JavaDoc;
40 import org.xml.sax.Locator JavaDoc;
41 import org.xml.sax.SAXException JavaDoc;
42 import org.xml.sax.ext.DeclHandler JavaDoc;
43 import org.xml.sax.ext.LexicalHandler JavaDoc;
44 import org.xml.sax.helpers.DefaultHandler JavaDoc;
45
46 /**
47  * Implementation of a JAXP1.1 TransformerHandler
48  * @author Morten Jorgensen
49  */

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

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

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

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

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

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

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

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

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

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

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

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

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

280     public void processingInstruction(String JavaDoc target, String JavaDoc data)
281     throws SAXException JavaDoc
282     {
283     _handler.processingInstruction(target, data);
284     }
285
286     /**
287      * Implements org.xml.sax.ext.LexicalHandler.startCDATA()
288      */

289     public void startCDATA() throws SAXException JavaDoc {
290     if (_lexHandler != null) {
291         _lexHandler.startCDATA();
292     }
293     }
294
295     /**
296      * Implements org.xml.sax.ext.LexicalHandler.endCDATA()
297      */

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

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

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

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

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

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

360     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
361     _handler.endPrefixMapping(prefix);
362     }
363
364     /**
365      * Implements org.xml.sax.ext.LexicalHandler.startDTD()
366      */

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

378     public void endDTD() throws SAXException JavaDoc {
379     if (_lexHandler != null) {
380         _lexHandler.endDTD();
381     }
382     }
383
384     /**
385      * Implements org.xml.sax.ext.LexicalHandler.startEntity()
386      */

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

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

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

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

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

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

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

461     public void internalEntityDecl(String JavaDoc name, String JavaDoc value)
462     throws SAXException JavaDoc
463     {
464         if (_declHandler != null) {
465         _declHandler.internalEntityDecl(name, value);
466         }
467     }
468
469
470    /** Implementation of the reset() method
471     *
472     */

473    public void reset() {
474        _systemId = null;
475        _dom = null;
476        _handler = null;
477        _lexHandler = null;
478        _dtdHandler = null;
479        _declHandler = null;
480        _result = null;
481        _locator = null;
482    }
483 }
484
Popular Tags