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