KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Enumeration JavaDoc;
22 import org.w3c.dom.*;
23 import org.xml.sax.InputSource JavaDoc;
24 import org.netbeans.modules.xml.api.model.DTDUtil;
25 import java.io.*;
26
27 /** XML Schema Grammar provided code completion for XML Schema file.
28  *
29  * @author Milan Kuchtiak
30  */

31 public class SchemaGrammarQueryManager extends org.netbeans.modules.xml.api.model.GrammarQueryManager
32 {
33     // actually code completion works only for xsd: and xs: prefixes
34
private static final String JavaDoc SCHEMA="schema"; //NOI18N
35
private static final String JavaDoc PUBLIC_JAXB="http://java.sun.com/xml/ns/jaxb"; //NOI18N
36

37     private String JavaDoc prefix, ns_jaxb;
38        
39     public java.util.Enumeration JavaDoc enabled(org.netbeans.modules.xml.api.model.GrammarEnvironment ctx) {
40         if (ctx.getFileObject() == null) return null;
41         Enumeration JavaDoc 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; // null for XML Documents specified by DTD
48
} else if (next.getNodeType() == next.ELEMENT_NODE) {
49                 Element element = (Element) next;
50                 String JavaDoc tagName = element.getTagName();
51                 if (tagName.endsWith(":"+SCHEMA)) { //NOI18N
52
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 JavaDoc name = attr.getName();
61                     if (PUBLIC_JAXB.equals(attr.getValue())) {
62                         if (name.startsWith("xmlns:")) ns_jaxb=name.substring(6); //NOI18N
63
}
64                 }
65                 return org.openide.util.Enumerations.singleton (next);
66             }
67         }
68         return null;
69     }
70     
71     public java.beans.FeatureDescriptor JavaDoc getDescriptor() {
72         return new java.beans.FeatureDescriptor JavaDoc();
73     }
74     
75     /** Returns DTD for code completion
76     */

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 JavaDoc inputSource = null;
80         StringBuffer JavaDoc buffer=new StringBuffer JavaDoc(512);
81         if (prefix.length()==0) {
82             buffer.append("<!ENTITY % p ''><!ENTITY % s ''>"); //NOI18N
83
} else {
84             buffer.append("<!ENTITY % p '"+prefix+":'><!ENTITY % s ':"+prefix+"'>"); //NOI18N
85
}
86         java.io.InputStream JavaDoc is = null;
87         if (ns_jaxb==null) {
88             is = getClass().getResourceAsStream("/org/netbeans/modules/xml/schema/resources/XMLSchema.dtd"); //NOI18N
89
} else {
90             is = getClass().getResourceAsStream("/org/netbeans/modules/xml/schema/resources/XMLSchema_jaxb.dtd"); //NOI18N
91
buffer.append("<!ENTITY % jaxb '"+ns_jaxb+"'>"); //NOI18N
92
}
93         BufferedReader br = new BufferedReader(new InputStreamReader(is));
94         String JavaDoc 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 JavaDoc(new StringReader(buffer.toString()));
104         if (ns_jaxb==null)
105             inputSource.setSystemId("nbres:/org/netbeans/modules/xml/schema/resources/XMLSchema.dtd"); //NOI18N
106
else
107             inputSource.setSystemId("nbres:/org/netbeans/modules/xml/schema/resources/XMLSchema_jaxb.dtd"); //NOI18N
108
if (inputSource!=null) {
109             return DTDUtil.parseDTD(true, inputSource);
110         }
111         return null;
112     }
113 }
114
Popular Tags