KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > validation > util > Utils


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.j2ee.sun.validation.util;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.lang.reflect.Constructor JavaDoc;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.text.MessageFormat JavaDoc;
29
30 import org.netbeans.modules.j2ee.sun.validation.util.BundleReader;
31
32 /**
33  * Utils is an utility class. Provides various utility methods.
34  *
35  *
36  * @author Rajeshwar Patil
37  * @version %I%, %G%
38  */

39 public class Utils {
40
41     private final String JavaDoc GET_PREFIX = "get"; //NOI18N
42

43
44     /** Creates a new instance of Utils */
45     public Utils() {
46     }
47
48
49     /**
50     * Gets the name from the absolute name/path
51     *
52     * @param absoluteName the complete path name
53     * @param delimiter the separator character used
54     * in the <code>absoluteName</code>
55     * @return the name
56     */

57     public String JavaDoc getName(String JavaDoc absoluteName, int delimiter) {
58         if(null == absoluteName){
59             return absoluteName;
60         }
61         int index = absoluteName.lastIndexOf(delimiter);
62         if( index != -1) {
63             index = index + 1;
64             return absoluteName.substring(index);
65         } else {
66             return absoluteName;
67         }
68     }
69
70
71     /**
72     * Gets the parent of the given absolute name
73     *
74     * @param absoluteName the complete path name
75     * @param delimiter the separator character used
76     * in the <code>absoluteName</code>
77     * @return the parent of the given absolute name;
78     * returns <code>null</code> if the given name does not have parent.
79     */

80     public String JavaDoc getParentName(String JavaDoc absoluteName, int delimiter) {
81         if(null == absoluteName){
82             return absoluteName;
83         }
84         int endIndex = absoluteName.lastIndexOf(delimiter);
85         if(endIndex != -1){
86             if(0 == endIndex){
87                 return null;
88             } else {
89                 return absoluteName.substring(0, endIndex);
90             }
91         } else {
92             return null;
93         }
94     }
95
96
97     /**
98     * Converts the first letter of the given string to Uppercase.
99     *
100     * @param string the input string
101     * @return the string with the Uppercase first letter
102     */

103     public String JavaDoc upperCaseFirstLetter(String JavaDoc string)
104     {
105         if(string == null || string.length() <= 0){
106             return string;
107         }
108         return string.substring(0, 1).toUpperCase() + string.substring(1);
109     }
110
111
112     /**
113     * Creates an instance of the given type.
114     * Uses constructor with no arguments to instantiate the type object.
115     *
116     * @param type the type name
117     * @return the <code>Object</code> of the given <code>type</code>
118     */

119     public Object JavaDoc createObject(String JavaDoc type) {
120         Object JavaDoc object = null;
121         try {
122           Class JavaDoc classObject = Class.forName(type);
123           object = classObject.newInstance();
124         } catch (InstantiationException JavaDoc e) {
125           System.out.println(e);
126         } catch (IllegalAccessException JavaDoc e) {
127           System.out.println(e);
128         } catch (ClassNotFoundException JavaDoc e) {
129           System.out.println(e);
130         }
131         return object;
132     }
133
134
135     /**
136     * Creates an instance of the type; given the <code>Class</code>
137     * object of the type. Uses constructor with no arguments to
138     * instantiate the type object.
139     *
140     * @param class the Class object of the type to create instance of.
141     * @return the <code>Object</code> of the given <code>type</code>
142     */

143     public Object JavaDoc createObject(Class JavaDoc classObject) {
144         Object JavaDoc object = null;
145         try {
146           object = classObject.newInstance();
147         } catch (InstantiationException JavaDoc e) {
148           System.out.println(e);
149         } catch (IllegalAccessException JavaDoc e) {
150           System.out.println(e);
151         }
152         return object;
153     }
154
155
156     /**
157     * Creates an instance of the type; given the <code>Constructor</code>
158     * object of the type and the array of argument values.
159     * Uses constructor, represented by the input <code>Constructor</code>
160     * object.
161     *
162     * @param constructor the <code>Constructor</code> object of the type
163     * @param arguments an array of arugments to the constructor
164     * @return the <code>Object</code> of the given <code>type</code>
165     */

166     public Object JavaDoc createObject(Constructor JavaDoc constructor,
167                                      Object JavaDoc[] arguments) {
168         //System.out.println ("Constructor: " + constructor.toString());
169
Object JavaDoc object = null;
170
171         try {
172         object = constructor.newInstance(arguments);
173         //System.out.println ("Object: " + object.toString());
174
return object;
175         } catch (InstantiationException JavaDoc e) {
176           System.out.println(e);
177         } catch (IllegalAccessException JavaDoc e) {
178           System.out.println(e);
179         } catch (IllegalArgumentException JavaDoc e) {
180           System.out.println(e);
181         } catch (InvocationTargetException JavaDoc e) {
182           System.out.println(e);
183         }
184         return object;
185     }
186
187
188     /**
189      * Gets a corresponding Constructor object of a given type,
190      * based on the Class objects of the arguments.
191      * Constructor object represents constructor which takes arguments
192      * represented by argumentClass array.
193      *
194      * @param type the type to get the Constructor of.
195      * @param argumentClass an array <code>Class</code> objects of the arugments
196      * @return the <code>Constructor</code> of the given <code>type</code>
197      */

198     public Constructor JavaDoc getConstructor(String JavaDoc type,
199             Class JavaDoc[] argumentClass){
200         Constructor JavaDoc constructor = null;
201         Class JavaDoc classObject = getClass(type);
202
203         try {
204             constructor = classObject.getConstructor(argumentClass);
205         } catch (NoSuchMethodException JavaDoc e) {
206             System.out.println(e);
207         }
208         return constructor;
209     }
210
211     
212     /**
213      * Gets a corresponding Constructor object of a given type,
214      * based on the Class objects of the arguments.
215      * Constructor object represents constructor which takes arguments
216      * represented by argumentClass array.
217      *
218      * @param classObject the <code>Class</code> object of the type to
219      * get Constructor of.
220      * @param argumentClass an array of <code>Class</code> objects of
221      * the arugments
222      * @return the <code>Constructor</code> of the given <code>type</code>
223      */

224     public Constructor JavaDoc getConstructor(Class JavaDoc classObject,
225         Class JavaDoc[] argumentClass){
226         Constructor JavaDoc constructor = null;
227
228         try {
229             constructor = classObject.getConstructor(argumentClass);
230         } catch (NoSuchMethodException JavaDoc e) {
231             System.out.println(e);
232         }
233         return constructor;
234     }
235
236     
237     /**
238      * Gets a <code>Class</code> object of the given <code>Object</code>.
239      *
240      * @param object the given <code>Object</code>
241      * @return the <code>Class</code> object of the given <code>Object</code>
242      */

243     public Class JavaDoc getClass(Object JavaDoc object){
244         Class JavaDoc classObject = null;
245
246         classObject = object.getClass();
247         ///System.out.println(classObject.toString());
248
return classObject;
249     }
250
251     
252     /**
253      * Gets a <code>Class</code> object of the given type.
254      *
255      * @param object the given type
256      * @return the <code>Class</code> object of the given type
257      */

258     public Class JavaDoc getClass(String JavaDoc type){
259         Class JavaDoc classObject = null;
260
261         try {
262             classObject = Class.forName(type);
263         } catch (ClassNotFoundException JavaDoc e) {
264             System.out.println(e);
265         }
266         return classObject;
267     }
268
269     
270     /**
271      * Gets a Method object of a given type, based on method name and
272      * the Class objects of the arguments.
273      *
274      * @param type the type to fetch method of.
275      * @param methodName the name of the method to fetch
276      * <code>Method</code> object of.
277      * @param argumentClass an array of <code>Class</code> objects of
278      * the arugments
279      * @return the <code>Method</code> object of the given type
280      */

281     public Method JavaDoc getMethod(String JavaDoc type, String JavaDoc methodName,
282         Class JavaDoc[] argumentClass){
283         Method JavaDoc method = null;
284         Class JavaDoc classObject = getClass(type);
285
286         try {
287             method = classObject.getMethod(methodName, argumentClass);
288         } catch (NoSuchMethodException JavaDoc e) {
289             System.out.println(e);
290         }
291         return method;
292     }
293
294     
295     /**
296      * Gets a Method object of a given type, based on method name and
297      * the Class objects of the arguments.
298      *
299      * @param classObject the <code>Class</code> object of
300      * the type to fetch method of.
301      * @param methodName the name of the method to fetch
302      * <code>Method</code> object of.
303      * @param argumentClass an array of <code>Class</code> objects of
304      * the arugments
305      * @return the <code>Method</code> object of the given type
306      */

307     public Method JavaDoc getMethod(Class JavaDoc classObject, String JavaDoc methodName,
308         Class JavaDoc[] argumentClass){
309         Method JavaDoc method = null;
310
311         try {
312             method = classObject.getMethod(methodName, argumentClass);
313         } catch (NoSuchMethodException JavaDoc e) {
314             System.out.println(e);
315         }
316         return method;
317     }
318
319     
320     /**
321      * Invokes the method, on the given object with the given arguments.
322      * Invokes method, represented by the <code>Method</code> object.
323      *
324      * @param object the <code>Object</code> to invoke the method of
325      * @param method the method to be invoked
326      * @param arguments an array of <code>Objects</code> to be used as
327      * arugments to the method being invoked
328      * @return an <code>Object</code>, returned by the invoked method.
329      */

330     public Object JavaDoc invoke(Object JavaDoc object, Method JavaDoc method,
331                                      Object JavaDoc[] arguments) {
332       Object JavaDoc result = null;
333       try {
334         result = method.invoke(object, arguments);
335       } catch (IllegalAccessException JavaDoc e) {
336           System.out.println(e);
337       } catch (InvocationTargetException JavaDoc e) {
338           System.out.println(e);
339       }
340       return result;
341     }
342
343
344     /**
345      * Gets the <code>Method</code> object of the given
346      * type; given the method name.
347      *
348      * @param type the given type, to get the method of
349      * @param methodName the method name to get the
350      * <code>Method</code> object of
351      * @return a <code>Method</code> object of the given type
352      */

353     public Method JavaDoc getMethod(String JavaDoc type, String JavaDoc methodName){
354         Method JavaDoc method = null;
355         Class JavaDoc classObject = getClass(type);
356
357         try {
358             method = classObject.getMethod(methodName, null);
359         } catch (NoSuchMethodException JavaDoc e) {
360             System.out.println(e);
361         }
362         return method;
363     }
364
365     
366     /**
367      * Gets the <code>Method</code> object of the given
368      * type; given the method name.
369      *
370      * @param classObject the <codeb>Class</code> object
371      * of the given type, to get the method of
372      * @param methodName the method name to get the
373      * <code>Method</code> object of
374      * @return a <code>Method</code> object of the given type
375      */

376     public Method JavaDoc getMethod(Class JavaDoc classObject,
377             String JavaDoc methodName){
378         Method JavaDoc method = null;
379
380         try {
381             method = classObject.getMethod(methodName, null);
382         } catch (NoSuchMethodException JavaDoc e) {
383             System.out.println(e);
384         }
385         return method;
386     }
387
388
389     /**
390      * Invokes the method, on the given object.
391      * Invokes method, represented by the Method object.
392      *
393      * @param object the <codeb>Olass</code> to invoke the method of
394      * @param method the method to be invoked
395      * @return an <code>Object</code> returned by the invoked method
396      */

397     public Object JavaDoc invoke(Object JavaDoc object, Method JavaDoc method) {
398             Object JavaDoc result = null;
399       try {
400         result = method.invoke(object, null);
401       } catch (IllegalAccessException JavaDoc e) {
402           System.out.println(e);
403       } catch (InvocationTargetException JavaDoc e) {
404           System.out.println(e);
405       }
406       return result;
407     }
408
409
410     /**
411      * Removes any hypens ( - ) from the given string.
412      * When it removes a hypen, it converts next immidiate
413      * character, if any, to an Uppercase.(schema2beans convention)
414      * @param string the input string
415      * @return a <code>String</code> resulted after removing the hypens
416      */

417     public String JavaDoc eleminateHypen(String JavaDoc string){
418         if(!(string == null || string.length() <= 0)){
419             int index = string.indexOf('-');
420             while(index != -1){
421                 if(index == 0){
422                     string = string.substring(1);
423                 } else {
424                     if(index == (string.length() - 1)){
425                         string = string.substring(0,string.length()-1);
426                     } else {
427                         string = string.substring(0,index) +
428                             upperCaseFirstLetter(string.substring(index + 1));
429                     }
430                 }
431                 index = string.indexOf('-');
432             }
433         }
434         return string;
435     }
436
437     
438     /**
439      * Constructs a method name from element's bean
440      * name for a given prefix.(schema2beans convention)
441      *
442      * @param elementName the given element name
443      * @param prefix the given prefix
444      * @return a method name formed from the given name and the prefix
445      */

446     public String JavaDoc methodNameFromBeanName(String JavaDoc elementName,
447             String JavaDoc prefix){
448         if((null == elementName) || (null == prefix) ||
449                 (prefix.length() <= 0 )){
450             return elementName;
451         }
452         String JavaDoc methodName = upperCaseFirstLetter(elementName);
453         return methodName = prefix + methodName;
454     }
455
456     
457     /**
458      * Constructs a method name from element's dtd name
459      * name for a given prefix.(schema2beans convention)
460      *
461      * @param elementName the given element name
462      * @param prefix the given prefix
463      * @return a method name formed from the given name and the prefix
464      */

465     public String JavaDoc methodNameFromDtdName(String JavaDoc elementName,
466             String JavaDoc prefix){
467         return methodNameFromBeanName(eleminateHypen(elementName), prefix);
468     }
469
470
471     /**
472      * Gets an element from the given <code>Object</code>;
473      * given the name of the element.
474      *
475      * @param elementName the name of the element to get from the given object
476      * @param object the given object
477      * @return the retrieved element from the given object; returns null if the
478      * following cases :
479      * the given <code>object</code> is null
480      * the given <code>elementName</code> is null or zero length.
481      */

482     public Object JavaDoc getElement(String JavaDoc elementName, Object JavaDoc object){
483         //Consvert the first letter of elementName to Uppercase
484
//Construct method name by appending given "get" to elementName //NOI18N
485
//Invoke this method on object to get the required value
486
if((null == object) || (null == elementName) ||
487             (elementName.length() <= 0)){
488             return null;
489         }
490
491         String JavaDoc methodName =
492                 methodNameFromDtdName(elementName, GET_PREFIX); //NOI18N
493
Method JavaDoc getMethod = null;
494         getMethod = getMethod(getClass(object), methodName);
495         return invoke(object, getMethod);
496     }
497
498
499     /**
500      * Gets the elements from the given <code>Object</code>;
501      * given the name of the element.
502      *
503      * @param elementName the name of the elements to get from the given object
504      * @param object the given object
505      * @return an array of the retrieved elements from the given object; returns
506      * null in the following cases :
507      * the given <code>object</code> is null
508      * the given <code>elementName</code> is null or zero length.
509      */

510     public Object JavaDoc[] getElements(String JavaDoc elementName, Object JavaDoc object){
511         return (Object JavaDoc[]) getElement(elementName, object);
512     }
513
514
515     /**
516      * Gets an element at a given index, from the given <code>Object</code>;
517      * given the name of the element.
518      *
519      * @param elementName the name of the element to get from the given object
520      * @param index the given index
521      * @param object the given object
522      * @return the retrieved element from the given object; returns null in the
523      * following cases:
524      * the given <code>object</code> is null
525      * the given <code>elementName</code> is null or zero length.
526      * the given <code>index</code> is less than zero.
527      */

528     public Object JavaDoc getElement(String JavaDoc elementName, int index,
529             Object JavaDoc object){
530         //Consvert the first letter of elementName to Uppercase
531
//Construct method name by appending given "get" to elementName //NOI18N
532
//Invoke this method on object to get the required value
533
if((null == object) || (null == elementName) ||
534             (elementName.length() <= 0) || (index < 0)){
535             return null;
536        }
537
538        String JavaDoc methodName =
539                 methodNameFromDtdName(elementName, GET_PREFIX); //NOI18N
540
Class JavaDoc[] argumentTypes = new Class JavaDoc[] {int.class};
541        Method JavaDoc getMethod = null;
542        getMethod = getMethod(getClass(object), methodName,
543                 argumentTypes);
544
545        Integer JavaDoc in = new Integer JavaDoc(index);
546        Object JavaDoc[] argumentValues = new Object JavaDoc[] {in};
547        return invoke(object, getMethod, argumentValues);
548     }
549
550
551     /**
552      * Gets an element from the given <code>Object</code>;
553      * given the name of the element and prefix to use to for the method name.
554      *
555      * @param elementName the name of the element to get from the given object
556      * @param object the given object
557      * @param prefix the prefix to use to formulate the name of the fetch method
558      * @return Object the retrieved element from the given object; returns
559      * null if the following cases :
560      * the given <code>object</code> is null
561      * the given <code>elementName</code> is null or zero length.
562      */

563     public Object JavaDoc getElement(String JavaDoc elementName, Object JavaDoc object,
564         String JavaDoc prefix){
565         //Consvert the first letter of elementName to Uppercase
566
//Construct method name by appending given "perfix" to elementName
567
//Invoke this method on object to get the required value
568
if((null == object) || (null == elementName) ||
569             (elementName.length() <= 0)){
570             return null;
571         }
572
573         String JavaDoc returnValue = null;
574         String JavaDoc methodName =
575             methodNameFromDtdName(elementName, prefix);
576         Class JavaDoc classObject = getClass(object);
577         Method JavaDoc method = getMethod(classObject, methodName);
578         return (Integer JavaDoc) invoke(object, method);
579     }
580
581
582     /**
583      * Gets an indexed name from a given name and index.
584      *
585      * @param name the given name
586      * @param index the given index
587      * @return the indexed name; returns null if the
588      * given <code>name</code> is null.
589      */

590     public String JavaDoc getIndexedName(String JavaDoc name, int index){
591         if(name != null) {
592             String JavaDoc format =
593                 BundleReader.getValue("Indexed_Name_Format"); //NOI18N
594
Object JavaDoc[] arguments = new Object JavaDoc[]{name, String.valueOf(index)};
595             name = MessageFormat.format(format, arguments);
596         }
597         return name;
598     }
599
600
601     /**
602      * Gets an Url object for a given relative path.
603      */

604     public URL JavaDoc getUrlObject(String JavaDoc relativePath){
605         Class JavaDoc cl = getClass();
606         ClassLoader JavaDoc classLoader = cl.getClassLoader();
607         return classLoader.getResource(relativePath);
608     }
609
610
611     /**
612      * Gets an input stream for the given file.
613      *
614      * @param relavtiveFilePath the relative path name of the file.
615      *
616      * @return InputStream the input stream for the given file; returns null
617      * in case of failure.
618      */

619     public InputStream JavaDoc getInputStream(String JavaDoc relavtiveFilePath){
620         InputStream JavaDoc inputStream = null;
621         URL JavaDoc url = null;
622         if(relavtiveFilePath != null){
623             url = getUrlObject(relavtiveFilePath);
624             if(url != null) {
625                 try {
626                     inputStream = url.openStream();
627                 } catch (IOException JavaDoc exception){
628                     System.out.println(exception.getMessage());
629                 }
630             } else {
631                 String JavaDoc format =
632                     BundleReader.getValue("Error_does_not_exists"); //NOI18N
633
Object JavaDoc[] arguments =
634                     new Object JavaDoc[]{"File", relavtiveFilePath}; //NOI18N
635
System.out.println(MessageFormat.format(format, arguments));
636             }
637         }
638         return inputStream;
639     }
640
641     
642     /**
643      * Determines if the given file exists.
644      *
645      * @param relativePath the relative path name of the file.
646      *
647      * @return boolean true if the given file exists; else false.
648      */

649     public boolean fileExists(String JavaDoc relativePath){
650         boolean returnValue = false;
651         InputStream JavaDoc inputStream = getInputStream(relativePath);
652         if(inputStream != null){
653             returnValue = true;
654         }
655         return returnValue;
656     }
657     
658 }
659
Popular Tags