KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jena > cmdline > ArgDecl


1 /*
2  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6 package jena.cmdline ;
7
8 import java.util.* ;
9
10 /** A command line argument specification.
11  *
12  * @author Andy Seaborne
13  * @version $Id: ArgDecl.java,v 1.4 2005/02/21 11:48:56 andy_seaborne Exp $
14  */

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 ; // i.e. no value
22
public static final int OPTION = 2 ; // takes a value as well
23

24     
25     public ArgDecl(boolean hasValue)
26     {
27         takesValue = hasValue ;
28     }
29     
30     // Convenience constructors
31

32     public ArgDecl(boolean hasValue, String JavaDoc name)
33     {
34         this(hasValue) ;
35         addName(name) ;
36     }
37     public ArgDecl(boolean hasValue, String JavaDoc name1, String JavaDoc name2)
38     {
39         this(hasValue) ;
40         addName(name1) ;
41         addName(name2) ;
42     }
43     
44     public ArgDecl(boolean hasValue, String JavaDoc name1, String JavaDoc name2, String JavaDoc name3)
45     {
46         this(hasValue) ;
47         addName(name1) ;
48         addName(name2) ;
49         addName(name3) ;
50     }
51     
52     public void addName(String JavaDoc name)
53     {
54         name = canonicalForm(name) ;
55         names.add(name) ;
56     }
57     
58     public Iterator getNames() { return names.iterator() ; }
59     
60     // Callback model
61

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 JavaDoc n = (String JavaDoc)iter.next() ;
83             if ( a.getName().equals(n) )
84                 return true ;
85         }
86         return false ;
87     }
88     
89     public boolean matches(String JavaDoc arg)
90     {
91         arg = canonicalForm(arg) ;
92         return names.contains(arg) ;
93     }
94     
95     static String JavaDoc canonicalForm(String JavaDoc 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 /*
108  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
109  * All rights reserved.
110  *
111  * Redistribution and use in source and binary forms, with or without
112  * modification, are permitted provided that the following conditions
113  * are met:
114  * 1. Redistributions of source code must retain the above copyright
115  * notice, this list of conditions and the following disclaimer.
116  * 2. Redistributions in binary form must reproduce the above copyright
117  * notice, this list of conditions and the following disclaimer in the
118  * documentation and/or other materials provided with the distribution.
119  * 3. The name of the author may not be used to endorse or promote products
120  * derived from this software without specific prior written permission.
121  *
122  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
123  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
124  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
125  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
126  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
127  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
128  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
129  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
130  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
131  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
132  */

133
Popular Tags