KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > transformer > TransformerHandlerImpl


1 /*
2  * Copyright 1999-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.19 2004/02/16 20:41:29 minchau Exp $
18  */

19 package org.apache.xalan.transformer;
20
21 import java.io.IOException JavaDoc;
22
23 import javax.xml.transform.Result JavaDoc;
24 import javax.xml.transform.Transformer JavaDoc;
25 import javax.xml.transform.sax.TransformerHandler JavaDoc;
26
27 import org.apache.xalan.res.XSLMessages;
28 import org.apache.xalan.res.XSLTErrorResources;
29 import org.apache.xml.dtm.DTM;
30 import org.apache.xml.dtm.DTMManager;
31 import org.apache.xml.dtm.ref.IncrementalSAXSource_Filter;
32 import org.apache.xml.dtm.ref.sax2dtm.SAX2DTM;
33 import org.apache.xpath.XPathContext;
34
35 import org.xml.sax.Attributes JavaDoc;
36 import org.xml.sax.ContentHandler JavaDoc;
37 import org.xml.sax.DTDHandler JavaDoc;
38 import org.xml.sax.EntityResolver JavaDoc;
39 import org.xml.sax.ErrorHandler JavaDoc;
40 import org.xml.sax.InputSource JavaDoc;
41 import org.xml.sax.Locator JavaDoc;
42 import org.xml.sax.SAXException JavaDoc;
43 import org.xml.sax.SAXParseException JavaDoc;
44 import org.xml.sax.ext.DeclHandler JavaDoc;
45 import org.xml.sax.ext.LexicalHandler JavaDoc;
46 import org.apache.xml.serializer.SerializationHandler;
47
48
49 /**
50  * A TransformerHandler
51  * listens for SAX ContentHandler parse events and transforms
52  * them to a Result.
53  */

54 public class TransformerHandlerImpl
55         implements EntityResolver JavaDoc, DTDHandler JavaDoc, ContentHandler JavaDoc, ErrorHandler JavaDoc,
56                    LexicalHandler JavaDoc, TransformerHandler JavaDoc, DeclHandler JavaDoc
57 {
58   
59   private boolean m_insideParse = false;
60
61   ////////////////////////////////////////////////////////////////////
62
// Constructors.
63
////////////////////////////////////////////////////////////////////
64

65   /**
66    * Construct a TransformerHandlerImpl.
67    *
68    * @param transformer Non-null reference to the Xalan transformer impl.
69    * @param doFragment True if the result should be a document fragement.
70    * @param baseSystemID The system ID to use as the base for relative URLs.
71    */

72   public TransformerHandlerImpl(TransformerImpl transformer,
73                                 boolean doFragment, String JavaDoc baseSystemID)
74   {
75
76     super();
77
78     m_transformer = transformer;
79     m_baseSystemID = baseSystemID;
80
81     XPathContext xctxt = transformer.getXPathContext();
82     DTM dtm = xctxt.getDTM(null, true, transformer, true, true);
83     
84     m_dtm = dtm;
85     dtm.setDocumentBaseURI(baseSystemID);
86
87     m_contentHandler = dtm.getContentHandler();
88     m_dtdHandler = dtm.getDTDHandler();
89     m_entityResolver = dtm.getEntityResolver();
90     m_errorHandler = dtm.getErrorHandler();
91     m_lexicalHandler = dtm.getLexicalHandler();
92   }
93   
94   /**
95    * Do what needs to be done to shut down the CoRoutine management.
96    */

97   protected void clearCoRoutine()
98   {
99     clearCoRoutine(null);
100   }
101   
102   /**
103    * Do what needs to be done to shut down the CoRoutine management.
104    */

105   protected void clearCoRoutine(SAXException JavaDoc ex)
106   {
107     if(null != ex)
108       m_transformer.setExceptionThrown(ex);
109     
110     if(m_dtm instanceof SAX2DTM)
111     {
112       if(DEBUG)
113         System.err.println("In clearCoRoutine...");
114       try
115       {
116         SAX2DTM sax2dtm = ((SAX2DTM)m_dtm);
117         if(null != m_contentHandler
118            && m_contentHandler instanceof IncrementalSAXSource_Filter)
119         {
120           IncrementalSAXSource_Filter sp =
121             (IncrementalSAXSource_Filter)m_contentHandler;
122           // This should now be all that's needed.
123
sp.deliverMoreNodes(false);
124         }
125         
126         sax2dtm.clearCoRoutine(true);
127         m_contentHandler = null;
128         m_dtdHandler = null;
129         m_entityResolver = null;
130         m_errorHandler = null;
131         m_lexicalHandler = null;
132       }
133       catch(Throwable JavaDoc throwable)
134       {
135         throwable.printStackTrace();
136       }
137       
138       if(DEBUG)
139         System.err.println("...exiting clearCoRoutine");
140     }
141   }
142   
143   ////////////////////////////////////////////////////////////////////
144
// Implementation of javax.xml.transform.sax.TransformerHandler.
145
////////////////////////////////////////////////////////////////////
146

147   /**
148    * Enables the user of the TransformerHandler to set the
149    * to set the Result for the transformation.
150    *
151    * @param result A Result instance, should not be null.
152    *
153    * @throws IllegalArgumentException if result is invalid for some reason.
154    */

155   public void setResult(Result JavaDoc result) throws IllegalArgumentException JavaDoc
156   {
157
158     if (null == result)
159       throw new IllegalArgumentException JavaDoc(XSLMessages.createMessage(XSLTErrorResources.ER_RESULT_NULL, null)); //"result should not be null");
160

161     try
162     {
163 // ContentHandler handler =
164
// m_transformer.createResultContentHandler(result);
165
// m_transformer.setContentHandler(handler);
166
SerializationHandler xoh =
167             m_transformer.createSerializationHandler(result);
168         m_transformer.setSerializationHandler(xoh);
169     }
170     catch (javax.xml.transform.TransformerException JavaDoc te)
171     {
172       throw new IllegalArgumentException JavaDoc(XSLMessages.createMessage(XSLTErrorResources.ER_RESULT_COULD_NOT_BE_SET, null)); //"result could not be set");
173
}
174
175     m_result = result;
176   }
177
178   /**
179    * Set the base ID (URI or system ID) from where relative
180    * URLs will be resolved.
181    * @param systemID Base URI for the source tree.
182    */

183   public void setSystemId(String JavaDoc systemID)
184   {
185     m_baseSystemID = systemID;
186     m_dtm.setDocumentBaseURI(systemID);
187   }
188
189   /**
190    * Get the base ID (URI or system ID) from where relative
191    * URLs will be resolved.
192    * @return The systemID that was set with {@link #setSystemId}.
193    */

194   public String JavaDoc getSystemId()
195   {
196     return m_baseSystemID;
197   }
198
199   /**
200    * Get the Transformer associated with this handler, which
201    * is needed in order to set parameters and output properties.
202    *
203    * @return The Transformer associated with this handler
204    */

205   public Transformer JavaDoc getTransformer()
206   {
207     return m_transformer;
208   }
209
210   ////////////////////////////////////////////////////////////////////
211
// Implementation of org.xml.sax.EntityResolver.
212
////////////////////////////////////////////////////////////////////
213

214   /**
215    * Filter an external entity resolution.
216    *
217    * @param publicId The entity's public identifier, or null.
218    * @param systemId The entity's system identifier.
219    * @return A new InputSource or null for the default.
220    *
221    * @throws IOException
222    * @throws SAXException The client may throw
223    * an exception during processing.
224    * @throws java.io.IOException The client may throw an
225    * I/O-related exception while obtaining the
226    * new InputSource.
227    * @see org.xml.sax.EntityResolver#resolveEntity
228    */

229   public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
230           throws SAXException JavaDoc, IOException JavaDoc
231   {
232
233     if (m_entityResolver != null)
234     {
235       return m_entityResolver.resolveEntity(publicId, systemId);
236     }
237     else
238     {
239       return null;
240     }
241   }
242
243   ////////////////////////////////////////////////////////////////////
244
// Implementation of org.xml.sax.DTDHandler.
245
////////////////////////////////////////////////////////////////////
246

247   /**
248    * Filter a notation declaration event.
249    *
250    * @param name The notation name.
251    * @param publicId The notation's public identifier, or null.
252    * @param systemId The notation's system identifier, or null.
253    * @throws SAXException The client may throw
254    * an exception during processing.
255    * @see org.xml.sax.DTDHandler#notationDecl
256    */

257   public void notationDecl(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
258           throws SAXException JavaDoc
259   {
260
261     if (m_dtdHandler != null)
262     {
263       m_dtdHandler.notationDecl(name, publicId, systemId);
264     }
265   }
266
267   /**
268    * Filter an unparsed entity declaration event.
269    *
270    * @param name The entity name.
271    * @param publicId The entity's public identifier, or null.
272    * @param systemId The entity's system identifier, or null.
273    * @param notationName The name of the associated notation.
274    * @throws SAXException The client may throw
275    * an exception during processing.
276    * @see org.xml.sax.DTDHandler#unparsedEntityDecl
277    */

278   public void unparsedEntityDecl(
279           String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId, String JavaDoc notationName)
280             throws SAXException JavaDoc
281   {
282
283     if (m_dtdHandler != null)
284     {
285       m_dtdHandler.unparsedEntityDecl(name, publicId, systemId, notationName);
286     }
287   }
288
289   ////////////////////////////////////////////////////////////////////
290
// Implementation of org.xml.sax.ContentHandler.
291
////////////////////////////////////////////////////////////////////
292

293   /**
294    * Filter a new document locator event.
295    *
296    * @param locator The document locator.
297    * @see org.xml.sax.ContentHandler#setDocumentLocator
298    */

299   public void setDocumentLocator(Locator JavaDoc locator)
300   {
301
302     if (DEBUG)
303       System.out.println("TransformerHandlerImpl#setDocumentLocator: "
304                          + locator.getSystemId());
305
306     this.m_locator = locator;
307     
308     if(null == m_baseSystemID)
309     {
310       setSystemId(locator.getSystemId());
311     }
312
313     if (m_contentHandler != null)
314     {
315       m_contentHandler.setDocumentLocator(locator);
316     }
317   }
318
319   /**
320    * Filter a start document event.
321    *
322    * @throws SAXException The client may throw
323    * an exception during processing.
324    * @see org.xml.sax.ContentHandler#startDocument
325    */

326   public void startDocument() throws SAXException JavaDoc
327   {
328
329     if (DEBUG)
330       System.out.println("TransformerHandlerImpl#startDocument");
331       
332     m_insideParse = true;
333
334    // Thread listener = new Thread(m_transformer);
335

336     if (m_contentHandler != null)
337     {
338       //m_transformer.setTransformThread(listener);
339
if(DTMManager.getIncremental())
340       {
341         m_transformer.setSourceTreeDocForThread(m_dtm.getDocument());
342             
343         int cpriority = Thread.currentThread().getPriority();
344     
345         // runTransformThread is equivalent with the 2.0.1 code,
346
// except that the Thread may come from a pool.
347
m_transformer.runTransformThread( cpriority );
348       }
349
350       // This is now done _last_, because IncrementalSAXSource_Filter
351
// will immediately go into a "wait until events are requested"
352
// pause. I believe that will close our timing window.
353
// %REVIEW%
354
m_contentHandler.startDocument();
355    }
356         
357    //listener.setDaemon(false);
358
//listener.start();
359

360   }
361
362   /**
363    * Filter an end document event.
364    *
365    * @throws SAXException The client may throw
366    * an exception during processing.
367    * @see org.xml.sax.ContentHandler#endDocument
368    */

369   public void endDocument() throws SAXException JavaDoc
370   {
371
372     if (DEBUG)
373       System.out.println("TransformerHandlerImpl#endDocument");
374
375     m_insideParse = false;
376     
377     if (m_contentHandler != null)
378     {
379       m_contentHandler.endDocument();
380     }
381     
382     if(DTMManager.getIncremental())
383     {
384       m_transformer.waitTransformThread();
385     }
386     else
387     {
388       m_transformer.setSourceTreeDocForThread(m_dtm.getDocument());
389       m_transformer.run();
390     }
391    /* Thread transformThread = m_transformer.getTransformThread();
392
393     if (null != transformThread)
394     {
395       try
396       {
397
398         // This should wait until the transformThread is considered not alive.
399         transformThread.join();
400
401         if (!m_transformer.hasTransformThreadErrorCatcher())
402         {
403           Exception e = m_transformer.getExceptionThrown();
404
405           if (null != e)
406             throw new org.xml.sax.SAXException(e);
407         }
408
409         m_transformer.setTransformThread(null);
410       }
411       catch (InterruptedException ie){}
412     }*/

413   }
414
415   /**
416    * Filter a start Namespace prefix mapping event.
417    *
418    * @param prefix The Namespace prefix.
419    * @param uri The Namespace URI.
420    * @throws SAXException The client may throw
421    * an exception during processing.
422    * @see org.xml.sax.ContentHandler#startPrefixMapping
423    */

424   public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
425           throws SAXException JavaDoc
426   {
427
428     if (DEBUG)
429       System.out.println("TransformerHandlerImpl#startPrefixMapping: "
430                          + prefix + ", " + uri);
431
432     if (m_contentHandler != null)
433     {
434       m_contentHandler.startPrefixMapping(prefix, uri);
435     }
436   }
437
438   /**
439    * Filter an end Namespace prefix mapping event.
440    *
441    * @param prefix The Namespace prefix.
442    * @throws SAXException The client may throw
443    * an exception during processing.
444    * @see org.xml.sax.ContentHandler#endPrefixMapping
445    */

446   public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc
447   {
448
449     if (DEBUG)
450       System.out.println("TransformerHandlerImpl#endPrefixMapping: "
451                          + prefix);
452
453     if (m_contentHandler != null)
454     {
455       m_contentHandler.endPrefixMapping(prefix);
456     }
457   }
458
459   /**
460    * Filter a start element event.
461    *
462    * @param uri The element's Namespace URI, or the empty string.
463    * @param localName The element's local name, or the empty string.
464    * @param qName The element's qualified (prefixed) name, or the empty
465    * string.
466    * @param atts The element's attributes.
467    * @throws SAXException The client may throw
468    * an exception during processing.
469    * @see org.xml.sax.ContentHandler#startElement
470    */

471   public void startElement(
472           String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts)
473             throws SAXException JavaDoc
474   {
475
476     if (DEBUG)
477       System.out.println("TransformerHandlerImpl#startElement: " + qName);
478
479     if (m_contentHandler != null)
480     {
481       m_contentHandler.startElement(uri, localName, qName, atts);
482     }
483   }
484
485   /**
486    * Filter an end element event.
487    *
488    * @param uri The element's Namespace URI, or the empty string.
489    * @param localName The element's local name, or the empty string.
490    * @param qName The element's qualified (prefixed) name, or the empty
491    * string.
492    * @throws SAXException The client may throw
493    * an exception during processing.
494    * @see org.xml.sax.ContentHandler#endElement
495    */

496   public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
497           throws SAXException JavaDoc
498   {
499
500     if (DEBUG)
501       System.out.println("TransformerHandlerImpl#endElement: " + qName);
502
503     if (m_contentHandler != null)
504     {
505       m_contentHandler.endElement(uri, localName, qName);
506     }
507   }
508
509   /**
510    * Filter a character data event.
511    *
512    * @param ch An array of characters.
513    * @param start The starting position in the array.
514    * @param length The number of characters to use from the array.
515    * @throws SAXException The client may throw
516    * an exception during processing.
517    * @see org.xml.sax.ContentHandler#characters
518    */

519   public void characters(char ch[], int start, int length) throws SAXException JavaDoc
520   {
521
522     if (DEBUG)
523       System.out.println("TransformerHandlerImpl#characters: " + start + ", "
524                          + length);
525
526     if (m_contentHandler != null)
527     {
528       m_contentHandler.characters(ch, start, length);
529     }
530   }
531
532   /**
533    * Filter an ignorable whitespace event.
534    *
535    * @param ch An array of characters.
536    * @param start The starting position in the array.
537    * @param length The number of characters to use from the array.
538    * @throws SAXException The client may throw
539    * an exception during processing.
540    * @see org.xml.sax.ContentHandler#ignorableWhitespace
541    */

542   public void ignorableWhitespace(char ch[], int start, int length)
543           throws SAXException JavaDoc
544   {
545
546     if (DEBUG)
547       System.out.println("TransformerHandlerImpl#ignorableWhitespace: "
548                          + start + ", " + length);
549
550     if (m_contentHandler != null)
551     {
552       m_contentHandler.ignorableWhitespace(ch, start, length);
553     }
554   }
555
556   /**
557    * Filter a processing instruction event.
558    *
559    * @param target The processing instruction target.
560    * @param data The text following the target.
561    * @throws SAXException The client may throw
562    * an exception during processing.
563    * @see org.xml.sax.ContentHandler#processingInstruction
564    */

565   public void processingInstruction(String JavaDoc target, String JavaDoc data)
566           throws SAXException JavaDoc
567   {
568
569     if (DEBUG)
570       System.out.println("TransformerHandlerImpl#processingInstruction: "
571                          + target + ", " + data);
572
573     if (m_contentHandler != null)
574     {
575       m_contentHandler.processingInstruction(target, data);
576     }
577   }
578
579   /**
580    * Filter a skipped entity event.
581    *
582    * @param name The name of the skipped entity.
583    * @throws SAXException The client may throw
584    * an exception during processing.
585    * @see org.xml.sax.ContentHandler#skippedEntity
586    */

587   public void skippedEntity(String JavaDoc name) throws SAXException JavaDoc
588   {
589
590     if (DEBUG)
591       System.out.println("TransformerHandlerImpl#skippedEntity: " + name);
592
593     if (m_contentHandler != null)
594     {
595       m_contentHandler.skippedEntity(name);
596     }
597   }
598
599   ////////////////////////////////////////////////////////////////////
600
// Implementation of org.xml.sax.ErrorHandler.
601
////////////////////////////////////////////////////////////////////
602

603   /**
604    * Filter a warning event.
605    *
606    * @param e The nwarning as an exception.
607    * @throws SAXException The client may throw
608    * an exception during processing.
609    * @see org.xml.sax.ErrorHandler#warning
610    */

611   public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc
612   {
613     // This is not great, but we really would rather have the error
614
// handler be the error listener if it is a error handler. Coroutine's fatalError
615
// can't really be configured, so I think this is the best thing right now
616
// for error reporting. Possibly another JAXP 1.1 hole. -sb
617
javax.xml.transform.ErrorListener JavaDoc errorListener = m_transformer.getErrorListener();
618     if(errorListener instanceof ErrorHandler JavaDoc)
619     {
620       ((ErrorHandler JavaDoc)errorListener).warning(e);
621     }
622     else
623     {
624       try
625       {
626         errorListener.warning(new javax.xml.transform.TransformerException JavaDoc(e));
627       }
628       catch(javax.xml.transform.TransformerException JavaDoc te)
629       {
630         throw e;
631       }
632     }
633   }
634
635   /**
636    * Filter an error event.
637    *
638    * @param e The error as an exception.
639    * @throws SAXException The client may throw
640    * an exception during processing.
641    * @see org.xml.sax.ErrorHandler#error
642    */

643   public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc
644   {
645     // %REVIEW% I don't think this should be called. -sb
646
// clearCoRoutine(e);
647

648     // This is not great, but we really would rather have the error
649
// handler be the error listener if it is a error handler. Coroutine's fatalError
650
// can't really be configured, so I think this is the best thing right now
651
// for error reporting. Possibly another JAXP 1.1 hole. -sb
652
javax.xml.transform.ErrorListener JavaDoc errorListener = m_transformer.getErrorListener();
653     if(errorListener instanceof ErrorHandler JavaDoc)
654     {
655       ((ErrorHandler JavaDoc)errorListener).error(e);
656       if(null != m_errorHandler)
657         m_errorHandler.error(e); // may not be called.
658
}
659     else
660     {
661       try
662       {
663         errorListener.error(new javax.xml.transform.TransformerException JavaDoc(e));
664         if(null != m_errorHandler)
665           m_errorHandler.error(e); // may not be called.
666
}
667       catch(javax.xml.transform.TransformerException JavaDoc te)
668       {
669         throw e;
670       }
671     }
672   }
673
674   /**
675    * Filter a fatal error event.
676    *
677    * @param e The error as an exception.
678    * @throws SAXException The client may throw
679    * an exception during processing.
680    * @see org.xml.sax.ErrorHandler#fatalError
681    */

682   public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc
683   {
684     if(null != m_errorHandler)
685     {
686       try
687       {
688         m_errorHandler.fatalError(e);
689       }
690       catch(SAXParseException JavaDoc se)
691       {
692         // ignore
693
}
694       // clearCoRoutine(e);
695
}
696
697     // This is not great, but we really would rather have the error
698
// handler be the error listener if it is a error handler. Coroutine's fatalError
699
// can't really be configured, so I think this is the best thing right now
700
// for error reporting. Possibly another JAXP 1.1 hole. -sb
701
javax.xml.transform.ErrorListener JavaDoc errorListener = m_transformer.getErrorListener();
702     
703     if(errorListener instanceof ErrorHandler JavaDoc)
704     {
705       ((ErrorHandler JavaDoc)errorListener).fatalError(e);
706       if(null != m_errorHandler)
707         m_errorHandler.fatalError(e); // may not be called.
708
}
709     else
710     {
711       try
712       {
713         errorListener.fatalError(new javax.xml.transform.TransformerException JavaDoc(e));
714         if(null != m_errorHandler)
715           m_errorHandler.fatalError(e); // may not be called.
716
}
717       catch(javax.xml.transform.TransformerException JavaDoc te)
718       {
719         throw e;
720       }
721     }
722   }
723
724   ////////////////////////////////////////////////////////////////////
725
// Implementation of org.xml.sax.ext.LexicalHandler.
726
////////////////////////////////////////////////////////////////////
727

728   /**
729    * Report the start of DTD declarations, if any.
730    *
731    * <p>Any declarations are assumed to be in the internal subset
732    * unless otherwise indicated by a {@link #startEntity startEntity}
733    * event.</p>
734    *
735    * <p>Note that the start/endDTD events will appear within
736    * the start/endDocument events from ContentHandler and
737    * before the first startElement event.</p>
738    *
739    * @param name The document type name.
740    * @param publicId The declared public identifier for the
741    * external DTD subset, or null if none was declared.
742    * @param systemId The declared system identifier for the
743    * external DTD subset, or null if none was declared.
744    * @throws SAXException The application may raise an
745    * exception.
746    * @see #endDTD
747    * @see #startEntity
748    */

749   public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
750           throws SAXException JavaDoc
751   {
752
753     if (DEBUG)
754       System.out.println("TransformerHandlerImpl#startDTD: " + name + ", "
755                          + publicId + ", " + systemId);
756
757     if (null != m_lexicalHandler)
758     {
759       m_lexicalHandler.startDTD(name, publicId, systemId);
760     }
761   }
762
763   /**
764    * Report the end of DTD declarations.
765    *
766    * @throws SAXException The application may raise an exception.
767    * @see #startDTD
768    */

769   public void endDTD() throws SAXException JavaDoc
770   {
771
772     if (DEBUG)
773       System.out.println("TransformerHandlerImpl#endDTD");
774
775     if (null != m_lexicalHandler)
776     {
777       m_lexicalHandler.endDTD();
778     }
779   }
780
781   /**
782    * Report the beginning of an entity in content.
783    *
784    * <p><strong>NOTE:</entity> entity references in attribute
785    * values -- and the start and end of the document entity --
786    * are never reported.</p>
787    *
788    * <p>The start and end of the external DTD subset are reported
789    * using the pseudo-name "[dtd]". All other events must be
790    * properly nested within start/end entity events.</p>
791    *
792    * <p>Note that skipped entities will be reported through the
793    * {@link org.xml.sax.ContentHandler#skippedEntity skippedEntity}
794    * event, which is part of the ContentHandler interface.</p>
795    *
796    * @param name The name of the entity. If it is a parameter
797    * entity, the name will begin with '%'.
798    * @throws SAXException The application may raise an exception.
799    * @see #endEntity
800    * @see org.xml.sax.ext.DeclHandler#internalEntityDecl
801    * @see org.xml.sax.ext.DeclHandler#externalEntityDecl
802    */

803   public void startEntity(String JavaDoc name) throws SAXException JavaDoc
804   {
805
806     if (DEBUG)
807       System.out.println("TransformerHandlerImpl#startEntity: " + name);
808
809     if (null != m_lexicalHandler)
810     {
811       m_lexicalHandler.startEntity(name);
812     }
813   }
814
815   /**
816    * Report the end of an entity.
817    *
818    * @param name The name of the entity that is ending.
819    * @throws SAXException The application may raise an exception.
820    * @see #startEntity
821    */

822   public void endEntity(String JavaDoc name) throws SAXException JavaDoc
823   {
824
825     if (DEBUG)
826       System.out.println("TransformerHandlerImpl#endEntity: " + name);
827
828     if (null != m_lexicalHandler)
829     {
830       m_lexicalHandler.endEntity(name);
831     }
832   }
833
834   /**
835    * Report the start of a CDATA section.
836    *
837    * <p>The contents of the CDATA section will be reported through
838    * the regular {@link org.xml.sax.ContentHandler#characters
839    * characters} event.</p>
840    *
841    * @throws SAXException The application may raise an exception.
842    * @see #endCDATA
843    */

844   public void startCDATA() throws SAXException JavaDoc
845   {
846
847     if (DEBUG)
848       System.out.println("TransformerHandlerImpl#startCDATA");
849
850     if (null != m_lexicalHandler)
851     {
852       m_lexicalHandler.startCDATA();
853     }
854   }
855
856   /**
857    * Report the end of a CDATA section.
858    *
859    * @throws SAXException The application may raise an exception.
860    * @see #startCDATA
861    */

862   public void endCDATA() throws SAXException JavaDoc
863   {
864
865     if (DEBUG)
866       System.out.println("TransformerHandlerImpl#endCDATA");
867
868     if (null != m_lexicalHandler)
869     {
870       m_lexicalHandler.endCDATA();
871     }
872   }
873
874   /**
875    * Report an XML comment anywhere in the document.
876    *
877    * <p>This callback will be used for comments inside or outside the
878    * document element, including comments in the external DTD
879    * subset (if read).</p>
880    *
881    * @param ch An array holding the characters in the comment.
882    * @param start The starting position in the array.
883    * @param length The number of characters to use from the array.
884    * @throws SAXException The application may raise an exception.
885    */

886   public void comment(char ch[], int start, int length) throws SAXException JavaDoc
887   {
888
889     if (DEBUG)
890       System.out.println("TransformerHandlerImpl#comment: " + start + ", "
891                          + length);
892
893     if (null != m_lexicalHandler)
894     {
895       m_lexicalHandler.comment(ch, start, length);
896     }
897   }
898
899   ////////////////////////////////////////////////////////////////////
900
// Implementation of org.xml.sax.ext.DeclHandler.
901
////////////////////////////////////////////////////////////////////
902

903   /**
904    * Report an element type declaration.
905    *
906    * <p>The content model will consist of the string "EMPTY", the
907    * string "ANY", or a parenthesised group, optionally followed
908    * by an occurrence indicator. The model will be normalized so
909    * that all whitespace is removed,and will include the enclosing
910    * parentheses.</p>
911    *
912    * @param name The element type name.
913    * @param model The content model as a normalized string.
914    * @throws SAXException The application may raise an exception.
915    */

916   public void elementDecl(String JavaDoc name, String JavaDoc model) throws SAXException JavaDoc
917   {
918
919     if (DEBUG)
920       System.out.println("TransformerHandlerImpl#elementDecl: " + name + ", "
921                          + model);
922
923     if (null != m_declHandler)
924     {
925       m_declHandler.elementDecl(name, model);
926     }
927   }
928
929   /**
930    * Report an attribute type declaration.
931    *
932    * <p>Only the effective (first) declaration for an attribute will
933    * be reported. The type will be one of the strings "CDATA",
934    * "ID", "IDREF", "IDREFS", "NMTOKEN", "NMTOKENS", "ENTITY",
935    * "ENTITIES", or "NOTATION", or a parenthesized token group with
936    * the separator "|" and all whitespace removed.</p>
937    *
938    * @param eName The name of the associated element.
939    * @param aName The name of the attribute.
940    * @param type A string representing the attribute type.
941    * @param valueDefault A string representing the attribute default
942    * ("#IMPLIED", "#REQUIRED", or "#FIXED") or null if
943    * none of these applies.
944    * @param value A string representing the attribute's default value,
945    * or null if there is none.
946    * @throws SAXException The application may raise an exception.
947    */

948   public void attributeDecl(
949           String JavaDoc eName, String JavaDoc aName, String JavaDoc type, String JavaDoc valueDefault, String JavaDoc value)
950             throws SAXException JavaDoc
951   {
952
953     if (DEBUG)
954       System.out.println("TransformerHandlerImpl#attributeDecl: " + eName
955                          + ", " + aName + ", etc...");
956
957     if (null != m_declHandler)
958     {
959       m_declHandler.attributeDecl(eName, aName, type, valueDefault, value);
960     }
961   }
962
963   /**
964    * Report an internal entity declaration.
965    *
966    * <p>Only the effective (first) declaration for each entity
967    * will be reported.</p>
968    *
969    * @param name The name of the entity. If it is a parameter
970    * entity, the name will begin with '%'.
971    * @param value The replacement text of the entity.
972    * @throws SAXException The application may raise an exception.
973    * @see #externalEntityDecl
974    * @see org.xml.sax.DTDHandler#unparsedEntityDecl
975    */

976   public void internalEntityDecl(String JavaDoc name, String JavaDoc value)
977           throws SAXException JavaDoc
978   {
979
980     if (DEBUG)
981       System.out.println("TransformerHandlerImpl#internalEntityDecl: " + name
982                          + ", " + value);
983
984     if (null != m_declHandler)
985     {
986       m_declHandler.internalEntityDecl(name, value);
987     }
988   }
989
990   /**
991    * Report a parsed external entity declaration.
992    *
993    * <p>Only the effective (first) declaration for each entity
994    * will be reported.</p>
995    *
996    * @param name The name of the entity. If it is a parameter
997    * entity, the name will begin with '%'.
998    * @param publicId The declared public identifier of the entity, or
999    * null if none was declared.
1000   * @param systemId The declared system identifier of the entity.
1001   * @throws SAXException The application may raise an exception.
1002   * @see #internalEntityDecl
1003   * @see org.xml.sax.DTDHandler#unparsedEntityDecl
1004   */

1005  public void externalEntityDecl(
1006          String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc
1007  {
1008
1009    if (DEBUG)
1010      System.out.println("TransformerHandlerImpl#externalEntityDecl: " + name
1011                         + ", " + publicId + ", " + systemId);
1012
1013    if (null != m_declHandler)
1014    {
1015      m_declHandler.externalEntityDecl(name, publicId, systemId);
1016    }
1017  }
1018
1019  ////////////////////////////////////////////////////////////////////
1020
// Internal state.
1021
////////////////////////////////////////////////////////////////////
1022

1023  /** Set to true for diagnostics output. */
1024  private static boolean DEBUG = false;
1025
1026  /**
1027   * The transformer this will use to transform a
1028   * source tree into a result tree.
1029   */

1030  private TransformerImpl m_transformer;
1031
1032  /** The system ID to use as a base for relative URLs. */
1033  private String JavaDoc m_baseSystemID;
1034
1035  /** The result for the transformation. */
1036  private Result JavaDoc m_result = null;
1037
1038  /** The locator for this TransformerHandler. */
1039  private Locator JavaDoc m_locator = null;
1040
1041  /** The entity resolver to aggregate to. */
1042  private EntityResolver JavaDoc m_entityResolver = null;
1043
1044  /** The DTD handler to aggregate to. */
1045  private DTDHandler JavaDoc m_dtdHandler = null;
1046
1047  /** The content handler to aggregate to. */
1048  private ContentHandler JavaDoc m_contentHandler = null;
1049
1050  /** The error handler to aggregate to. */
1051  private ErrorHandler JavaDoc m_errorHandler = null;
1052
1053  /** The lexical handler to aggregate to. */
1054  private LexicalHandler JavaDoc m_lexicalHandler = null;
1055
1056  /** The decl handler to aggregate to. */
1057  private DeclHandler JavaDoc m_declHandler = null;
1058  
1059  /** The Document Table Instance we are transforming. */
1060  DTM m_dtm;
1061}
1062
Popular Tags