KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xs > QueryXS


1 /*
2  * Copyright 2003-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 xs;
18
19 import org.apache.xerces.xs.XSConstants;
20 import org.apache.xerces.xs.XSImplementation;
21 import org.apache.xerces.xs.XSLoader;
22 import org.apache.xerces.xs.XSModel;
23 import org.apache.xerces.xs.XSNamedMap;
24 import org.apache.xerces.xs.XSObject;
25 import org.w3c.dom.DOMConfiguration JavaDoc;
26 import org.w3c.dom.DOMError JavaDoc;
27 import org.w3c.dom.DOMErrorHandler JavaDoc;
28 import org.w3c.dom.bootstrap.DOMImplementationRegistry JavaDoc;
29 import org.w3c.dom.ls.LSParser JavaDoc;
30
31 /**
32  * This sample program illustrates how to use load XML Schemas and
33  * use XML Schema API (org.apache.xerces.xs) to navigate XML Schema components.
34  *
35  * @author Elena Litani, IBM
36  * @version $Id: QueryXS.java,v 1.6 2005/05/02 22:07:08 mrglavas Exp $
37  */

38 public class QueryXS implements DOMErrorHandler JavaDoc {
39
40     /** Default namespaces support (true). */
41     protected static final boolean DEFAULT_NAMESPACES = true;
42
43     /** Default validation support (false). */
44     protected static final boolean DEFAULT_VALIDATION = false;
45
46     /** Default Schema validation support (false). */
47     protected static final boolean DEFAULT_SCHEMA_VALIDATION = false;
48
49     static LSParser JavaDoc builder;
50     public static void main(String JavaDoc[] argv) {
51
52         if (argv.length == 0) {
53             printUsage();
54             System.exit(1);
55         }
56
57         try {
58             // get DOM Implementation using DOM Registry
59
System.setProperty(
60                 DOMImplementationRegistry.PROPERTY,
61                 "org.apache.xerces.dom.DOMXSImplementationSourceImpl");
62             DOMImplementationRegistry JavaDoc registry = DOMImplementationRegistry.newInstance();
63
64             XSImplementation impl = (XSImplementation) registry.getDOMImplementation("XS-Loader");
65
66             XSLoader schemaLoader = impl.createXSLoader(null);
67
68             DOMConfiguration JavaDoc config = schemaLoader.getConfig();
69
70             // create Error Handler
71
DOMErrorHandler JavaDoc errorHandler = new QueryXS();
72
73             // set error handler
74
config.setParameter("error-handler", errorHandler);
75
76             // set validation feature
77
config.setParameter("validate", Boolean.TRUE);
78
79             // parse document
80
System.out.println("Parsing " + argv[0] + "...");
81             XSModel model = schemaLoader.loadURI(argv[0]);
82             if (model != null) {
83                 // element declarations
84
XSNamedMap map = model.getComponents(XSConstants.ELEMENT_DECLARATION);
85                 if (map.getLength() != 0) {
86                     System.out.println("*************************************************");
87                     System.out.println("* Global element declarations: {namespace} name ");
88                     System.out.println("*************************************************");
89                     for (int i = 0; i < map.getLength(); i++) {
90                         XSObject item = map.item(i);
91                         System.out.println("{" + item.getNamespace() + "}" + item.getName());
92                     }
93                 }
94                 // attribute declarations
95
map = model.getComponents(XSConstants.ATTRIBUTE_DECLARATION);
96                 if (map.getLength() != 0) {
97                     System.out.println("*************************************************");
98                     System.out.println("* Global attribute declarations: {namespace} name");
99                     System.out.println("*************************************************");
100                     for (int i = 0; i < map.getLength(); i++) {
101                         XSObject item = map.item(i);
102                         System.out.println("{" + item.getNamespace() + "}" + item.getName());
103                     }
104                 }
105                 // notation declarations
106
map = model.getComponents(XSConstants.TYPE_DEFINITION);
107                 if (map.getLength() != 0) {
108                     System.out.println("*************************************************");
109                     System.out.println("* Global type declarations: {namespace} name");
110                     System.out.println("*************************************************");
111                     for (int i = 0; i < map.getLength(); i++) {
112                         XSObject item = map.item(i);
113                         System.out.println("{" + item.getNamespace() + "}" + item.getName());
114                     }
115                 }
116                 
117                 // notation declarations
118
map = model.getComponents(XSConstants.NOTATION_DECLARATION);
119                 if (map.getLength() != 0) {
120                     System.out.println("*************************************************");
121                     System.out.println("* Global notation declarations: {namespace} name");
122                     System.out.println("*************************************************");
123                     for (int i = 0; i < map.getLength(); i++) {
124                         XSObject item = map.item(i);
125                         System.out.println("{" + item.getNamespace() + "}" + item.getName());
126                     }
127                 }
128
129             }
130
131         } catch (Exception JavaDoc ex) {
132             ex.printStackTrace();
133         }
134     }
135
136     private static void printUsage() {
137         
138         System.err.println("usage: java dom.QueryXS uri ...");
139         System.err.println();
140
141     } // printUsage()
142

143
144     public boolean handleError(DOMError JavaDoc error){
145         short severity = error.getSeverity();
146         if (severity == DOMError.SEVERITY_ERROR) {
147             System.out.println("[xs-error]: "+error.getMessage());
148         }
149
150         if (severity == DOMError.SEVERITY_WARNING) {
151             System.out.println("[xs-warning]: "+error.getMessage());
152         }
153         return true;
154
155     }
156
157 }
158
159
160
161
Popular Tags