- java.lang.Object
-
- Apache.CLI.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
.- The suggested license for using this file may be read here:
Apache License
- The Source-Code Header for this file may be viewed here:
Source-File Header
- 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.
Hi-Lited Source-Code:- View Here: Apache/CLI/TypeHandler.java
- Open New Browser-Tab: Apache/CLI/TypeHandler.java
File Size: 7,679 Bytes Line Count: 200 '\n' Characters Found
- The suggested license for using this file may be read here:
-
-
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 isclassName
.static Date
createDate(String str)
Returns the date represented bystr
.static File
createFile(String str)
Returns the File represented bystr
.static File[]
createFiles(String str)
Returns the File[] represented bystr
.static Number
createNumber(String str)
Create a number from aString
.static Object
createObject(String className)
Create an Object from the class name and empty constructor.static URL
createURL(String str)
Returns the URL represented bystr
.static <T> T
createValue(String str, Class<T> clazz)
Returns theObject
of typeclazz
with the value ofstr
.static Object
createValue(String str, Object obj)
Returns theObject
of typeobj
with the value ofstr
.static FileInputStream
openFile(String str)
Returns the opened FileInputStream represented bystr
.
-
-
-
Constructor Detail
-
TypeHandler
public TypeHandler()
-
-
Method Detail
-
createClass
public static java.lang.Class<?> createClass(java.lang.String className) throws ParseException
Returns the class whose name isclassName
.- 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 bystr
.
This method is not yet implemented and always throws anUnsupportedOperationException
.- 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 bystr
.- 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 bystr
.
This method is not yet implemented and always throws anUnsupportedOperationException
.- 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 aString
. If a'.'
is present, it creates aDouble
, otherwise aLong
.- Parameters:
str
- the value- Returns:
- the number represented by
str
- Throws:
ParseException
- ifstr
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 bystr
.- Parameters:
str
- the URL string- Returns:
- The URL in
str
is well-formed - Throws:
ParseException
- if the URL instr
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 theObject
of typeclazz
with the value ofstr
.- Type Parameters:
T
- type of argument- Parameters:
str
- the command line valueclazz
- the class representing the type of argument- Returns:
- The instance of
clazz
initialized with the value ofstr
. - 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 theObject
of typeobj
with the value ofstr
.- Parameters:
str
- the command line valueobj
- the type of argument- Returns:
- The instance of
obj
initialized with the value ofstr
. - 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 bystr
.- 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); }
-
-