1 /*****************************************************************************/
2 /* Software Testing Automation Framework (STAF)                              */
3 /* (C) Copyright IBM Corp. 2001                                              */
4 /*                                                                           */
5 /* This software is licensed under the Eclipse Public License (EPL) V1.0.    */
6 /*****************************************************************************/
7 
8 package com.ibm.staf.service;
9 
10 import java.util.*;
11 
12 // STAFCommandParseResult - This class contains the results of parsing a
13 //                          command string with the STAFCommandParser.
14 //
15 // This class provides the following instance data
16 //
17 //   rc - This indicates whether the command was parsed successfully.  Zero
18 //        indicates a successful parse.  Non-zero indicates an error.
19 //
20 //   errorBuffer - If rc is non-zero, this will contain a textual description
21 //                 of the error.
22 //
23 // This class provides the following methods
24 //
25 //   optionTimes - This returns the number of times a particular option was
26 //                 specified.
27 //
28 //   optionValue - This returns the value of a specific instance of an option.
29 //                 If the given instance does not exist, an empty string is
30 //                 returned.
31 //
32 //   numInstances - This returns the total number of options specified.
33 //
34 //   instanceName - Returns the name of the option for the given instance.
35 //
36 //   instanceValue - Returns the value of option for the given instance.
37 //
38 //   numArgs - Returns the total number of arguments specified.
39 //
40 //   arg - Returns the specified argument.
41 
42 public class STAFCommandParseResult
43 {
STAFCommandParseResult()44     STAFCommandParseResult()
45     {
46         this(false);
47     }
48 
STAFCommandParseResult(boolean caseSensitive)49     STAFCommandParseResult(boolean caseSensitive)
50     {
51         fCaseSensitive = caseSensitive;
52         fOptionInstances = new Vector();
53         fArgs = new Vector();
54 
55         rc = 0;
56         errorBuffer = new String();
57     }
58 
59     public int rc;
60     public String errorBuffer;
61 
optionTimes(String name)62     public int optionTimes(String name)
63     {
64         int count = 0;
65 
66         for(int i = 0; i < fOptionInstances.size(); ++i)
67         {
68             OptionInstance instance =
69                            (OptionInstance)fOptionInstances.elementAt(i);
70 
71             if ((fCaseSensitive && instance.name.equals(name)) ||
72                 (!fCaseSensitive && instance.name.equalsIgnoreCase(name)))
73             {
74                 ++count;
75             }
76         }
77 
78         return count;
79     }
80 
optionValue(String name)81     public String optionValue(String name)
82     {
83         return optionValue(name, 1);
84     }
85 
optionValue(String name, int instanceNumber)86     public String optionValue(String name, int instanceNumber)
87     {
88         int count = 0;
89 
90         for(int i = 0; i < fOptionInstances.size(); ++i)
91         {
92             OptionInstance instance =
93                            (OptionInstance)fOptionInstances.elementAt(i);
94 
95             if ((fCaseSensitive && instance.name.equals(name)) ||
96                 (!fCaseSensitive && instance.name.equalsIgnoreCase(name)))
97             {
98                 if (++count == instanceNumber)
99                     return instance.value;
100             }
101         }
102 
103         return new String();
104     }
105 
numInstances()106     public int numInstances() { return fOptionInstances.size(); }
107 
instanceName(int instanceNumber)108     public String instanceName(int instanceNumber)
109     {
110         OptionInstance instance =
111             (OptionInstance)fOptionInstances.elementAt(instanceNumber - 1);
112 
113         return instance.name;
114     }
115 
instanceValue(int instanceNumber)116     public String instanceValue(int instanceNumber)
117     {
118         OptionInstance instance =
119             (OptionInstance)fOptionInstances.elementAt(instanceNumber - 1);
120 
121         return instance.value;
122     }
123 
numArgs()124     public int numArgs() { return fArgs.size(); }
125 
arg(int argNumber)126     public String arg(int argNumber)
127     {
128         return (String)fArgs.elementAt(argNumber - 1);
129     }
130 
131 
132     // These methods are used by the STAFCommandParser class to add the
133     // various options and arguments to the parse result.
134 
addOptionInstance(OptionInstance instance)135     void addOptionInstance(OptionInstance instance)
136     {
137         fOptionInstances.addElement(instance);
138     }
139 
addArg(String value)140     void addArg(String value)
141     {
142         fArgs.addElement(value);
143     }
144 
145 
146     // Instance data
147 
148     private boolean fCaseSensitive;
149     private Vector fOptionInstances;
150     private Vector fArgs;
151 
152 
153     // This class is used to represent a given instance of an option.
154 
155     class OptionInstance
156     {
OptionInstance()157         OptionInstance()
158         {
159             this(new String(), new String());
160         }
161 
OptionInstance(String theName, String theValue)162         OptionInstance(String theName, String theValue)
163         {
164             name = theName;
165             value = theValue;
166         }
167 
168         String name;
169         String value;
170     }
171 }
172