KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > core > wizard > SchemaParser


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.core.wizard;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.TreeSet JavaDoc;
25 import org.netbeans.api.xml.services.UserCatalog;
26 import org.openide.util.Lookup;
27 import org.openide.xml.XMLUtil;
28 import org.xml.sax.*;
29 import org.xml.sax.helpers.*;
30
31 /**
32  * Get (potentially partial) SchemaInfo from passed XML Schema.
33  *
34  * @author Petr Kuzel
35  * @see SchemaInfo
36  */

37 final class SchemaParser extends DefaultHandler {
38
39     private SchemaInfo info = new SchemaInfo();
40
41     // root elemnt depth is 0, its children has 1 etc.
42
private int depth = 0;
43     
44     /** Creates a new instance of SchemaParser */
45     public SchemaParser() {
46     }
47     
48     public SchemaInfo parse(String JavaDoc sid) {
49         if (sid == null) {
50             return null;
51         } else {
52             return parse( new InputSource(sid));
53         }
54     }
55     
56     public SchemaInfo parse(InputSource in) {
57     
58         Util.THIS.debug("SchemaParser started."); // NOI18N
59

60         try {
61             depth = 0;
62             XMLReader parser = XMLUtil.createXMLReader(false, true);
63             parser.setContentHandler(this);
64             parser.setErrorHandler(this);
65
66             UserCatalog catalog = UserCatalog.getDefault();
67             EntityResolver res = (catalog == null ? null : catalog.getEntityResolver());
68             
69             if (res != null) parser.setEntityResolver(res);
70             
71             parser.parse(in);
72             
73             return info;
74             
75         } catch (SAXException ex) {
76             Util.THIS.debug("Ignoring ex. thrown while looking for Schema roots:", ex); // NOI18N
77
if (ex.getException() instanceof RuntimeException JavaDoc) {
78                 Util.THIS.debug("Nested exception:", ex.getException()); // NOI18N
79
}
80             return info; // better partial result than nothing
81
} catch (IOException JavaDoc ex) {
82             Util.THIS.debug("Ignoring ex. thrown while looking for Schema roots:", ex); // NOI18N
83
return info; // better partial result than nothing
84
} finally {
85             Util.THIS.debug("SchemaParser stopped."); // NOI18N
86
}
87         
88     }
89     
90     public void startElement (String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes atts) throws SAXException {
91         depth++;
92         if (depth > 2) return;
93         
94         //??? should be more accurate, check ns etc
95
// may be, we should be also interested in "defaultForm" attributes
96

97         if ("element".equals(localName)) { // NOI18N
98
String JavaDoc root = atts.getValue("name");
99             if (root != null) {
100                 Util.THIS.debug("\telement decl: " + root); // NOI18N
101
info.roots.add(root);
102             }
103         } else if ("schema".equals(localName)) { // NOI18N
104
String JavaDoc ns = atts.getValue("targetNamespace"); // NOI18N
105
if (ns != null) {
106                 Util.THIS.debug("\ttarget namespace: " + ns); // NOI18N
107
info.namespace = ns;
108             }
109         }
110     }
111     
112     public void endElement (String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) {
113         depth--;
114     }
115     
116     /**
117      * Very basic information structure about schema.
118      */

119     public static final class SchemaInfo {
120         /**
121          * Root candidates
122          */

123         public final Set JavaDoc roots = new TreeSet JavaDoc();
124         
125         /**
126          * Target namespace or <code>null</code>
127          */

128         public String JavaDoc namespace;
129     }
130 }
131
Popular Tags