01: /**
02: * Objective Database Abstraction Layer (ODAL)
03: * Copyright (c) 2004, The ODAL Development Group
04: * All rights reserved.
05: * For definition of the ODAL Development Group please refer to LICENCE.txt file
06: *
07: * Distributable under LGPL license.
08: * See terms of license at gnu.org.
09: */package com.completex.objective.util.cmd;
10:
11: import java.util.ArrayList;
12: import java.util.HashMap;
13: import java.util.Map;
14:
15: /**
16: * @author Gennady Krizhevsky
17: */
18: class ArgProcessor {
19:
20: private static final boolean DEBUG = false;
21: // -- Args as passed into application
22: private String initArgs[] = null;
23: // -- All arg types we will process. Must be derived from AbstractArg.
24: private ArrayList args = new ArrayList();
25:
26: // -- Adds one argument type for when we process
27: protected void add(AbstractArg arg) {
28: debug("ArgProcessor::add arg=" + arg);
29: args.add(arg);
30: }
31:
32: // -- Look for an argument type that matches the passed-in target
33: protected AbstractArg findArg(String argName) {
34: AbstractArg foundArg = null;
35: for (int i = 0; i < args.size(); i++) {
36: AbstractArg testArg = (AbstractArg) args.get(i);
37: if (testArg.getName().equalsIgnoreCase(argName)) {
38: foundArg = testArg;
39: if (foundArg != null) {
40: break;
41: }
42: }
43: }
44: return foundArg;
45: }
46:
47: // -- Constructor. Hang on to the arguments for when we need to
48: // -- process them later.
49: protected ArgProcessor(String args[]) {
50: this .initArgs = args;
51: }
52:
53: // -- Process the initArgs that were passed into the application and store
54: // -- the results back in the argument types passed into the add method
55: protected Map process() {
56: Map namedArgs = new HashMap();
57: for (int index = 1; index <= initArgs.length; index++) {
58: String argName = initArgs[index - 1];
59: AbstractArg arg = findArg(argName);
60: if (arg != null) {
61: index = arg.initialize(initArgs, index);
62: namedArgs.put(argName, arg);
63: }
64: }
65: return namedArgs;
66: }
67:
68: private static void debug(String s) {
69: if (DEBUG) {
70: System.err.println(s);
71: }
72: }
73: }
|