View Javadoc
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  import java.io.FileInputStream;
22  import java.io.FileNotFoundException;
23  
24  import java.net.MalformedURLException;
25  import java.net.URL;
26  
27  import java.util.Date;
28  
29  /**
30   * This is a temporary implementation. TypeHandler will handle the
31   * pluggableness of OptionTypes and it will direct all of these types
32   * of conversion functionalities to ConvertUtils component in Commons
33   * already. BeanUtils I think.
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(final String str, final 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 class representing the type of argument
58       * @param <T> type of argument
59       * @return The instance of <code>clazz</code> initialised with
60       * the value of <code>str</code>.
61       * @throws ParseException if the value creation for the given class failed
62       */
63      @SuppressWarnings("unchecked") // returned value will have type T because it is fixed by clazz
64      public static <T> T createValue(final String str, final Class<T> clazz) throws ParseException
65      {
66          if (PatternOptionBuilder.STRING_VALUE == clazz)
67          {
68              return (T) str;
69          }
70          else if (PatternOptionBuilder.OBJECT_VALUE == clazz)
71          {
72              return (T) createObject(str);
73          }
74          else if (PatternOptionBuilder.NUMBER_VALUE == clazz)
75          {
76              return (T) createNumber(str);
77          }
78          else if (PatternOptionBuilder.DATE_VALUE == clazz)
79          {
80              return (T) createDate(str);
81          }
82          else if (PatternOptionBuilder.CLASS_VALUE == clazz)
83          {
84              return (T) createClass(str);
85          }
86          else if (PatternOptionBuilder.FILE_VALUE == clazz)
87          {
88              return (T) createFile(str);
89          }
90          else if (PatternOptionBuilder.EXISTING_FILE_VALUE == clazz)
91          {
92              return (T) openFile(str);
93          }
94          else if (PatternOptionBuilder.FILES_VALUE == clazz)
95          {
96              return (T) createFiles(str);
97          }
98          else if (PatternOptionBuilder.URL_VALUE == clazz)
99          {
100             return (T) createURL(str);
101         }
102         else
103         {
104             throw new ParseException("Unable to handle the class: " + clazz);
105         }
106     }
107 
108     /**
109       * Create an Object from the classname and empty constructor.
110       *
111       * @param classname the argument value
112       * @return the initialised object
113       * @throws ParseException if the class could not be found or the object could not be created
114       */
115     public static Object createObject(final String classname) throws ParseException
116     {
117         Class<?> cl;
118 
119         try
120         {
121             cl = Class.forName(classname);
122         }
123         catch (final ClassNotFoundException cnfe)
124         {
125             throw new ParseException("Unable to find the class: " + classname);
126         }
127         
128         try
129         {
130             return cl.newInstance();
131         }
132         catch (final Exception e)
133         {
134             throw new ParseException(e.getClass().getName() + "; Unable to create an instance of: " + classname);
135         }
136     }
137 
138     /**
139      * Create a number from a String. If a . is present, it creates a
140      * Double, otherwise a Long.
141      *
142      * @param str the value
143      * @return the number represented by <code>str</code>
144      * @throws ParseException if <code>str</code> is not a number
145      */
146     public static Number createNumber(final String str) throws ParseException
147     {
148         try
149         {
150             if (str.indexOf('.') != -1)
151             {
152                 return Double.valueOf(str);
153             }
154             return Long.valueOf(str);
155         }
156         catch (final NumberFormatException e)
157         {
158             throw new ParseException(e.getMessage());
159         }
160     }
161 
162     /**
163      * Returns the class whose name is <code>classname</code>.
164      *
165      * @param classname the class name
166      * @return The class if it is found
167      * @throws ParseException if the class could not be found
168      */
169     public static Class<?> createClass(final String classname) throws ParseException
170     {
171         try
172         {
173             return Class.forName(classname);
174         }
175         catch (final ClassNotFoundException e)
176         {
177             throw new ParseException("Unable to find the class: " + classname);
178         }
179     }
180 
181     /**
182      * Returns the date represented by <code>str</code>.
183      * <p>
184      * This method is not yet implemented and always throws an
185      * {@link UnsupportedOperationException}.
186      *
187      * @param str the date string
188      * @return The date if <code>str</code> is a valid date string,
189      * otherwise return null.
190      * @throws UnsupportedOperationException always
191      */
192     public static Date createDate(final String str)
193     {
194         throw new UnsupportedOperationException("Not yet implemented");
195     }
196 
197     /**
198      * Returns the URL represented by <code>str</code>.
199      *
200      * @param str the URL string
201      * @return The URL in <code>str</code> is well-formed
202      * @throws ParseException if the URL in <code>str</code> is not well-formed
203      */
204     public static URL createURL(final String str) throws ParseException
205     {
206         try
207         {
208             return new URL(str);
209         }
210         catch (final MalformedURLException e)
211         {
212             throw new ParseException("Unable to parse the URL: " + str);
213         }
214     }
215 
216     /**
217      * Returns the File represented by <code>str</code>.
218      *
219      * @param str the File location
220      * @return The file represented by <code>str</code>.
221      */
222     public static File createFile(final String str)
223     {
224         return new File(str);
225     }
226 
227     /**
228      * Returns the opened FileInputStream represented by <code>str</code>.
229      *
230      * @param str the file location
231      * @return The file input stream represented by <code>str</code>.
232      * @throws ParseException if the file is not exist or not readable
233      */
234     public static FileInputStream openFile(String str) throws ParseException
235     {
236         try
237         {
238             return new FileInputStream(str);
239         }
240         catch (FileNotFoundException e)
241         {
242             throw new ParseException("Unable to find file: " + str);
243         }
244     }
245 
246     /**
247      * Returns the File[] represented by <code>str</code>.
248      * <p>
249      * This method is not yet implemented and always throws an
250      * {@link UnsupportedOperationException}.
251      *
252      * @param str the paths to the files
253      * @return The File[] represented by <code>str</code>.
254      * @throws UnsupportedOperationException always
255      */
256     public static File[] createFiles(final String str)
257     {
258         // to implement/port:
259         //        return FileW.findFiles(str);
260         throw new UnsupportedOperationException("Not yet implemented");
261     }
262 }