FileDocCategorySizeDatePackage
Utils.javaAPI DocGlassfish v2 API23265Fri May 04 22:35:00 BST 2007com.sun.enterprise.tools.common.validation.util

Utils

public class Utils extends Object
Utils is an utility class. Provides various utility methods.
author
Rajeshwar Patil
version
%I%, %G%

Fields Summary
private final String
GET_PREFIX
Constructors Summary
public Utils()
Creates a new instance of Utils

                     //NOI18N


           
      
    
Methods Summary
public java.lang.ObjectcreateObject(java.lang.String type)
Creates an instance of the given type. Uses constructor with no arguments to instantiate the type object.

param
type the type name
return
the Object of the given type

        Object object = null;
        try {
          Class classObject = Class.forName(type);
          object = classObject.newInstance();
        } catch (InstantiationException e) {
          System.out.println(e);
        } catch (IllegalAccessException e) {
          System.out.println(e);
        } catch (ClassNotFoundException e) {
          System.out.println(e);
        }
        return object;
    
public java.lang.ObjectcreateObject(java.lang.Class classObject)
Creates an instance of the type; given the Class object of the type. Uses constructor with no arguments to instantiate the type object.

param
class the Class object of the type to create instance of.
return
the Object of the given type

        Object object = null;
        try {
          object = classObject.newInstance();
        } catch (InstantiationException e) {
          System.out.println(e);
        } catch (IllegalAccessException e) {
          System.out.println(e);
        }
        return object;
    
public java.lang.ObjectcreateObject(java.lang.reflect.Constructor constructor, java.lang.Object[] arguments)
Creates an instance of the type; given the Constructor object of the type and the array of argument values. Uses constructor, represented by the input Constructor object.

param
constructor the Constructor object of the type
param
arguments an array of arugments to the constructor
return
the Object of the given type

        //System.out.println ("Constructor: " + constructor.toString());
        Object object = null;

        try {
        object = constructor.newInstance(arguments);
        //System.out.println ("Object: " + object.toString());
        return object;
        } catch (InstantiationException e) {
          System.out.println(e);
        } catch (IllegalAccessException e) {
          System.out.println(e);
        } catch (IllegalArgumentException e) {
          System.out.println(e);
        } catch (InvocationTargetException e) {
          System.out.println(e);
        }
        return object;
    
public java.lang.StringeleminateHypen(java.lang.String string)
Removes any hypens ( - ) from the given string. When it removes a hypen, it converts next immidiate character, if any, to an Uppercase.(schema2beans convention)

param
string the input string
return
a String resulted after removing the hypens

        if(!(string == null || string.length() <= 0)){
            int index = string.indexOf('-");
            while(index != -1){
                if(index == 0){
                    string = string.substring(1);
                } else {
                    if(index == (string.length() - 1)){
                        string = string.substring(0,string.length()-1);
                    } else {
                        string = string.substring(0,index) +
                            upperCaseFirstLetter(string.substring(index + 1));
                    }
                }
                index = string.indexOf('-");
            }
        }
        return string;
    
public booleanfileExists(java.lang.String relativePath)
Determines if the given file exists.

param
relativePath the relative path name of the file.
return
boolean true if the given file exists; else false.

        boolean returnValue = false;
        InputStream inputStream = getInputStream(relativePath);
        if(inputStream != null){
            returnValue = true;
        }
        return returnValue;
    
public java.lang.ClassgetClass(java.lang.Object object)
Gets a Class object of the given Object.

param
object the given Object
return
the Class object of the given Object

        Class classObject = null;

        classObject =  object.getClass();
        ///System.out.println(classObject.toString());
        return classObject;
    
public java.lang.ClassgetClass(java.lang.String type)
Gets a Class object of the given type.

param
object the given type
return
the Class object of the given type

        Class classObject = null;

        try {
            classObject =  Class.forName(type);
        } catch (ClassNotFoundException e) {
            System.out.println(e);
        }
        return classObject;
    
public java.lang.reflect.ConstructorgetConstructor(java.lang.String type, java.lang.Class[] argumentClass)
Gets a corresponding Constructor object of a given type, based on the Class objects of the arguments. Constructor object represents constructor which takes arguments represented by argumentClass array.

param
type the type to get the Constructor of.
param
argumentClass an array Class objects of the arugments
return
the Constructor of the given type

        Constructor constructor = null;
        Class classObject = getClass(type);

        try {
            constructor = classObject.getConstructor(argumentClass);
        } catch (NoSuchMethodException e) {
            System.out.println(e);
        }            
        return constructor;
    
public java.lang.reflect.ConstructorgetConstructor(java.lang.Class classObject, java.lang.Class[] argumentClass)
Gets a corresponding Constructor object of a given type, based on the Class objects of the arguments. Constructor object represents constructor which takes arguments represented by argumentClass array.

param
classObject the Class object of the type to get Constructor of.
param
argumentClass an array of Class objects of the arugments
return
the Constructor of the given type

        Constructor constructor = null;

        try {
            constructor = classObject.getConstructor(argumentClass);
        } catch (NoSuchMethodException e) {
            System.out.println(e);
        }            
        return constructor;
    
public java.lang.ObjectgetElement(java.lang.String elementName, java.lang.Object object)
Gets an element from the given Object; given the name of the element.

param
elementName the name of the element to get from the given object
param
object the given object
return
the retrieved element from the given object; returns null if the following cases : the given object is null the given elementName is null or zero length.

        //Consvert the first letter of elementName to Uppercase
        //Construct method name by appending given "get" to elementName //NOI18N
        //Invoke this method on object to get the required value
        if((null ==  object) || (null == elementName) || 
            (elementName.length() <= 0)){
            return null;
        }

        String methodName = 
                methodNameFromDtdName(elementName, GET_PREFIX);        //NOI18N
        Method getMethod = null;
        getMethod = getMethod(getClass(object), methodName);
        return invoke(object, getMethod);
    
public java.lang.ObjectgetElement(java.lang.String elementName, int index, java.lang.Object object)
Gets an element at a given index, from the given Object; given the name of the element.

param
elementName the name of the element to get from the given object
param
index the given index
param
object the given object
return
the retrieved element from the given object; returns null in the following cases: the given object is null the given elementName is null or zero length. the given index is less than zero.

        //Consvert the first letter of elementName to Uppercase
        //Construct method name by appending given "get" to elementName //NOI18N
        //Invoke this method on object to get the required value
       if((null ==  object) || (null == elementName) || 
            (elementName.length() <= 0) || (index < 0)){
            return null;
       }

       String methodName = 
                methodNameFromDtdName(elementName, GET_PREFIX);        //NOI18N
       Class[] argumentTypes = new Class[] {int.class}; 
       Method getMethod = null;
       getMethod = getMethod(getClass(object), methodName,
                argumentTypes);

       Integer in = new Integer(index);
       Object[] argumentValues = new Object[] {in};
       return invoke(object, getMethod, argumentValues);
    
public java.lang.ObjectgetElement(java.lang.String elementName, java.lang.Object object, java.lang.String prefix)
Gets an element from the given Object; given the name of the element and prefix to use to for the method name.

param
elementName the name of the element to get from the given object
param
object the given object
param
prefix the prefix to use to formulate the name of the fetch method
return
Object the retrieved element from the given object; returns null if the following cases : the given object is null the given elementName is null or zero length.

        //Consvert the first letter of elementName to Uppercase
        //Construct method name by appending given "perfix" to elementName
        //Invoke this method on object to get the required value
        if((null ==  object) || (null == elementName) || 
            (elementName.length() <= 0)){
            return null;
        }

        String returnValue = null;
        String methodName = 
            methodNameFromDtdName(elementName, prefix);
        Class classObject = getClass(object);
        Method method = getMethod(classObject, methodName);
        return (Integer) invoke(object, method);
    
public java.lang.Object[]getElements(java.lang.String elementName, java.lang.Object object)
Gets the elements from the given Object; given the name of the element.

param
elementName the name of the elements to get from the given object
param
object the given object
return
an array of the retrieved elements from the given object; returns null in the following cases : the given object is null the given elementName is null or zero length.

        return (Object[]) getElement(elementName, object);
    
public java.lang.StringgetIndexedName(java.lang.String name, int index)
Gets an indexed name from a given name and index.

param
name the given name
param
index the given index
return
the indexed name; returns null if the given name is null.

        if(name != null) {
            String format = 
                BundleReader.getValue("Indexed_Name_Format");           //NOI18N
            Object[] arguments = new Object[]{name, String.valueOf(index)};
            name = MessageFormat.format(format, arguments);
        }
        return name;
    
public java.io.InputStreamgetInputStream(java.lang.String relavtiveFilePath)
Gets an input stream for the given file.

param
relavtiveFilePath the relative path name of the file.
return
InputStream the input stream for the given file; returns null in case of failure.

        InputStream inputStream = null;
        URL url = null;
        if(relavtiveFilePath != null){
            url = getUrlObject(relavtiveFilePath);
            if(url != null) {
                try {
                    inputStream = url.openStream();
                } catch (IOException exception){
                    System.out.println(exception.getMessage());
                }
            } else {
                String format = 
                    BundleReader.getValue("Error_does_not_exists");     //NOI18N
                Object[] arguments =    
                    new Object[]{"File", relavtiveFilePath};            //NOI18N
                System.out.println(MessageFormat.format(format, arguments));
            }
        }
        return inputStream;
    
public java.lang.reflect.MethodgetMethod(java.lang.String type, java.lang.String methodName, java.lang.Class[] argumentClass)
Gets a Method object of a given type, based on method name and the Class objects of the arguments.

param
type the type to fetch method of.
param
methodName the name of the method to fetch Method object of.
param
argumentClass an array of Class objects of the arugments
return
the Method object of the given type

        Method method = null;
        Class classObject = getClass(type);

        try {
            method = classObject.getMethod(methodName, argumentClass);
        } catch (NoSuchMethodException e) {
            System.out.println(e);
        }            
        return method;
    
public java.lang.reflect.MethodgetMethod(java.lang.Class classObject, java.lang.String methodName, java.lang.Class[] argumentClass)
Gets a Method object of a given type, based on method name and the Class objects of the arguments.

param
classObject the Class object of the type to fetch method of.
param
methodName the name of the method to fetch Method object of.
param
argumentClass an array of Class objects of the arugments
return
the Method object of the given type

        Method method = null;

        try {
            method = classObject.getMethod(methodName, argumentClass);
        } catch (NoSuchMethodException e) {
            System.out.println(e);
        }            
        return method;
    
public java.lang.reflect.MethodgetMethod(java.lang.String type, java.lang.String methodName)
Gets the Method object of the given type; given the method name.

param
type the given type, to get the method of
param
methodName the method name to get the Method object of
return
a Method object of the given type

        Method method = null;
        Class classObject = getClass(type);

        try {
            method = classObject.getMethod(methodName, null);
        } catch (NoSuchMethodException e) {
            System.out.println(e);
        }            
        return method;
    
public java.lang.reflect.MethodgetMethod(java.lang.Class classObject, java.lang.String methodName)
Gets the Method object of the given type; given the method name.

param
classObject the Class object of the given type, to get the method of
param
methodName the method name to get the Method object of
return
a Method object of the given type

        Method method = null;

        try {
            method = classObject.getMethod(methodName, null);
        } catch (NoSuchMethodException e) {
            System.out.println(e);
        }            
        return method;
    
public java.lang.StringgetName(java.lang.String absoluteName, int delimiter)
Gets the name from the absolute name/path

param
absoluteName the complete path name
param
delimiter the separator character used in the absoluteName
return
the name

        if(null == absoluteName){
            return absoluteName;
        }
        int index = absoluteName.lastIndexOf(delimiter);
        if( index != -1) {
            index = index + 1;
            return absoluteName.substring(index);
        } else {
            return absoluteName;
        }
    
public java.lang.StringgetParentName(java.lang.String absoluteName, int delimiter)
Gets the parent of the given absolute name

param
absoluteName the complete path name
param
delimiter the separator character used in the absoluteName
return
the parent of the given absolute name; returns null if the given name does not have parent.

        if(null == absoluteName){
            return absoluteName;
        }
        int endIndex = absoluteName.lastIndexOf(delimiter);
        if(endIndex != -1){
            if(0 == endIndex){
                return null;
            } else {
                return absoluteName.substring(0, endIndex);
            }
        } else {
            return null;
        }
    
public java.net.URLgetUrlObject(java.lang.String relativePath)
Gets an Url object for a given relative path.

        Class cl = getClass();
        ClassLoader classLoader = cl.getClassLoader();
        return classLoader.getResource(relativePath);
    
public java.lang.Objectinvoke(java.lang.Object object, java.lang.reflect.Method method, java.lang.Object[] arguments)
Invokes the method, on the given object with the given arguments. Invokes method, represented by the Method object.

param
object the Object to invoke the method of
param
method the method to be invoked
param
arguments an array of Objects to be used as arugments to the method being invoked
return
an Object, returned by the invoked method.

      Object result = null;
      try {
        result = method.invoke(object, arguments);
      } catch (IllegalAccessException e) {
          System.out.println(e);
      } catch (InvocationTargetException e) {
          System.out.println(e);
      }
      return result;
    
public java.lang.Objectinvoke(java.lang.Object object, java.lang.reflect.Method method)
Invokes the method, on the given object. Invokes method, represented by the Method object.

param
object the Olass to invoke the method of
param
method the method to be invoked
return
an Object returned by the invoked method

            Object result = null;
      try {
        result = method.invoke(object, null);
      } catch (IllegalAccessException e) {
          System.out.println(e);
      } catch (InvocationTargetException e) {
          System.out.println(e);
      }
      return result;
    
public java.lang.StringmethodNameFromBeanName(java.lang.String elementName, java.lang.String prefix)
Constructs a method name from element's bean name for a given prefix.(schema2beans convention)

param
elementName the given element name
param
prefix the given prefix
return
a method name formed from the given name and the prefix

        if((null == elementName) || (null == prefix) ||
                (prefix.length() <= 0 )){
            return elementName;
        }
        String methodName = upperCaseFirstLetter(elementName);
        return methodName = prefix + methodName;
    
public java.lang.StringmethodNameFromDtdName(java.lang.String elementName, java.lang.String prefix)
Constructs a method name from element's dtd name name for a given prefix.(schema2beans convention)

param
elementName the given element name
param
prefix the given prefix
return
a method name formed from the given name and the prefix

        return methodNameFromBeanName(eleminateHypen(elementName), prefix);
    
public java.lang.StringupperCaseFirstLetter(java.lang.String string)
Converts the first letter of the given string to Uppercase.

param
string the input string
return
the string with the Uppercase first letter

        if(string == null || string.length() <= 0){
            return string;
        }
        return string.substring(0, 1).toUpperCase() + string.substring(1);