KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsdl > WSSchemaGrammarQueryMgr


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.websvc.wsdl;
21
22 import java.io.*;
23 import java.util.Enumeration JavaDoc;
24
25 import org.w3c.dom.*;
26 import org.xml.sax.InputSource JavaDoc;
27
28 import org.openide.util.Enumerations;
29
30 import org.netbeans.modules.xml.api.model.GrammarEnvironment;
31 import org.netbeans.modules.xml.api.model.GrammarQuery;
32 import org.netbeans.modules.xml.api.model.GrammarQueryManager;
33 import org.netbeans.modules.xml.api.model.DTDUtil;
34
35 /** WS Schema Grammar provider for WSDL and jax-rpc config files
36  *
37  * @author Peter Williams, Milan Kuchtiak
38  */

39 public class WSSchemaGrammarQueryMgr extends GrammarQueryManager
40 {
41     private static final String JavaDoc XMLNS_ATTR = "xmlns"; //NOI18N
42
private static final String JavaDoc JAXRPC_CONFIG_TAG = "configuration"; // NOI18N
43
private static final String JavaDoc WSDL_DEFINITIONS = "definitions"; // NOI18N
44

45     private static final int TYPE_WSDL = 0;
46     private static final int TYPE_JAXRPC_CONFIG = 1;
47     
48     private static final String JavaDoc PUBLIC_WSDL="http://schemas.xmlsoap.org/wsdl/"; //NOI18N
49
private static final String JavaDoc PUBLIC_SOAP="http://schemas.xmlsoap.org/wsdl/soap/"; //NOI18N
50
private static final String JavaDoc PUBLIC_SOAPENC="http://schemas.xmlsoap.org/soap/encoding/"; //NOI18N
51
private static final String JavaDoc PUBLIC_SCHEMA="http://www.w3.org/2001/XMLSchema"; //NOI18N
52

53     private int schema_type;
54     private String JavaDoc prefix, ns_wsdl, ns_soap, ns_soapenc, ns_schema;
55     
56     public Enumeration JavaDoc enabled(GrammarEnvironment ctx) {
57         if (ctx.getFileObject() == null) {
58             return null;
59         }
60
61         Enumeration JavaDoc en = ctx.getDocumentChildren();
62         while(en.hasMoreElements()) {
63             Node next = (Node) en.nextElement();
64             if(next.getNodeType() == next.DOCUMENT_TYPE_NODE) {
65                 schema_type=-1;
66                 return null; // null for web.xml specified by DTD -- !PW not sure of the relevance here for jaxrpc
67
} else if(next.getNodeType() == next.ELEMENT_NODE) {
68                 prefix=null;
69                 ns_wsdl="wsdl"; //NOI18N
70
ns_soap="soap"; //NOI18N
71
ns_soapenc="soapenc"; //NOI18N
72
ns_schema="xsd"; //NOI18N
73
Element element = (Element) next;
74                 String JavaDoc tag = element.getTagName();
75                 if(JAXRPC_CONFIG_TAG.equals(tag)) {
76                     String JavaDoc xmlns = element.getAttribute(XMLNS_ATTR);
77                     if(WSSchemaCatalog.JAXRPC_CONFIG_1_1.equals(xmlns) || WSSchemaCatalog.JAXRPC_CONFIG_1_1_SLASH.equals(xmlns)) {
78                         schema_type=TYPE_JAXRPC_CONFIG;
79                         return Enumerations.singleton (next);
80                     }
81                 } else if (WSDL_DEFINITIONS.equals(tag)) {
82                     schema_type=TYPE_WSDL;
83                     prefix="";
84                 } else if (tag.endsWith(":"+WSDL_DEFINITIONS)) {
85                     schema_type=TYPE_WSDL;
86                     prefix = tag.substring(0,tag.indexOf(":"+WSDL_DEFINITIONS));
87                 }
88                 if (prefix!=null) {
89                     NamedNodeMap map = element.getAttributes();
90                     for (int i=0; i<map.getLength();i++) {
91                         Attr attr = (Attr)map.item(i);
92                         String JavaDoc name = attr.getName();
93                         if (name.startsWith("xmlns:")) { //NOI18N
94
if (PUBLIC_WSDL.equals(attr.getValue()))
95                                 ns_wsdl=name.substring(6); //NOI18N
96
else if (PUBLIC_SOAP.equals(attr.getValue()))
97                                 ns_soap=name.substring(6); //NOI18N
98
else if (PUBLIC_SOAPENC.equals(attr.getValue()))
99                                 ns_soapenc=name.substring(6); //NOI18N
100
else if (PUBLIC_SCHEMA.equals(attr.getValue()))
101                                 ns_schema=name.substring(6); //NOI18N
102
}
103                     }
104                     return Enumerations.singleton (next);
105                 }
106             }
107         }
108
109         return null;
110     }
111
112     public java.beans.FeatureDescriptor JavaDoc getDescriptor() {
113         return new java.beans.FeatureDescriptor JavaDoc();
114     }
115
116     /** Returns pseudo DTD for code completion
117     */

118     public GrammarQuery getGrammar(GrammarEnvironment ctx) {
119         InputSource JavaDoc inputSource=null;
120         switch (schema_type) {
121             case TYPE_WSDL: {
122                 StringBuffer JavaDoc buffer=new StringBuffer JavaDoc();
123                 if (prefix.length()==0) {
124                     buffer.append("<!ENTITY % root_prefix ''>"); //NOI18N
125
} else {
126                     buffer.append("<!ENTITY % root_prefix '"+prefix+":'>"); //NOI18N
127
}
128                 buffer.append("<!ENTITY % wsdl '"+ns_wsdl+"'>"); //NOI18N
129
buffer.append("<!ENTITY % soap '"+ns_soap+"'>"); //NOI18N
130
buffer.append("<!ENTITY % soapenc '"+ns_soapenc+"'>"); //NOI18N
131
buffer.append("<!ENTITY % wsdl_prefix '"+ns_wsdl+":'>"); //NOI18N
132
buffer.append("<!ENTITY % soap_prefix '"+ns_soap+":'>"); //NOI18N
133
buffer.append("<!ENTITY % p '"+ns_schema+":'><!ENTITY % s ':"+ns_schema+"'>"); //NOI18N
134
InputStream is = getClass().getResourceAsStream("/org/netbeans/modules/websvc/wsdl/resources/wsdl-soap.dtd"); //NOI18N
135
BufferedReader br = new BufferedReader(new InputStreamReader(is));
136                 String JavaDoc line = null;
137                 try {
138                     while ((line=br.readLine())!=null) {
139                         buffer.append(line);
140                     }
141                     br.close();
142                 } catch (IOException ex) {
143                     return null;
144                 }
145                 inputSource = new InputSource JavaDoc(new StringReader(buffer.toString()));
146                 inputSource.setSystemId("nbres:/org/netbeans/modules/websvc/wsdl/resources/wsdl-soap.dtd"); //NOI18N
147
} break;
148             case TYPE_JAXRPC_CONFIG: {
149                 inputSource = new InputSource JavaDoc("nbres:/org/netbeans/modules/websvc/wsdl/config/resources/jax-rpc-ri-config_1_1.dtd"); //NOI18N
150
}break;
151         }
152         if (inputSource!=null) {
153             return DTDUtil.parseDTD(true, inputSource);
154         }
155         return null;
156     }
157 }
158
Popular Tags