1 19 20 package org.netbeans.modules.xml.xsd; 21 22 import java.io.PrintStream ; 23 import java.util.List ; 24 import java.util.ArrayList ; 25 import java.util.Map ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import org.xml.sax.ContentHandler ; 29 import org.xml.sax.Attributes ; 30 31 35 class XSDContentHandler implements ContentHandler { 36 37 38 private List elementsStack; 39 40 private Map elements; 41 42 private Map types; 43 44 private PrintStream ps; 45 46 private boolean resolveNamespaces; 48 private Map uri2Namespace; 49 private Map prefix2Namespace; 50 private Namespace schemaNamespace; 51 private Namespace targetNamespace; 52 53 54 public XSDContentHandler(PrintStream ps) { 55 this.ps = ps; 56 this.elements = new HashMap (); 57 this.types = new HashMap (); 58 this.elementsStack = new ArrayList (); 59 this.resolveNamespaces = true; 60 this.uri2Namespace = new HashMap (); 61 this.prefix2Namespace = new HashMap (); 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 { 69 } 71 72 public void endDocument() throws org.xml.sax.SAXException { 73 System.out.println("Stack size: " + elementsStack.size()); 74 75 SchemaElement e = (SchemaElement) elementsStack.get(0); 76 78 } 80 81 public void endElement(String namespaceURI, String localName, String qName) throws org.xml.sax.SAXException { 82 elementsStack.remove(elementsStack.size() - 1); 85 } 86 87 public void endPrefixMapping(String prefix) throws org.xml.sax.SAXException { 88 } 90 91 public void ignorableWhitespace(char[] ch, int start, int length) throws org.xml.sax.SAXException { 92 } 93 94 public void processingInstruction(String target, String data) throws org.xml.sax.SAXException { 95 } 97 98 public void setDocumentLocator(org.xml.sax.Locator locator) { 99 } 100 101 public void skippedEntity(String name) throws org.xml.sax.SAXException { 102 } 104 105 public void startDocument() throws org.xml.sax.SAXException { 106 elementsStack.add(SchemaElement.createSchemaElement(null, "TOP_LEVEL", null, null)); 107 } 109 110 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws org.xml.sax.SAXException { 111 if (resolveNamespaces) { 112 resolveNamespaces = false; 113 114 for (int i = 0; i < atts.getLength(); i++) { 115 String name = atts.getQName(i); 116 if (name.startsWith(Namespace.XMLNS_ATTR)) { 117 String uri = atts.getValue(i); 118 String 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 String 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 uri = atts.getValue("targetNamespace"); 137 this.targetNamespace = (Namespace) uri2Namespace.get(uri); 138 assert targetNamespace != null; 139 } else { 141 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 elementsStack.add(e); 156 } 159 160 public void startPrefixMapping(String prefix, String uri) throws org.xml.sax.SAXException { 161 } 163 } 164 | Popular Tags |