1 19 20 package org.netbeans.spi.sendopts; 21 22 import java.io.File ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.io.PrintWriter ; 26 import java.util.Arrays ; 27 import java.util.Collections ; 28 import java.util.Locale ; 29 import java.util.Map ; 30 import java.util.MissingResourceException ; 31 import java.util.ResourceBundle ; 32 import java.util.logging.Level ; 33 import org.netbeans.api.sendopts.CommandException; 34 import org.netbeans.modules.sendopts.OptionImpl; 35 import org.openide.util.Lookup; 36 37 45 public final class Option { 46 48 private final int shortName; 49 50 private final String longName; 51 52 final OptionImpl impl; 53 54 private final String [] keys; 55 private final String [] bundles; 56 57 59 public static final char NO_SHORT_NAME = (char)-1; 60 61 private static String [] EMPTY = new String [2]; 62 63 64 private Option(char shortName, String longName, int type) { 65 this.shortName = shortName == NO_SHORT_NAME ? -1 : (int)shortName; 66 this.longName = longName; 67 switch (type) { 68 case 0: this.impl = OptionImpl.createNoArg(this); break; 69 case 1: this.impl = OptionImpl.createOneArg(this, false); break; 70 case 2: this.impl = OptionImpl.createOneArg(this, true); break; 71 case 3: this.impl = OptionImpl.createAdd(this, false); break; 72 case 4: this.impl = OptionImpl.createAdd(this, true); break; 73 default: throw new IllegalArgumentException ("Type: " + type); } 75 this.keys = EMPTY; 76 this.bundles = EMPTY; 77 } 78 79 80 Option(int type, Option[] arr) { 81 this.shortName = -1; 82 this.longName = null; 83 this.impl = OptionImpl.create(this, type, Arrays.asList(arr)); 84 this.keys = EMPTY; 85 this.bundles = EMPTY; 86 } 87 88 90 private Option(Option old, int typeOfDescription, String bundle, String description) { 91 this.shortName = old.shortName; 92 this.longName = old.longName; 93 this.impl = OptionImpl.cloneImpl(old.impl, this, null); 94 this.keys = (String [])old.keys.clone(); 95 this.bundles = (String [])old.bundles.clone(); 96 97 this.keys[typeOfDescription] = description; 98 this.bundles[typeOfDescription] = bundle; 99 100 } 101 102 106 public String toString() { 107 StringBuffer sb = new StringBuffer (); 108 sb.append(getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(this))); sb.append('['); 110 sb.append(shortName); 111 sb.append(','); 112 sb.append(longName); 113 sb.append(','); 114 impl.append(sb); 115 sb.append(']'); 116 return sb.toString(); 117 } 118 119 122 public boolean equals(Object o) { 123 if (o instanceof Option) { 124 Option option = (Option)o; 125 return impl.root == option.impl.root; 126 } 127 return false; 128 } 129 130 public int hashCode() { 131 return System.identityHashCode(impl.root); 132 } 133 134 152 public static Option withoutArgument(char shortName, String longName) { 153 return new Option(shortName, longName, 0); 154 } 155 156 184 public static Option optionalArgument(char shortName, String longName) { 185 return new Option(shortName, longName, 1); 186 } 187 188 213 public static Option requiredArgument(char shortName, String longName) { 214 return new Option(shortName, longName, 2); 215 } 216 217 251 public static Option additionalArguments(char shortName, String longName) { 252 return new Option(shortName, longName, 3); 253 } 254 291 public static Option defaultArguments() { 292 return new Option(NO_SHORT_NAME, null, 4); 293 } 294 295 305 public static Option displayName(Option option, String bundleName, String key) { 306 return new Option(option, 0, bundleName, key); 307 } 308 309 318 public static Option shortDescription(Option option, String bundleName, String key) { 319 return new Option(option, 1, bundleName, key); 320 } 321 322 323 static { 324 OptionImpl.Trampoline.DEFAULT = new OptionImpl.Trampoline() { 325 public OptionImpl impl(Option o) { 326 return o.impl; 327 } 328 public Env create(InputStream is, OutputStream os, OutputStream err, File currentDir) { 329 return new Env(is, os, err, currentDir); 330 } 331 332 public void usage(PrintWriter w, Option o, int max) { 333 if (o.keys[1] != null) { 334 w.print(key(o.bundles[1], o.keys[1], Locale.getDefault())); 335 } 336 } 337 public Option[] getOptions(OptionProcessor p) { 338 return p.getOptions().toArray(new Option[0]); 339 } 340 public void process(OptionProcessor provider, Env env, Map <Option,String []> options) throws CommandException { 341 provider.process(env, Collections.unmodifiableMap(options)); 342 } 343 public String getLongName(Option o) { 344 return o.longName; 345 } 346 public int getShortName(Option o) { 347 return o.shortName; 348 } 349 public String getDisplayName(Option o, Locale l) { 350 return key(o.bundles[0], o.keys[0], l); 351 } 352 private String key(String bundle, String key, Locale l) { 353 if (key == null) { 354 return null; 355 } 356 ClassLoader loader = Lookup.getDefault().lookup(ClassLoader .class); 357 if (loader == null) { 358 loader = Thread.currentThread().getContextClassLoader(); 359 } 360 if (loader == null) { 361 loader = getClass().getClassLoader(); 362 } 363 try { 364 ResourceBundle b = ResourceBundle.getBundle(bundle, l, loader); 365 return b.getString(key); 366 } catch (MissingResourceException ex) { 367 OptionImpl.LOG.log(Level.WARNING, null, ex); 368 return key; 369 } 370 371 } 372 }; 373 } 374 } 375 | Popular Tags |