KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > devtools > XMLParse


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: XMLParse.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.devtools;
25
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30
31 import org.enhydra.apache.xerces.parsers.DOMParser;
32 import org.enhydra.xml.dom.DOMInfo;
33 import org.enhydra.xml.io.XMLEntityResolver;
34 import org.w3c.dom.Document JavaDoc;
35 import org.xml.sax.ErrorHandler JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37 import org.xml.sax.SAXParseException JavaDoc;
38
39 /**
40  * Parse files with the Xerces parser and optionally display information.
41  * XMLParse [options] in.xml [output]
42  * -dump
43  * -ns
44  * -no-validation
45  * -cp-resolve
46  * -debug
47  * -grammar
48  */

49 public class XMLParse implements ErrorHandler JavaDoc {
50     /** Count of errors */
51     private int fErrorCnt = 0;
52
53     /** Arguments parsed from the command line */
54     private boolean fDump = false;
55     private boolean fNameSpaces = false;
56     private boolean fClassPathResolve = false;
57     private boolean fDebugFlag = false;
58     private boolean fGrammar = false;
59     private boolean fValidate = true;
60
61     /**
62      * Print an error message.
63      */

64     private void printError(String JavaDoc msg,
65                             SAXParseException JavaDoc ex) {
66         System.err.println(ex.getSystemId() + ":"
67                            + ex.getLineNumber() + ":"
68                            + ex.getColumnNumber() + ":"
69                            + msg + ":"
70                            + ex.toString());
71     }
72     
73     /**
74      * @see ErrorHandler.warning
75      */

76     public void warning(SAXParseException JavaDoc ex) {
77         printError("Warning", ex);
78     }
79     
80     /**
81      * @see ErrorHandler.error
82      */

83     public void error(SAXParseException JavaDoc ex) {
84         printError("Error", ex);
85         fErrorCnt++;
86     }
87     
88     /**
89      * @see ErrorHandler.fatalError
90      */

91     public void fatalError(SAXParseException JavaDoc ex) throws SAXException JavaDoc {
92         printError("Fatal error", ex);
93         throw ex;
94     }
95
96     /**
97      * DOM parser class. Allows control over features of Xerces parser.
98      */

99     class XercesDOMParser extends DOMParser {
100         /**
101          * Constructor.
102          */

103         public XercesDOMParser() throws SAXException JavaDoc {
104             setValidation(fValidate);
105             setNamespaces(fNameSpaces);
106             setDeferNodeExpansion(false);
107             if (fGrammar) {
108                 setFeature("http://apache.org/xml/features/domx/grammar-access",
109                            true);
110             }
111         }
112     }
113
114     /*
115      * Parse and XML object and dump information about it.
116      */

117     private Document JavaDoc parse(String JavaDoc xmlFile) throws IOException JavaDoc, SAXException JavaDoc {
118         XercesDOMParser parser = new XercesDOMParser();
119         parser.setErrorHandler(this);
120         if (fClassPathResolve) {
121             XMLEntityResolver resolver = new XMLEntityResolver();
122             resolver.setDefaultResolving();
123             parser.setEntityResolver(resolver);
124             if (fDebugFlag) {
125                 resolver.setDebugWriter(new PrintWriter JavaDoc(System.err, true));
126             }
127         }
128
129         parser.parse(xmlFile);
130         return parser.getDocument();
131     }
132
133     /**
134      * Parse and dump the document.
135      */

136     private void process(String JavaDoc xmlFile,
137                          OutputStream JavaDoc output) throws IOException JavaDoc, SAXException JavaDoc {
138         Document JavaDoc doc = parse(xmlFile);
139         if (fDump) {
140             DOMInfo.printTree(null, doc, output);
141         }
142     }
143
144     /*
145      * Parse arguments and execute request.
146      */

147     private void run(String JavaDoc[] args) throws Throwable JavaDoc {
148         int argIdx = 0;
149         while ((argIdx < args.length) && (args[argIdx].startsWith("-"))) {
150             if (args[argIdx].equals("-dump")) {
151                 fDump = true;
152             } else if (args[argIdx].equals("-ns")) {
153                 fNameSpaces = true;
154             } else if (args[argIdx].equals("-cp-resolve")) {
155                 fClassPathResolve = true;
156             } else if (args[argIdx].equals("-debug")) {
157                 fDebugFlag = true;
158             } else if (args[argIdx].equals("-grammar")) {
159                 fGrammar = true;
160             } else if (args[argIdx].equals("-no-validation")) {
161                 fValidate = false;
162             } else {
163                 System.err.println("Error: invalid argument \"" + args[argIdx]
164                                    + "\", expected one of -dump, -ns, -cp-resolve, -debug, -grammar");
165                 System.exit(1);
166             }
167             argIdx++;
168         }
169
170         int argsLeft = args.length-argIdx;
171         if ((argsLeft < 1) || (argsLeft > 2)) {
172             System.err.println("wrong # args: xml-parse [options] xml.file [output]");
173             System.exit(1);
174         }
175         String JavaDoc xmlFile = args[argIdx];
176         String JavaDoc outFile = (argsLeft > 1) ? args[argIdx+1] : null;
177
178         OutputStream JavaDoc out;
179         if (argsLeft == 1) {
180             out = System.out;
181         } else {
182             out = new FileOutputStream JavaDoc(args[argIdx+1]);
183         }
184         process(xmlFile, out);
185         out.flush();
186     }
187
188     /*
189      * Entry point.
190      */

191     public static void main(String JavaDoc[] args) throws Throwable JavaDoc {
192         try {
193             new XMLParse().run(args);
194         } catch (Throwable JavaDoc error) {
195             if (error instanceof ExceptionInInitializerError JavaDoc) {
196                 error = ((ExceptionInInitializerError JavaDoc)error).getException();
197             }
198             System.err.println(error.toString());
199             error.printStackTrace();
200             System.exit(2);
201         }
202         
203     }
204 }
205
Popular Tags