KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.PrintStream JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import org.xml.sax.ContentHandler JavaDoc;
29 import org.xml.sax.Attributes JavaDoc;
30
31 /**
32  * ContentHandler impl for building XSD grammar
33  * @author anovak
34  */

35 class XSDContentHandler implements ContentHandler JavaDoc {
36
37     /** Stack for parsed elements */
38     private List JavaDoc /*<Element>*/ elementsStack;
39     /** All elements */
40     private Map JavaDoc elements;
41     /** All types */
42     private Map JavaDoc types;
43     
44     private PrintStream JavaDoc ps;
45     
46     // namespace processing
47
private boolean resolveNamespaces;
48     private Map JavaDoc /* <String, Namespace> */ uri2Namespace;
49     private Map JavaDoc /* <String, Namespace> */ prefix2Namespace;
50     private Namespace schemaNamespace;
51     private Namespace targetNamespace;
52     
53     /** Creates a new instance of XSDContentHandler */
54     public XSDContentHandler(PrintStream JavaDoc ps) {
55         this.ps = ps;
56         this.elements = new HashMap JavaDoc();
57         this.types = new HashMap JavaDoc();
58         this.elementsStack = new ArrayList JavaDoc();
59         this.resolveNamespaces = true;
60         this.uri2Namespace = new HashMap JavaDoc();
61         this.prefix2Namespace = new HashMap JavaDoc();
62     }
63
64     public XSDGrammar getGrammar() {
65         return new XSDGrammar(elements, types, targetNamespace, schemaNamespace);
66     }
67     
68     public void characters(char[] ch, int start, int length) throws org.xml.sax.SAXException JavaDoc {
69     // println("characters: " + new String(ch, start, length));
70
}
71     
72     public void endDocument() throws org.xml.sax.SAXException JavaDoc {
73         System.out.println("Stack size: " + elementsStack.size());
74         
75         SchemaElement e = (SchemaElement) elementsStack.get(0);
76         //printlnElement(e, " ");
77

78         // println("END Document");
79
}
80     
81     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws org.xml.sax.SAXException JavaDoc {
82         // println("endELement: " + namespaceURI + " name: " + localName + " qname: " + qName);
83
// pop element
84
elementsStack.remove(elementsStack.size() - 1);
85     }
86     
87     public void endPrefixMapping(String JavaDoc prefix) throws org.xml.sax.SAXException JavaDoc {
88         // println("endPrefixMapping: " + prefix);
89
}
90     
91     public void ignorableWhitespace(char[] ch, int start, int length) throws org.xml.sax.SAXException JavaDoc {
92     }
93     
94     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws org.xml.sax.SAXException JavaDoc {
95         // println("Processing instruction: " + target + " data: " + data);
96
}
97     
98     public void setDocumentLocator(org.xml.sax.Locator JavaDoc locator) {
99     }
100     
101     public void skippedEntity(String JavaDoc name) throws org.xml.sax.SAXException JavaDoc {
102         // println("skippedEntity: " + name);
103
}
104     
105     public void startDocument() throws org.xml.sax.SAXException JavaDoc {
106         elementsStack.add(SchemaElement.createSchemaElement(null, "TOP_LEVEL", null, null));
107         // println("START Doc");
108
}
109     
110     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws org.xml.sax.SAXException JavaDoc {
111         if (resolveNamespaces) {
112             resolveNamespaces = false;
113             
114             for (int i = 0; i < atts.getLength(); i++) {
115                 String JavaDoc name = atts.getQName(i);
116                 if (name.startsWith(Namespace.XMLNS_ATTR)) {
117                     String JavaDoc uri = atts.getValue(i);
118                     String JavaDoc prefix = Namespace.getSufix(name);
119                     Namespace ns = new Namespace(uri, prefix);
120                     if (prefix != null) {
121                         this.prefix2Namespace.put(prefix, ns);
122                     }
123                     this.uri2Namespace.put(uri, ns);
124                     System.err.println("NAMESPACE ADDED: " + prefix + " uri: " + uri);
125                 } else {
126                     System.err.println("ATTR not taken: " + name + " xxx " + atts.getQName(i) + " xxx " + atts.getValue(i));
127                 }
128             }
129             
130             // expect qName as xs:schema, xsd:schema and the likes or just schema
131
String JavaDoc myprefix = Namespace.getPrefix(qName);
132             Namespace xs = (Namespace) uri2Namespace.get(Namespace.XSD_SCHEMA_URI);
133             assert xs != null : "Namespace http://www.w3.org/2001/XMLSchema not found";
134             if (myprefix == xs.getPrefix() || myprefix.equals(xs.getPrefix())) {
135                 this.schemaNamespace = xs;
136                 String JavaDoc uri = atts.getValue("targetNamespace");
137                 this.targetNamespace = (Namespace) uri2Namespace.get(uri);
138                 assert targetNamespace != null;
139                 // OK
140
} else {
141                 // ERRORneous schema
142
assert false : "Unknown schema, prefix of schema element does not match http://www.w3.org/2001/XMLSchema namespace";
143             }
144         }
145         
146         SchemaElement e = SchemaElement.createSchemaElement(namespaceURI, qName, atts, schemaNamespace.getPrefix());
147         System.err.println("ELEMENTS ADDING: " + atts.getValue("name") + " qname: " + qName);
148         SchemaElement parent = (SchemaElement) elementsStack.get(elementsStack.size() - 1);
149         if (parent != null && parent.getSAXAttributes() != null) {
150             System.err.println("INTO: " + parent.getSAXAttributes().getValue("name"));
151         }
152         elements.put(atts.getValue("name"), e);
153         parent.addSubelement(e);
154         // push
155
elementsStack.add(e);
156        // println("startElements: " + namespaceURI + "locName: " + localName + " qName: " + qName);
157
// printlnAttributes(atts);
158
}
159     
160     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws org.xml.sax.SAXException JavaDoc {
161         // println("startPrefixMapping: prefix: " + prefix + " URI: " + uri);
162
}
163 }
164
Popular Tags