KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > js > JavaQNameImpl


1 /*
2  * Copyright 2003, 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  */

17 package org.apache.ws.jaxme.js;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22
23
24 /** <p>Accessor class for JavaQName.</p>
25  *
26  *
27  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
28  * @version $Id: JavaQNameImpl.java,v 1.4 2005/05/11 10:41:49 mac Exp $
29  */

30 public class JavaQNameImpl {
31   private static final Map JavaDoc names = new HashMap JavaDoc();
32   private abstract static class DefaultImpl implements JavaQName {
33     public int compareTo(Object JavaDoc pOther) {
34       JavaQName other = (JavaQName) pOther;
35       if (isArray()) {
36         if (!other.isArray()) {
37           return -1;
38         } else {
39           return getInstanceClass().compareTo(other.getInstanceClass());
40         }
41       } else {
42         if (other.isArray()) {
43           return 1;
44         }
45       }
46       int result = getPackageName().compareTo(other.getPackageName());
47       if (result != 0) {
48         return result;
49       }
50       result = getClassName().compareTo(other.getClassName());
51       if (result != 0) {
52         return result;
53       }
54       return 0;
55     }
56     public int hashCode() {
57       return getPackageName().hashCode() + getClassName().hashCode();
58     }
59     public boolean equals(Object JavaDoc pOther) {
60       if (pOther == null || !(pOther instanceof JavaQName)) { return false; }
61       return compareTo(pOther) == 0;
62     }
63   }
64
65
66   private static class StandardImpl extends DefaultImpl {
67     private String JavaDoc packageName, className;
68     public StandardImpl(String JavaDoc pPackageName, String JavaDoc pClassName) {
69       checkPackageName(pPackageName);
70       packageName = pPackageName;
71       className = pClassName;
72     }
73     protected void checkPackageName(String JavaDoc pPackageName) {
74       for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(pPackageName, ".");
75            st.hasMoreTokens(); ) {
76         String JavaDoc tok = st.nextToken();
77         if (tok.length() == 0) {
78           throw new IllegalArgumentException JavaDoc("Invalid package name: " + pPackageName);
79         }
80         for (int i = 0; i < tok.length(); i++) {
81           char c = tok.charAt(i);
82           if ((i == 0 && !Character.isJavaIdentifierStart(c)) ||
83               (i > 0 && !Character.isJavaIdentifierPart(c))) {
84             throw new IllegalArgumentException JavaDoc("Invalid package name: " + pPackageName);
85           }
86         }
87       }
88     }
89     public String JavaDoc getPackageName() { return packageName; }
90     public String JavaDoc getClassName() { return className; }
91     public boolean isArray() { return false; }
92     public JavaQName getInstanceClass() {
93       throw new IllegalStateException JavaDoc("The class " + this + "is not an array class.");
94     }
95     public boolean isImportable() { return true; }
96     public boolean isPrimitive() { return false; }
97     public String JavaDoc toString() {
98       String JavaDoc c = getClassName();
99       String JavaDoc p = getPackageName();
100       return p.length() > 0 ? (p + "." + c) : c;
101     }
102     public String JavaDoc getOuterClassName() {
103       int offset = className.lastIndexOf('$');
104       if (offset == -1) {
105         return null;
106       } else {
107         return className.substring(0, offset);
108       }
109     }
110     public String JavaDoc getInnerClassName() {
111       int offset = className.lastIndexOf('$');
112       if (offset == -1) {
113         return className;
114       } else {
115         return className.substring(offset+1);
116       }
117     }
118     public boolean isInnerClass() { return className.indexOf('$') > 0; }
119     public JavaQName getObjectType() { return null; }
120     public String JavaDoc getPrimitiveConversionMethod() { return null; }
121   }
122
123   private static class PrimitiveImpl extends StandardImpl {
124     private final JavaQName objectType;
125     private final String JavaDoc primitiveConversionMethod;
126     public PrimitiveImpl(String JavaDoc pClassName, JavaQName pObjectType, String JavaDoc pConversionMethod) {
127       super("", pClassName);
128       objectType = pObjectType;
129       primitiveConversionMethod = pConversionMethod;
130     }
131     public boolean isImportable() { return false; }
132     public boolean isPrimitive() { return true; }
133     public void checkPackageName() {}
134     public String JavaDoc toString() { return getClassName(); }
135     public JavaQName getObjectType() { return objectType; }
136     public String JavaDoc getPrimitiveConversionMethod() { return primitiveConversionMethod; }
137   }
138
139   private static class ArrayImpl extends DefaultImpl {
140     private JavaQName qName;
141     public ArrayImpl(JavaQName pQName) {
142       qName = pQName;
143     }
144     public String JavaDoc getPackageName() { return qName.getPackageName(); }
145     public String JavaDoc getClassName() { return qName.getClassName(); }
146     public boolean isArray() { return true; }
147     public boolean isImportable() { return false; }
148     public boolean isPrimitive() { return false; }
149     public JavaQName getInstanceClass() { return qName; }
150     public String JavaDoc toString() { return qName.toString() + "[]"; }
151     public String JavaDoc getOuterClassName() { return null; }
152     public String JavaDoc getInnerClassName() { return qName.getClassName(); }
153     public boolean isInnerClass() { return false; }
154     public JavaQName getObjectType() { return null; }
155     public String JavaDoc getPrimitiveConversionMethod() { return null; }
156   }
157
158   /** <p>For use in return types or method parameters: The
159    * void type.</p>
160    */

161   public static final JavaQName VOID = new PrimitiveImpl(void.class.getName(), null, null);
162   /** <p>For use in return types or method parameters: The
163    * boolean type.</p>
164    */

165   public static final JavaQName BOOLEAN = new PrimitiveImpl(boolean.class.getName(), JavaQNameImpl.getInstance(Boolean JavaDoc.class), "booleanValue()");
166   /** <p>For use in return types or method parameters: The
167    * byte type.</p>
168    */

169   public static final JavaQName BYTE = new PrimitiveImpl(byte.class.getName(), JavaQNameImpl.getInstance(Byte JavaDoc.class), "byteValue");
170   /** <p>For use in return types or method parameters: The
171    * short type.</p>
172    */

173   public static final JavaQName SHORT = new PrimitiveImpl(short.class.getName(), JavaQNameImpl.getInstance(Short JavaDoc.class), "shortValue");
174   /** <p>For use in return types or method parameters: The
175    * int type.</p>
176    */

177   public static final JavaQName INT = new PrimitiveImpl(int.class.getName(), JavaQNameImpl.getInstance(Integer JavaDoc.class), "intValue");
178   /** <p>For use in return types or method parameters: The
179    * long type.</p>
180    */

181   public static final JavaQName LONG = new PrimitiveImpl(long.class.getName(), JavaQNameImpl.getInstance(Long JavaDoc.class), "longValue");
182   /** <p>For use in return types or method parameters: The
183    * float type.</p>
184    */

185   public static final JavaQName FLOAT = new PrimitiveImpl(float.class.getName(), JavaQNameImpl.getInstance(Float JavaDoc.class), "floatValue");
186   /** <p>For use in return types or method parameters: The
187    * double type.</p>
188    */

189   public static final JavaQName DOUBLE = new PrimitiveImpl(double.class.getName(), JavaQNameImpl.getInstance(Double JavaDoc.class), "doubleValue");
190   /** <p>For use in return types or method parameters: The
191    * char type.</p>
192    */

193   public static final JavaQName CHAR = new PrimitiveImpl(char.class.getName(), JavaQNameImpl.getInstance(Character JavaDoc.class), "charValue");
194
195   private static final JavaQName[] primitives = new JavaQName[]{
196     VOID, BOOLEAN, BYTE, SHORT, INT, LONG, FLOAT, DOUBLE, CHAR
197   };
198   private static final Class JavaDoc[] primitiveClasses = new Class JavaDoc[]{
199     void.class, boolean.class, byte.class, short.class, int.class,
200     long.class, float.class, double.class, char.class
201   };
202
203   public static JavaQName getInstance(Class JavaDoc pClass) {
204     if (pClass.isArray()) {
205         return getArray(getInstance(pClass.getComponentType()));
206     }
207     if (pClass.isPrimitive()) {
208       for (int i = 0; i < primitives.length; i++) {
209         if (primitiveClasses[i].equals(pClass)) {
210           return primitives[i];
211         }
212       }
213       throw new IllegalArgumentException JavaDoc("Unknown primitive type: " + pClass.getClass().getName());
214     }
215     if (void.class.equals(pClass)) {
216       return VOID;
217     }
218
219     String JavaDoc name = pClass.getName();
220     int offset = name.lastIndexOf('.');
221     if (offset == -1) {
222       return getInstance(null, name);
223     } else {
224       return getInstance(name.substring(0, offset),
225                           name.substring(offset+1));
226     }
227   }
228
229   public static JavaQName getInstance(String JavaDoc pPackageName, String JavaDoc pClassName) {
230     if (pClassName == null || pClassName.length() == 0) {
231       throw new NullPointerException JavaDoc("The class name must not be null or empty.");
232     }
233     String JavaDoc name;
234     if (pPackageName == null || pPackageName.length() == 0) {
235       for (int i = 0; i < primitives.length; i++) {
236         if (primitiveClasses[i].getName().equals(pClassName)) {
237           return primitives[i];
238         }
239       }
240       name = pClassName;
241       pPackageName = "";
242     } else {
243       name = pPackageName + "." + pClassName;
244     }
245     JavaQName result;
246     synchronized (names) {
247       result = (JavaQName) names.get(name);
248       if (result == null) {
249         if (pClassName.endsWith("[]")) {
250           String JavaDoc instanceClassName = pClassName.substring(0, pClassName.length()-2);
251           JavaQName instanceClass = getInstance(pPackageName, instanceClassName);
252           result = new ArrayImpl(instanceClass);
253         } else {
254           result = new StandardImpl(pPackageName, pClassName);
255         }
256         names.put(name, result);
257       }
258     }
259     return result;
260   }
261
262   /** <p>Equivalent to <code>getInstance(pClassName, false)</code>.</p>
263    */

264   public static JavaQName getInstance(String JavaDoc pClassName) {
265     return getInstance(null, pClassName);
266   }
267
268   /** <p>Returns an instance with the given class name.</p>
269    * @param pClassName The class name
270    * @param pAssumePackage True, if the class name is assumed to contain a
271    * package prefix, in which case the prefix up to the last dot will be
272    * the package name and the suffix following the last dot will be the
273    * class name. Otherwise the root package is assumed.
274    */

275   public static JavaQName getInstance(String JavaDoc pClassName, boolean pAssumePackage) {
276      if (!pAssumePackage) {
277         return getInstance(pClassName);
278      } else {
279         int offset = pClassName.lastIndexOf('.');
280         if (offset == -1) {
281             return getInstance(pClassName);
282         } else {
283             return getInstance(pClassName.substring(0, offset), pClassName.substring(offset+1));
284         }
285      }
286   }
287
288   public static JavaQName getArray(JavaQName pQName) {
289     String JavaDoc name = pQName.toString() + "[]";
290     JavaQName result;
291     synchronized (names) {
292       result = (JavaQName) names.get(name);
293       if (result == null) {
294         result = new ArrayImpl(pQName);
295         names.put(name, result);
296       }
297     }
298     return result;
299   }
300
301   /** <p>Returns the fully qualified name of an inner class of
302    * <code>pQName</code> with the name <code>pInnerClassName</code>.</p>
303    */

304   public static JavaQName getInnerInstance(JavaQName pQName, String JavaDoc pInnerClassName) {
305      return getInstance(pQName.getPackageName(), pQName.getClassName() + "$" + pInnerClassName);
306   }
307 }
308
Popular Tags