Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/axis/wsdl/toJava/Namespaces.java


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;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.Map;
24  import java.util.StringTokenizer;
25  import java.util.Vector;
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 {
32  
33      /** Field root */
34      private String root;
35  
36      /** Field defaultPackage */
37      private String 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 pkg2NamespacesMap = new HashMap();
47  
48      /**
49       * Method normalizePackageName
50       * 
51       * @param pkg       
52       * @param separator 
53       * @return 
54       */
55      private static String normalizePackageName(String 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 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 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 getCreate(String 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 getCreate(String key, boolean create) {
118 
119         if (defaultPackage != null) {
120             put(key, defaultPackage);
121             return defaultPackage;
122         }
123 
124         String value = (String) super.get(key);
125 
126         if ((value == null) && create) {
127             value = normalizePackageName((String) Utils.makePackageName(key),
128                     javaPkgSeparator);
129 
130             put(key, value);
131         }
132 
133         return (String) 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 getAsDir(String key) {
144 
145         if (defaultPackage != null) {
146             return toDir(defaultPackage);
147         }
148 
149         String pkg = (String) 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 toDir(String pkg) {
162 
163         String 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 map) {
187 
188         Iterator i = map.entrySet().iterator();
189 
190         while (i.hasNext()) {
191             Map.Entry entry = (Map.Entry) i.next();
192             Object key = entry.getKey();
193             String pkg = (String) 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 javify(String pkg) {
209 
210         StringTokenizer st = new StringTokenizer(pkg, ".");
211 
212         pkg = "";
213 
214         while (st.hasMoreTokens()) {
215             String 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 pkg) {
237 
238         String pkgDirString = toDir(pkg);
239         File packageDir = new File(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 defaultPackage) {
250         this.defaultPackage = defaultPackage;
251     }
252     
253     public Object put(Object key, Object value) {
254         // Store pakcage->namespaces vector mapping
255         Vector v = null;
256         if (!pkg2NamespacesMap.containsKey(value)) {
257             v = new Vector();                       
258         } else {
259             v = (Vector)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 getPkg2NamespacesMap() {
271         return pkg2NamespacesMap;
272     }
273 }    // class Namespaces