1   package gate.creole.morph;
2   
3   import java.util.ArrayList;
4   
5   /**
6    * <p>Title: StringSet </p>
7    * <p>Description: This is one of the variable types that is allowed to define.
8    * It stores different possible strings for this variable
9    * The format of the value of this variable should be </p>
10   * <p> "string1" OR "string2" OR "string3" ... </p>
11   */
12  
13  public class StringSet extends Variable {
14  
15    private String varName;
16    private ArrayList variables;
17  
18    /**
19     * Constructor
20     */
21    public StringSet() {
22      variables = new ArrayList();
23    }
24  
25    /**
26     * Tells if any value available which can be retrieved
27     * @return true if value available, false otherwise
28     */
29    public boolean hasNext() {
30      if(pointer<variables.size()) {
31        return true;
32      } else {
33        return false;
34      }
35    }
36  
37    /**
38     * Returns the next available value for this variable
39     * @return value of the variable in the String format
40     */
41    public String next() {
42      if(pointer<variables.size()) {
43        pointer++;
44        return (String)(variables.get(pointer-1));
45      } else {
46        return null;
47      }
48    }
49  
50    /**
51     * Process the provided value and stores in the underlying data structure
52     * @param varName name of the variable
53     * @param varValue String that contains possible different values
54     * @return true if successfully stored, false otherwise (means some syntax error)
55     */
56    public boolean set(String varName, String varValue) {
57      this.varName = varName;
58      this.varValue = "";
59      // now we need to process the varValue
60      // lets first split between the | sign
61  
62      String [] values = varValue.split(" OR ");
63      //check for its syntax
64      for(int i=0;i<values.length;i++) {
65  
66        // remove all extra spaces
67        values[i] = values[i].trim();
68  
69        // now check if it has been qouted properly
70        if(values[i].length()<3 || !(values[i].charAt(0)=='\"') || !(values[i].charAt(values[i].length()-1)=='\"')) {
71          return false;
72        } else {
73          values[i] = values[i].substring(1,values[i].length()-1);
74        }
75      }
76  
77      // store each value in the arrayList
78      for(int i=0;i<values.length;i++) {
79        variables.add(values[i]);
80        this.varValue = this.varValue + "("+values[i]+")";
81        if ((i+1) != values.length) {
82          this.varValue = this.varValue + "|";
83        }
84      }
85      return true;
86    }
87  
88    /**
89     * A method that tells if value is available in the StringSet
90     * @param value String that is to be searched in the String set
91     * @return true if value found in the StringSet, false otherwise
92     */
93    public boolean contains(String value) {
94      return variables.contains(value);
95    }
96  }