KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > wsdl > fromJava > Namespaces


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis.wsdl.fromJava;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24
25 /**
26  * <p>Description: A HashMap of packageNames and namespaces with some helper methods </p>
27  *
28  * @author rkumar@borland.com
29  */

30 public class Namespaces extends HashMap JavaDoc {
31
32     /** Field prefixCount */
33     private int prefixCount = 1;
34
35     /** Field namespacePrefixMap */
36     private HashMap JavaDoc namespacePrefixMap = null;
37
38     /**
39      * Constructor Namespaces
40      */

41     public Namespaces() {
42
43         super();
44
45         namespacePrefixMap = new HashMap JavaDoc();
46     }
47
48     /**
49      * Get the namespaace for the given package If there is no entry in the HashMap for
50      * this namespace, create one.
51      *
52      * @param key String representing packagename
53      * @return the namespace either created or existing
54      */

55     public String JavaDoc getCreate(String JavaDoc key) {
56
57         Object JavaDoc value = super.get(key);
58
59         if (value == null) {
60             value = makeNamespaceFromPackageName(key);
61
62             put(key, value, null);
63         }
64
65         return (String JavaDoc) value;
66     }
67
68     /**
69      * Get the namespaace for the given package If there is no entry in the HashMap for
70      * this namespace, create one.
71      *
72      * @param key String representing packagename
73      * @param prefix the prefix to use for the generated namespace
74      * @return the namespace either created or existing
75      */

76     public String JavaDoc getCreate(String JavaDoc key, String JavaDoc prefix) {
77
78         Object JavaDoc value = super.get(key);
79
80         if (value == null) {
81             value = makeNamespaceFromPackageName(key);
82
83             put(key, value, prefix);
84         }
85
86         return (String JavaDoc) value;
87     }
88
89     /**
90      * adds an entry to the packagename/namespace HashMap. In addition,
91      * also makes an entry in the auxillary namespace/prefix HashMap if an
92      * entry doesn't already exists
93      *
94      * @param key packageName String
95      * @param value namespace value
96      * @param prefix the prefix to use for the given namespace
97      * @return old value for the specified key
98      */

99     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value, String JavaDoc prefix) {
100
101         if (prefix != null) {
102             namespacePrefixMap.put(value, prefix);
103         } else {
104             getCreatePrefix((String JavaDoc) value);
105         }
106
107         return super.put(key, value);
108     }
109
110     /**
111      * adds an entry to the packagename/namespace HashMap
112      * for each of the entry in the map. In addition, also add an entries in the
113      * auxillary namespace/prefix HashMap
114      *
115      * @param map packageName/namespace map
116      */

117     public void putAll(Map JavaDoc map) {
118
119         Iterator JavaDoc i = map.entrySet().iterator();
120
121         while (i.hasNext()) {
122             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
123
124             put(entry.getKey(), entry.getValue(), null);
125         }
126     }
127
128     /**
129      * Get the prefix for the given namespace. If one exists, create one
130      *
131      * @param namespace namespace
132      * @return prefix String
133      */

134     public String JavaDoc getCreatePrefix(String JavaDoc namespace) {
135
136         if (namespacePrefixMap.get(namespace) == null) {
137             namespacePrefixMap.put(namespace, "tns" + prefixCount++);
138         }
139
140         return (String JavaDoc) namespacePrefixMap.get(namespace);
141     }
142
143     /**
144      * put the gine namespace / prefix into the appropriate HashMap
145      *
146      * @param namespace
147      * @param prefix
148      */

149     public void putPrefix(String JavaDoc namespace, String JavaDoc prefix) {
150         namespacePrefixMap.put(namespace, prefix);
151     }
152
153     /**
154      * adds an entry to the namespace / prefix HashMap
155      * for each of the entry in the map.
156      *
157      * @param map packageName/namespace map
158      */

159     public void putAllPrefix(Map JavaDoc map) {
160
161         Iterator JavaDoc i = map.entrySet().iterator();
162
163         while (i.hasNext()) {
164             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
165
166             put(entry.getKey(), entry.getValue());
167         }
168     }
169
170     /**
171      * Make namespace from a fully qualified class name
172      * use the default protocol for the namespace
173      *
174      * @param clsName fully qualified class name
175      * @return namespace namespace String
176      */

177     public static String JavaDoc makeNamespace(String JavaDoc clsName) {
178         return makeNamespace(clsName, "http");
179     }
180
181     /**
182      * Make namespace from a fully qualified class name
183      * and the given protocol
184      *
185      * @param clsName fully qualified class name
186      * @param protocol protocol String
187      * @return namespace namespace String
188      */

189     public static String JavaDoc makeNamespace(String JavaDoc clsName, String JavaDoc protocol) {
190
191         if (clsName.startsWith("[L")) {
192             clsName = clsName.substring(2, clsName.length() - 1);
193         }
194
195         if (clsName.lastIndexOf('.') == -1) {
196             return protocol + "://" + "DefaultNamespace";
197         }
198
199         String JavaDoc packageName = clsName.substring(0, clsName.lastIndexOf('.'));
200
201         return makeNamespaceFromPackageName(packageName, protocol);
202     }
203
204     /**
205      * Reverse the process. Get the package name from the namespace.
206      * @param namespace
207      * @return
208      */

209     public static String JavaDoc getPackage(String JavaDoc namespace) {
210         try {
211             URL JavaDoc url = new URL JavaDoc(namespace);
212             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(url.getHost(), ".");
213             String JavaDoc[] words = new String JavaDoc[st.countTokens()];
214             for (int i = 0; i < words.length; ++i) {
215                 words[i] = st.nextToken();
216             }
217             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(80);
218             for (int i = words.length - 1; i >= 0; --i) {
219                 String JavaDoc word = words[i];
220                 // seperate with dot
221
if (i != words.length - 1) {
222                     sb.append('.');
223                 }
224                 sb.append(word);
225             }
226             String JavaDoc pkg = sb.toString();
227             if (pkg.equals("DefaultNamespace")) {
228                 return "";
229             }
230             return pkg;
231         } catch (MalformedURLException JavaDoc e) {
232         }
233         return null;
234     }
235     
236     /**
237      * Method makeNamespaceFromPackageName
238      *
239      * @param packageName
240      * @return
241      */

242     private static String JavaDoc makeNamespaceFromPackageName(String JavaDoc packageName) {
243         return makeNamespaceFromPackageName(packageName, "http");
244     }
245
246     /**
247      * Method makeNamespaceFromPackageName
248      *
249      * @param packageName
250      * @param protocol
251      * @return
252      */

253     private static String JavaDoc makeNamespaceFromPackageName(String JavaDoc packageName,
254                                                        String JavaDoc protocol) {
255
256         if ((packageName == null) || packageName.equals("")) {
257             return protocol + "://" + "DefaultNamespace";
258         }
259
260         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(packageName, ".");
261         String JavaDoc[] words = new String JavaDoc[st.countTokens()];
262
263         for (int i = 0; i < words.length; ++i) {
264             words[i] = st.nextToken();
265         }
266
267         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(80);
268
269         for (int i = words.length - 1; i >= 0; --i) {
270             String JavaDoc word = words[i];
271
272             // seperate with dot
273
if (i != words.length - 1) {
274                 sb.append('.');
275             }
276
277             sb.append(word);
278         }
279
280         return protocol + "://" + sb.toString();
281     }
282
283     /**
284      * Get the list of namespaces currently registered
285      *
286      * @return iterator
287      */

288     public Iterator JavaDoc getNamespaces() {
289         return namespacePrefixMap.keySet().iterator();
290     }
291 }
292
Popular Tags