KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > wsdl > toJava > 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.toJava;
56
57 import org.jboss.axis.utils.JavaUtils;
58
59 import java.io.File JavaDoc;
60 import java.util.HashMap JavaDoc;
61 import java.util.Iterator JavaDoc;
62 import java.util.Map JavaDoc;
63 import java.util.StringTokenizer JavaDoc;
64
65 /**
66  * This class is essentially a HashMap of <namespace, package name> pairs with
67  * a few extra wizzbangs.
68  */

69 public class Namespaces extends HashMap JavaDoc
70 {
71    private String JavaDoc root;
72    private String JavaDoc defaultPackage = null;
73
74    /**
75     * Toknens in a namespace that are treated as package name part separators.
76     */

77    private static final char[] pkgSeparators = {'.', ':'};
78    private static final char javaPkgSeparator = pkgSeparators[0];
79
80    private static String JavaDoc normalizePackageName(String JavaDoc pkg, char separator)
81    {
82       for (int i = 0; i < pkgSeparators.length; i++)
83          pkg = pkg.replace(pkgSeparators[i], separator);
84       return pkg;
85    }
86
87    /**
88     * Instantiate a Namespaces object whose packages will all reside under root.
89     */

90    public Namespaces(String JavaDoc root)
91    {
92       super();
93       this.root = root;
94    } // ctor
95

96    /**
97     * Instantiate a clone of an existing Namespaces object.
98     */

99    private Namespaces(Namespaces clone)
100    {
101       super(clone);
102       this.root = clone.root;
103       this.defaultPackage = clone.defaultPackage;
104    } // ctor
105

106    /**
107     * Instantiate a clone of this Namespaces object.
108     */

109    public Object JavaDoc clone()
110    {
111       return new Namespaces(this);
112    } // clone
113

114    /**
115     * Get the package name for the given namespace. If there is no entry in the HashMap for
116     * this namespace, create one.
117     */

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

134    /**
135     * Get the package name in directory format (dots replaced by slashes). If the package name
136     * doesn't exist in the HashMap, return "".
137     */

138    public String JavaDoc getAsDir(String JavaDoc key)
139    {
140       if (defaultPackage != null)
141       {
142          return toDir(defaultPackage);
143       }
144       String JavaDoc pkg = (String JavaDoc)get(key);
145       return toDir(pkg);
146    } // getAsDir
147

148    /**
149     * Return the given package name in directory format (dots replaced by slashes). If pkg is null,
150     * "" is returned.
151     */

152    public String JavaDoc toDir(String JavaDoc pkg)
153    {
154       String JavaDoc dir = null;
155       if (pkg != null)
156       {
157          pkg = normalizePackageName(pkg, File.separatorChar);
158       }
159       if (root == null)
160       {
161          dir = pkg;
162       }
163       else
164       {
165          dir = root + File.separatorChar + pkg;
166       }
167
168       return dir == null ? "" : dir + File.separatorChar;
169    } // toDir
170

171    /**
172     * Like HashMap's putAll, this adds the given map's contents to this map. But it
173     * also makes sure the value strings are javified.
174     */

175    public void putAll(Map JavaDoc map)
176    {
177       Iterator JavaDoc i = map.entrySet().iterator();
178       while (i.hasNext())
179       {
180          Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
181          Object JavaDoc key = entry.getKey();
182          String JavaDoc pkg = (String JavaDoc)entry.getValue();
183          pkg = javify(pkg);
184          put(key, pkg);
185       }
186    } // putAll
187

188    /**
189     * Make sure each package name doesn't conflict with a Java keyword.
190     * Ie., org.apache.import.test becomes org.apache.import_.test.
191     */

192    private String JavaDoc javify(String JavaDoc pkg)
193    {
194       StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(pkg, ".");
195       pkg = "";
196       while (st.hasMoreTokens())
197       {
198          String JavaDoc token = st.nextToken();
199          if (JavaUtils.isJavaKeyword(token))
200          {
201             token = JavaUtils.makeNonJavaKeyword(token);
202          }
203          pkg = pkg + token;
204          if (st.hasMoreTokens())
205          {
206             pkg = pkg + '.';
207          }
208       }
209       return pkg;
210    } // javify
211

212    /**
213     * Make a directory for the given package under root.
214     */

215    public void mkdir(String JavaDoc pkg)
216    {
217       String JavaDoc pkgDirString = toDir(pkg);
218       File JavaDoc packageDir = new File JavaDoc(pkgDirString);
219       packageDir.mkdirs();
220    } // mkdir
221

222    /**
223     * Set a package name that overrides the namespace map
224     *
225     * @param defaultPackage a java package name (e.g. com.foo)
226     */

227    public void setDefaultPackage(String JavaDoc defaultPackage)
228    {
229       this.defaultPackage = defaultPackage;
230    }
231
232 } // class Namespaces
233
Popular Tags