1 5 6 package jena.cmdline ; 7 8 import java.util.* ; 9 10 15 public class ArgDecl 16 { 17 boolean takesValue ; 18 Set names = new HashSet() ; 19 boolean takesArg = false ; 20 List argHooks = new ArrayList() ; 21 public static final int FLAG = 1 ; public static final int OPTION = 2 ; 24 25 public ArgDecl(boolean hasValue) 26 { 27 takesValue = hasValue ; 28 } 29 30 32 public ArgDecl(boolean hasValue, String name) 33 { 34 this(hasValue) ; 35 addName(name) ; 36 } 37 public ArgDecl(boolean hasValue, String name1, String name2) 38 { 39 this(hasValue) ; 40 addName(name1) ; 41 addName(name2) ; 42 } 43 44 public ArgDecl(boolean hasValue, String name1, String name2, String name3) 45 { 46 this(hasValue) ; 47 addName(name1) ; 48 addName(name2) ; 49 addName(name3) ; 50 } 51 52 public void addName(String name) 53 { 54 name = canonicalForm(name) ; 55 names.add(name) ; 56 } 57 58 public Iterator getNames() { return names.iterator() ; } 59 60 62 public void addHook(ArgHandler argHandler) 63 { 64 argHooks.add(argHandler) ; 65 } 66 67 protected void trigger(Arg arg) 68 { 69 for ( Iterator iter = argHooks.iterator() ; iter.hasNext() ; ) 70 { 71 ArgHandler handler = (ArgHandler)iter.next() ; 72 handler.action(arg.getName(), arg.getValue()) ; 73 } 74 } 75 76 public boolean takesValue() { return takesValue ; } 77 78 public boolean matches(Arg a) 79 { 80 for ( Iterator iter = names.iterator() ; iter.hasNext() ; ) 81 { 82 String n = (String )iter.next() ; 83 if ( a.getName().equals(n) ) 84 return true ; 85 } 86 return false ; 87 } 88 89 public boolean matches(String arg) 90 { 91 arg = canonicalForm(arg) ; 92 return names.contains(arg) ; 93 } 94 95 static String canonicalForm(String str) 96 { 97 if ( str.startsWith("--") ) 98 return str.substring(2) ; 99 100 if ( str.startsWith("-") ) 101 return str.substring(1) ; 102 103 return str ; 104 } 105 } 106 107 133 | Popular Tags |