KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > schema2beans > Common


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.schema2beans;
21
22 import java.text.*;
23 import java.util.ResourceBundle JavaDoc;
24 import java.util.Locale JavaDoc;
25 import java.util.MissingResourceException JavaDoc;
26
27
28 /**
29  * This class contains the schema2beans constants and helper methods.
30  */

31 public class Common {
32
33     // Constants
34
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 JavaDoc DTD_STRING = "#PCDATA"; // NOI18N
74
static public final String JavaDoc DTD_EMPTY = "EMPTY"; // NOI18N
75

76     static public final String JavaDoc CLASS_STRING = "String"; // NOI18N
77
static public final String JavaDoc CLASS_BOOLEAN = "Boolean"; // NOI18N
78

79     static public final String JavaDoc 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     /*
104     public static boolean isInt(int type) {
105         return ((type & MASK_TYPE) == TYPE_INT);
106     }
107     */

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     /**
122      * Is it a Java primitive or not?
123      */

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 JavaDoc(Common.getMessage(
141                                                                  "UnknownType_msg", new Integer JavaDoc(type)));
142         }
143     }
144     
145     public static String JavaDoc wrapperGetMethod(int type) {
146         switch(type & MASK_TYPE) {
147         case TYPE_BOOLEAN:
148             return "booleanValue"; // NOI18N
149
case TYPE_BYTE:
150             return "byteValue"; // NOI18N
151
case TYPE_CHAR:
152             return "charValue"; // NOI18N
153
case TYPE_SHORT:
154             return "shortValue"; // NOI18N
155
case TYPE_INT:
156             return "intValue"; // NOI18N
157
case TYPE_LONG:
158             return "longValue"; // NOI18N
159
case TYPE_FLOAT:
160             return "floatValue"; // NOI18N
161
case TYPE_DOUBLE:
162             return "doubleValue"; // NOI18N
163
default:
164             throw new IllegalArgumentException JavaDoc(Common.getMessage(
165                                                                  "UnknownType_msg", new Integer JavaDoc(type)));
166         }
167     }
168     
169     public static String JavaDoc wrapperClass(int type) {
170         switch(type & MASK_TYPE) {
171         case TYPE_BOOLEAN:
172             return "Boolean"; // NOI18N
173
case TYPE_BYTE:
174             return "Byte"; // NOI18N
175
case TYPE_CHAR:
176             return "Character"; // NOI18N
177
case TYPE_SHORT:
178             return "Short"; // NOI18N
179
case TYPE_INT:
180             return "Integer"; // NOI18N
181
case TYPE_LONG:
182             return "Long"; // NOI18N
183
case TYPE_FLOAT:
184             return "Float"; // NOI18N
185
case TYPE_DOUBLE:
186             return "Double"; // NOI18N
187
default:
188             throw new IllegalArgumentException JavaDoc(Common.getMessage(
189                                                                  "UnknownType_msg", new Integer JavaDoc(type)));
190         }
191     }
192     
193     public static int wrapperToType(String JavaDoc wrapper) {
194         if (wrapper == null)
195             return NONE;
196         String JavaDoc s = wrapper.trim();
197         if (s.endsWith("boolean")) // NOI18N
198
return TYPE_BOOLEAN;
199         if (s.endsWith("byte")) // NOI18N
200
return TYPE_BYTE;
201         if (s.endsWith("char")) // NOI18N
202
return TYPE_CHAR;
203         if (s.endsWith("short")) // NOI18N
204
return TYPE_SHORT;
205         if (s.endsWith("int")) // NOI18N
206
return TYPE_INT;
207         if (s.endsWith("long")) // NOI18N
208
return TYPE_LONG;
209         if (s.endsWith("float")) // NOI18N
210
return TYPE_FLOAT;
211         if (s.endsWith("double")) // NOI18N
212
return TYPE_DOUBLE;
213         if (s.equals("String") || s.equals("java.lang.String"))
214             return TYPE_STRING;
215         //System.out.println("schema2beans Common.wrapperToType: couldn't find type for "+wrapper);
216
return NONE;
217     }
218     
219     public static String JavaDoc scalarType(int type) {
220         switch(type & MASK_TYPE) {
221         case TYPE_BOOLEAN:
222             return "boolean"; // NOI18N
223
case TYPE_BYTE:
224             return "byte"; // NOI18N
225
case TYPE_CHAR:
226             return "char"; // NOI18N
227
case TYPE_SHORT:
228             return "short"; // NOI18N
229
case TYPE_INT:
230             return "int"; // NOI18N
231
case TYPE_LONG:
232             return "long"; // NOI18N
233
case TYPE_FLOAT:
234             return "float"; // NOI18N
235
case TYPE_DOUBLE:
236             return "double"; // NOI18N
237
default:
238             throw new IllegalArgumentException JavaDoc(Common.getMessage(
239                                                                  "UnknownType_msg", new Integer JavaDoc(type)));
240         }
241     }
242     
243     public static String JavaDoc typeToString(int type) {
244         switch(type & MASK_TYPE) {
245         case TYPE_STRING:
246             return "TYPE_STRING"; // NOI18N
247
case TYPE_COMMENT:
248             return "TYPE_COMMENT";
249         case TYPE_BEAN:
250             return "TYPE_BEAN"; // NOI18N
251
case TYPE_BOOLEAN:
252             return "TYPE_BOOLEAN"; // NOI18N
253
case TYPE_BYTE:
254             return "TYPE_BYTE"; // NOI18N
255
case TYPE_CHAR:
256             return "TYPE_CHAR"; // NOI18N
257
case TYPE_SHORT:
258             return "TYPE_SHORT"; // NOI18N
259
case TYPE_INT:
260             return "TYPE_INT"; // NOI18N
261
case TYPE_LONG:
262             return "TYPE_LONG"; // NOI18N
263
case TYPE_FLOAT:
264             return "TYPE_FLOAT"; // NOI18N
265
case TYPE_DOUBLE:
266             return "TYPE_DOUBLE"; // NOI18N
267
default:
268             throw new IllegalArgumentException JavaDoc(Common.getMessage(
269                                                                  "UnknownType_msg", new Integer JavaDoc(type)));
270         }
271     }
272     
273     public static String JavaDoc dumpHex(String JavaDoc v) {
274         String JavaDoc s;
275     
276         if (v != null) {
277             s = "hex[ "; // NOI18N
278
byte[] b = v.getBytes();
279             for (int i=0; i<b.length; i++)
280                 s += Integer.toHexString((int)b[i]) + " "; // NOI18N
281
s += "]"; // NOI18N
282
}
283         else
284             s = "<null>"; // NOI18N
285

286         return s;
287     }
288     
289     public static String JavaDoc constName(String JavaDoc name) {
290         return name.replace('-', '_').replace('#', '_').replace('.', '_').replace(':', '_').toUpperCase();
291     }
292     
293     /**
294      * Convert a DTD name into a bean name:
295      *
296      * Any - or _ character is removed. The letter following - and _
297      * is changed to be upper-case.
298      * If the user mixes upper-case and lower-case, the case is not
299      * changed.
300      * If the Word is entirely in upper-case, the word is changed to
301      * lower-case (except the characters following - and _)
302      * The first letter is always upper-case.
303      */

304     public static String JavaDoc convertName(String JavaDoc name) {
305         return convertName(name, true);
306     }
307
308     /**
309      * Same as convertName, except the name that comes out will
310      * be suitable as an instance variable (first letter is lowercase).
311      */

312     public static String JavaDoc convertNameInstance(String JavaDoc name) {
313         return convertName(name, false);
314     }
315     
316     private static String JavaDoc convertName(String JavaDoc name, boolean up) {
317         CharacterIterator ci;
318         StringBuffer JavaDoc n = new StringBuffer JavaDoc();
319         boolean keepCase = false;
320         char c;
321     
322         ci = new StringCharacterIterator(name);
323         c = ci.first();
324     
325         // If everything is uppercase, we'll lowercase the name.
326
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     /**
353      * Often, an object from the DOM graph will contain spaces or
354      * LF characters, making String comparison impossible with values
355      * specified by the user. The goal of this method is to cleanup
356      * an object from such characters before being compared with
357      * a value specified by the user.
358      */

359     public static Object JavaDoc getComparableObject(Object JavaDoc obj) {
360         Object JavaDoc ret = obj;
361         if (obj instanceof java.lang.String JavaDoc) {
362             String JavaDoc s = (String JavaDoc)obj;
363             ret = s.trim();
364         }
365     
366         return ret;
367     }
368     
369     public static Object JavaDoc defaultScalarValue(int type) {
370         switch(type & Common.MASK_TYPE) {
371         case Common.TYPE_STRING:
372             return ""; // NOI18N
373
case Common.TYPE_COMMENT:
374             return ""; // NOI18N
375
case Common.TYPE_BOOLEAN:
376             return Boolean.FALSE;
377         case Common.TYPE_BYTE:
378             return new Byte JavaDoc((byte)0);
379         case Common.TYPE_CHAR:
380             return new Character JavaDoc('\0');
381         case Common.TYPE_SHORT:
382             return new Short JavaDoc((short)0);
383         case Common.TYPE_INT:
384             return new Integer JavaDoc(0);
385         case Common.TYPE_LONG:
386             return new Long JavaDoc(0);
387         case Common.TYPE_FLOAT:
388             return new Float JavaDoc(0.0);
389         case Common.TYPE_DOUBLE:
390             return new Double JavaDoc(0.0);
391         default:
392             throw new IllegalArgumentException JavaDoc(Common.getMessage(
393                                                                  "UnknownType_msg", new Integer JavaDoc(type)));
394         }
395     }
396     
397     
398     /*
399      * Bundle utility methods. The following methods return a formated message
400      * using the message bundle key and the optional parameters.
401      *
402      * The different flavors of getMessage(String, String, ...) call the
403      * getMessage(String, Object[]) method.
404      */

405     
406     //static private ResourceBundle rb = null;
407
static private String JavaDoc rbName = "org.netbeans.modules.schema2beans.Bundle"; // NOI18N
408

409     public static String JavaDoc getMessage(String JavaDoc key) {
410         return Common.getMessage(key, null);
411     }
412     
413     public static String JavaDoc getMessage(String JavaDoc key, Object JavaDoc p1) {
414         return Common.getMessage(key, new Object JavaDoc[] {p1});
415     }
416     
417     public static String JavaDoc getMessage(String JavaDoc key, int p1) {
418         return Common.getMessage(key, new Object JavaDoc[] {new Integer JavaDoc(p1)});
419     }
420     
421     public static String JavaDoc getMessage(String JavaDoc key, Object JavaDoc p1, Object JavaDoc p2) {
422         return Common.getMessage(key, new Object JavaDoc[] {p1, p2});
423     }
424     
425     public static String JavaDoc getMessage(String JavaDoc key, Object JavaDoc p1, Object JavaDoc p2, Object JavaDoc p3) {
426         return Common.getMessage(key, new Object JavaDoc[] {p1, p2, p3});
427     }
428     
429     public static String JavaDoc getMessage(String JavaDoc key, Object JavaDoc[] args) {
430         ResourceBundle JavaDoc rb = null;
431         
432         // Find the resource bundle if it is not loaded yet
433
if (rb == null) {
434             try {
435                 rb = ResourceBundle.getBundle(rbName, Locale.getDefault(),
436                                                      (Common.class).getClassLoader());
437             } catch(MissingResourceException JavaDoc e) {
438                 // Do without bundle
439
System.err.println("Couldn't find the bundle " + rbName + // NOI18N
440
" for the locale " + Locale.getDefault()); // NOI18N
441
}
442         }
443     
444         // Get and format the message...
445
if (rb != null) {
446             // ...using the resource bundle
447
if (args != null) {
448                 return MessageFormat.format(rb.getString(key), args);
449             } else {
450                 return rb.getString(key);
451             }
452         } else {
453             // ...without the resource bundle
454
String JavaDoc p = " "; // NOI18N
455
if (args != null) {
456                 for (int i=0; i<args.length; i++) {
457                     if (args[i] != null) {
458                         p += (args[i].toString() + " "); // NOI18N
459
} else {
460                         p += "null "; // NOI18N
461
}
462                 }
463             }
464             return key + p;
465         }
466     }
467     
468     static public String JavaDoc 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 JavaDoc 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     /**
495      * Return the widest instance set. Widest as in has the most elements.
496      * For instance, TYPE_0_N is widder than TYPE_1.
497      */

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