001: /*
002: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * [See end of file]
004: */
005:
006: package jena.cmdline;
007:
008: import java.util.*;
009:
010: /** A command line argument specification.
011: *
012: * @author Andy Seaborne
013: * @version $Id: ArgDecl.java,v 1.10 2008/01/02 12:09:55 andy_seaborne Exp $
014: */
015: public class ArgDecl {
016: boolean takesValue;
017: Set names = new HashSet();
018: boolean takesArg = false;
019: List argHooks = new ArrayList();
020: public static final boolean HasValue = true;
021: public static final boolean NoValue = false;
022:
023: /** Create a declaration for a command argument.
024: *
025: * @param hasValue Does it take a value or not?
026: */
027:
028: public ArgDecl(boolean hasValue) {
029: takesValue = hasValue;
030: }
031:
032: /** Create a declaration for a command argument.
033: *
034: * @param hasValue Does it take a value or not?
035: * @param name Name of argument
036: */
037:
038: public ArgDecl(boolean hasValue, String name) {
039: this (hasValue);
040: addName(name);
041: }
042:
043: /** Create a declaration for a command argument.
044: *
045: * @param hasValue Does it take a value or not?
046: * @param name Name of argument
047: * @param handler ArgHandler
048: */
049:
050: public ArgDecl(boolean hasValue, String name, ArgHandler handler) {
051: this (hasValue);
052: addName(name);
053: addHook(handler);
054: }
055:
056: /** Create a declaration for a command argument.
057: *
058: * @param hasValue Does it take a value or not?
059: * @param name1 Name of argument
060: * @param name2 Name of argument
061: */
062:
063: public ArgDecl(boolean hasValue, String name1, String name2) {
064: this (hasValue);
065: addName(name1);
066: addName(name2);
067: }
068:
069: /** Create a declaration for a command argument.
070: *
071: * @param hasValue Does it take a value or not?
072: * @param name1 Name of argument
073: * @param name2 Name of argument
074: * @param handler ArgHandler
075: */
076:
077: public ArgDecl(boolean hasValue, String name1, String name2,
078: ArgHandler handler) {
079: this (hasValue);
080: addName(name1);
081: addName(name2);
082: addHook(handler);
083: }
084:
085: /** Create a declaration for a command argument.
086: *
087: * @param hasValue Does it take a value or not?
088: * @param name1 Name of argument
089: * @param name2 Name of argument
090: * @param name3 Name of argument
091: */
092:
093: public ArgDecl(boolean hasValue, String name1, String name2,
094: String name3) {
095: this (hasValue);
096: addName(name1);
097: addName(name2);
098: addName(name3);
099: }
100:
101: /** Create a declaration for a command argument.
102: *
103: * @param hasValue Does it take a value or not?
104: * @param name1 Name of argument
105: * @param name2 Name of argument
106: * @param name3 Name of argument
107: * @param handler ArgHandler
108: */
109:
110: public ArgDecl(boolean hasValue, String name1, String name2,
111: String name3, ArgHandler handler) {
112: this (hasValue);
113: addName(name1);
114: addName(name2);
115: addName(name3);
116: addHook(handler);
117: }
118:
119: /** Create a declaration for a command argument.
120: *
121: * @param hasValue Does it take a value or not?
122: * @param name1 Name of argument
123: * @param name2 Name of argument
124: * @param name3 Name of argument
125: * @param name4 Name of argument
126: */
127:
128: public ArgDecl(boolean hasValue, String name1, String name2,
129: String name3, String name4) {
130: this (hasValue);
131: addName(name1);
132: addName(name2);
133: addName(name3);
134: addName(name4);
135: }
136:
137: /** Create a declaration for a command argument.
138: *
139: * @param hasValue Does it take a value or not?
140: * @param name1 Name of argument
141: * @param name2 Name of argument
142: * @param name3 Name of argument
143: * @param name4 Name of argument
144: * @param handler ArgHandler
145: */
146:
147: public ArgDecl(boolean hasValue, String name1, String name2,
148: String name3, String name4, ArgHandler handler) {
149: this (hasValue);
150: addName(name1);
151: addName(name2);
152: addName(name3);
153: addName(name4);
154: addHook(handler);
155: }
156:
157: /** Create a declaration for a command argument.
158: *
159: * @param hasValue Does it take a value or not?
160: * @param name1 Name of argument
161: * @param name2 Name of argument
162: * @param name3 Name of argument
163: * @param name4 Name of argument
164: * @param name5 Name of argument
165: * @param handler ArgHandler
166: */
167:
168: public ArgDecl(boolean hasValue, String name1, String name2,
169: String name3, String name4, String name5, ArgHandler handler) {
170: this (hasValue);
171: addName(name1);
172: addName(name2);
173: addName(name3);
174: addName(name4);
175: addName(name5);
176: addHook(handler);
177: }
178:
179: public void addName(String name) {
180: name = canonicalForm(name);
181: names.add(name);
182: }
183:
184: public Set getNames() {
185: return names;
186: }
187:
188: public Iterator names() {
189: return names.iterator();
190: }
191:
192: // Callback model
193:
194: public void addHook(ArgHandler argHandler) {
195: argHooks.add(argHandler);
196: }
197:
198: protected void trigger(Arg arg) {
199: for (Iterator iter = argHooks.iterator(); iter.hasNext();) {
200: ArgHandler handler = (ArgHandler) iter.next();
201: handler.action(arg.getName(), arg.getValue());
202: }
203: }
204:
205: public boolean takesValue() {
206: return takesValue;
207: }
208:
209: public boolean matches(Arg a) {
210: for (Iterator iter = names.iterator(); iter.hasNext();) {
211: String n = (String) iter.next();
212: if (a.getName().equals(n))
213: return true;
214: }
215: return false;
216: }
217:
218: public boolean matches(String arg) {
219: arg = canonicalForm(arg);
220: return names.contains(arg);
221: }
222:
223: static String canonicalForm(String str) {
224: if (str.startsWith("--"))
225: return str.substring(2);
226:
227: if (str.startsWith("-"))
228: return str.substring(1);
229:
230: return str;
231: }
232: }
233:
234: /*
235: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
236: * All rights reserved.
237: *
238: * Redistribution and use in source and binary forms, with or without
239: * modification, are permitted provided that the following conditions
240: * are met:
241: * 1. Redistributions of source code must retain the above copyright
242: * notice, this list of conditions and the following disclaimer.
243: * 2. Redistributions in binary form must reproduce the above copyright
244: * notice, this list of conditions and the following disclaimer in the
245: * documentation and/or other materials provided with the distribution.
246: * 3. The name of the author may not be used to endorse or promote products
247: * derived from this software without specific prior written permission.
248: *
249: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
250: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
251: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
252: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
253: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
254: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
255: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
256: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
257: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
258: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
259: */
|