Package Apache.CLI



Class TypeHandler


  • public class TypeHandler
    extends java.lang.Object
    This Class-File has been copied, directly from the Apache Commons Software Foundation. Other than the minor, cosmetic changes (mentioned in the second-list below), this file is perfectly identical to the original that may be found on the Apache Website: apache.org.

    1. The suggested license for using this file may be read here: Apache License
    2. The Source-Code Header for this file may be viewed here: Source-File Header
    3. Partial contents of the 'site/resources/images' directory: Resource Images

    Notes about changes that have been made as a result of the import process:

    • Within all files, the original Package-Name declaration has been changed:
      Original:   package org.apache.commons.cli;
      Updated:   package Apache.CLI;

    • All Classes and Class-Members (Methods, Fields, etc...) that were previously Annotated with the @Deprecated Annotation have been summarily removed.

    • Code Formattings and Styling has been heavily modified to conform to the Standard Java-HTML Indentation & Styling Choices. The code itself remains identical, with only a few squiggly-braces '{' and '}' being removed, for cosmetic reasons (and for lowering "Developer Stress" Levels)..
    This is a temporary implementation. TypeHandler will handle the pluggableness of OptionTypes and it will direct all of these types of conversion functionalities to ConvertUtils component in Commons already. BeanUtils I think.


    • Constructor Summary

      Constructors 
      Constructor Description
      TypeHandler()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Class<?> createClass​(String className)
      Returns the class whose name is className.
      static Date createDate​(String str)
      Returns the date represented by str.
      static File createFile​(String str)
      Returns the File represented by str.
      static File[] createFiles​(String str)
      Returns the File[] represented by str.
      static Number createNumber​(String str)
      Create a number from a String.
      static Object createObject​(String className)
      Create an Object from the class name and empty constructor.
      static URL createURL​(String str)
      Returns the URL represented by str.
      static <T> T createValue​(String str, Class<T> clazz)
      Returns the Object of type clazz with the value of str.
      static Object createValue​(String str, Object obj)
      Returns the Object of type obj with the value of str.
      static FileInputStream openFile​(String str)
      Returns the opened FileInputStream represented by str.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • createClass

        🡅  🡇     🗕  🗗  🗖
        public static java.lang.Class<?> createClass​(java.lang.String className)
                                              throws ParseException
        Returns the class whose name is className.
        Parameters:
        className - the class name
        Returns:
        The class if it is found
        Throws:
        ParseException - if the class could not be found
        Code:
        Exact Method Body:
         try
             { return Class.forName(className); }
        
         catch (final ClassNotFoundException e)
             { throw new ParseException("Unable to find the class: " + className); }
        
      • createDate

        🡅  🡇     🗕  🗗  🗖
        public static java.util.Date createDate​(java.lang.String str)
        Returns the date represented by str.

        This method is not yet implemented and always throws an UnsupportedOperationException.
        Parameters:
        str - the date string
        Returns:
        The date if str is a valid date string, otherwise return null.
        Throws:
        java.lang.UnsupportedOperationException - always
        Code:
        Exact Method Body:
         throw new UnsupportedOperationException("Not yet implemented");
        
      • createFile

        🡅  🡇     🗕  🗗  🗖
        public static java.io.File createFile​(java.lang.String str)
        Returns the File represented by str.
        Parameters:
        str - the File location
        Returns:
        The file represented by str.
        Code:
        Exact Method Body:
         return new File(str);
        
      • createFiles

        🡅  🡇     🗕  🗗  🗖
        public static java.io.File[] createFiles​(java.lang.String str)
        Returns the File[] represented by str.

        This method is not yet implemented and always throws an UnsupportedOperationException.
        Parameters:
        str - the paths to the files
        Returns:
        The File[] represented by str.
        Throws:
        java.lang.UnsupportedOperationException - always
        Code:
        Exact Method Body:
         // to implement/port:
         // return FileW.findFiles(str);
        
         throw new UnsupportedOperationException("Not yet implemented");
        
      • createNumber

        🡅  🡇     🗕  🗗  🗖
        public static java.lang.Number createNumber​(java.lang.String str)
                                             throws ParseException
        Create a number from a String. If a '.' is present, it creates a Double, otherwise a Long.
        Parameters:
        str - the value
        Returns:
        the number represented by str
        Throws:
        ParseException - if str is not a number
        Code:
        Exact Method Body:
         try
         {
             return (str.indexOf('.') != -1) 
                 ? Double.valueOf(str)
                 : Long.valueOf(str);
         }
        
         catch (final NumberFormatException e)
             { throw new ParseException(e.getMessage()); }
        
      • createObject

        🡅  🡇     🗕  🗗  🗖
        public static java.lang.Object createObject​(java.lang.String className)
                                             throws ParseException
        Create an Object from the class name and empty constructor.
        Parameters:
        className - the argument value
        Returns:
        the initialized object
        Throws:
        ParseException - if the class could not be found or the object could not be created
        Code:
        Exact Method Body:
         final Class<?> cl;
        
         try
             { cl = Class.forName(className); }
        
         catch (final ClassNotFoundException cnfe)
             { throw new ParseException("Unable to find the class: " + className); }
        
         try
             { return cl.getConstructor().newInstance(); }
        
         catch (final Exception e)
         {
             throw new ParseException
                 (e.getClass().getName() + "; Unable to create an instance of: " + className);
         }
        
      • createURL

        🡅  🡇     🗕  🗗  🗖
        public static java.net.URL createURL​(java.lang.String str)
                                      throws ParseException
        Returns the URL represented by str.
        Parameters:
        str - the URL string
        Returns:
        The URL in str is well-formed
        Throws:
        ParseException - if the URL in str is not well-formed
        Code:
        Exact Method Body:
         try
             { return new URL(str); }
        
         catch (final MalformedURLException e)
             { throw new ParseException("Unable to parse the URL: " + str); }
        
      • createValue

        🡅  🡇     🗕  🗗  🗖
        public static <T> T createValue​(java.lang.String str,
                                        java.lang.Class<T> clazz)
                                 throws ParseException
        Returns the Object of type clazz with the value of str.
        Type Parameters:
        T - type of argument
        Parameters:
        str - the command line value
        clazz - the class representing the type of argument
        Returns:
        The instance of clazz initialized with the value of str.
        Throws:
        ParseException - if the value creation for the given class failed
        Code:
        Exact Method Body:
         if (PatternOptionBuilder.STRING_VALUE == clazz)         return (T) str;
         if (PatternOptionBuilder.OBJECT_VALUE == clazz)         return (T) createObject(str);
         if (PatternOptionBuilder.NUMBER_VALUE == clazz)         return (T) createNumber(str);
         if (PatternOptionBuilder.DATE_VALUE == clazz)           return (T) createDate(str);
         if (PatternOptionBuilder.CLASS_VALUE == clazz)          return (T) createClass(str);
         if (PatternOptionBuilder.FILE_VALUE == clazz)           return (T) createFile(str);
         if (PatternOptionBuilder.EXISTING_FILE_VALUE == clazz)  return (T) openFile(str);
         if (PatternOptionBuilder.FILES_VALUE == clazz)          return (T) createFiles(str);
         if (PatternOptionBuilder.URL_VALUE == clazz)            return (T) createURL(str);
        
         throw new ParseException("Unable to handle the class: " + clazz);
        
      • createValue

        🡅  🡇     🗕  🗗  🗖
        public static java.lang.Object createValue​(java.lang.String str,
                                                   java.lang.Object obj)
                                            throws ParseException
        Returns the Object of type obj with the value of str.
        Parameters:
        str - the command line value
        obj - the type of argument
        Returns:
        The instance of obj initialized with the value of str.
        Throws:
        ParseException - if the value creation for the given object type failed
        Code:
        Exact Method Body:
         return createValue(str, (Class<?>) obj);
        
      • openFile

        🡅     🗕  🗗  🗖
        public static java.io.FileInputStream openFile​(java.lang.String str)
                                                throws ParseException
        Returns the opened FileInputStream represented by str.
        Parameters:
        str - the file location
        Returns:
        The file input stream represented by str.
        Throws:
        ParseException - if the file is not exist or not readable
        Code:
        Exact Method Body:
         try
             { return new FileInputStream(str); }
                
         catch (final FileNotFoundException e)
             { throw new ParseException("Unable to find file: " + str); }