001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.xsd;
043:
044: import java.io.PrintStream;
045: import java.util.List;
046: import java.util.ArrayList;
047: import java.util.Map;
048: import java.util.HashMap;
049: import java.util.Iterator;
050: import org.xml.sax.ContentHandler;
051: import org.xml.sax.Attributes;
052:
053: /**
054: * ContentHandler impl for building XSD grammar
055: * @author anovak
056: */
057: class XSDContentHandler implements ContentHandler {
058:
059: /** Stack for parsed elements */
060: private List /*<Element>*/elementsStack;
061: /** All elements */
062: private Map elements;
063: /** All types */
064: private Map types;
065:
066: private PrintStream ps;
067:
068: // namespace processing
069: private boolean resolveNamespaces;
070: private Map /* <String, Namespace> */uri2Namespace;
071: private Map /* <String, Namespace> */prefix2Namespace;
072: private Namespace schemaNamespace;
073: private Namespace targetNamespace;
074:
075: /** Creates a new instance of XSDContentHandler */
076: public XSDContentHandler(PrintStream ps) {
077: this .ps = ps;
078: this .elements = new HashMap();
079: this .types = new HashMap();
080: this .elementsStack = new ArrayList();
081: this .resolveNamespaces = true;
082: this .uri2Namespace = new HashMap();
083: this .prefix2Namespace = new HashMap();
084: }
085:
086: public XSDGrammar getGrammar() {
087: return new XSDGrammar(elements, types, targetNamespace,
088: schemaNamespace);
089: }
090:
091: public void characters(char[] ch, int start, int length)
092: throws org.xml.sax.SAXException {
093: // println("characters: " + new String(ch, start, length));
094: }
095:
096: public void endDocument() throws org.xml.sax.SAXException {
097: System.out.println("Stack size: " + elementsStack.size());
098:
099: SchemaElement e = (SchemaElement) elementsStack.get(0);
100: //printlnElement(e, " ");
101:
102: // println("END Document");
103: }
104:
105: public void endElement(String namespaceURI, String localName,
106: String qName) throws org.xml.sax.SAXException {
107: // println("endELement: " + namespaceURI + " name: " + localName + " qname: " + qName);
108: // pop element
109: elementsStack.remove(elementsStack.size() - 1);
110: }
111:
112: public void endPrefixMapping(String prefix)
113: throws org.xml.sax.SAXException {
114: // println("endPrefixMapping: " + prefix);
115: }
116:
117: public void ignorableWhitespace(char[] ch, int start, int length)
118: throws org.xml.sax.SAXException {
119: }
120:
121: public void processingInstruction(String target, String data)
122: throws org.xml.sax.SAXException {
123: // println("Processing instruction: " + target + " data: " + data);
124: }
125:
126: public void setDocumentLocator(org.xml.sax.Locator locator) {
127: }
128:
129: public void skippedEntity(String name)
130: throws org.xml.sax.SAXException {
131: // println("skippedEntity: " + name);
132: }
133:
134: public void startDocument() throws org.xml.sax.SAXException {
135: elementsStack.add(SchemaElement.createSchemaElement(null,
136: "TOP_LEVEL", null, null));
137: // println("START Doc");
138: }
139:
140: public void startElement(String namespaceURI, String localName,
141: String qName, Attributes atts)
142: throws org.xml.sax.SAXException {
143: if (resolveNamespaces) {
144: resolveNamespaces = false;
145:
146: for (int i = 0; i < atts.getLength(); i++) {
147: String name = atts.getQName(i);
148: if (name.startsWith(Namespace.XMLNS_ATTR)) {
149: String uri = atts.getValue(i);
150: String prefix = Namespace.getSufix(name);
151: Namespace ns = new Namespace(uri, prefix);
152: if (prefix != null) {
153: this .prefix2Namespace.put(prefix, ns);
154: }
155: this .uri2Namespace.put(uri, ns);
156: System.err.println("NAMESPACE ADDED: " + prefix
157: + " uri: " + uri);
158: } else {
159: System.err.println("ATTR not taken: " + name
160: + " xxx " + atts.getQName(i) + " xxx "
161: + atts.getValue(i));
162: }
163: }
164:
165: // expect qName as xs:schema, xsd:schema and the likes or just schema
166: String myprefix = Namespace.getPrefix(qName);
167: Namespace xs = (Namespace) uri2Namespace
168: .get(Namespace.XSD_SCHEMA_URI);
169: assert xs != null : "Namespace http://www.w3.org/2001/XMLSchema not found";
170: if (myprefix == xs.getPrefix()
171: || myprefix.equals(xs.getPrefix())) {
172: this .schemaNamespace = xs;
173: String uri = atts.getValue("targetNamespace");
174: this .targetNamespace = (Namespace) uri2Namespace
175: .get(uri);
176: assert targetNamespace != null;
177: // OK
178: } else {
179: // ERRORneous schema
180: assert false : "Unknown schema, prefix of schema element does not match http://www.w3.org/2001/XMLSchema namespace";
181: }
182: }
183:
184: SchemaElement e = SchemaElement.createSchemaElement(
185: namespaceURI, qName, atts, schemaNamespace.getPrefix());
186: System.err.println("ELEMENTS ADDING: " + atts.getValue("name")
187: + " qname: " + qName);
188: SchemaElement parent = (SchemaElement) elementsStack
189: .get(elementsStack.size() - 1);
190: if (parent != null && parent.getSAXAttributes() != null) {
191: System.err.println("INTO: "
192: + parent.getSAXAttributes().getValue("name"));
193: }
194: elements.put(atts.getValue("name"), e);
195: parent.addSubelement(e);
196: // push
197: elementsStack.add(e);
198: // println("startElements: " + namespaceURI + "locName: " + localName + " qName: " + qName);
199: // printlnAttributes(atts);
200: }
201:
202: public void startPrefixMapping(String prefix, String uri)
203: throws org.xml.sax.SAXException {
204: // println("startPrefixMapping: prefix: " + prefix + " URI: " + uri);
205: }
206: }
|