1 19 20 package org.netbeans.modules.xml.xsd; 21 22 import java.util.*; 23 import org.w3c.dom.Node ; 24 import org.xml.sax.InputSource ; 25 import org.xml.sax.EntityResolver ; 26 27 28 import org.openide.filesystems.FileObject; 29 import org.netbeans.modules.xml.api.model.*; 30 import org.netbeans.api.xml.services.UserCatalog; 31 32 36 class XSDGrammarResolver implements GrammarQuery { 37 38 39 private final Map prefix2Namespace; 40 41 private final Map uri2Namespace; 42 43 private Namespace defaultNamespace; 44 45 private Node target; 46 private FileObject document; 47 48 49 private XSDGrammarResolver() { 50 this.uri2Namespace = new HashMap(); 51 this.prefix2Namespace = new HashMap(); 52 this.defaultNamespace = null; 53 this.target = null; 54 this.document = null; 55 } 56 57 58 public static XSDGrammarResolver createResolver(org.netbeans.modules.xml.api.model.GrammarEnvironment ctx) { 59 XSDGrammarResolver ret = null; 60 61 Enumeration en = ctx.getDocumentChildren(); 62 while (en.hasMoreElements()) { 63 Node next = (Node ) en.nextElement(); 64 if (next.getNodeType() == next.ELEMENT_NODE) { 66 org.w3c.dom.Element element = (org.w3c.dom.Element ) next; 67 org.w3c.dom.NamedNodeMap atts = element.getAttributes(); 68 String eprefix = Namespace.getPrefix(element.getNodeName()); 69 70 for (int i = 0; i < atts.getLength(); i++) { 72 Node attribute = atts.item(i); 73 String name = attribute.getNodeName(); 74 if (name.startsWith(Namespace.XMLNS_ATTR)) { 75 String uri = attribute.getNodeValue(); 76 String prefix = Namespace.getSufix(name); 77 Namespace ns = new Namespace(uri, prefix); 78 if (ret == null) { 79 ret = new XSDGrammarResolver(); 80 } 81 ret.addNamespace(ns); 82 83 if (prefix == null || prefix.equals(eprefix)) { 84 ret.defaultNamespace = ns; 85 } 86 } 87 } 88 89 if (ret == null) { 90 continue; 91 } 92 93 assert ret.defaultNamespace != null; 94 95 ret.resolveSchemaLocation(element); 97 98 if (ret.defaultNamespace.getSchemaLocation() == null) { 99 org.openide.ErrorManager.getDefault().log(org.openide.ErrorManager.WARNING, "SCHEMA is null: " + element.getLocalName()); 101 } 103 104 ret.setTarget(next); 105 ret.setDocument(ctx.getFileObject()); 106 System.err.println("create resolver - success"); 107 return ret; 108 } } 111 return null; 112 } 113 114 115 private void resolveSchemaLocation(org.w3c.dom.Element element) { 116 Namespace ns = findNamespaceByURI(Namespace.XSI_NAMESPACE_URI); 118 String prefix = ns.getPrefix().concat(":"); 119 String schema = element.getAttribute(prefix.concat(Namespace.XSI_LOCATION)); 120 if (schema == null) { 121 schema = element.getAttribute(prefix.concat(Namespace.XSI_NO_NAMESPACE_LOCATION)); 122 } 123 124 System.err.println("SCHEMA LOC: " + schema); 125 defaultNamespace.setSchemaLocation(schema); 126 } 127 128 private void setDocument(FileObject fileObject) { 129 this.document = fileObject; 130 } 131 132 private XSDGrammar findGrammar(HintContext virtualElementCtx) throws java.io.IOException { 133 Namespace ns = findNamespace(virtualElementCtx); 134 XSDGrammar grammar = ns.getGrammar(); 135 if (grammar == null) { 136 grammar = createGrammar(ns); 137 ns.setGrammar(grammar); 138 grammar.setNamespace(ns); 139 } 140 141 return grammar; 142 } 143 144 private XSDGrammar createGrammar(Namespace ns) throws java.io.IOException { 145 String uri = ns.getSchemaLocation(); 147 if (uri == null) { 148 uri = ns.getURI(); 149 } 150 System.err.println("findSchema: " + uri); 151 152 int idx = uri.indexOf(' '); 153 if (idx >= 0) { 154 uri = uri.substring(idx + 1); 155 } 156 157 try { 159 UserCatalog catalog = UserCatalog.getDefault(); 160 if (catalog != null) { 161 EntityResolver resolver = catalog.getEntityResolver(); 162 if (resolver != null) { 163 InputSource inputSource = resolver.resolveEntity(uri, null); 164 if (inputSource != null) { 165 return new XSDParser().parse(inputSource); 166 } 167 } 168 } 169 } catch (org.xml.sax.SAXException e) { 170 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.EXCEPTION, e); 171 } 172 173 try { 175 java.net.URL url = new java.net.URL (uri); 176 return new XSDParser().parse(new InputSource (url.openStream())); 177 } catch (java.net.MalformedURLException e) { } 181 182 if (document == null) { 184 return null; 185 } 186 187 FileObject fo = document.getParent().getFileObject(uri); 188 189 if (fo == null) { 190 return null; 193 } 194 195 return new XSDParser().parse(new InputSource (fo.getInputStream())); 196 } 197 198 private void addNamespace(Namespace ns) { 199 if (ns.getPrefix() != null) { 200 prefix2Namespace.put(ns.getPrefix(), ns); 201 } 202 203 uri2Namespace.put(ns.getURI(), ns); 204 } 205 206 private Namespace findNamespaceByURI(String uri) { 207 return (Namespace) uri2Namespace.get(uri); 208 } 209 210 private Namespace findNamespace(Node node) { 211 System.err.println("find namespace"); 213 System.err.println("PARENT: " + node.getParentNode().getNodeName()); 214 231 return defaultNamespace; 232 } 233 234 237 public boolean isAllowed(Enumeration en) { 238 return true; 239 } 240 241 public Enumeration queryAttributes(HintContext ownerElementCtx) { 242 Thread.dumpStack(); 243 return Collections.enumeration(new ArrayList()); 244 } 245 246 public GrammarResult queryDefault(HintContext parentNodeCtx) { 247 Thread.dumpStack(); 248 return null; 249 } 250 251 public Enumeration queryElements(HintContext virtualElementCtx) { 252 try { 253 XSDGrammar grammar = findGrammar(virtualElementCtx); 254 return grammar.queryElements(virtualElementCtx); 255 } catch (java.io.IOException e) { 256 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.EXCEPTION, e); 257 return Collections.enumeration(new ArrayList(0)); 258 } 259 } 260 261 public Enumeration queryEntities(String prefix) { 262 Thread.dumpStack(); 263 return Collections.enumeration(new ArrayList()); 264 } 265 266 public Enumeration queryNotations(String prefix) { 267 Thread.dumpStack(); 268 return Collections.enumeration(new ArrayList()); 269 } 270 271 public Enumeration queryValues(HintContext virtualTextCtx) { 272 Thread.dumpStack(); 273 return Collections.enumeration(new ArrayList()); 274 } 275 276 278 279 public java.awt.Component getCustomizer(HintContext nodeCtx) { 280 return null; 281 } 282 283 284 public org.openide.nodes.Node.Property[] getProperties(HintContext nodeCtx) { 285 return null; 286 } 287 288 289 public boolean hasCustomizer(HintContext nodeCtx) { 290 return false; 291 } 292 293 297 public org.w3c.dom.Node getTarget() { 298 return target; 299 } 300 301 305 public void setTarget(org.w3c.dom.Node target) { 306 this.target = target; 307 } 308 309 } | Popular Tags |