KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > common > validation > util > Utils


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.tools.common.validation.util;
25
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.lang.reflect.Constructor JavaDoc;
29 import java.lang.reflect.InvocationTargetException JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.text.MessageFormat JavaDoc;
33
34 import com.sun.enterprise.tools.common.validation.util.BundleReader;
35
36 /**
37  * Utils is an utility class. Provides various utility methods.
38  *
39  *
40  * @author Rajeshwar Patil
41  * @version %I%, %G%
42  */

43 public class Utils {
44
45     private final String JavaDoc GET_PREFIX = "get"; //NOI18N
46

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

653     public boolean fileExists(String JavaDoc relativePath){
654         boolean returnValue = false;
655         InputStream JavaDoc inputStream = getInputStream(relativePath);
656         if(inputStream != null){
657             returnValue = true;
658         }
659         return returnValue;
660     }
661     
662 }
663
Popular Tags