1 17 package org.apache.ws.jaxme.js; 18 19 import java.util.HashMap ; 20 import java.util.Map ; 21 import java.util.StringTokenizer ; 22 23 24 30 public class JavaQNameImpl { 31 private static final Map names = new HashMap (); 32 private abstract static class DefaultImpl implements JavaQName { 33 public int compareTo(Object 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 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 packageName, className; 68 public StandardImpl(String pPackageName, String pClassName) { 69 checkPackageName(pPackageName); 70 packageName = pPackageName; 71 className = pClassName; 72 } 73 protected void checkPackageName(String pPackageName) { 74 for (StringTokenizer st = new StringTokenizer (pPackageName, "."); 75 st.hasMoreTokens(); ) { 76 String tok = st.nextToken(); 77 if (tok.length() == 0) { 78 throw new IllegalArgumentException ("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 ("Invalid package name: " + pPackageName); 85 } 86 } 87 } 88 } 89 public String getPackageName() { return packageName; } 90 public String getClassName() { return className; } 91 public boolean isArray() { return false; } 92 public JavaQName getInstanceClass() { 93 throw new IllegalStateException ("The class " + this + "is not an array class."); 94 } 95 public boolean isImportable() { return true; } 96 public boolean isPrimitive() { return false; } 97 public String toString() { 98 String c = getClassName(); 99 String p = getPackageName(); 100 return p.length() > 0 ? (p + "." + c) : c; 101 } 102 public String 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 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 getPrimitiveConversionMethod() { return null; } 121 } 122 123 private static class PrimitiveImpl extends StandardImpl { 124 private final JavaQName objectType; 125 private final String primitiveConversionMethod; 126 public PrimitiveImpl(String pClassName, JavaQName pObjectType, String 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 toString() { return getClassName(); } 135 public JavaQName getObjectType() { return objectType; } 136 public String 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 getPackageName() { return qName.getPackageName(); } 145 public String 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 toString() { return qName.toString() + "[]"; } 151 public String getOuterClassName() { return null; } 152 public String getInnerClassName() { return qName.getClassName(); } 153 public boolean isInnerClass() { return false; } 154 public JavaQName getObjectType() { return null; } 155 public String getPrimitiveConversionMethod() { return null; } 156 } 157 158 161 public static final JavaQName VOID = new PrimitiveImpl(void.class.getName(), null, null); 162 165 public static final JavaQName BOOLEAN = new PrimitiveImpl(boolean.class.getName(), JavaQNameImpl.getInstance(Boolean .class), "booleanValue()"); 166 169 public static final JavaQName BYTE = new PrimitiveImpl(byte.class.getName(), JavaQNameImpl.getInstance(Byte .class), "byteValue"); 170 173 public static final JavaQName SHORT = new PrimitiveImpl(short.class.getName(), JavaQNameImpl.getInstance(Short .class), "shortValue"); 174 177 public static final JavaQName INT = new PrimitiveImpl(int.class.getName(), JavaQNameImpl.getInstance(Integer .class), "intValue"); 178 181 public static final JavaQName LONG = new PrimitiveImpl(long.class.getName(), JavaQNameImpl.getInstance(Long .class), "longValue"); 182 185 public static final JavaQName FLOAT = new PrimitiveImpl(float.class.getName(), JavaQNameImpl.getInstance(Float .class), "floatValue"); 186 189 public static final JavaQName DOUBLE = new PrimitiveImpl(double.class.getName(), JavaQNameImpl.getInstance(Double .class), "doubleValue"); 190 193 public static final JavaQName CHAR = new PrimitiveImpl(char.class.getName(), JavaQNameImpl.getInstance(Character .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 [] primitiveClasses = new Class []{ 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 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 ("Unknown primitive type: " + pClass.getClass().getName()); 214 } 215 if (void.class.equals(pClass)) { 216 return VOID; 217 } 218 219 String 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 pPackageName, String pClassName) { 230 if (pClassName == null || pClassName.length() == 0) { 231 throw new NullPointerException ("The class name must not be null or empty."); 232 } 233 String 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 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 264 public static JavaQName getInstance(String pClassName) { 265 return getInstance(null, pClassName); 266 } 267 268 275 public static JavaQName getInstance(String 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 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 304 public static JavaQName getInnerInstance(JavaQName pQName, String pInnerClassName) { 305 return getInstance(pQName.getPackageName(), pQName.getClassName() + "$" + pInnerClassName); 306 } 307 } 308 | Popular Tags |