1 19 20 package org.netbeans.modules.schema2beans; 21 22 import java.text.*; 23 import java.util.ResourceBundle ; 24 import java.util.Locale ; 25 import java.util.MissingResourceException ; 26 27 28 31 public class Common { 32 33 static public final int NONE = 0x00000; 35 36 static public final int MASK_USER = 0xFFFF; 37 static public final int USE_DEFAULT_VALUES = 0x0001; 38 static public final int NO_DEFAULT_VALUES = 0x0002; 39 40 static public final int MASK_SEQUENCE = 0x0000F; 41 static public final int SEQUENCE_AND = 0x00001; 42 static public final int SEQUENCE_OR = 0x00002; 43 44 static public final int MASK_INSTANCE = 0x000F0; 45 static public final int TYPE_0_1 = 0x00010; 46 static public final int TYPE_1 = 0x00020; 47 static public final int TYPE_0_N = 0x00030; 48 static public final int TYPE_1_N = 0x00040; 49 50 static public final int MASK_TYPE = 0x0FF00; 51 static public final int TYPE_STRING = 0x00100; 52 static public final int TYPE_BEAN = 0x00200; 53 static public final int TYPE_BOOLEAN = 0x00300; 54 static public final int TYPE_BYTE = 0x00400; 55 static public final int TYPE_CHAR = 0x00500; 56 static public final int TYPE_SHORT = 0x00600; 57 static public final int TYPE_INT = 0x00700; 58 static public final int TYPE_LONG = 0x00800; 59 static public final int TYPE_FLOAT = 0x00900; 60 static public final int TYPE_DOUBLE = 0x00a00; 61 static public final int TYPE_COMMENT = 0x00f00; 62 63 static public final int MASK_PROP = 0xF0000; 64 static public final int TYPE_KEY = 0x10000; 65 static public final int TYPE_SHOULD_NOT_BE_EMPTY = 0x20000; 66 67 static public final int TYPE_VETOABLE = 0x100000; 68 69 static public final int COMMENT = 0x01; 70 static public final int ELEMENT = 0x02; 71 static public final int ATTLIST = 0x03; 72 73 static public final String DTD_STRING = "#PCDATA"; static public final String DTD_EMPTY = "EMPTY"; 76 static public final String CLASS_STRING = "String"; static public final String CLASS_BOOLEAN = "Boolean"; 79 static public final String GENERATED_TAG = "Generated"; 80 81 82 public static boolean isSequenceOr(int type) { 83 return ((type & MASK_SEQUENCE) == SEQUENCE_OR); 84 } 85 86 public static boolean isArray(int type) { 87 int t = type & MASK_INSTANCE; 88 return (t == TYPE_0_N || t == TYPE_1_N); 89 } 90 91 public static boolean isBean(int type) { 92 return ((type & MASK_TYPE) == TYPE_BEAN); 93 } 94 95 public static boolean isString(int type) { 96 return ((type & MASK_TYPE) == TYPE_STRING); 97 } 98 99 public static boolean isBoolean(int type) { 100 return ((type & MASK_TYPE) == TYPE_BOOLEAN); 101 } 102 103 108 109 public static boolean isKey(int type) { 110 return ((type & TYPE_KEY) == TYPE_KEY); 111 } 112 113 public static boolean shouldNotBeEmpty(int type) { 114 return ((type & TYPE_SHOULD_NOT_BE_EMPTY) == TYPE_SHOULD_NOT_BE_EMPTY); 115 } 116 117 public static boolean isVetoable(int type) { 118 return ((type & TYPE_VETOABLE) == TYPE_VETOABLE); 119 } 120 121 124 public static boolean isScalar(int type) { 125 switch(type & MASK_TYPE) { 126 case TYPE_STRING: 127 case TYPE_BEAN: 128 case TYPE_COMMENT: 129 return false; 130 case TYPE_BOOLEAN: 131 case TYPE_BYTE: 132 case TYPE_CHAR: 133 case TYPE_SHORT: 134 case TYPE_INT: 135 case TYPE_LONG: 136 case TYPE_FLOAT: 137 case TYPE_DOUBLE: 138 return true; 139 default: 140 throw new IllegalArgumentException (Common.getMessage( 141 "UnknownType_msg", new Integer (type))); 142 } 143 } 144 145 public static String wrapperGetMethod(int type) { 146 switch(type & MASK_TYPE) { 147 case TYPE_BOOLEAN: 148 return "booleanValue"; case TYPE_BYTE: 150 return "byteValue"; case TYPE_CHAR: 152 return "charValue"; case TYPE_SHORT: 154 return "shortValue"; case TYPE_INT: 156 return "intValue"; case TYPE_LONG: 158 return "longValue"; case TYPE_FLOAT: 160 return "floatValue"; case TYPE_DOUBLE: 162 return "doubleValue"; default: 164 throw new IllegalArgumentException (Common.getMessage( 165 "UnknownType_msg", new Integer (type))); 166 } 167 } 168 169 public static String wrapperClass(int type) { 170 switch(type & MASK_TYPE) { 171 case TYPE_BOOLEAN: 172 return "Boolean"; case TYPE_BYTE: 174 return "Byte"; case TYPE_CHAR: 176 return "Character"; case TYPE_SHORT: 178 return "Short"; case TYPE_INT: 180 return "Integer"; case TYPE_LONG: 182 return "Long"; case TYPE_FLOAT: 184 return "Float"; case TYPE_DOUBLE: 186 return "Double"; default: 188 throw new IllegalArgumentException (Common.getMessage( 189 "UnknownType_msg", new Integer (type))); 190 } 191 } 192 193 public static int wrapperToType(String wrapper) { 194 if (wrapper == null) 195 return NONE; 196 String s = wrapper.trim(); 197 if (s.endsWith("boolean")) return TYPE_BOOLEAN; 199 if (s.endsWith("byte")) return TYPE_BYTE; 201 if (s.endsWith("char")) return TYPE_CHAR; 203 if (s.endsWith("short")) return TYPE_SHORT; 205 if (s.endsWith("int")) return TYPE_INT; 207 if (s.endsWith("long")) return TYPE_LONG; 209 if (s.endsWith("float")) return TYPE_FLOAT; 211 if (s.endsWith("double")) return TYPE_DOUBLE; 213 if (s.equals("String") || s.equals("java.lang.String")) 214 return TYPE_STRING; 215 return NONE; 217 } 218 219 public static String scalarType(int type) { 220 switch(type & MASK_TYPE) { 221 case TYPE_BOOLEAN: 222 return "boolean"; case TYPE_BYTE: 224 return "byte"; case TYPE_CHAR: 226 return "char"; case TYPE_SHORT: 228 return "short"; case TYPE_INT: 230 return "int"; case TYPE_LONG: 232 return "long"; case TYPE_FLOAT: 234 return "float"; case TYPE_DOUBLE: 236 return "double"; default: 238 throw new IllegalArgumentException (Common.getMessage( 239 "UnknownType_msg", new Integer (type))); 240 } 241 } 242 243 public static String typeToString(int type) { 244 switch(type & MASK_TYPE) { 245 case TYPE_STRING: 246 return "TYPE_STRING"; case TYPE_COMMENT: 248 return "TYPE_COMMENT"; 249 case TYPE_BEAN: 250 return "TYPE_BEAN"; case TYPE_BOOLEAN: 252 return "TYPE_BOOLEAN"; case TYPE_BYTE: 254 return "TYPE_BYTE"; case TYPE_CHAR: 256 return "TYPE_CHAR"; case TYPE_SHORT: 258 return "TYPE_SHORT"; case TYPE_INT: 260 return "TYPE_INT"; case TYPE_LONG: 262 return "TYPE_LONG"; case TYPE_FLOAT: 264 return "TYPE_FLOAT"; case TYPE_DOUBLE: 266 return "TYPE_DOUBLE"; default: 268 throw new IllegalArgumentException (Common.getMessage( 269 "UnknownType_msg", new Integer (type))); 270 } 271 } 272 273 public static String dumpHex(String v) { 274 String s; 275 276 if (v != null) { 277 s = "hex[ "; byte[] b = v.getBytes(); 279 for (int i=0; i<b.length; i++) 280 s += Integer.toHexString((int)b[i]) + " "; s += "]"; } 283 else 284 s = "<null>"; 286 return s; 287 } 288 289 public static String constName(String name) { 290 return name.replace('-', '_').replace('#', '_').replace('.', '_').replace(':', '_').toUpperCase(); 291 } 292 293 304 public static String convertName(String name) { 305 return convertName(name, true); 306 } 307 308 312 public static String convertNameInstance(String name) { 313 return convertName(name, false); 314 } 315 316 private static String convertName(String name, boolean up) { 317 CharacterIterator ci; 318 StringBuffer n = new StringBuffer (); 319 boolean keepCase = false; 320 char c; 321 322 ci = new StringCharacterIterator(name); 323 c = ci.first(); 324 325 while (c != CharacterIterator.DONE) { 327 if (Character.isLowerCase(c)) { 328 keepCase = true; 329 break; 330 } 331 c = ci.next(); 332 } 333 334 c = ci.first(); 335 while (c != CharacterIterator.DONE) { 336 if (c == '-' || c == '_' || !Character.isJavaIdentifierPart(c)) 337 up = true; 338 else { 339 if (up) 340 c = Character.toUpperCase(c); 341 else 342 if (!keepCase) 343 c = Character.toLowerCase(c); 344 n.append(c); 345 up = false; 346 } 347 c = ci.next(); 348 } 349 return n.toString(); 350 } 351 352 359 public static Object getComparableObject(Object obj) { 360 Object ret = obj; 361 if (obj instanceof java.lang.String ) { 362 String s = (String )obj; 363 ret = s.trim(); 364 } 365 366 return ret; 367 } 368 369 public static Object defaultScalarValue(int type) { 370 switch(type & Common.MASK_TYPE) { 371 case Common.TYPE_STRING: 372 return ""; case Common.TYPE_COMMENT: 374 return ""; case Common.TYPE_BOOLEAN: 376 return Boolean.FALSE; 377 case Common.TYPE_BYTE: 378 return new Byte ((byte)0); 379 case Common.TYPE_CHAR: 380 return new Character ('\0'); 381 case Common.TYPE_SHORT: 382 return new Short ((short)0); 383 case Common.TYPE_INT: 384 return new Integer (0); 385 case Common.TYPE_LONG: 386 return new Long (0); 387 case Common.TYPE_FLOAT: 388 return new Float (0.0); 389 case Common.TYPE_DOUBLE: 390 return new Double (0.0); 391 default: 392 throw new IllegalArgumentException (Common.getMessage( 393 "UnknownType_msg", new Integer (type))); 394 } 395 } 396 397 398 405 406 static private String rbName = "org.netbeans.modules.schema2beans.Bundle"; 409 public static String getMessage(String key) { 410 return Common.getMessage(key, null); 411 } 412 413 public static String getMessage(String key, Object p1) { 414 return Common.getMessage(key, new Object [] {p1}); 415 } 416 417 public static String getMessage(String key, int p1) { 418 return Common.getMessage(key, new Object [] {new Integer (p1)}); 419 } 420 421 public static String getMessage(String key, Object p1, Object p2) { 422 return Common.getMessage(key, new Object [] {p1, p2}); 423 } 424 425 public static String getMessage(String key, Object p1, Object p2, Object p3) { 426 return Common.getMessage(key, new Object [] {p1, p2, p3}); 427 } 428 429 public static String getMessage(String key, Object [] args) { 430 ResourceBundle rb = null; 431 432 if (rb == null) { 434 try { 435 rb = ResourceBundle.getBundle(rbName, Locale.getDefault(), 436 (Common.class).getClassLoader()); 437 } catch(MissingResourceException e) { 438 System.err.println("Couldn't find the bundle " + rbName + " for the locale " + Locale.getDefault()); } 442 } 443 444 if (rb != null) { 446 if (args != null) { 448 return MessageFormat.format(rb.getString(key), args); 449 } else { 450 return rb.getString(key); 451 } 452 } else { 453 String p = " "; if (args != null) { 456 for (int i=0; i<args.length; i++) { 457 if (args[i] != null) { 458 p += (args[i].toString() + " "); } else { 460 p += "null "; } 462 } 463 } 464 return key + p; 465 } 466 } 467 468 static public String instanceToString(int instance) { 469 switch (instance) { 470 case Common.TYPE_0_1: 471 return "optional"; 472 case Common.TYPE_0_N: 473 return "an array, possibly empty"; 474 case Common.TYPE_1_N: 475 return "an array containing at least one element"; 476 default: 477 return "mandatory"; 478 } 479 } 480 481 static public String instanceToCommonString(int instance) { 482 switch (instance) { 483 case Common.TYPE_0_1: 484 return "TYPE_0_1"; 485 case Common.TYPE_0_N: 486 return "TYPE_0_N"; 487 case Common.TYPE_1_N: 488 return "TYPE_1_N"; 489 default: 490 return "TYPE_1"; 491 } 492 } 493 494 498 static public int widestInstance(int instance1, int instance2) { 499 if (instance1 == TYPE_0_N || instance2 == TYPE_0_N) 500 return TYPE_0_N; 501 if (instance1 == TYPE_1_N || instance2 == TYPE_1_N) 502 return TYPE_1_N; 503 if (instance1 == TYPE_0_1 || instance2 == TYPE_0_1) 504 return TYPE_0_1; 505 return instance1; 506 } 507 } 508 | Popular Tags |