KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > parsers > xerces > XercesTracer


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: XercesTracer.java,v 1.3 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.parsers.xerces;
25
26 import org.enhydra.apache.xerces.framework.XMLAttrList;
27 import org.enhydra.apache.xerces.framework.XMLContentSpec;
28 import org.enhydra.apache.xerces.readers.XMLEntityHandler;
29 import org.enhydra.apache.xerces.utils.QName;
30 import org.enhydra.apache.xerces.utils.StringPool;
31 import org.enhydra.apache.xerces.validators.common.XMLAttributeDecl;
32 import org.enhydra.xml.io.InputSourceOps;
33 import org.enhydra.xml.xmlc.XMLCError;
34 import org.enhydra.xml.xmlc.parsers.ParseTracer;
35 import org.xml.sax.EntityResolver JavaDoc;
36 import org.xml.sax.InputSource JavaDoc;
37
38 /**
39  * Tracing for parser callbacks. Isolated in a class because it
40  * was getting to complex and crowding the actuall handlers.
41  */

42 class XercesTracer {
43     /**
44      * Verbose tracing.
45      */

46     private ParseTracer fTracer;
47
48     /**
49      * String pool used to lookup strings by index.
50      */

51     private StringPool fStringPool;
52
53     /**
54      * Is the tracer enabled?
55      */

56     private boolean fIsEnabled;
57
58     /**
59      * Constructor.
60      */

61     protected XercesTracer(StringPool stringPool,
62                            ParseTracer tracer) {
63         fStringPool = stringPool;
64         fTracer = tracer;
65         fIsEnabled = (fTracer != null) && fTracer.enabled();
66     }
67
68     /*
69      * Determine if tracing is enabled.
70      */

71     public boolean enabled() {
72         return fIsEnabled;
73     }
74
75     /**
76      * Convert XMLEntityHandler.ENTITYTYPE_* to a string
77      */

78     private String JavaDoc getEntityTypeString(int entityType) {
79         switch (entityType) {
80         case XMLEntityHandler.ENTITYTYPE_INTERNAL_PE:
81             return "INTERNAL_PE";
82         case XMLEntityHandler.ENTITYTYPE_EXTERNAL_PE:
83             return "EXTERNAL_PE";
84         case XMLEntityHandler.ENTITYTYPE_INTERNAL:
85             return "INTERNAL";
86         case XMLEntityHandler.ENTITYTYPE_EXTERNAL:
87             return "EXTERNAL";
88         case XMLEntityHandler.ENTITYTYPE_UNPARSED:
89             return "UNPARSED";
90         case XMLEntityHandler.ENTITYTYPE_DOCUMENT:
91             return "DOCUMENT";
92         case XMLEntityHandler.ENTITYTYPE_EXTERNAL_SUBSET:
93             return "EXTERNAL_SUBSET";
94         default:
95             throw new XMLCError("Xerces parser bug: request for entity type");
96         }
97     }
98
99     /**
100      * Convert XMLEntityHandler.CONTEXT_* to a string
101      */

102     private String JavaDoc getEntityContextString(int entityContent) {
103         switch (entityContent) {
104         case XMLEntityHandler.ENTITYREF_IN_ATTVALUE:
105             return "IN_ATTVALUE";
106         case XMLEntityHandler.ENTITYREF_IN_DEFAULTATTVALUE:
107             return "IN_DEFAULTATTVALUE";
108         case XMLEntityHandler.ENTITYREF_IN_CONTENT:
109             return "IN_CONTENT";
110         case XMLEntityHandler.ENTITYREF_IN_DTD_AS_MARKUP:
111             return "IN_DTD_AS_MARKUP";
112         case XMLEntityHandler.ENTITYREF_IN_ENTITYVALUE:
113             return "IN_ENTITYVALUE";
114         case XMLEntityHandler.ENTITYREF_IN_DTD_WITHIN_MARKUP:
115             return "IN_DTD_WITHIN_MARKUP";
116         case XMLEntityHandler.ENTITYREF_DOCUMENT:
117             return "DOCUMENT";
118         case XMLEntityHandler.ENTITYREF_EXTERNAL_SUBSET:
119             return "EXTERNAL_SUBSET";
120         default:
121             throw new XMLCError("Xerces parser bug: request for unknown entity context");
122         }
123     }
124
125     /**
126      * Convert XMLContentSpec.CONTEXTSPECNODE_* to a string
127      */

128     private String JavaDoc getContentSpecTypeString(int contentSpecType) {
129         switch (contentSpecType) {
130         case XMLContentSpec.CONTENTSPECNODE_LEAF:
131             return "LEAF";
132         case XMLContentSpec.CONTENTSPECNODE_ZERO_OR_ONE:
133             return "ZERO_OR_ONE";
134         case XMLContentSpec.CONTENTSPECNODE_ZERO_OR_MORE:
135             return "ZERO_OR_MORE";
136         case XMLContentSpec.CONTENTSPECNODE_ONE_OR_MORE:
137             return "ONE_OR_MORE";
138         case XMLContentSpec.CONTENTSPECNODE_CHOICE:
139             return "CHOICE";
140         case XMLContentSpec.CONTENTSPECNODE_SEQ:
141             return "SEQ";
142         case XMLContentSpec.CONTENTSPECNODE_ANY:
143             return "ANY";
144         case XMLContentSpec.CONTENTSPECNODE_ANY_OTHER:
145             return "ANY_OTHER";
146         case XMLContentSpec.CONTENTSPECNODE_ANY_NS:
147             return "ANY_NS";
148         case XMLContentSpec.CONTENTSPECNODE_ANY_LAX:
149             return "ANY_LAX";
150         case XMLContentSpec.CONTENTSPECNODE_ANY_OTHER_LAX:
151             return "ANY_OTHER_LAX";
152         case XMLContentSpec.CONTENTSPECNODE_ANY_NS_LAX:
153             return "ANY_NS_LAX";
154         case XMLContentSpec.CONTENTSPECNODE_ANY_SKIP:
155             return "ANY_SKIP";
156         case XMLContentSpec.CONTENTSPECNODE_ANY_OTHER_SKIP:
157             return "ANY_OTHER_SKIP";
158         case XMLContentSpec.CONTENTSPECNODE_ANY_NS_SKIP:
159             return "ANY_NS_SKIP";
160         default:
161             throw new XMLCError("Xerces parser bug: request for unknown contentspec type");
162         }
163     }
164
165     /**
166      * Get a string from the string pool.
167      */

168     private String JavaDoc getString(int index) {
169         return fStringPool.toString(index);
170     }
171
172     /**
173      * Get a formatted QName.
174      */

175     private String JavaDoc formatQName(QName qname) {
176         return getString(qname.rawname);
177     }
178
179     /**
180      * @see EntityResolver#resolveEntity
181      */

182     public void resolveEntity(String JavaDoc publicId,
183                               String JavaDoc systemId,
184                               InputSource JavaDoc source) {
185         if (fIsEnabled) {
186             fTracer.trace("resolveEntity: \""
187                          + publicId + "\" \""
188                          + systemId + "\" => "
189                          + InputSourceOps.getName(source));
190         }
191         
192     }
193
194     /**
195      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler#startDocument
196      */

197     public void startDocument() {
198         if (fIsEnabled) {
199             fTracer.trace("startDocument");
200         }
201     }
202
203     /**
204      * Handle end document.
205      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler#endDocument
206      */

207     public void endDocument() {
208         if (fIsEnabled) {
209             fTracer.trace("endDocument");
210         }
211     }
212
213     /**
214      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler#xmlDecl
215      */

216     public void xmlDecl(int version, int encoding, int standalone) throws Exception JavaDoc {
217         if (fIsEnabled) {
218             fTracer.trace("xmlDecl: " + getString(version)
219                           + " " + getString(encoding)
220                           + " " + getString(standalone));
221         }
222     }
223
224     /**
225      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler#textDecl
226      */

227     public void textDecl(int version, int encoding) throws Exception JavaDoc {
228         if (fIsEnabled) {
229             fTracer.trace("textDecl: " + getString(version)
230                           + " " + getString(encoding));
231         }
232     }
233
234     /**
235      * Handle start of element.
236      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler#startElement
237      */

238     public void startElement(QName element,
239                              XMLAttrList attrList,
240                              int attrListHandle) {
241         if (fIsEnabled) {
242             StringBuffer JavaDoc buf = new StringBuffer JavaDoc("startElement: " +
243                                                 formatQName(element));
244             int attrIndex = attrListHandle;
245             while (attrIndex >= 0) {
246                 if (attrList.isSpecified(attrIndex)) {
247                     String JavaDoc uri = getString(attrList.getAttrURI(attrIndex));
248                     buf.append(" ");
249                     if (uri != null) {
250                         buf.append("<");
251                         buf.append(uri);
252                         buf.append(">");
253                     }
254                     buf.append(getString(attrList.getAttrName(attrIndex)));
255                     buf.append("=\"");
256                     buf.append(getString(attrList.getAttValue(attrIndex)));
257                     buf.append("\"");
258                 }
259                 attrIndex = attrList.getNextAttr(attrIndex);
260             }
261             fTracer.trace(buf.toString());
262             fTracer.enter();
263         }
264     }
265
266     /**
267      * Handle end element.
268      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler#endElement
269      */

270     public void endElement(QName element) {
271         if (fIsEnabled) {
272             fTracer.leave();
273             fTracer.trace("endElement: " + formatQName(element));
274         }
275     }
276
277     /**
278      * Handle start of a namespace declaration scope.
279      *
280      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler#startNamespaceDeclScope
281      */

282     public void startNamespaceDeclScope(int prefix, int uri) {
283         if (fIsEnabled) {
284             fTracer.trace("startNamespaceDeclScope: " + getString(prefix)
285                           + " " + getString(uri));
286             fTracer.enter();
287         }
288     }
289
290     /**
291      * Handle end of a namespace declaration scope.
292      *
293      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler#startNamespaceDeclScope
294      */

295     public void endNamespaceDeclScope(int prefix) {
296         if (fIsEnabled) {
297             fTracer.leave();
298             fTracer.trace("endNamespaceDeclScope: " + getString(prefix));
299         }
300     }
301
302     /**
303      * Handle the start of an entity reference.
304      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler#startEntityReference
305      */

306     public void startEntityReference(int entityName,
307                                      int entityType,
308                                      int entityContext,
309                                      boolean processed) {
310         if (fIsEnabled) {
311             fTracer.trace("startEntityReference: "
312                           + getString(entityName)
313                           + " " + getEntityTypeString(entityType)
314                           + " " + getEntityContextString(entityContext)
315                           + ((processed) ? "" : " (ignored)"));
316             fTracer.enter();
317         }
318     }
319
320     /**
321      * Handle the end of an entity reference.
322      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler#endEntityReference
323      */

324     public void endEntityReference(int entityName,
325                                    int entityType,
326                                    int entityContext,
327                                    boolean processed) {
328         if (fIsEnabled) {
329             fTracer.leave();
330             fTracer.trace("endEntityReference: " + getString(entityName)
331                           + ((processed) ? "" : " (ignored)"));
332
333         }
334     }
335
336     /**
337      * Handle start of CDATA section.
338      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler#startCDATA
339      */

340     public void startCDATA() {
341         if (fIsEnabled) {
342             fTracer.trace("startCDATA");
343             fTracer.enter();
344         }
345     }
346
347     /**
348      * Handle for end of CDATA section.
349      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler#endCDATA
350      */

351     public void endCDATA() {
352         if (fIsEnabled) {
353             fTracer.leave();
354             fTracer.trace("endCDATA");
355         }
356     }
357
358     /**
359      * Handle processing instruction.
360      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler#processingInstruction
361      */

362     public void processingInstruction(int target,
363                                       int data) {
364         if (fIsEnabled) {
365             fTracer.trace("processingInstruction: " + getString(target));
366         }
367     }
368
369     /**
370      * Handle a comment.
371      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler#comment
372      */

373     public void comment(int comment) {
374         if (fIsEnabled) {
375             fTracer.trace("comment: " + getString(comment));
376         }
377     }
378
379     /**
380      * Handle characters.
381      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler#characters
382      */

383     public void characters(char ch[],
384                            int start,
385                            int length) {
386         if (fIsEnabled) {
387             fTracer.trace("characters: \"" + new String JavaDoc(ch, start, length) + "\"");
388         }
389     }
390
391     /**
392      * Handle ignorable whitespace.
393      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler#ignorableWhitespace
394      */

395     public void ignorableWhitespace(char ch[],
396                                     int start,
397                                     int length) {
398         if (fIsEnabled) {
399             fTracer.trace("ignorableWhitespace: " + length);
400         }
401     }
402
403     /**
404      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler.DTDHandler#startDTD
405      */

406     public void startDTD(QName rootElement,
407                          int publicId,
408                          int systemId) {
409         if (fIsEnabled) {
410             fTracer.trace("startDTD: " + formatQName(rootElement)
411                          + " " + getString(publicId)
412                          + " " + getString(systemId));
413             fTracer.enter();
414         }
415     }
416
417     /**
418      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler.DTDHandler#internalSubset
419      */

420     public void internalSubset(int internalSubset) {
421         if (fIsEnabled) {
422             fTracer.trace("internalSubset: " + getString(internalSubset));
423         }
424     }
425
426     /**
427      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler.DTDHandler#endDTD
428      */

429     public void endDTD() {
430         if (fIsEnabled) {
431             fTracer.leave();
432         }
433     }
434
435     /**
436      * <!ELEMENT Name contentspec>
437      *
438      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler.DTDHandler#elementDecl
439      */

440     public void elementDecl(QName elementDecl,
441                             int contentSpecType,
442                             int contentSpecIndex,
443                             XMLContentSpec.Provider contentSpecProvider) {
444         if (fIsEnabled) {
445             fTracer.trace("elementDecl: " + formatQName(elementDecl)
446                           + " " + getContentSpecTypeString(contentSpecType)
447                           + " " + XMLContentSpec.toString(contentSpecProvider,
448                                                           fStringPool,
449                                                           contentSpecIndex));
450         }
451     }
452     
453     /**
454      * Get the attribute type as a string.
455      */

456     private String JavaDoc getAttrTypeString(int attType,
457                                      boolean attList) {
458         switch (attType) {
459         case XMLAttributeDecl.TYPE_ENTITY:
460             return (attList ? "ENTITIES" : "ENTITY");
461
462         case XMLAttributeDecl.TYPE_ID:
463             return "ID";
464
465         case XMLAttributeDecl.TYPE_IDREF:
466             return (attList ? "IDREFS" : "IDREF");
467
468         case XMLAttributeDecl.TYPE_NMTOKEN:
469             return (attList ? "NMTOKENS" : "NMTOKEN");
470
471         case XMLAttributeDecl.TYPE_NOTATION:
472             return "NOTATION";
473
474         case XMLAttributeDecl.TYPE_CDATA:
475             return "string";
476         default:
477             return "invalid-attType";
478         }
479     }
480
481     /**
482      * <!ATTLIST Name AttDef>
483      *
484      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler.DTDHandler#attlistDecl
485      */

486     public void attlistDecl(QName elementDecl,
487                             QName attributeDecl,
488                             int attType,
489                             boolean attList,
490                             String JavaDoc enumString,
491                             int attDefaultType,
492                             int attDefaultValue) {
493         //FIXME: boolean attList not handled
494
if (fIsEnabled) {
495             fTracer.trace("attlistDecl: " + formatQName(elementDecl)
496                           + " " + formatQName(attributeDecl)
497                           + " " + getAttrTypeString(attType, attList)
498                           + " " + enumString
499                           + " " + getString(attDefaultType)
500                           + " " + getString(attDefaultValue));
501         }
502     }
503
504     /**
505      * <!ENTITY % Name EntityValue>
506      *
507      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler.DTDHandler#internalPEDecl
508      */

509     public void internalPEDecl(int entityName,
510                                int entityValue) {
511         if (fIsEnabled) {
512             fTracer.trace("internalPEDecl: " + getString(entityName)
513                           + " \"" + getString(entityValue) + "\"");
514         }
515     }
516
517     /**
518      * <!ENTITY % Name ExternalID>
519      *
520      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler.DTDHandler#externalPEDecl
521      */

522     public void externalPEDecl(int entityName,
523                                int publicId,
524                                int systemId) {
525         if (fIsEnabled) {
526             fTracer.trace("externalPEDecl: " + getString(entityName)
527                           + " " + getString(publicId)
528                           + " " + getString(systemId));
529         }
530     }
531
532     /**
533      * <!ENTITY Name EntityValue>
534      *
535      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler.DTDHandler#internalEntityDecl
536      */

537     public void internalEntityDecl(int entityName,
538                                    int entityValue) {
539         if (fIsEnabled) {
540             fTracer.trace("internalEntityDecl: " + getString(entityName)
541                          + " \"" + getString(entityValue) + "\"");
542         }
543     }
544
545     /**
546      * <!ENTITY Name ExternalID>
547      *
548      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler.DTDHandler#externalEntityDecl
549      */

550     public void externalEntityDecl(int entityName,
551                                    int publicId,
552                                    int systemId) {
553         if (fIsEnabled) {
554             fTracer.trace("externalEntityDecl: " + getString(entityName)
555                           + " " + getString(publicId)
556                           + " " + getString(systemId));
557         }
558     }
559
560     /**
561      * <!ENTITY Name ExternalID NDataDecl>
562      *
563      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler.DTDHandler#unparsedEntityDecl
564      */

565     public void unparsedEntityDecl(int entityName,
566                                    int publicId,
567                                    int systemId,
568                                    int notationName) {
569         if (fIsEnabled) {
570             fTracer.trace("unparsedEntityDecl: " + getString(entityName)
571                           + " " + getString(publicId)
572                           + " " + getString(systemId)
573                           + " " + getString(notationName));
574         }
575     }
576
577     /**
578      * <!NOTATION Name ExternalId>
579      *
580      * @see org.enhydra.apache.xerces.framework.XMLDocumentHandler.DTDHandler#notationDecl
581      */

582     public void notationDecl(int notationName,
583                              int publicId,
584                              int systemId) {
585         if (fIsEnabled) {
586             fTracer.trace("notationDecl: " + getString(notationName)
587                           + " " + getString(publicId)
588                           + " " + getString(systemId));
589         }
590     }
591 }
592
Popular Tags