KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 package org.jboss.axis.wsdl.fromJava;
56
57 import java.util.HashMap JavaDoc;
58 import java.util.Iterator JavaDoc;
59 import java.util.Map JavaDoc;
60 import java.util.StringTokenizer JavaDoc;
61
62 /**
63  * <p>Description: A HashMap of packageNames and namespaces with some helper methods </p>
64  *
65  * @author rkumar@borland.com
66  */

67 public class Namespaces extends HashMap JavaDoc
68 {
69    private int prefixCount = 1;
70    private HashMap JavaDoc namespacePrefixMap = null;
71
72    public Namespaces()
73    {
74       super();
75       namespacePrefixMap = new HashMap JavaDoc();
76    }
77
78    /**
79     * Get the namespaace for the given package If there is no entry in the HashMap for
80     * this namespace, create one.
81     *
82     * @param key String representing packagename
83     * @return the namespace either created or existing
84     */

85    public String JavaDoc getCreate(String JavaDoc key)
86    {
87       Object JavaDoc value = super.get(key);
88       if (value == null)
89       {
90          value = makeNamespaceFromPackageName(key);
91          put(key, value, null);
92       }
93       return (String JavaDoc)value;
94    }
95
96    /**
97     * Get the namespaace for the given package If there is no entry in the HashMap for
98     * this namespace, create one.
99     *
100     * @param key String representing packagename
101     * @param prefix the prefix to use for the generated namespace
102     * @return the namespace either created or existing
103     */

104    public String JavaDoc getCreate(String JavaDoc key, String JavaDoc prefix)
105    {
106       Object JavaDoc value = super.get(key);
107       if (value == null)
108       {
109          value = makeNamespaceFromPackageName(key);
110          put(key, value, prefix);
111       }
112       return (String JavaDoc)value;
113    }
114
115    /**
116     * adds an entry to the packagename/namespace HashMap. In addition,
117     * also makes an entry in the auxillary namespace/prefix HashMap if an
118     * entry doesn't already exists
119     *
120     * @param key packageName String
121     * @param value namespace value
122     * @param prefix the prefix to use for the given namespace
123     * @return old value for the specified key
124     */

125    public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value, String JavaDoc prefix)
126    {
127       if (prefix != null)
128          namespacePrefixMap.put(value, prefix);
129       else
130          getCreatePrefix((String JavaDoc)value);
131       return super.put(key, value);
132    }
133
134    /**
135     * adds an entry to the packagename/namespace HashMap
136     * for each of the entry in the map. In addition, also add an entries in the
137     * auxillary namespace/prefix HashMap
138     *
139     * @param map packageName/namespace map
140     */

141    public void putAll(Map JavaDoc map)
142    {
143       Iterator JavaDoc i = map.entrySet().iterator();
144       while (i.hasNext())
145       {
146          Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
147          put(entry.getKey(), entry.getValue(), null);
148       }
149    }
150
151    /**
152     * Get the prefix for the given namespace. If one exists, create one
153     *
154     * @param namespace namespace
155     * @return prefix String
156     */

157    public String JavaDoc getCreatePrefix(String JavaDoc namespace)
158    {
159       if (namespacePrefixMap.get(namespace) == null)
160       {
161          namespacePrefixMap.put(namespace, "tns" + prefixCount++);
162       }
163       return (String JavaDoc)namespacePrefixMap.get(namespace);
164    }
165
166    /**
167     * put the gine namespace / prefix into the appropriate HashMap
168     *
169     * @param namespace
170     * @param prefix
171     */

172    public void putPrefix(String JavaDoc namespace, String JavaDoc prefix)
173    {
174       namespacePrefixMap.put(namespace, prefix);
175    }
176
177    /**
178     * adds an entry to the namespace / prefix HashMap
179     * for each of the entry in the map.
180     *
181     * @param map packageName/namespace map
182     */

183    public void putAllPrefix(Map JavaDoc map)
184    {
185       Iterator JavaDoc i = map.entrySet().iterator();
186       while (i.hasNext())
187       {
188          Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
189          put(entry.getKey(), entry.getValue());
190       }
191    }
192
193    /**
194     * Make namespace from a fully qualified class name
195     * use the default protocol for the namespace
196     *
197     * @param clsName fully qualified class name
198     * @return namespace namespace String
199     */

200    public static String JavaDoc makeNamespace(String JavaDoc clsName)
201    {
202       return makeNamespace(clsName, "http");
203    }
204
205    /**
206     * Make namespace from a fully qualified class name
207     * and the given protocol
208     *
209     * @param clsName fully qualified class name
210     * @param protocol protocol String
211     * @return namespace namespace String
212     */

213    public static String JavaDoc makeNamespace(String JavaDoc clsName, String JavaDoc protocol)
214    {
215       if (clsName.lastIndexOf('.') == -1)
216          return protocol + "://" + "DefaultNamespace";
217       String JavaDoc packageName = clsName.substring(0, clsName.lastIndexOf('.'));
218       return makeNamespaceFromPackageName(packageName, protocol);
219    }
220
221    private static String JavaDoc makeNamespaceFromPackageName(String JavaDoc packageName)
222    {
223       return makeNamespaceFromPackageName(packageName, "http");
224    }
225
226
227    private static String JavaDoc makeNamespaceFromPackageName(String JavaDoc packageName, String JavaDoc protocol)
228    {
229       if (packageName == null || packageName.equals(""))
230          return protocol + "://" + "DefaultNamespace";
231       StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(packageName, ".");
232       String JavaDoc[] words = new String JavaDoc[st.countTokens()];
233       for (int i = 0; i < words.length; ++i)
234          words[i] = st.nextToken();
235
236       StringBuffer JavaDoc sb = new StringBuffer JavaDoc(80);
237       for (int i = words.length - 1; i >= 0; --i)
238       {
239          String JavaDoc word = words[i];
240          // seperate with dot
241
if (i != words.length - 1)
242             sb.append('.');
243          sb.append(word);
244       }
245       return protocol + "://" + sb.toString();
246    }
247
248    /**
249     * Get the list of namespaces currently registered
250     *
251     * @return iterator
252     */

253    public Iterator JavaDoc getNamespaces()
254    {
255       return namespacePrefixMap.keySet().iterator();
256    }
257 }
258
Popular Tags