KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > wsdl > toJava > 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.toJava;
17
18 import org.apache.axis.utils.JavaUtils;
19
20 import java.io.File JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 /**
28  * This class is essentially a HashMap of <namespace, package name> pairs with
29  * a few extra wizzbangs.
30  */

31 public class Namespaces extends HashMap JavaDoc {
32
33     /** Field root */
34     private String JavaDoc root;
35
36     /** Field defaultPackage */
37     private String JavaDoc defaultPackage = null;
38
39     /** Toknens in a namespace that are treated as package name part separators. */
40     private static final char[] pkgSeparators = {'.', ':'};
41
42     /** Field javaPkgSeparator */
43     private static final char javaPkgSeparator = pkgSeparators[0];
44     
45     /** Field pkg2Namespaces : reverse mapping of Namespaces */
46     private Map JavaDoc pkg2NamespacesMap = new HashMap JavaDoc();
47
48     /**
49      * Method normalizePackageName
50      *
51      * @param pkg
52      * @param separator
53      * @return
54      */

55     private static String JavaDoc normalizePackageName(String JavaDoc pkg, char separator) {
56
57         for (int i = 0; i < pkgSeparators.length; i++) {
58             pkg = pkg.replace(pkgSeparators[i], separator);
59         }
60
61         return pkg;
62     }
63
64     /**
65      * Instantiate a Namespaces object whose packages will all reside under root.
66      *
67      * @param root
68      */

69     public Namespaces(String JavaDoc root) {
70
71         super();
72
73         this.root = root;
74     } // ctor
75

76     /**
77      * Instantiate a clone of an existing Namespaces object.
78      *
79      * @param clone
80      */

81     private Namespaces(Namespaces clone) {
82
83         super(clone);
84
85         this.root = clone.root;
86         this.defaultPackage = clone.defaultPackage;
87     } // ctor
88

89     /**
90      * Instantiate a clone of this Namespaces object.
91      *
92      * @return
93      */

94     public Object JavaDoc clone() {
95         return new Namespaces(this);
96     } // clone
97

98     /**
99      * Get the package name for the given namespace. If there is no entry in the HashMap for
100      * this namespace, create one.
101      *
102      * @param key
103      * @return
104      */

105     public String JavaDoc getCreate(String JavaDoc key) {
106         return getCreate(key, true);
107     } // getCreate
108

109     /**
110      * Get the package name for the given namespace. If there is no entry in the HashMap for
111      * this namespace, create one if create flag is on, return <tt>null</tt> otherwise.
112      *
113      * @param key
114      * @param create
115      * @return
116      */

117     String JavaDoc getCreate(String JavaDoc key, boolean create) {
118
119         if (defaultPackage != null) {
120             put(key, defaultPackage);
121             return defaultPackage;
122         }
123
124         String JavaDoc value = (String JavaDoc) super.get(key);
125
126         if ((value == null) && create) {
127             value = normalizePackageName((String JavaDoc) Utils.makePackageName(key),
128                     javaPkgSeparator);
129
130             put(key, value);
131         }
132
133         return (String JavaDoc) value;
134     } // getCreate
135

136     /**
137      * Get the package name in directory format (dots replaced by slashes). If the package name
138      * doesn't exist in the HashMap, return "".
139      *
140      * @param key
141      * @return
142      */

143     public String JavaDoc getAsDir(String JavaDoc key) {
144
145         if (defaultPackage != null) {
146             return toDir(defaultPackage);
147         }
148
149         String JavaDoc pkg = (String JavaDoc) get(key);
150
151         return toDir(pkg);
152     } // getAsDir
153

154     /**
155      * Return the given package name in directory format (dots replaced by slashes). If pkg is null,
156      * "" is returned.
157      *
158      * @param pkg
159      * @return
160      */

161     public String JavaDoc toDir(String JavaDoc pkg) {
162
163         String JavaDoc dir = null;
164
165         if (pkg != null) {
166             pkg = normalizePackageName(pkg, File.separatorChar);
167         }
168
169         if (root == null) {
170             dir = pkg;
171         } else {
172             dir = root + File.separatorChar + pkg;
173         }
174
175         return (dir == null)
176                 ? ""
177                 : dir + File.separatorChar;
178     } // toDir
179

180     /**
181      * Like HashMap's putAll, this adds the given map's contents to this map. But it
182      * also makes sure the value strings are javified.
183      *
184      * @param map
185      */

186     public void putAll(Map JavaDoc map) {
187
188         Iterator JavaDoc i = map.entrySet().iterator();
189
190         while (i.hasNext()) {
191             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
192             Object JavaDoc key = entry.getKey();
193             String JavaDoc pkg = (String JavaDoc) entry.getValue();
194
195             pkg = javify(pkg);
196
197             put(key, pkg);
198         }
199     } // putAll
200

201     /**
202      * Make sure each package name doesn't conflict with a Java keyword.
203      * Ie., org.apache.import.test becomes org.apache.import_.test.
204      *
205      * @param pkg
206      * @return
207      */

208     private String JavaDoc javify(String JavaDoc pkg) {
209
210         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(pkg, ".");
211
212         pkg = "";
213
214         while (st.hasMoreTokens()) {
215             String JavaDoc token = st.nextToken();
216
217             if (JavaUtils.isJavaKeyword(token)) {
218                 token = JavaUtils.makeNonJavaKeyword(token);
219             }
220
221             pkg = pkg + token;
222
223             if (st.hasMoreTokens()) {
224                 pkg = pkg + '.';
225             }
226         }
227
228         return pkg;
229     } // javify
230

231     /**
232      * Make a directory for the given package under root.
233      *
234      * @param pkg
235      */

236     public void mkdir(String JavaDoc pkg) {
237
238         String JavaDoc pkgDirString = toDir(pkg);
239         File JavaDoc packageDir = new File JavaDoc(pkgDirString);
240
241         packageDir.mkdirs();
242     } // mkdir
243

244     /**
245      * Set a package name that overrides the namespace map
246      *
247      * @param defaultPackage a java package name (e.g. com.foo)
248      */

249     public void setDefaultPackage(String JavaDoc defaultPackage) {
250         this.defaultPackage = defaultPackage;
251     }
252     
253     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
254         // Store pakcage->namespaces vector mapping
255
Vector JavaDoc v = null;
256         if (!pkg2NamespacesMap.containsKey(value)) {
257             v = new Vector JavaDoc();
258         } else {
259             v = (Vector JavaDoc)pkg2NamespacesMap.get(value);
260         }
261         // NOT need to add an input key (namespace value) to v (package vector)
262
if (!v.contains(key)) {
263             v.add(key);
264         }
265         pkg2NamespacesMap.put(value, v);
266          
267         return super.put(key, value);
268     }
269     
270     public Map JavaDoc getPkg2NamespacesMap() {
271         return pkg2NamespacesMap;
272     }
273 } // class Namespaces
274
Popular Tags