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