001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.cli;
019
020 import java.io.File;
021
022 import java.net.MalformedURLException;
023 import java.net.URL;
024
025 import java.util.Date;
026
027 /**
028 * This is a temporary implementation. TypeHandler will handle the
029 * pluggableness of OptionTypes and it will direct all of these types
030 * of conversion functionalities to ConvertUtils component in Commons
031 * already. BeanUtils I think.
032 *
033 * @version $Revision: 741425 $, $Date: 2009-02-05 22:10:54 -0800 (Thu, 05 Feb 2009) $
034 */
035 public class TypeHandler
036 {
037 /**
038 * Returns the <code>Object</code> of type <code>obj</code>
039 * with the value of <code>str</code>.
040 *
041 * @param str the command line value
042 * @param obj the type of argument
043 * @return The instance of <code>obj</code> initialised with
044 * the value of <code>str</code>.
045 */
046 public static Object createValue(String str, Object obj)
047 throws ParseException
048 {
049 return createValue(str, (Class) obj);
050 }
051
052 /**
053 * Returns the <code>Object</code> of type <code>clazz</code>
054 * with the value of <code>str</code>.
055 *
056 * @param str the command line value
057 * @param clazz the type of argument
058 * @return The instance of <code>clazz</code> initialised with
059 * the value of <code>str</code>.
060 */
061 public static Object createValue(String str, Class clazz)
062 throws ParseException
063 {
064 if (PatternOptionBuilder.STRING_VALUE == clazz)
065 {
066 return str;
067 }
068 else if (PatternOptionBuilder.OBJECT_VALUE == clazz)
069 {
070 return createObject(str);
071 }
072 else if (PatternOptionBuilder.NUMBER_VALUE == clazz)
073 {
074 return createNumber(str);
075 }
076 else if (PatternOptionBuilder.DATE_VALUE == clazz)
077 {
078 return createDate(str);
079 }
080 else if (PatternOptionBuilder.CLASS_VALUE == clazz)
081 {
082 return createClass(str);
083 }
084 else if (PatternOptionBuilder.FILE_VALUE == clazz)
085 {
086 return createFile(str);
087 }
088 else if (PatternOptionBuilder.EXISTING_FILE_VALUE == clazz)
089 {
090 return createFile(str);
091 }
092 else if (PatternOptionBuilder.FILES_VALUE == clazz)
093 {
094 return createFiles(str);
095 }
096 else if (PatternOptionBuilder.URL_VALUE == clazz)
097 {
098 return createURL(str);
099 }
100 else
101 {
102 return null;
103 }
104 }
105
106 /**
107 * Create an Object from the classname and empty constructor.
108 *
109 * @param classname the argument value
110 * @return the initialised object, or null if it couldn't create
111 * the Object.
112 */
113 public static Object createObject(String classname)
114 throws ParseException
115 {
116 Class cl = null;
117
118 try
119 {
120 cl = Class.forName(classname);
121 }
122 catch (ClassNotFoundException cnfe)
123 {
124 throw new ParseException("Unable to find the class: " + classname);
125 }
126
127 Object instance = null;
128
129 try
130 {
131 instance = cl.newInstance();
132 }
133 catch (Exception e)
134 {
135 throw new ParseException(e.getClass().getName() + "; Unable to create an instance of: " + classname);
136 }
137
138 return instance;
139 }
140
141 /**
142 * Create a number from a String. If a . is present, it creates a
143 * Double, otherwise a Long.
144 *
145 * @param str the value
146 * @return the number represented by <code>str</code>, if <code>str</code>
147 * is not a number, null is returned.
148 */
149 public static Number createNumber(String str)
150 throws ParseException
151 {
152 try
153 {
154 if (str.indexOf('.') != -1)
155 {
156 return Double.valueOf(str);
157 }
158 else
159 {
160 return Long.valueOf(str);
161 }
162 }
163 catch (NumberFormatException e)
164 {
165 throw new ParseException(e.getMessage());
166 }
167 }
168
169 /**
170 * Returns the class whose name is <code>classname</code>.
171 *
172 * @param classname the class name
173 * @return The class if it is found, otherwise return null
174 */
175 public static Class createClass(String classname)
176 throws ParseException
177 {
178 try
179 {
180 return Class.forName(classname);
181 }
182 catch (ClassNotFoundException e)
183 {
184 throw new ParseException("Unable to find the class: " + classname);
185 }
186 }
187
188 /**
189 * Returns the date represented by <code>str</code>.
190 *
191 * @param str the date string
192 * @return The date if <code>str</code> is a valid date string,
193 * otherwise return null.
194 */
195 public static Date createDate(String str)
196 throws ParseException
197 {
198 throw new UnsupportedOperationException("Not yet implemented");
199 }
200
201 /**
202 * Returns the URL represented by <code>str</code>.
203 *
204 * @param str the URL string
205 * @return The URL is <code>str</code> is well-formed, otherwise
206 * return null.
207 */
208 public static URL createURL(String str)
209 throws ParseException
210 {
211 try
212 {
213 return new URL(str);
214 }
215 catch (MalformedURLException e)
216 {
217 throw new ParseException("Unable to parse the URL: " + str);
218 }
219 }
220
221 /**
222 * Returns the File represented by <code>str</code>.
223 *
224 * @param str the File location
225 * @return The file represented by <code>str</code>.
226 */
227 public static File createFile(String str)
228 throws ParseException
229 {
230 return new File(str);
231 }
232
233 /**
234 * Returns the File[] represented by <code>str</code>.
235 *
236 * @param str the paths to the files
237 * @return The File[] represented by <code>str</code>.
238 */
239 public static File[] createFiles(String str)
240 throws ParseException
241 {
242 // to implement/port:
243 // return FileW.findFiles(str);
244 throw new UnsupportedOperationException("Not yet implemented");
245 }
246 }