KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xni > DocumentTracer


1 /*
2  * Copyright 2000-2005 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 package xni;
18
19 import java.io.OutputStream JavaDoc;
20 import java.io.OutputStreamWriter JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import java.io.UnsupportedEncodingException JavaDoc;
23 import java.io.Writer JavaDoc;
24
25 import org.apache.xerces.parsers.XMLDocumentParser;
26 import org.apache.xerces.xni.Augmentations;
27 import org.apache.xerces.xni.NamespaceContext;
28 import org.apache.xerces.xni.QName;
29 import org.apache.xerces.xni.XMLAttributes;
30 import org.apache.xerces.xni.XMLDTDContentModelHandler;
31 import org.apache.xerces.xni.XMLDTDHandler;
32 import org.apache.xerces.xni.XMLLocator;
33 import org.apache.xerces.xni.XMLResourceIdentifier;
34 import org.apache.xerces.xni.XMLString;
35 import org.apache.xerces.xni.XNIException;
36 import org.apache.xerces.xni.parser.XMLConfigurationException;
37 import org.apache.xerces.xni.parser.XMLErrorHandler;
38 import org.apache.xerces.xni.parser.XMLInputSource;
39 import org.apache.xerces.xni.parser.XMLParseException;
40 import org.apache.xerces.xni.parser.XMLParserConfiguration;
41
42 /**
43  * Provides a complete trace of XNI document and DTD events for
44  * files parsed.
45  *
46  * @author Andy Clark, IBM
47  * @author Arnaud Le Hors, IBM
48  *
49  * @version $Id: DocumentTracer.java,v 1.28 2005/06/14 20:51:13 mrglavas Exp $
50  */

51 public class DocumentTracer
52     extends XMLDocumentParser
53     implements XMLErrorHandler {
54
55     //
56
// Constants
57
//
58

59     // feature ids
60

61     /** Namespaces feature id (http://xml.org/sax/features/namespaces). */
62     protected static final String JavaDoc NAMESPACES_FEATURE_ID =
63         "http://xml.org/sax/features/namespaces";
64
65     /** Validation feature id (http://xml.org/sax/features/validation). */
66     protected static final String JavaDoc VALIDATION_FEATURE_ID =
67         "http://xml.org/sax/features/validation";
68
69     /** Schema validation feature id (http://apache.org/xml/features/validation/schema). */
70     protected static final String JavaDoc SCHEMA_VALIDATION_FEATURE_ID =
71         "http://apache.org/xml/features/validation/schema";
72
73     /** Schema full checking feature id (http://apache.org/xml/features/validation/schema-full-checking). */
74     protected static final String JavaDoc SCHEMA_FULL_CHECKING_FEATURE_ID =
75         "http://apache.org/xml/features/validation/schema-full-checking";
76
77     /** Character ref notification feature id (http://apache.org/xml/features/scanner/notify-char-refs). */
78     protected static final String JavaDoc NOTIFY_CHAR_REFS_FEATURE_ID =
79         "http://apache.org/xml/features/scanner/notify-char-refs";
80
81     // default settings
82

83     /** Default parser configuration (org.apache.xerces.parsers.XIncludeAwareParserConfiguration). */
84     protected static final String JavaDoc DEFAULT_PARSER_CONFIG =
85         "org.apache.xerces.parsers.XIncludeAwareParserConfiguration";
86
87     /** Default namespaces support (true). */
88     protected static final boolean DEFAULT_NAMESPACES = true;
89
90     /** Default validation support (false). */
91     protected static final boolean DEFAULT_VALIDATION = false;
92
93     /** Default Schema validation support (false). */
94     protected static final boolean DEFAULT_SCHEMA_VALIDATION = false;
95
96     /** Default Schema full checking support (false). */
97     protected static final boolean DEFAULT_SCHEMA_FULL_CHECKING = false;
98
99     /** Default character notifications (false). */
100     protected static final boolean DEFAULT_NOTIFY_CHAR_REFS = false;
101
102     //
103
// Data
104
//
105

106     /** Temporary QName. */
107     private QName fQName = new QName();
108
109     /** Print writer. */
110     protected PrintWriter JavaDoc fOut;
111
112     /** Indent level. */
113     protected int fIndent;
114
115     protected NamespaceContext fNamespaceContext;
116
117     //
118
// Constructors
119
//
120

121     /** Default constructor. */
122     public DocumentTracer() {
123         this(null);
124     } // <init>()
125

126     /** Default constructor. */
127     public DocumentTracer(XMLParserConfiguration config) {
128         super(config);
129         setOutput(new PrintWriter JavaDoc(System.out));
130         fConfiguration.setErrorHandler(this);
131     } // <init>(XMLParserConfiguration)
132

133     //
134
// Public methods
135
//
136

137     /** Sets the output stream for printing. */
138     public void setOutput(OutputStream JavaDoc stream, String JavaDoc encoding)
139         throws UnsupportedEncodingException JavaDoc {
140
141         if (encoding == null) {
142             encoding = "UTF8";
143         }
144
145         Writer writer = new OutputStreamWriter JavaDoc(stream, encoding);
146         fOut = new PrintWriter JavaDoc(writer);
147
148     } // setOutput(OutputStream,String)
149

150     /** Sets the output writer. */
151     public void setOutput(Writer writer) {
152
153         fOut = writer instanceof PrintWriter JavaDoc
154              ? (PrintWriter JavaDoc)writer : new PrintWriter JavaDoc(writer);
155
156     } // setOutput(Writer)
157

158     //
159
// XMLDocumentHandler methods
160
//
161

162     /**
163      * The start of the document.
164      *
165      * @param systemId The system identifier of the entity if the entity
166      * is external, null otherwise.
167      * @param encoding The auto-detected IANA encoding name of the entity
168      * stream. This value will be null in those situations
169      * where the entity encoding is not auto-detected (e.g.
170      * internal entities or a document entity that is
171      * parsed from a java.io.Reader).
172      *
173      * @throws XNIException Thrown by handler to signal an error.
174      */

175     public void startDocument(XMLLocator locator, String JavaDoc encoding,
176                               NamespaceContext namespaceContext, Augmentations augs)
177         throws XNIException {
178         fNamespaceContext = namespaceContext;
179         fIndent = 0;
180         printIndent();
181         fOut.print("startDocument(");
182         fOut.print("locator=");
183         if (locator == null) {
184             fOut.print("null");
185         }
186         else {
187             fOut.print('{');
188             fOut.print("publicId=");
189             printQuotedString(locator.getPublicId());
190             fOut.print(',');
191             fOut.print("literal systemId=");
192             printQuotedString(locator.getLiteralSystemId());
193             fOut.print(',');
194             fOut.print("baseSystemId=");
195             printQuotedString(locator.getBaseSystemId());
196             fOut.print(',');
197             fOut.print("expanded systemId=");
198             printQuotedString(locator.getExpandedSystemId());
199             fOut.print(',');
200             fOut.print("lineNumber=");
201             fOut.print(locator.getLineNumber());
202             fOut.print(',');
203             fOut.print("columnNumber=");
204             fOut.print(locator.getColumnNumber());
205             fOut.print('}');
206         }
207         fOut.print(',');
208         fOut.print("encoding=");
209         printQuotedString(encoding);
210         if (augs != null) {
211             fOut.print(',');
212             printAugmentations(augs);
213         }
214         fOut.println(')');
215         fOut.flush();
216         fIndent++;
217
218     } // XMLLocator()
219

220     /** XML Declaration. */
221     public void xmlDecl(String JavaDoc version, String JavaDoc encoding,
222                         String JavaDoc standalone, Augmentations augs) throws XNIException {
223
224         printIndent();
225         fOut.print("xmlDecl(");
226         fOut.print("version=");
227         printQuotedString(version);
228         fOut.print(',');
229         fOut.print("encoding=");
230         printQuotedString(encoding);
231         fOut.print(',');
232         fOut.print("standalone=");
233         printQuotedString(standalone);
234         if (augs != null) {
235             fOut.print(',');
236             printAugmentations(augs);
237         }
238         fOut.println(')');
239
240     } // xmlDecl(String,String,String,String)
241

242     /** Doctype declaration. */
243     public void doctypeDecl(String JavaDoc rootElement, String JavaDoc publicId,
244                             String JavaDoc systemId, Augmentations augs) throws XNIException {
245
246         printIndent();
247         fOut.print("doctypeDecl(");
248         fOut.print("rootElement=");
249         printQuotedString(rootElement);
250         fOut.print(',');
251         fOut.print("publicId=");
252         printQuotedString(publicId);
253         fOut.print(',');
254         fOut.print("systemId=");
255         printQuotedString(systemId);
256         fOut.println(')');
257         if (augs != null) {
258             fOut.print(',');
259             printAugmentations(augs);
260         }
261         fOut.flush();
262
263     } // doctypeDecl(String,String,String)
264

265     /** Start element. */
266     public void startElement(QName element, XMLAttributes attributes, Augmentations augs)
267         throws XNIException {
268
269         printInScopeNamespaces();
270
271         printIndent();
272         fOut.print("startElement(");
273         printElement(element, attributes);
274         if (augs != null) {
275             fOut.print(',');
276             printAugmentations(augs);
277         }
278         fOut.println(')');
279         fOut.flush();
280         fIndent++;
281
282     } // startElement(QName,XMLAttributes)
283

284     /** Empty element. */
285     public void emptyElement(QName element, XMLAttributes attributes, Augmentations augs)
286         throws XNIException {
287         printInScopeNamespaces();
288         printIndent();
289         fOut.print("emptyElement(");
290         printElement(element, attributes);
291         if (augs != null) {
292             fOut.print(',');
293             printAugmentations(augs);
294         }
295         fOut.println(')');
296         fOut.flush();
297         printEndNamespaceMapping();
298     } // emptyElement(QName,XMLAttributes)
299

300
301     public void characters(XMLString text, Augmentations augs) throws XNIException {
302
303         printIndent();
304         fOut.print("characters(");
305         fOut.print("text=");
306         printQuotedString(text.ch, text.offset, text.length);
307         /***
308         if (augs != null) {
309             ElementPSVI element = (ElementPSVI)augs.getItem(Constants.ELEMENT_PSVI);
310             fOut.print(",schemaNormalized=");
311             printQuotedString(element.getSchemaNormalizedValue());
312         }
313         /***/

314         if (augs != null) {
315             fOut.print(',');
316             printAugmentations(augs);
317         }
318         fOut.println(')');
319         fOut.flush();
320
321     } // characters(XMLString)
322

323
324
325     /** Ignorable whitespace. */
326     public void ignorableWhitespace(XMLString text, Augmentations augs) throws XNIException {
327
328         printIndent();
329         fOut.print("ignorableWhitespace(");
330         fOut.print("text=");
331         printQuotedString(text.ch, text.offset, text.length);
332         if (augs != null) {
333             fOut.print(',');
334             printAugmentations(augs);
335         }
336         fOut.println(')');
337         fOut.flush();
338
339     } // ignorableWhitespace(XMLString)
340

341     /** End element. */
342     public void endElement(QName element, Augmentations augs) throws XNIException {
343
344         fIndent--;
345         printIndent();
346         fOut.print("endElement(");
347         fOut.print("element=");
348         fOut.print('{');
349         fOut.print("prefix=");
350         printQuotedString(element.prefix);
351         fOut.print(',');
352         fOut.print("localpart=");
353         printQuotedString(element.localpart);
354         fOut.print(',');
355         fOut.print("rawname=");
356         printQuotedString(element.rawname);
357         fOut.print(',');
358         fOut.print("uri=");
359         printQuotedString(element.uri);
360         fOut.print('}');
361         if (augs != null) {
362             fOut.print(',');
363             printAugmentations(augs);
364         }
365         fOut.println(')');
366         fOut.flush();
367
368         printEndNamespaceMapping();
369
370     } // endElement(QName)
371

372
373     /** Start CDATA section. */
374     public void startCDATA(Augmentations augs) throws XNIException {
375
376         printIndent();
377         fOut.print("startCDATA(");
378         if (augs != null) {
379             printAugmentations(augs);
380         }
381         fOut.println(')');
382         fOut.flush();
383         fIndent++;
384
385     } // startCDATA()
386

387     /** End CDATA section. */
388     public void endCDATA(Augmentations augs) throws XNIException {
389
390         fIndent--;
391         printIndent();
392         fOut.print("endCDATA(");
393         if (augs != null) {
394             fOut.print(',');
395             printAugmentations(augs);
396         }
397         fOut.println(')');
398         fOut.flush();
399
400     } // endCDATA()
401

402     /** Start entity. */
403     public void startGeneralEntity(String JavaDoc name,
404                                    XMLResourceIdentifier identifier,
405                                    String JavaDoc encoding, Augmentations augs)
406         throws XNIException {
407
408         printIndent();
409         fOut.print("startGeneralEntity(");
410         fOut.print("name=");
411         printQuotedString(name);
412         fOut.print(',');
413         fOut.print("identifier=");
414         fOut.print(identifier);
415         fOut.print(',');
416         fOut.print("encoding=");
417         printQuotedString(encoding);
418         if (augs != null) {
419             fOut.print(',');
420             printAugmentations(augs);
421         }
422         fOut.println(')');
423         fOut.flush();
424         fIndent++;
425
426     } // startEntity(String,String,String,String)
427

428     /** Text declaration. */
429     public void textDecl(String JavaDoc version, String JavaDoc encoding, Augmentations augs) throws XNIException {
430
431         printIndent();
432         fOut.print("textDecl(");
433         fOut.print("version=");
434         printQuotedString(version);
435         fOut.print(',');
436         fOut.print("encoding=");
437         printQuotedString(encoding);
438         if (augs != null) {
439             fOut.print(',');
440             printAugmentations(augs);
441         }
442         fOut.println(')');
443         fOut.flush();
444
445     } // textDecl(String,String)
446

447     /** Comment. */
448     public void comment(XMLString text, Augmentations augs) throws XNIException {
449
450         printIndent();
451         fOut.print("comment(");
452         fOut.print("text=");
453         printQuotedString(text.ch, text.offset, text.length);
454         if (augs != null) {
455             fOut.print(',');
456             printAugmentations(augs);
457         }
458         fOut.println(')');
459         fOut.flush();
460
461     } // comment(XMLText)
462

463     /** Processing instruction. */
464     public void processingInstruction(String JavaDoc target, XMLString data, Augmentations augs)
465         throws XNIException {
466
467         printIndent();
468         fOut.print("processingInstruction(");
469         fOut.print("target=");
470         printQuotedString(target);
471         fOut.print(',');
472         fOut.print("data=");
473         printQuotedString(data.ch, data.offset, data.length);
474         if (augs != null) {
475             fOut.print(',');
476             printAugmentations(augs);
477         }
478         fOut.println(')');
479         fOut.flush();
480
481     } // processingInstruction(String,XMLString)
482

483     /** End entity. */
484     public void endGeneralEntity(String JavaDoc name, Augmentations augs) throws XNIException {
485
486         fIndent--;
487         printIndent();
488         fOut.print("endGeneralEntity(");
489         fOut.print("name=");
490         printQuotedString(name);
491         if (augs != null) {
492             fOut.print(',');
493             printAugmentations(augs);
494         }
495         fOut.println(')');
496         fOut.flush();
497
498     } // endEntity(String)
499

500     /** End document. */
501     public void endDocument(Augmentations augs) throws XNIException {
502
503         fIndent--;
504         printIndent();
505         fOut.print("endDocument(");
506         if (augs != null) {
507             fOut.print(',');
508             printAugmentations(augs);
509         }
510         fOut.println(')');
511         fOut.flush();
512
513     } // endDocument();
514

515     //
516
// XMLDTDHandler
517
//
518

519     /** Start DTD. */
520     public void startDTD(XMLLocator locator, Augmentations augs) throws XNIException {
521
522         printIndent();
523         fOut.print("startDTD(");
524         fOut.print("locator=");
525         if (locator == null) {
526             fOut.print("null");
527         }
528         else {
529             fOut.print('{');
530             fOut.print("publicId=");
531             printQuotedString(locator.getPublicId());
532             fOut.print(',');
533             fOut.print("literal systemId=");
534             printQuotedString(locator.getLiteralSystemId());
535             fOut.print(',');
536             fOut.print("baseSystemId=");
537             printQuotedString(locator.getBaseSystemId());
538             fOut.print(',');
539             fOut.print("expanded systemId=");
540             printQuotedString(locator.getExpandedSystemId());
541             fOut.print(',');
542             fOut.print("lineNumber=");
543             fOut.print(locator.getLineNumber());
544             fOut.print(',');
545             fOut.print("columnNumber=");
546             fOut.print(locator.getColumnNumber());
547             fOut.print('}');
548         }
549         if (augs != null) {
550             fOut.print(',');
551             printAugmentations(augs);
552         }
553         fOut.println(')');
554         fOut.flush();
555         fIndent++;
556
557     } // startDTD(XMLLocator)
558

559     /** Start external subset. */
560     public void startExternalSubset(XMLResourceIdentifier identifier,
561                                     Augmentations augs) throws XNIException {
562
563         printIndent();
564         fOut.print("startExternalSubset(");
565         if (augs != null) {
566             fOut.print(',');
567             printAugmentations(augs);
568         }
569         fOut.println(')');
570         fOut.flush();
571         fIndent++;
572
573     } // startExternalSubset(Augmentations)
574

575     /** End external subset. */
576     public void endExternalSubset(Augmentations augs) throws XNIException {
577
578         fIndent--;
579         printIndent();
580         fOut.print("endExternalSubset(");
581         if (augs != null) {
582             fOut.print(',');
583             printAugmentations(augs);
584         }
585         fOut.println(')');
586         fOut.flush();
587
588     } // endExternalSubset(Augmentations)
589

590     /** Characters.*/
591     public void ignoredCharacters(XMLString text, Augmentations augs) throws XNIException {
592
593         printIndent();
594         fOut.print("ignoredCharacters(");
595         fOut.print("text=");
596         printQuotedString(text.ch, text.offset, text.length);
597         if (augs != null) {
598             fOut.print(',');
599             printAugmentations(augs);
600         }
601         fOut.println(')');
602         fOut.flush();
603
604     } // characters(XMLString)
605

606     /** Start entity. */
607     public void startParameterEntity(String JavaDoc name,
608                                      XMLResourceIdentifier identifier,
609                                      String JavaDoc encoding, Augmentations augs)
610         throws XNIException {
611
612         printIndent();
613         fOut.print("startParameterEntity(");
614         fOut.print("name=");
615         printQuotedString(name);
616         fOut.print(',');
617         fOut.print("identifier=");
618         fOut.print(identifier);
619         fOut.print(',');
620         fOut.print("encoding=");
621         printQuotedString(encoding);
622         if (augs != null) {
623             fOut.print(',');
624             printAugmentations(augs);
625         }
626         fOut.println(')');
627         fOut.flush();
628         fIndent++;
629
630     } // startEntity(String,String,String,String)
631

632     /** End entity. */
633     public void endParameterEntity(String JavaDoc name, Augmentations augs) throws XNIException {
634
635         fIndent--;
636         printIndent();
637         fOut.print("endParameterEntity(");
638         fOut.print("name=");
639         printQuotedString(name);
640         if (augs != null) {
641             fOut.print(',');
642             printAugmentations(augs);
643         }
644         fOut.println(')');
645         fOut.flush();
646
647     } // endEntity(String)
648

649     /** Element declaration. */
650     public void elementDecl(String JavaDoc name, String JavaDoc contentModel, Augmentations augs)
651         throws XNIException {
652
653         printIndent();
654         fOut.print("elementDecl(");
655         fOut.print("name=");
656         printQuotedString(name);
657         fOut.print(',');
658         fOut.print("contentModel=");
659         printQuotedString(contentModel);
660         if (augs != null) {
661             fOut.print(',');
662             printAugmentations(augs);
663         }
664         fOut.println(')');
665         fOut.flush();
666
667     } // elementDecl(String,String)
668

669     /** Start attribute list. */
670     public void startAttlist(String JavaDoc elementName, Augmentations augs) throws XNIException {
671
672         printIndent();
673         fOut.print("startAttlist(");
674         fOut.print("elementName=");
675         printQuotedString(elementName);
676         if (augs != null) {
677             fOut.print(',');
678             printAugmentations(augs);
679         }
680         fOut.println(')');
681         fOut.flush();
682         fIndent++;
683
684     } // startAttlist(String)
685

686     /** Attribute declaration. */
687     public void attributeDecl(String JavaDoc elementName, String JavaDoc attributeName,
688                               String JavaDoc type, String JavaDoc[] enumeration,
689                               String JavaDoc defaultType, XMLString defaultValue,
690                               XMLString nonNormalizedDefaultValue,
691                               Augmentations augs) throws XNIException {
692
693         printIndent();
694         fOut.print("attributeDecl(");
695         fOut.print("elementName=");
696         printQuotedString(elementName);
697         fOut.print(',');
698         fOut.print("attributeName=");
699         printQuotedString(attributeName);
700         fOut.print(',');
701         fOut.print("type=");
702         printQuotedString(type);
703         fOut.print(',');
704         fOut.print("enumeration=");
705         if (enumeration == null) {
706             fOut.print("null");
707         }
708         else {
709             fOut.print('{');
710             for (int i = 0; i < enumeration.length; i++) {
711                 printQuotedString(enumeration[i]);
712                 if (i < enumeration.length - 1) {
713                     fOut.print(',');
714                 }
715             }
716             fOut.print('}');
717         }
718         fOut.print(',');
719         fOut.print("defaultType=");
720         printQuotedString(defaultType);
721         fOut.print(',');
722         fOut.print("defaultValue=");
723         if (defaultValue == null) {
724             fOut.print("null");
725         }
726         else {
727             printQuotedString(defaultValue.ch, defaultValue.offset,
728                               defaultValue.length);
729         }
730         fOut.print(',');
731         fOut.print("nonNormalizedDefaultValue=");
732         if (nonNormalizedDefaultValue == null) {
733             fOut.print("null");
734         }
735         else {
736             printQuotedString(nonNormalizedDefaultValue.ch, nonNormalizedDefaultValue.offset,
737                               nonNormalizedDefaultValue.length);
738         }
739         if (augs != null) {
740             fOut.print(',');
741             printAugmentations(augs);
742         }
743         fOut.println(')');
744         fOut.flush();
745
746     } // attributeDecl(String,String,String,String[],String,XMLString)
747

748     /** End attribute list. */
749     public void endAttlist(Augmentations augs) throws XNIException {
750
751         fIndent--;
752         printIndent();
753         fOut.print("endAttlist(");
754         if (augs != null) {
755             fOut.print(',');
756             printAugmentations(augs);
757         }
758         fOut.println(')');
759         fOut.flush();
760
761     } // endAttlist()
762

763     /** Internal entity declaration. */
764     public void internalEntityDecl(String JavaDoc name, XMLString text,
765                                    XMLString nonNormalizedText,
766                                    Augmentations augs)
767         throws XNIException {
768
769         printIndent();
770         fOut.print("internalEntityDecl(");
771         fOut.print("name=");
772         printQuotedString(name);
773         fOut.print(',');
774         fOut.print("text=");
775         printQuotedString(text.ch, text.offset, text.length);
776         fOut.print(',');
777         fOut.print("nonNormalizedText=");
778         printQuotedString(nonNormalizedText.ch, nonNormalizedText.offset,
779                           nonNormalizedText.length);
780         if (augs != null) {
781             fOut.print(',');
782             printAugmentations(augs);
783         }
784         fOut.println(')');
785         fOut.flush();
786
787     } // internalEntityDecl(String,XMLString)
788

789     /** External entity declaration. */
790     public void externalEntityDecl(String JavaDoc name,XMLResourceIdentifier identifier, Augmentations augs)
791         throws XNIException {
792
793         printIndent();
794         fOut.print("externalEntityDecl(");
795         fOut.print("name=");
796         printQuotedString(name);
797         fOut.print(',');
798         fOut.print("publicId=");
799         printQuotedString(identifier.getPublicId());
800         fOut.print(',');
801         fOut.print("systemId=");
802         printQuotedString(identifier.getLiteralSystemId());
803         fOut.print(',');
804         fOut.print("baseSystemId=");
805         printQuotedString(identifier.getBaseSystemId());
806         if (augs != null) {
807             fOut.print(',');
808             printAugmentations(augs);
809         }
810         fOut.println(')');
811         fOut.flush();
812
813     } // externalEntityDecl(String,String,String)
814

815     /** Unparsed entity declaration. */
816     public void unparsedEntityDecl(String JavaDoc name, XMLResourceIdentifier identifier,
817                                    String JavaDoc notation, Augmentations augs) throws XNIException {
818
819         printIndent();
820         fOut.print("unparsedEntityDecl(");
821         fOut.print("name=");
822         printQuotedString(name);
823         fOut.print(',');
824         fOut.print("publicId=");
825         printQuotedString(identifier.getPublicId());
826         fOut.print(',');
827         fOut.print("systemId=");
828         printQuotedString(identifier.getLiteralSystemId());
829         fOut.print(',');
830         fOut.print("baseSystemId=");
831         printQuotedString(identifier.getBaseSystemId());
832         fOut.print(',');
833         fOut.print("notation=");
834         printQuotedString(notation);
835         if (augs != null) {
836             fOut.print(',');
837             printAugmentations(augs);
838         }
839         fOut.println(')');
840         fOut.flush();
841
842     } // unparsedEntityDecl(String,String,String,String)
843

844     /** Notation declaration. */
845     public void notationDecl(String JavaDoc name, XMLResourceIdentifier identifier,
846                              Augmentations augs) throws XNIException {
847
848         printIndent();
849         fOut.print("notationDecl(");
850         fOut.print("name=");
851         printQuotedString(name);
852         fOut.print(',');
853         fOut.print("publicId=");
854         printQuotedString(identifier.getPublicId());
855         fOut.print(',');
856         fOut.print("systemId=");
857         printQuotedString(identifier.getLiteralSystemId());
858         fOut.print(',');
859         fOut.print("baseSystemId=");
860         printQuotedString(identifier.getBaseSystemId());
861         if (augs != null) {
862             fOut.print(',');
863             printAugmentations(augs);
864         }
865         fOut.println(')');
866         fOut.flush();
867
868     } // notationDecl(String,String,String)
869

870     /** Start conditional section. */
871     public void startConditional(short type, Augmentations augs)
872         throws XNIException {
873
874         printIndent();
875         fOut.print("startConditional(");
876         fOut.print("type=");
877         switch (type) {
878             case XMLDTDHandler.CONDITIONAL_IGNORE: {
879                 fOut.print("CONDITIONAL_IGNORE");
880                 break;
881             }
882             case XMLDTDHandler.CONDITIONAL_INCLUDE: {
883                 fOut.print("CONDITIONAL_INCLUDE");
884                 break;
885             }
886             default: {
887                 fOut.print("??? ("+type+')');
888             }
889         }
890         if (augs != null) {
891             fOut.print(',');
892             printAugmentations(augs);
893         }
894         fOut.println(')');
895         fOut.flush();
896         fIndent++;
897
898     } // startConditional(short)
899

900     /** End conditional section. */
901     public void endConditional(Augmentations augs) throws XNIException {
902
903         fIndent--;
904         printIndent();
905         fOut.print("endConditional(");
906         if (augs != null) {
907             fOut.print(',');
908             printAugmentations(augs);
909         }
910         fOut.println(')');
911         fOut.flush();
912
913     } // endConditional()
914

915     /** End DTD. */
916     public void endDTD(Augmentations augs) throws XNIException {
917
918         fIndent--;
919         printIndent();
920         fOut.print("endDTD(");
921         if (augs != null) {
922             fOut.print(',');
923             printAugmentations(augs);
924         }
925         fOut.println(')');
926         fOut.flush();
927
928     } // endDTD()
929

930     //
931
// XMLDTDContentModelHandler methods
932
//
933

934     /** Start content model. */
935     public void startContentModel(String JavaDoc elementName, Augmentations augs)
936         throws XNIException {
937
938         printIndent();
939         fOut.print("startContentModel(");
940         fOut.print("elementName=");
941         printQuotedString(elementName);
942         if (augs != null) {
943             fOut.print(',');
944             printAugmentations(augs);
945         }
946         fOut.println(')');
947         fOut.flush();
948         fIndent++;
949
950     } // startContentModel(String)
951

952     /** Any. */
953     public void any(Augmentations augs) throws XNIException {
954
955         printIndent();
956         fOut.print("any(");
957         if (augs != null) {
958             fOut.print(',');
959             printAugmentations(augs);
960         }
961         fOut.println(')');
962         fOut.flush();
963
964     } // any()
965

966     /** Empty. */
967     public void empty(Augmentations augs) throws XNIException {
968
969         printIndent();
970         fOut.print("empty(");
971         if (augs != null) {
972             fOut.print(',');
973             printAugmentations(augs);
974         }
975         fOut.println(')');
976         fOut.flush();
977
978     } // empty()
979

980     /** Start group. */
981     public void startGroup(Augmentations augs) throws XNIException {
982
983         printIndent();
984         fOut.print("startGroup(");
985         if (augs != null) {
986             fOut.print(',');
987             printAugmentations(augs);
988         }
989         fOut.println(')');
990         fOut.flush();
991         fIndent++;
992
993     } // childrenStartGroup()
994

995     /** #PCDATA. */
996     public void pcdata(Augmentations augs) throws XNIException {
997
998         printIndent();
999         fOut.print("pcdata(");
1000        if (augs != null) {
1001            fOut.print(',');
1002            printAugmentations(augs);
1003        }
1004        fOut.println(')');
1005        fOut.flush();
1006
1007    } // pcdata()
1008

1009    /** Element. */
1010    public void element(String JavaDoc elementName, Augmentations augs)
1011        throws XNIException {
1012
1013        printIndent();
1014        fOut.print("element(");
1015        fOut.print("elementName=");
1016        printQuotedString(elementName);
1017        if (augs != null) {
1018            fOut.print(',');
1019            printAugmentations(augs);
1020        }
1021        fOut.println(')');
1022        fOut.flush();
1023
1024    } // element(String)
1025

1026    /** separator. */
1027    public void separator(short separator, Augmentations augs)
1028        throws XNIException {
1029
1030        printIndent();
1031        fOut.print("separator(");
1032        fOut.print("separator=");
1033        switch (separator) {
1034            case XMLDTDContentModelHandler.SEPARATOR_CHOICE: {
1035                fOut.print("SEPARATOR_CHOICE");
1036                break;
1037            }
1038            case XMLDTDContentModelHandler.SEPARATOR_SEQUENCE: {
1039                fOut.print("SEPARATOR_SEQUENCE");
1040                break;
1041            }
1042            default: {
1043                fOut.print("??? ("+separator+')');
1044            }
1045        }
1046        if (augs != null) {
1047            fOut.print(',');
1048            printAugmentations(augs);
1049        }
1050        fOut.println(')');
1051        fOut.flush();
1052
1053    } // separator(short)
1054

1055    /** Occurrence. */
1056    public void occurrence(short occurrence, Augmentations augs)
1057        throws XNIException {
1058
1059        printIndent();
1060        fOut.print("occurrence(");
1061        fOut.print("occurrence=");
1062        switch (occurrence) {
1063            case XMLDTDContentModelHandler.OCCURS_ONE_OR_MORE: {
1064                fOut.print("OCCURS_ONE_OR_MORE");
1065                break;
1066            }
1067            case XMLDTDContentModelHandler.OCCURS_ZERO_OR_MORE: {
1068                fOut.print("OCCURS_ZERO_OR_MORE");
1069                break;
1070            }
1071            case XMLDTDContentModelHandler.OCCURS_ZERO_OR_ONE: {
1072                fOut.print("OCCURS_ZERO_OR_ONE");
1073                break;
1074            }
1075            default: {
1076                fOut.print("??? ("+occurrence+')');
1077            }
1078        }
1079        if (augs != null) {
1080            fOut.print(',');
1081            printAugmentations(augs);
1082        }
1083        fOut.println(')');
1084        fOut.flush();
1085
1086    } // occurrence(short)
1087

1088    /** End group. */
1089    public void endGroup(Augmentations augs) throws XNIException {
1090
1091        fIndent--;
1092        printIndent();
1093        fOut.print("endGroup(");
1094        if (augs != null) {
1095            fOut.print(',');
1096            printAugmentations(augs);
1097        }
1098        fOut.println(')');
1099        fOut.flush();
1100
1101    } // childrenEndGroup()
1102

1103    /** End content model. */
1104    public void endContentModel(Augmentations augs) throws XNIException {
1105
1106        fIndent--;
1107        printIndent();
1108        fOut.print("endContentModel(");
1109        if (augs != null) {
1110            fOut.print(',');
1111            printAugmentations(augs);
1112        }
1113        fOut.println(')');
1114        fOut.flush();
1115
1116    } // endContentModel()
1117

1118    //
1119
// XMLErrorHandler methods
1120
//
1121

1122    /** Warning. */
1123    public void warning(String JavaDoc domain, String JavaDoc key, XMLParseException ex)
1124        throws XNIException {
1125        printError("Warning", ex);
1126    } // warning(String,String,XMLParseException)
1127

1128    /** Error. */
1129    public void error(String JavaDoc domain, String JavaDoc key, XMLParseException ex)
1130        throws XNIException {
1131        printError("Error", ex);
1132    } // error(String,String,XMLParseException)
1133

1134    /** Fatal error. */
1135    public void fatalError(String JavaDoc domain, String JavaDoc key, XMLParseException ex)
1136        throws XNIException {
1137        printError("Fatal Error", ex);
1138        throw ex;
1139    } // fatalError(String,String,XMLParseException)
1140

1141    //
1142
// Protected methods
1143
//
1144
protected void printInScopeNamespaces(){
1145        int count = fNamespaceContext.getDeclaredPrefixCount();
1146        if (count>0){
1147            for (int i = 0; i < count; i++) {
1148                printIndent();
1149                fOut.print("declaredPrefix(");
1150                fOut.print("prefix=");
1151                String JavaDoc prefix = fNamespaceContext.getDeclaredPrefixAt(i);
1152                printQuotedString(prefix);
1153                fOut.print(',');
1154                fOut.print("uri=");
1155                printQuotedString(fNamespaceContext.getURI(prefix));
1156                fOut.println(')');
1157                fOut.flush();
1158            }
1159        }
1160   }
1161
1162   protected void printEndNamespaceMapping(){
1163        int count = fNamespaceContext.getDeclaredPrefixCount();
1164        if (count > 0) {
1165            for (int i = 0; i < count; i++) {
1166                printIndent();
1167                fOut.print("endPrefix(");
1168                fOut.print("prefix=");
1169                String JavaDoc prefix = fNamespaceContext.getDeclaredPrefixAt(i);
1170                printQuotedString(prefix);
1171                fOut.println(')');
1172                fOut.flush();
1173            }
1174        }
1175
1176   }
1177    /** Prints an element. */
1178    protected void printElement(QName element, XMLAttributes attributes) {
1179
1180        fOut.print("element=");
1181        fOut.print('{');
1182        fOut.print("prefix=");
1183        printQuotedString(element.prefix);
1184        fOut.print(',');
1185        fOut.print("localpart=");
1186        printQuotedString(element.localpart);
1187        fOut.print(',');
1188        fOut.print("rawname=");
1189        printQuotedString(element.rawname);
1190        fOut.print(',');
1191        fOut.print("uri=");
1192        printQuotedString(element.uri);
1193        fOut.print('}');
1194        fOut.print(',');
1195        fOut.print("attributes=");
1196        if (attributes == null) {
1197            fOut.println("null");
1198        }
1199        else {
1200            fOut.print('{');
1201            int length = attributes.getLength();
1202            for (int i = 0; i < length; i++) {
1203                if (i > 0) {
1204                    fOut.print(',');
1205                }
1206                attributes.getName(i, fQName);
1207                String JavaDoc attrType = attributes.getType(i);
1208                String JavaDoc attrValue = attributes.getValue(i);
1209                String JavaDoc attrNonNormalizedValue = attributes.getNonNormalizedValue(i);
1210                Augmentations augs = attributes.getAugmentations(i);
1211                fOut.print("name=");
1212                fOut.print('{');
1213                fOut.print("prefix=");
1214                printQuotedString(fQName.prefix);
1215                fOut.print(',');
1216                fOut.print("localpart=");
1217                printQuotedString(fQName.localpart);
1218                fOut.print(',');
1219                fOut.print("rawname=");
1220                printQuotedString(fQName.rawname);
1221                fOut.print(',');
1222                fOut.print("uri=");
1223                printQuotedString(fQName.uri);
1224                fOut.print('}');
1225                fOut.print(',');
1226                fOut.print("type=");
1227                printQuotedString(attrType);
1228                fOut.print(',');
1229                fOut.print("value=");
1230                printQuotedString(attrValue);
1231                fOut.print(',');
1232                fOut.print("nonNormalizedValue=");
1233                printQuotedString(attrNonNormalizedValue);
1234                if (attributes.isSpecified(i) == false ) {
1235                   fOut.print("(default)");
1236                }
1237                if (augs != null) {
1238                    fOut.print(',');
1239                    printAugmentations(augs);
1240                }
1241                fOut.print('}');
1242            }
1243            fOut.print('}');
1244        }
1245
1246    } // printElement(QName,XMLAttributes)
1247

1248    /** Prints augmentations. */
1249    protected void printAugmentations(Augmentations augs) {
1250        fOut.print("augs={");
1251        java.util.Enumeration JavaDoc keys = augs.keys();
1252        while (keys.hasMoreElements()) {
1253            String JavaDoc key = (String JavaDoc)keys.nextElement();
1254            Object JavaDoc value = augs.getItem(key);
1255            fOut.print(key);
1256            fOut.print('#');
1257            fOut.print(String.valueOf(value));
1258        }
1259        fOut.print('}');
1260    } // printAugmentations(Augmentations)
1261

1262    /** Print quoted string. */
1263    protected void printQuotedString(String JavaDoc s) {
1264
1265        if (s == null) {
1266            fOut.print("null");
1267            return;
1268        }
1269
1270        fOut.print('"');
1271        int length = s.length();
1272        for (int i = 0; i < length; i++) {
1273            char c = s.charAt(i);
1274            normalizeAndPrint(c);
1275        }
1276        fOut.print('"');
1277
1278    } // printQuotedString(String)
1279

1280    /** Print quoted string. */
1281    protected void printQuotedString(char[] ch, int offset, int length) {
1282
1283        fOut.print('"');
1284        for (int i = 0; i < length; i++) {
1285            normalizeAndPrint(ch[offset + i]);
1286        }
1287        fOut.print('"');
1288
1289    } // printQuotedString(char[],int,int)
1290

1291    /** Normalize and print. */
1292    protected void normalizeAndPrint(char c) {
1293
1294        switch (c) {
1295            case '\n': {
1296                fOut.print("\\n");
1297                break;
1298            }
1299            case '\r': {
1300                fOut.print("\\r");
1301                break;
1302            }
1303            case '\t': {
1304                fOut.print("\\t");
1305                break;
1306            }
1307            case '\\': {
1308                fOut.print("\\\\");
1309                break;
1310            }
1311            case '"': {
1312                fOut.print("\\\"");
1313                break;
1314            }
1315            default: {
1316                fOut.print(c);
1317            }
1318        }
1319
1320    } // normalizeAndPrint(char)
1321

1322    /** Prints the error message. */
1323    protected void printError(String JavaDoc type, XMLParseException ex) {
1324
1325        System.err.print("[");
1326        System.err.print(type);
1327        System.err.print("] ");
1328        String JavaDoc systemId = ex.getExpandedSystemId();
1329        if (systemId != null) {
1330            int index = systemId.lastIndexOf('/');
1331            if (index != -1)
1332                systemId = systemId.substring(index + 1);
1333            System.err.print(systemId);
1334        }
1335        System.err.print(':');
1336        System.err.print(ex.getLineNumber());
1337        System.err.print(':');
1338        System.err.print(ex.getColumnNumber());
1339        System.err.print(": ");
1340        System.err.print(ex.getMessage());
1341        System.err.println();
1342        System.err.flush();
1343
1344    } // printError(String,XMLParseException)
1345

1346    /** Prints the indent. */
1347    protected void printIndent() {
1348
1349        for (int i = 0; i < fIndent; i++) {
1350            fOut.print(' ');
1351        }
1352
1353    } // printIndent()
1354

1355    //
1356
// MAIN
1357
//
1358

1359    /** Main. */
1360    public static void main(String JavaDoc[] argv) throws Exception JavaDoc {
1361
1362        // is there anything to do?
1363
if (argv.length == 0) {
1364            printUsage();
1365            System.exit(1);
1366        }
1367
1368        // variables
1369
XMLDocumentParser parser = null;
1370        XMLParserConfiguration parserConfig = null;
1371        boolean namespaces = DEFAULT_NAMESPACES;
1372        boolean validation = DEFAULT_VALIDATION;
1373        boolean schemaValidation = DEFAULT_SCHEMA_VALIDATION;
1374        boolean schemaFullChecking = DEFAULT_SCHEMA_FULL_CHECKING;
1375        boolean notifyCharRefs = DEFAULT_NOTIFY_CHAR_REFS;
1376
1377        // process arguments
1378
for (int i = 0; i < argv.length; i++) {
1379            String JavaDoc arg = argv[i];
1380            if (arg.startsWith("-")) {
1381                String JavaDoc option = arg.substring(1);
1382                if (option.equals("p")) {
1383                    // get parser name
1384
if (++i == argv.length) {
1385                        System.err.println("error: Missing argument to -p option.");
1386                        continue;
1387                    }
1388                    String JavaDoc parserName = argv[i];
1389
1390                    // create parser
1391
try {
1392                        parserConfig = (XMLParserConfiguration)ObjectFactory.newInstance(parserName,
1393                            ObjectFactory.findClassLoader(), true);
1394                        parser = null;
1395                    }
1396                    catch (Exception JavaDoc e) {
1397                        parserConfig = null;
1398                        System.err.println("error: Unable to instantiate parser configuration ("+parserName+")");
1399                    }
1400                    continue;
1401                }
1402                if (option.equalsIgnoreCase("n")) {
1403                    namespaces = option.equals("n");
1404                    continue;
1405                }
1406                if (option.equalsIgnoreCase("v")) {
1407                    validation = option.equals("v");
1408                    continue;
1409                }
1410                if (option.equalsIgnoreCase("s")) {
1411                    schemaValidation = option.equals("s");
1412                    continue;
1413                }
1414                if (option.equalsIgnoreCase("f")) {
1415                    schemaFullChecking = option.equals("f");
1416                    continue;
1417                }
1418                if (option.equalsIgnoreCase("c")) {
1419                    notifyCharRefs = option.equals("c");
1420                    continue;
1421                }
1422                if (option.equals("h")) {
1423                    printUsage();
1424                    continue;
1425                }
1426            }
1427
1428            // use default parser?
1429
if (parserConfig == null) {
1430
1431                // create parser
1432
try {
1433                    parserConfig = (XMLParserConfiguration)ObjectFactory.newInstance(DEFAULT_PARSER_CONFIG,
1434                        ObjectFactory.findClassLoader(), true);
1435                }
1436                catch (Exception JavaDoc e) {
1437                    System.err.println("error: Unable to instantiate parser configuration ("+DEFAULT_PARSER_CONFIG+")");
1438                    continue;
1439                }
1440            }
1441
1442            // set parser features
1443
if (parser == null) {
1444                parser = new DocumentTracer(parserConfig);
1445            }
1446            try {
1447                parserConfig.setFeature(NAMESPACES_FEATURE_ID, namespaces);
1448            }
1449            catch (XMLConfigurationException e) {
1450                System.err.println("warning: Parser does not support feature ("+NAMESPACES_FEATURE_ID+")");
1451            }
1452            try {
1453                parserConfig.setFeature(VALIDATION_FEATURE_ID, validation);
1454            }
1455            catch (XMLConfigurationException e) {
1456                System.err.println("warning: Parser does not support feature ("+VALIDATION_FEATURE_ID+")");
1457            }
1458            try {
1459                parserConfig.setFeature(SCHEMA_VALIDATION_FEATURE_ID, schemaValidation);
1460            }
1461            catch (XMLConfigurationException e) {
1462                if (e.getType() == XMLConfigurationException.NOT_SUPPORTED) {
1463                    System.err.println("warning: Parser does not support feature ("+SCHEMA_VALIDATION_FEATURE_ID+")");
1464                }
1465            }
1466            try {
1467                parserConfig.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking);
1468            }
1469            catch (XMLConfigurationException e) {
1470                if (e.getType() == XMLConfigurationException.NOT_SUPPORTED) {
1471                    System.err.println("warning: Parser does not support feature ("+SCHEMA_FULL_CHECKING_FEATURE_ID+")");
1472                }
1473            }
1474            try {
1475                parserConfig.setFeature(NOTIFY_CHAR_REFS_FEATURE_ID, notifyCharRefs);
1476            }
1477            catch (XMLConfigurationException e) {
1478                if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) {
1479                    //e.printStackTrace();
1480
System.err.println("warning: Parser does not recognize feature ("+NOTIFY_CHAR_REFS_FEATURE_ID+")");
1481                }
1482                else {
1483                    System.err.println("warning: Parser does not support feature ("+NOTIFY_CHAR_REFS_FEATURE_ID+")");
1484                }
1485            }
1486
1487            // parse file
1488
try {
1489                parser.parse(new XMLInputSource(null, arg, null));
1490            }
1491            catch (XMLParseException e) {
1492                // ignore
1493
}
1494            catch (Exception JavaDoc e) {
1495                System.err.println("error: Parse error occurred - "+e.getMessage());
1496                if (e instanceof XNIException) {
1497                    e = ((XNIException)e).getException();
1498                }
1499                e.printStackTrace(System.err);
1500            }
1501        }
1502
1503    } // main(String[])
1504

1505    //
1506
// Private static methods
1507
//
1508

1509    /** Prints the usage. */
1510    private static void printUsage() {
1511
1512        System.err.println("usage: java xni.DocumentTracer (options) uri ...");
1513        System.err.println();
1514
1515        System.err.println("options:");
1516        System.out.println(" -p name Specify parser configuration by name.");
1517        System.err.println(" -n | -N Turn on/off namespace processing.");
1518        System.err.println(" -v | -V Turn on/off validation.");
1519        System.err.println(" -s | -S Turn on/off Schema validation support.");
1520        System.err.println(" NOTE: Not supported by all parser configurations.");
1521        System.err.println(" -f | -F Turn on/off Schema full checking.");
1522        System.err.println(" NOTE: Requires use of -s and not supported by all parsers.");
1523        System.err.println(" -c | -C Turn on/off character notifications");
1524        System.err.println(" -h This help screen.");
1525        System.err.println();
1526
1527        System.err.println("defaults:");
1528        System.out.println(" Config: "+DEFAULT_PARSER_CONFIG);
1529        System.out.print(" Namespaces: ");
1530        System.err.println(DEFAULT_NAMESPACES ? "on" : "off");
1531        System.out.print(" Validation: ");
1532        System.err.println(DEFAULT_VALIDATION ? "on" : "off");
1533        System.out.print(" Schema: ");
1534        System.err.println(DEFAULT_SCHEMA_VALIDATION ? "on" : "off");
1535        System.err.print(" Schema full checking: ");
1536        System.err.println(DEFAULT_SCHEMA_FULL_CHECKING ? "on" : "off");
1537        System.out.print(" Char refs: ");
1538        System.err.println(DEFAULT_NOTIFY_CHAR_REFS ? "on" : "off" );
1539
1540    } // printUsage()
1541

1542} // class DocumentTracer
1543
Popular Tags