KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dom > Writer


1 /*
2  * Copyright 1999-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 dom;
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.lang.reflect.Method JavaDoc;
24
25 import org.w3c.dom.Attr JavaDoc;
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.DocumentType JavaDoc;
28 import org.w3c.dom.NamedNodeMap JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31 import org.xml.sax.SAXParseException JavaDoc;
32
33 /**
34  * A sample DOM writer. This sample program illustrates how to
35  * traverse a DOM tree in order to print a document that is parsed.
36  *
37  * @author Andy Clark, IBM
38  *
39  * @version $Id: Writer.java,v 1.16 2005/05/09 00:38:01 mrglavas Exp $
40  */

41 public class Writer {
42
43     //
44
// Constants
45
//
46

47     // feature ids
48

49     /** Namespaces feature id (http://xml.org/sax/features/namespaces). */
50     protected static final String JavaDoc NAMESPACES_FEATURE_ID = "http://xml.org/sax/features/namespaces";
51
52     /** Validation feature id (http://xml.org/sax/features/validation). */
53     protected static final String JavaDoc VALIDATION_FEATURE_ID = "http://xml.org/sax/features/validation";
54
55     /** Schema validation feature id (http://apache.org/xml/features/validation/schema). */
56     protected static final String JavaDoc SCHEMA_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/schema";
57
58     /** Schema full checking feature id (http://apache.org/xml/features/validation/schema-full-checking). */
59     protected static final String JavaDoc SCHEMA_FULL_CHECKING_FEATURE_ID = "http://apache.org/xml/features/validation/schema-full-checking";
60     
61     /** Validate schema annotations feature id (http://apache.org/xml/features/validate-annotations). */
62     protected static final String JavaDoc VALIDATE_ANNOTATIONS_ID = "http://apache.org/xml/features/validate-annotations";
63     
64     /** Generate synthetic schema annotations feature id (http://apache.org/xml/features/generate-synthetic-annotations). */
65     protected static final String JavaDoc GENERATE_SYNTHETIC_ANNOTATIONS_ID = "http://apache.org/xml/features/generate-synthetic-annotations";
66     
67     /** Dynamic validation feature id (http://apache.org/xml/features/validation/dynamic). */
68     protected static final String JavaDoc DYNAMIC_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/dynamic";
69     
70     /** Load external DTD feature id (http://apache.org/xml/features/nonvalidating/load-external-dtd). */
71     protected static final String JavaDoc LOAD_EXTERNAL_DTD_FEATURE_ID = "http://apache.org/xml/features/nonvalidating/load-external-dtd";
72     
73     /** XInclude feature id (http://apache.org/xml/features/xinclude). */
74     protected static final String JavaDoc XINCLUDE_FEATURE_ID = "http://apache.org/xml/features/xinclude";
75     
76     /** XInclude fixup base URIs feature id (http://apache.org/xml/features/xinclude/fixup-base-uris). */
77     protected static final String JavaDoc XINCLUDE_FIXUP_BASE_URIS_FEATURE_ID = "http://apache.org/xml/features/xinclude/fixup-base-uris";
78     
79     /** XInclude fixup language feature id (http://apache.org/xml/features/xinclude/fixup-language). */
80     protected static final String JavaDoc XINCLUDE_FIXUP_LANGUAGE_FEATURE_ID = "http://apache.org/xml/features/xinclude/fixup-language";
81
82     // default settings
83

84     /** Default parser name. */
85     protected static final String JavaDoc DEFAULT_PARSER_NAME = "dom.wrappers.Xerces";
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 load external DTD (true). */
94     protected static final boolean DEFAULT_LOAD_EXTERNAL_DTD = true;
95
96     /** Default Schema validation support (false). */
97     protected static final boolean DEFAULT_SCHEMA_VALIDATION = false;
98
99     /** Default Schema full checking support (false). */
100     protected static final boolean DEFAULT_SCHEMA_FULL_CHECKING = false;
101     
102     /** Default validate schema annotations (false). */
103     protected static final boolean DEFAULT_VALIDATE_ANNOTATIONS = false;
104     
105     /** Default generate synthetic schema annotations (false). */
106     protected static final boolean DEFAULT_GENERATE_SYNTHETIC_ANNOTATIONS = false;
107     
108     /** Default dynamic validation support (false). */
109     protected static final boolean DEFAULT_DYNAMIC_VALIDATION = false;
110     
111     /** Default XInclude processing support (false). */
112     protected static final boolean DEFAULT_XINCLUDE = false;
113     
114     /** Default XInclude fixup base URIs support (true). */
115     protected static final boolean DEFAULT_XINCLUDE_FIXUP_BASE_URIS = true;
116     
117     /** Default XInclude fixup language support (true). */
118     protected static final boolean DEFAULT_XINCLUDE_FIXUP_LANGUAGE = true;
119
120     /** Default canonical output (false). */
121     protected static final boolean DEFAULT_CANONICAL = false;
122
123     //
124
// Data
125
//
126

127     /** Print writer. */
128     protected PrintWriter JavaDoc fOut;
129
130     /** Canonical output. */
131     protected boolean fCanonical;
132     
133     /** Processing XML 1.1 document. */
134     protected boolean fXML11;
135
136     //
137
// Constructors
138
//
139

140     /** Default constructor. */
141     public Writer() {
142     } // <init>()
143

144     public Writer(boolean canonical) {
145         fCanonical = canonical;
146     } // <init>(boolean)
147

148     //
149
// Public methods
150
//
151

152     /** Sets whether output is canonical. */
153     public void setCanonical(boolean canonical) {
154         fCanonical = canonical;
155     } // setCanonical(boolean)
156

157     /** Sets the output stream for printing. */
158     public void setOutput(OutputStream JavaDoc stream, String JavaDoc encoding)
159         throws UnsupportedEncodingException JavaDoc {
160
161         if (encoding == null) {
162             encoding = "UTF8";
163         }
164
165         java.io.Writer JavaDoc writer = new OutputStreamWriter JavaDoc(stream, encoding);
166         fOut = new PrintWriter JavaDoc(writer);
167
168     } // setOutput(OutputStream,String)
169

170     /** Sets the output writer. */
171     public void setOutput(java.io.Writer JavaDoc writer) {
172
173         fOut = writer instanceof PrintWriter JavaDoc
174              ? (PrintWriter JavaDoc)writer : new PrintWriter JavaDoc(writer);
175
176     } // setOutput(java.io.Writer)
177

178     /** Writes the specified node, recursively. */
179     public void write(Node JavaDoc node) {
180
181         // is there anything to do?
182
if (node == null) {
183             return;
184         }
185
186         short type = node.getNodeType();
187         switch (type) {
188             case Node.DOCUMENT_NODE: {
189                 Document JavaDoc document = (Document JavaDoc)node;
190                 fXML11 = "1.1".equals(getVersion(document));
191                 if (!fCanonical) {
192                     if (fXML11) {
193                         fOut.println("<?xml version=\"1.1\" encoding=\"UTF-8\"?>");
194                     }
195                     else {
196                         fOut.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
197                     }
198                     fOut.flush();
199                     write(document.getDoctype());
200                 }
201                 write(document.getDocumentElement());
202                 break;
203             }
204
205             case Node.DOCUMENT_TYPE_NODE: {
206                 DocumentType JavaDoc doctype = (DocumentType JavaDoc)node;
207                 fOut.print("<!DOCTYPE ");
208                 fOut.print(doctype.getName());
209                 String JavaDoc publicId = doctype.getPublicId();
210                 String JavaDoc systemId = doctype.getSystemId();
211                 if (publicId != null) {
212                     fOut.print(" PUBLIC '");
213                     fOut.print(publicId);
214                     fOut.print("' '");
215                     fOut.print(systemId);
216                     fOut.print('\'');
217                 }
218                 else if (systemId != null) {
219                     fOut.print(" SYSTEM '");
220                     fOut.print(systemId);
221                     fOut.print('\'');
222                 }
223                 String JavaDoc internalSubset = doctype.getInternalSubset();
224                 if (internalSubset != null) {
225                     fOut.println(" [");
226                     fOut.print(internalSubset);
227                     fOut.print(']');
228                 }
229                 fOut.println('>');
230                 break;
231             }
232
233             case Node.ELEMENT_NODE: {
234                 fOut.print('<');
235                 fOut.print(node.getNodeName());
236                 Attr JavaDoc attrs[] = sortAttributes(node.getAttributes());
237                 for (int i = 0; i < attrs.length; i++) {
238                     Attr JavaDoc attr = attrs[i];
239                     fOut.print(' ');
240                     fOut.print(attr.getNodeName());
241                     fOut.print("=\"");
242                     normalizeAndPrint(attr.getNodeValue(), true);
243                     fOut.print('"');
244                 }
245                 fOut.print('>');
246                 fOut.flush();
247
248                 Node JavaDoc child = node.getFirstChild();
249                 while (child != null) {
250                     write(child);
251                     child = child.getNextSibling();
252                 }
253                 break;
254             }
255
256             case Node.ENTITY_REFERENCE_NODE: {
257                 if (fCanonical) {
258                     Node JavaDoc child = node.getFirstChild();
259                     while (child != null) {
260                         write(child);
261                         child = child.getNextSibling();
262                     }
263                 }
264                 else {
265                     fOut.print('&');
266                     fOut.print(node.getNodeName());
267                     fOut.print(';');
268                     fOut.flush();
269                 }
270                 break;
271             }
272
273             case Node.CDATA_SECTION_NODE: {
274                 if (fCanonical) {
275                     normalizeAndPrint(node.getNodeValue(), false);
276                 }
277                 else {
278                     fOut.print("<![CDATA[");
279                     fOut.print(node.getNodeValue());
280                     fOut.print("]]>");
281                 }
282                 fOut.flush();
283                 break;
284             }
285
286             case Node.TEXT_NODE: {
287                 normalizeAndPrint(node.getNodeValue(), false);
288                 fOut.flush();
289                 break;
290             }
291
292             case Node.PROCESSING_INSTRUCTION_NODE: {
293                 fOut.print("<?");
294                 fOut.print(node.getNodeName());
295                 String JavaDoc data = node.getNodeValue();
296                 if (data != null && data.length() > 0) {
297                     fOut.print(' ');
298                     fOut.print(data);
299                 }
300                 fOut.print("?>");
301                 fOut.flush();
302                 break;
303             }
304             
305             case Node.COMMENT_NODE: {
306                 if (!fCanonical) {
307                     fOut.print("<!--");
308                     String JavaDoc comment = node.getNodeValue();
309                     if (comment != null && comment.length() > 0) {
310                         fOut.print(comment);
311                     }
312                     fOut.print("-->");
313                     fOut.flush();
314                 }
315             }
316         }
317
318         if (type == Node.ELEMENT_NODE) {
319             fOut.print("</");
320             fOut.print(node.getNodeName());
321             fOut.print('>');
322             fOut.flush();
323         }
324
325     } // write(Node)
326

327     /** Returns a sorted list of attributes. */
328     protected Attr JavaDoc[] sortAttributes(NamedNodeMap JavaDoc attrs) {
329
330         int len = (attrs != null) ? attrs.getLength() : 0;
331         Attr JavaDoc array[] = new Attr JavaDoc[len];
332         for (int i = 0; i < len; i++) {
333             array[i] = (Attr JavaDoc)attrs.item(i);
334         }
335         for (int i = 0; i < len - 1; i++) {
336             String JavaDoc name = array[i].getNodeName();
337             int index = i;
338             for (int j = i + 1; j < len; j++) {
339                 String JavaDoc curName = array[j].getNodeName();
340                 if (curName.compareTo(name) < 0) {
341                     name = curName;
342                     index = j;
343                 }
344             }
345             if (index != i) {
346                 Attr JavaDoc temp = array[i];
347                 array[i] = array[index];
348                 array[index] = temp;
349             }
350         }
351
352         return array;
353
354     } // sortAttributes(NamedNodeMap):Attr[]
355

356     //
357
// Protected methods
358
//
359

360     /** Normalizes and prints the given string. */
361     protected void normalizeAndPrint(String JavaDoc s, boolean isAttValue) {
362
363         int len = (s != null) ? s.length() : 0;
364         for (int i = 0; i < len; i++) {
365             char c = s.charAt(i);
366             normalizeAndPrint(c, isAttValue);
367         }
368
369     } // normalizeAndPrint(String,boolean)
370

371     /** Normalizes and print the given character. */
372     protected void normalizeAndPrint(char c, boolean isAttValue) {
373
374         switch (c) {
375             case '<': {
376                 fOut.print("&lt;");
377                 break;
378             }
379             case '>': {
380                 fOut.print("&gt;");
381                 break;
382             }
383             case '&': {
384                 fOut.print("&amp;");
385                 break;
386             }
387             case '"': {
388                 // A '"' that appears in character data
389
// does not need to be escaped.
390
if (isAttValue) {
391                     fOut.print("&quot;");
392                 }
393                 else {
394                     fOut.print("\"");
395                 }
396                 break;
397             }
398             case '\r': {
399                 // If CR is part of the document's content, it
400
// must not be printed as a literal otherwise
401
// it would be normalized to LF when the document
402
// is reparsed.
403
fOut.print("&#xD;");
404                 break;
405             }
406             case '\n': {
407                 if (fCanonical) {
408                     fOut.print("&#xA;");
409                     break;
410                 }
411                 // else, default print char
412
}
413             default: {
414                 // In XML 1.1, control chars in the ranges [#x1-#x1F, #x7F-#x9F] must be escaped.
415
//
416
// Escape space characters that would be normalized to #x20 in attribute values
417
// when the document is reparsed.
418
//
419
// Escape NEL (0x85) and LSEP (0x2028) that appear in content
420
// if the document is XML 1.1, since they would be normalized to LF
421
// when the document is reparsed.
422
if (fXML11 && ((c >= 0x01 && c <= 0x1F && c != 0x09 && c != 0x0A)
423                     || (c >= 0x7F && c <= 0x9F) || c == 0x2028)
424                     || isAttValue && (c == 0x09 || c == 0x0A)) {
425                     fOut.print("&#x");
426                     fOut.print(Integer.toHexString(c).toUpperCase());
427                     fOut.print(";");
428                 }
429                 else {
430                     fOut.print(c);
431                 }
432             }
433         }
434     } // normalizeAndPrint(char,boolean)
435

436     /** Extracts the XML version from the Document. */
437     protected String JavaDoc getVersion(Document JavaDoc document) {
438         if (document == null) {
439             return null;
440         }
441         String JavaDoc version = null;
442         Method JavaDoc getXMLVersion = null;
443         try {
444             getXMLVersion = document.getClass().getMethod("getXmlVersion", new Class JavaDoc[]{});
445             // If Document class implements DOM L3, this method will exist.
446
if (getXMLVersion != null) {
447                 version = (String JavaDoc) getXMLVersion.invoke(document, (Object JavaDoc[]) null);
448             }
449         }
450         catch (Exception JavaDoc e) {
451             // Either this locator object doesn't have
452
// this method, or we're on an old JDK.
453
}
454         return version;
455     } // getVersion(Document)
456

457     //
458
// Main
459
//
460

461     /** Main program entry point. */
462     public static void main(String JavaDoc argv[]) {
463
464         // is there anything to do?
465
if (argv.length == 0) {
466             printUsage();
467             System.exit(1);
468         }
469
470         // variables
471
Writer writer = null;
472         ParserWrapper parser = null;
473         boolean namespaces = DEFAULT_NAMESPACES;
474         boolean validation = DEFAULT_VALIDATION;
475         boolean externalDTD = DEFAULT_LOAD_EXTERNAL_DTD;
476         boolean schemaValidation = DEFAULT_SCHEMA_VALIDATION;
477         boolean schemaFullChecking = DEFAULT_SCHEMA_FULL_CHECKING;
478         boolean validateAnnotations = DEFAULT_VALIDATE_ANNOTATIONS;
479         boolean generateSyntheticAnnotations = DEFAULT_GENERATE_SYNTHETIC_ANNOTATIONS;
480         boolean dynamicValidation = DEFAULT_DYNAMIC_VALIDATION;
481         boolean xincludeProcessing = DEFAULT_XINCLUDE;
482         boolean xincludeFixupBaseURIs = DEFAULT_XINCLUDE_FIXUP_BASE_URIS;
483         boolean xincludeFixupLanguage = DEFAULT_XINCLUDE_FIXUP_LANGUAGE;
484         boolean canonical = DEFAULT_CANONICAL;
485
486         // process arguments
487
for (int i = 0; i < argv.length; i++) {
488             String JavaDoc arg = argv[i];
489             if (arg.startsWith("-")) {
490                 String JavaDoc option = arg.substring(1);
491                 if (option.equals("p")) {
492                     // get parser name
493
if (++i == argv.length) {
494                         System.err.println("error: Missing argument to -p option.");
495                     }
496                     String JavaDoc parserName = argv[i];
497
498                     // create parser
499
try {
500                         parser = (ParserWrapper)Class.forName(parserName).newInstance();
501                     }
502                     catch (Exception JavaDoc e) {
503                         parser = null;
504                         System.err.println("error: Unable to instantiate parser ("+parserName+")");
505                     }
506                     continue;
507                 }
508                 if (option.equalsIgnoreCase("n")) {
509                     namespaces = option.equals("n");
510                     continue;
511                 }
512                 if (option.equalsIgnoreCase("v")) {
513                     validation = option.equals("v");
514                     continue;
515                 }
516                 if (option.equalsIgnoreCase("xd")) {
517                     externalDTD = option.equals("xd");
518                     continue;
519                 }
520                 if (option.equalsIgnoreCase("s")) {
521                     schemaValidation = option.equals("s");
522                     continue;
523                 }
524                 if (option.equalsIgnoreCase("f")) {
525                     schemaFullChecking = option.equals("f");
526                     continue;
527                 }
528                 if (option.equalsIgnoreCase("va")) {
529                     validateAnnotations = option.equals("va");
530                     continue;
531                 }
532                 if (option.equalsIgnoreCase("ga")) {
533                     generateSyntheticAnnotations = option.equals("ga");
534                     continue;
535                 }
536                 if (option.equalsIgnoreCase("dv")) {
537                     dynamicValidation = option.equals("dv");
538                     continue;
539                 }
540                 if (option.equalsIgnoreCase("xi")) {
541                     xincludeProcessing = option.equals("xi");
542                     continue;
543                 }
544                 if (option.equalsIgnoreCase("xb")) {
545                     xincludeFixupBaseURIs = option.equals("xb");
546                     continue;
547                 }
548                 if (option.equalsIgnoreCase("xl")) {
549                     xincludeFixupLanguage = option.equals("xl");
550                     continue;
551                 }
552                 if (option.equalsIgnoreCase("c")) {
553                     canonical = option.equals("c");
554                     continue;
555                 }
556                 if (option.equals("h")) {
557                     printUsage();
558                     continue;
559                 }
560             }
561
562             // use default parser?
563
if (parser == null) {
564
565                 // create parser
566
try {
567                     parser = (ParserWrapper)Class.forName(DEFAULT_PARSER_NAME).newInstance();
568                 }
569                 catch (Exception JavaDoc e) {
570                     System.err.println("error: Unable to instantiate parser ("+DEFAULT_PARSER_NAME+")");
571                     continue;
572                 }
573             }
574
575             // set parser features
576
try {
577                 parser.setFeature(NAMESPACES_FEATURE_ID, namespaces);
578             }
579             catch (SAXException JavaDoc e) {
580                 System.err.println("warning: Parser does not support feature ("+NAMESPACES_FEATURE_ID+")");
581             }
582             try {
583                 parser.setFeature(VALIDATION_FEATURE_ID, validation);
584             }
585             catch (SAXException JavaDoc e) {
586                 System.err.println("warning: Parser does not support feature ("+VALIDATION_FEATURE_ID+")");
587             }
588             try {
589                 parser.setFeature(LOAD_EXTERNAL_DTD_FEATURE_ID, externalDTD);
590             }
591             catch (SAXException JavaDoc e) {
592                 System.err.println("warning: Parser does not support feature ("+LOAD_EXTERNAL_DTD_FEATURE_ID+")");
593             }
594             try {
595                 parser.setFeature(SCHEMA_VALIDATION_FEATURE_ID, schemaValidation);
596             }
597             catch (SAXException JavaDoc e) {
598                 System.err.println("warning: Parser does not support feature ("+SCHEMA_VALIDATION_FEATURE_ID+")");
599             }
600             try {
601                 parser.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking);
602             }
603             catch (SAXException JavaDoc e) {
604                 System.err.println("warning: Parser does not support feature ("+SCHEMA_FULL_CHECKING_FEATURE_ID+")");
605             }
606             try {
607                 parser.setFeature(VALIDATE_ANNOTATIONS_ID, validateAnnotations);
608             }
609             catch (SAXException JavaDoc e) {
610                 System.err.println("warning: Parser does not support feature ("+VALIDATE_ANNOTATIONS_ID+")");
611             }
612             try {
613                 parser.setFeature(GENERATE_SYNTHETIC_ANNOTATIONS_ID, generateSyntheticAnnotations);
614             }
615             catch (SAXException JavaDoc e) {
616                 System.err.println("warning: Parser does not support feature ("+GENERATE_SYNTHETIC_ANNOTATIONS_ID+")");
617             }
618             try {
619                 parser.setFeature(DYNAMIC_VALIDATION_FEATURE_ID, dynamicValidation);
620             }
621             catch (SAXException JavaDoc e) {
622                 System.err.println("warning: Parser does not support feature ("+DYNAMIC_VALIDATION_FEATURE_ID+")");
623             }
624             try {
625                 parser.setFeature(XINCLUDE_FEATURE_ID, xincludeProcessing);
626             }
627             catch (SAXException JavaDoc e) {
628                 System.err.println("warning: Parser does not support feature ("+XINCLUDE_FEATURE_ID+")");
629             }
630             try {
631                 parser.setFeature(XINCLUDE_FIXUP_BASE_URIS_FEATURE_ID, xincludeFixupBaseURIs);
632             }
633             catch (SAXException JavaDoc e) {
634                 System.err.println("warning: Parser does not support feature ("+XINCLUDE_FIXUP_BASE_URIS_FEATURE_ID+")");
635             }
636             try {
637                 parser.setFeature(XINCLUDE_FIXUP_LANGUAGE_FEATURE_ID, xincludeFixupLanguage);
638             }
639             catch (SAXException JavaDoc e) {
640                 System.err.println("warning: Parser does not support feature ("+XINCLUDE_FIXUP_LANGUAGE_FEATURE_ID+")");
641             }
642
643             // setup writer
644
if (writer == null) {
645                 writer = new Writer();
646                 try {
647                     writer.setOutput(System.out, "UTF8");
648                 }
649                 catch (UnsupportedEncodingException JavaDoc e) {
650                     System.err.println("error: Unable to set output. Exiting.");
651                     System.exit(1);
652                 }
653             }
654
655             // parse file
656
writer.setCanonical(canonical);
657             try {
658                 Document JavaDoc document = parser.parse(arg);
659                 writer.write(document);
660             }
661             catch (SAXParseException JavaDoc e) {
662                 // ignore
663
}
664             catch (Exception JavaDoc e) {
665                 System.err.println("error: Parse error occurred - "+e.getMessage());
666                 if (e instanceof SAXException JavaDoc) {
667                     Exception JavaDoc nested = ((SAXException JavaDoc)e).getException();
668                     if (nested != null) {
669                         e = nested;
670                     }
671                 }
672                 e.printStackTrace(System.err);
673             }
674         }
675
676     } // main(String[])
677

678     //
679
// Private static methods
680
//
681

682     /** Prints the usage. */
683     private static void printUsage() {
684
685         System.err.println("usage: java dom.Writer (options) uri ...");
686         System.err.println();
687
688         System.err.println("options:");
689         System.err.println(" -p name Select parser by name.");
690         System.err.println(" -n | -N Turn on/off namespace processing.");
691         System.err.println(" -v | -V Turn on/off validation.");
692         System.err.println(" -xd | -XD Turn on/off loading of external DTDs.");
693         System.err.println(" NOTE: Always on when -v in use and not supported by all parsers.");
694         System.err.println(" -s | -S Turn on/off Schema validation support.");
695         System.err.println(" NOTE: Not supported by all parsers.");
696         System.err.println(" -f | -F Turn on/off Schema full checking.");
697         System.err.println(" NOTE: Requires use of -s and not supported by all parsers.");
698         System.err.println(" -va | -VA Turn on/off validation of schema annotations.");
699         System.err.println(" NOTE: Requires use of -s and not supported by all parsers.");
700         System.err.println(" -ga | -GA Turn on/off generation of synthetic schema annotations.");
701         System.err.println(" NOTE: Requires use of -s and not supported by all parsers.");
702         System.err.println(" -dv | -DV Turn on/off dynamic validation.");
703         System.err.println(" NOTE: Not supported by all parsers.");
704         System.err.println(" -xi | -XI Turn on/off XInclude processing.");
705         System.err.println(" NOTE: Not supported by all parsers.");
706         System.err.println(" -xb | -XB Turn on/off base URI fixup during XInclude processing.");
707         System.err.println(" NOTE: Requires use of -xi and not supported by all parsers.");
708         System.err.println(" -xl | -XL Turn on/off language fixup during XInclude processing.");
709         System.err.println(" NOTE: Requires use of -xi and not supported by all parsers.");
710         System.err.println(" -c | -C Turn on/off Canonical XML output.");
711         System.err.println(" NOTE: This is not W3C canonical output.");
712         System.err.println(" -h This help screen.");
713         System.err.println();
714
715         System.err.println("defaults:");
716         System.err.println(" Parser: "+DEFAULT_PARSER_NAME);
717         System.err.print(" Namespaces: ");
718         System.err.println(DEFAULT_NAMESPACES ? "on" : "off");
719         System.err.print(" Validation: ");
720         System.err.println(DEFAULT_VALIDATION ? "on" : "off");
721         System.err.print(" Load External DTD: ");
722         System.err.println(DEFAULT_LOAD_EXTERNAL_DTD ? "on" : "off");
723         System.err.print(" Schema: ");
724         System.err.println(DEFAULT_SCHEMA_VALIDATION ? "on" : "off");
725         System.err.print(" Schema full checking: ");
726         System.err.println(DEFAULT_SCHEMA_FULL_CHECKING ? "on" : "off");
727         System.err.print(" Dynamic: ");
728         System.err.println(DEFAULT_DYNAMIC_VALIDATION ? "on" : "off");
729         System.err.print(" Canonical: ");
730         System.err.println(DEFAULT_CANONICAL ? "on" : "off");
731         System.err.print(" Validate Annotations: ");
732         System.err.println(DEFAULT_VALIDATE_ANNOTATIONS ? "on" : "off");
733         System.err.print(" Generate Synthetic Annotations: ");
734         System.err.println(DEFAULT_GENERATE_SYNTHETIC_ANNOTATIONS ? "on" : "off");
735         System.err.print(" XInclude: ");
736         System.err.println(DEFAULT_XINCLUDE ? "on" : "off");
737         System.err.print(" XInclude base URI fixup: ");
738         System.err.println(DEFAULT_XINCLUDE_FIXUP_BASE_URIS ? "on" : "off");
739         System.err.print(" XInclude language fixup: ");
740         System.err.println(DEFAULT_XINCLUDE_FIXUP_LANGUAGE ? "on" : "off");
741
742     } // printUsage()
743

744 } // class Writer
745
Popular Tags