1 61 62 package org.apache.commons.cli; 63 64 71 public class PatternOptionBuilder { 72 73 75 76 public static final Class STRING_VALUE = java.lang.String .class; 77 78 public static final Class OBJECT_VALUE = java.lang.Object .class; 79 80 public static final Class NUMBER_VALUE = java.lang.Number .class; 81 82 public static final Class DATE_VALUE = java.util.Date .class; 83 84 public static final Class CLASS_VALUE = java.lang.Class .class; 85 86 90 public static final Class EXISTING_FILE_VALUE = java.io.FileInputStream .class; 91 92 public static final Class FILE_VALUE = java.io.File .class; 93 94 public static final Class FILES_VALUE = java.io.File [].class; 95 96 public static final Class URL_VALUE = java.net.URL .class; 97 98 104 public static Object getValueClass(char ch) { 105 if (ch == '@') { 106 return PatternOptionBuilder.OBJECT_VALUE; 107 } else if (ch == ':') { 108 return PatternOptionBuilder.STRING_VALUE; 109 } else if (ch == '%') { 110 return PatternOptionBuilder.NUMBER_VALUE; 111 } else if (ch == '+') { 112 return PatternOptionBuilder.CLASS_VALUE; 113 } else if (ch == '#') { 114 return PatternOptionBuilder.DATE_VALUE; 115 } else if (ch == '<') { 116 return PatternOptionBuilder.EXISTING_FILE_VALUE; 117 } else if (ch == '>') { 118 return PatternOptionBuilder.FILE_VALUE; 119 } else if (ch == '*') { 120 return PatternOptionBuilder.FILES_VALUE; 121 } else if (ch == '/') { 122 return PatternOptionBuilder.URL_VALUE; 123 } 124 return null; 125 } 126 127 134 public static boolean isValueCode(char ch) { 135 if( (ch != '@') && 136 (ch != ':') && 137 (ch != '%') && 138 (ch != '+') && 139 (ch != '#') && 140 (ch != '<') && 141 (ch != '>') && 142 (ch != '*') && 143 (ch != '/') 144 ) 145 { 146 return false; 147 } 148 return true; 149 } 150 151 158 public static Options parsePattern(String pattern) { 159 int sz = pattern.length(); 160 161 char opt = ' '; 162 char ch = ' '; 163 boolean required = false; 164 Object type = null; 165 166 Options options = new Options(); 167 168 for(int i=0; i<sz; i++) { 169 ch = pattern.charAt(i); 170 171 if(!isValueCode(ch)) { 174 if(opt != ' ') { 175 options.addOption( OptionBuilder.hasArg( type != null ) 177 .isRequired( required ) 178 .withType( type ) 179 .create( opt ) ); 180 required = false; 181 type = null; 182 opt = ' '; 183 } 184 opt = ch; 185 } else 186 if(ch == '!') { 187 required = true; 188 } else { 189 type = getValueClass(ch); 190 } 191 } 192 193 if(opt != ' ') { 194 options.addOption( OptionBuilder.hasArg( type != null ) 196 .isRequired( required ) 197 .withType( type ) 198 .create( opt ) ); 199 } 200 201 return options; 202 } 203 204 } 205 | Popular Tags |