1 19 20 package org.netbeans.modules.xml.xsd; 21 import java.util.Enumeration ; 22 import org.w3c.dom.*; 23 import org.xml.sax.InputSource ; 24 import org.netbeans.modules.xml.api.model.DTDUtil; 25 import java.io.*; 26 27 31 public class SchemaGrammarQueryManager extends org.netbeans.modules.xml.api.model.GrammarQueryManager 32 { 33 private static final String SCHEMA="schema"; private static final String PUBLIC_JAXB="http://java.sun.com/xml/ns/jaxb"; 37 private String prefix, ns_jaxb; 38 39 public java.util.Enumeration enabled(org.netbeans.modules.xml.api.model.GrammarEnvironment ctx) { 40 if (ctx.getFileObject() == null) return null; 41 Enumeration en = ctx.getDocumentChildren(); 42 prefix=null; 43 ns_jaxb=null; 44 while (en.hasMoreElements()) { 45 Node next = (Node) en.nextElement(); 46 if (next.getNodeType() == next.DOCUMENT_TYPE_NODE) { 47 return null; } else if (next.getNodeType() == next.ELEMENT_NODE) { 49 Element element = (Element) next; 50 String tagName = element.getTagName(); 51 if (tagName.endsWith(":"+SCHEMA)) { prefix = tagName.substring(0,tagName.indexOf(":"+SCHEMA)); 53 } else if (tagName.equals(SCHEMA)) { 54 prefix = ""; 55 } 56 if (prefix==null) return null; 57 NamedNodeMap map = element.getAttributes(); 58 for (int i=0; i<map.getLength();i++) { 59 Attr attr = (Attr)map.item(i); 60 String name = attr.getName(); 61 if (PUBLIC_JAXB.equals(attr.getValue())) { 62 if (name.startsWith("xmlns:")) ns_jaxb=name.substring(6); } 64 } 65 return org.openide.util.Enumerations.singleton (next); 66 } 67 } 68 return null; 69 } 70 71 public java.beans.FeatureDescriptor getDescriptor() { 72 return new java.beans.FeatureDescriptor (); 73 } 74 75 77 public org.netbeans.modules.xml.api.model.GrammarQuery getGrammar(org.netbeans.modules.xml.api.model.GrammarEnvironment ctx) { 78 if (prefix==null) return null; 79 InputSource inputSource = null; 80 StringBuffer buffer=new StringBuffer (512); 81 if (prefix.length()==0) { 82 buffer.append("<!ENTITY % p ''><!ENTITY % s ''>"); } else { 84 buffer.append("<!ENTITY % p '"+prefix+":'><!ENTITY % s ':"+prefix+"'>"); } 86 java.io.InputStream is = null; 87 if (ns_jaxb==null) { 88 is = getClass().getResourceAsStream("/org/netbeans/modules/xml/schema/resources/XMLSchema.dtd"); } else { 90 is = getClass().getResourceAsStream("/org/netbeans/modules/xml/schema/resources/XMLSchema_jaxb.dtd"); buffer.append("<!ENTITY % jaxb '"+ns_jaxb+"'>"); } 93 BufferedReader br = new BufferedReader(new InputStreamReader(is)); 94 String line = null; 95 try { 96 while ((line=br.readLine())!=null) { 97 buffer.append(line); 98 } 99 br.close(); 100 } catch (IOException ex) { 101 return null; 102 } 103 inputSource = new InputSource (new StringReader(buffer.toString())); 104 if (ns_jaxb==null) 105 inputSource.setSystemId("nbres:/org/netbeans/modules/xml/schema/resources/XMLSchema.dtd"); else 107 inputSource.setSystemId("nbres:/org/netbeans/modules/xml/schema/resources/XMLSchema_jaxb.dtd"); if (inputSource!=null) { 109 return DTDUtil.parseDTD(true, inputSource); 110 } 111 return null; 112 } 113 } 114 | Popular Tags |