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
018package org.apache.commons.cli;
019
020import java.io.File;
021
022import java.net.MalformedURLException;
023import java.net.URL;
024
025import 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 $Id$
034 */
035public 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     * @throws ParseException if the value creation for the given object type failed
046     */
047    public static Object createValue(final String str, final Object obj) 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     * @throws ParseException if the value creation for the given class failed
061     */
062    public static Object createValue(final String str, final Class<?> clazz) 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
111      * @throws ParseException if the class could not be found or the object could not be created
112      */
113    public static Object createObject(final String classname) throws ParseException
114    {
115        Class<?> cl;
116
117        try
118        {
119            cl = Class.forName(classname);
120        }
121        catch (final 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 (final 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(final String str) throws ParseException
145    {
146        try
147        {
148            if (str.indexOf('.') != -1)
149            {
150                return Double.valueOf(str);
151            }
152            return Long.valueOf(str);
153        }
154        catch (final NumberFormatException e)
155        {
156            throw new ParseException(e.getMessage());
157        }
158    }
159
160    /**
161     * Returns the class whose name is <code>classname</code>.
162     *
163     * @param classname the class name
164     * @return The class if it is found
165     * @throws ParseException if the class could not be found
166     */
167    public static Class<?> createClass(final String classname) throws ParseException
168    {
169        try
170        {
171            return Class.forName(classname);
172        }
173        catch (final ClassNotFoundException e)
174        {
175            throw new ParseException("Unable to find the class: " + classname);
176        }
177    }
178
179    /**
180     * Returns the date represented by <code>str</code>.
181     * <p>
182     * This method is not yet implemented and always throws an
183     * {@link UnsupportedOperationException}.
184     *
185     * @param str the date string
186     * @return The date if <code>str</code> is a valid date string,
187     * otherwise return null.
188     * @throws UnsupportedOperationException always
189     */
190    public static Date createDate(final String str)
191    {
192        throw new UnsupportedOperationException("Not yet implemented");
193    }
194
195    /**
196     * Returns the URL represented by <code>str</code>.
197     *
198     * @param str the URL string
199     * @return The URL in <code>str</code> is well-formed
200     * @throws ParseException if the URL in <code>str</code> is not well-formed
201     */
202    public static URL createURL(final String str) throws ParseException
203    {
204        try
205        {
206            return new URL(str);
207        }
208        catch (final MalformedURLException e)
209        {
210            throw new ParseException("Unable to parse the URL: " + str);
211        }
212    }
213
214    /**
215     * Returns the File represented by <code>str</code>.
216     *
217     * @param str the File location
218     * @return The file represented by <code>str</code>.
219     */
220    public static File createFile(final String str)
221    {
222        return new File(str);
223    }
224
225    /**
226     * Returns the File[] represented by <code>str</code>.
227     * <p>
228     * This method is not yet implemented and always throws an
229     * {@link UnsupportedOperationException}.
230     *
231     * @param str the paths to the files
232     * @return The File[] represented by <code>str</code>.
233     * @throws UnsupportedOperationException always
234     */
235    public static File[] createFiles(final String str)
236    {
237        // to implement/port:
238        //        return FileW.findFiles(str);
239        throw new UnsupportedOperationException("Not yet implemented");
240    }
241}