KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xsd > XSDParser


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xsd;
21
22 import javax.xml.parsers.SAXParserFactory JavaDoc;
23 import org.xml.sax.XMLReader JavaDoc;
24 import org.xml.sax.EntityResolver JavaDoc;
25 import org.xml.sax.InputSource JavaDoc;
26
27 import org.netbeans.api.xml.services.UserCatalog;
28
29 /**
30  *
31  * @author anovak
32  */

33 public class XSDParser {
34
35     /** Creates a new instance of XSDParser */
36     public XSDParser() {
37     }
38
39     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
40         if (args.length != 1) {
41             System.err.println("Error: missing file parameter required or too many args");
42         }
43         
44         java.io.FileInputStream JavaDoc fistr = new java.io.FileInputStream JavaDoc(args[0]);
45         
46         SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
47         XMLReader JavaDoc reader = factory.newSAXParser().getXMLReader();
48         XSDContentHandler handler = new XSDContentHandler(System.out);
49         reader.setContentHandler(handler);
50         reader.parse(new InputSource JavaDoc(fistr));
51     }
52
53     public XSDGrammar parse(InputSource JavaDoc in) {
54
55         XSDContentHandler handler = new XSDContentHandler(System.out);
56         
57         try {
58             SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
59             XMLReader JavaDoc reader = factory.newSAXParser().getXMLReader();
60
61             UserCatalog catalog = UserCatalog.getDefault();
62             EntityResolver JavaDoc res = (catalog == null ? null : catalog.getEntityResolver());
63
64             if (res != null) {
65                 reader.setEntityResolver(res);
66             }
67
68             reader.setContentHandler(handler);
69             reader.parse(in);
70             return handler.getGrammar();
71         } catch (org.xml.sax.SAXException JavaDoc ex) {
72             if (Boolean.getBoolean("netbeans.debug.xml") || Boolean.getBoolean("netbeans.debug.exceptions")) { //NOI18N
73
ex.printStackTrace();
74                 if (ex.getException() instanceof RuntimeException JavaDoc) {
75                     ex.getException().printStackTrace(); //???
76
}
77             }
78             return handler.getGrammar(); // better partial result than nothing
79
} catch (java.io.IOException JavaDoc ex) {
80             if (Boolean.getBoolean("netbeans.debug.xml")) { // NOI18N
81
ex.printStackTrace();
82             }
83             return handler.getGrammar(); // better partial result than nothing
84
} catch (javax.xml.parsers.ParserConfigurationException JavaDoc e) {
85             if (Boolean.getBoolean("netbeans.debug.xml")) { // NOI18N
86
e.printStackTrace();
87             }
88             return handler.getGrammar(); // better partial result than nothing
89
}
90     }
91 }
92
Popular Tags