1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.cli;
19
20 import java.io.File;
21
22 import java.net.MalformedURLException;
23 import java.net.URL;
24
25 import java.util.Date;
26
27 /**
28 * This is a temporary implementation. TypeHandler will handle the
29 * pluggableness of OptionTypes and it will direct all of these types
30 * of conversion functionalities to ConvertUtils component in Commons
31 * already. BeanUtils I think.
32 *
33 * @version $Id: TypeHandler.java 1443102 2013-02-06 18:12:16Z tn $
34 */
35 public class TypeHandler
36 {
37 /**
38 * Returns the <code>Object</code> of type <code>obj</code>
39 * with the value of <code>str</code>.
40 *
41 * @param str the command line value
42 * @param obj the type of argument
43 * @return The instance of <code>obj</code> initialised with
44 * the value of <code>str</code>.
45 * @throws ParseException if the value creation for the given object type failed
46 */
47 public static Object createValue(String str, Object obj) throws ParseException
48 {
49 return createValue(str, (Class<?>) obj);
50 }
51
52 /**
53 * Returns the <code>Object</code> of type <code>clazz</code>
54 * with the value of <code>str</code>.
55 *
56 * @param str the command line value
57 * @param clazz the type of argument
58 * @return The instance of <code>clazz</code> initialised with
59 * the value of <code>str</code>.
60 * @throws ParseException if the value creation for the given class failed
61 */
62 public static Object createValue(String str, Class<?> clazz) throws ParseException
63 {
64 if (PatternOptionBuilder.STRING_VALUE == clazz)
65 {
66 return str;
67 }
68 else if (PatternOptionBuilder.OBJECT_VALUE == clazz)
69 {
70 return createObject(str);
71 }
72 else if (PatternOptionBuilder.NUMBER_VALUE == clazz)
73 {
74 return createNumber(str);
75 }
76 else if (PatternOptionBuilder.DATE_VALUE == clazz)
77 {
78 return createDate(str);
79 }
80 else if (PatternOptionBuilder.CLASS_VALUE == clazz)
81 {
82 return createClass(str);
83 }
84 else if (PatternOptionBuilder.FILE_VALUE == clazz)
85 {
86 return createFile(str);
87 }
88 else if (PatternOptionBuilder.EXISTING_FILE_VALUE == clazz)
89 {
90 return createFile(str);
91 }
92 else if (PatternOptionBuilder.FILES_VALUE == clazz)
93 {
94 return createFiles(str);
95 }
96 else if (PatternOptionBuilder.URL_VALUE == clazz)
97 {
98 return createURL(str);
99 }
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
111 * @throws ParseException if the class could not be found or the object could not be created
112 */
113 public static Object createObject(String classname) throws ParseException
114 {
115 Class<?> cl;
116
117 try
118 {
119 cl = Class.forName(classname);
120 }
121 catch (ClassNotFoundException cnfe)
122 {
123 throw new ParseException("Unable to find the class: " + classname);
124 }
125
126 try
127 {
128 return cl.newInstance();
129 }
130 catch (Exception e)
131 {
132 throw new ParseException(e.getClass().getName() + "; Unable to create an instance of: " + classname);
133 }
134 }
135
136 /**
137 * Create a number from a String. If a . is present, it creates a
138 * Double, otherwise a Long.
139 *
140 * @param str the value
141 * @return the number represented by <code>str</code>
142 * @throws ParseException if <code>str</code> is not a number
143 */
144 public static Number createNumber(String str) throws ParseException
145 {
146 try
147 {
148 if (str.indexOf('.') != -1)
149 {
150 return Double.valueOf(str);
151 }
152 else
153 {
154 return Long.valueOf(str);
155 }
156 }
157 catch (NumberFormatException e)
158 {
159 throw new ParseException(e.getMessage());
160 }
161 }
162
163 /**
164 * Returns the class whose name is <code>classname</code>.
165 *
166 * @param classname the class name
167 * @return The class if it is found
168 * @throws ParseException if the class could not be found
169 */
170 public static Class<?> createClass(String classname) throws ParseException
171 {
172 try
173 {
174 return Class.forName(classname);
175 }
176 catch (ClassNotFoundException e)
177 {
178 throw new ParseException("Unable to find the class: " + classname);
179 }
180 }
181
182 /**
183 * Returns the date represented by <code>str</code>.
184 * <p>
185 * This method is not yet implemented and always throws an
186 * {@link UnsupportedOperationException}.
187 *
188 * @param str the date string
189 * @return The date if <code>str</code> is a valid date string,
190 * otherwise return null.
191 * @throws UnsupportedOperationException always
192 */
193 public static Date createDate(String str)
194 {
195 throw new UnsupportedOperationException("Not yet implemented");
196 }
197
198 /**
199 * Returns the URL represented by <code>str</code>.
200 *
201 * @param str the URL string
202 * @return The URL in <code>str</code> is well-formed
203 * @throws ParseException if the URL in <code>str</code> is not well-formed
204 */
205 public static URL createURL(String str) throws ParseException
206 {
207 try
208 {
209 return new URL(str);
210 }
211 catch (MalformedURLException e)
212 {
213 throw new ParseException("Unable to parse the URL: " + str);
214 }
215 }
216
217 /**
218 * Returns the File represented by <code>str</code>.
219 *
220 * @param str the File location
221 * @return The file represented by <code>str</code>.
222 */
223 public static File createFile(String str)
224 {
225 return new File(str);
226 }
227
228 /**
229 * Returns the File[] represented by <code>str</code>.
230 * <p>
231 * This method is not yet implemented and always throws an
232 * {@link UnsupportedOperationException}.
233 *
234 * @param str the paths to the files
235 * @return The File[] represented by <code>str</code>.
236 * @throws UnsupportedOperationException always
237 */
238 public static File[] createFiles(String str)
239 {
240 // to implement/port:
241 // return FileW.findFiles(str);
242 throw new UnsupportedOperationException("Not yet implemented");
243 }
244 }