KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > config > VectorOption


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2006 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.config;
35
36 import java.util.Vector JavaDoc;
37 import java.util.StringTokenizer JavaDoc;
38 /**
39  * Abstract class defining behavior shared by all
40  * configuration options with values of type
41  * Vector<T>.
42  * @version $Id: VectorOption.java 4031 2006-11-15 22:09:06Z rcartwright $
43  */

44 public class VectorOption<T> extends Option<Vector JavaDoc<T>> {
45
46   protected ParseStrategy<T> parser;
47   protected FormatStrategy<T> formatter;
48   public final String JavaDoc header;
49   public final char delim;
50   public final String JavaDoc footer;
51
52   /**
53    * @param key The name of this option.
54    * @param parser the parsing strategy for an element in this option
55    * @param formatter the formatting strategy for an element in this option
56    */

57   private VectorOption(String JavaDoc key, ParseStrategy<T> parser, FormatStrategy<T> formatter,
58                        String JavaDoc header, char delim, String JavaDoc footer, Vector JavaDoc<T> def) {
59     super(key,def);
60     this.parser = parser;
61     this.formatter = formatter;
62     this.header = header;
63     this.delim = delim;
64     this.footer = footer;
65   }
66
67   public VectorOption(String JavaDoc key, Option<T> strategy, String JavaDoc header,
68                       char delim, String JavaDoc footer, Vector JavaDoc<T> def) {
69     this(key, strategy, strategy, header, delim, footer,def);
70   }
71
72   /**
73    * Defaults the "header", "footer", and "delim" fields
74    * to open bracket, close bracket, and comma, repsectively.
75    * @param key The name of this option.
76    * @param option The object that knows how to parse and format
77    * an element of type T.
78    */

79   public VectorOption(String JavaDoc key, Option<T> option, Vector JavaDoc<T> def) {
80     this(key,option,option,"[",',',"]",def);
81   }
82
83   /**
84    * @param s The String to be parsed.
85    * @return An instance of Vector<T> represented by "s".
86    * @exception IllegalArgumentException if "s" is not formatted
87    * according to the method Vector<T>.toString().
88    */

89   public Vector JavaDoc<T> parse(String JavaDoc s) {
90     s= s.trim();
91     int startFirstElement = header.length();
92     int startFooter = s.length() - footer.length();
93
94     if (startFooter < startFirstElement || !s.startsWith(header) || ! s.endsWith(footer)) {
95       throw new OptionParseException(name, s, "Value must start with " + header + " and end " + "with " + footer +
96                                      " to be a valid vector.");
97     }
98     s = s.substring(startFirstElement, startFooter);
99     String JavaDoc d = String.valueOf(delim);
100     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s,d,true);
101
102     Vector JavaDoc<T> res = new Vector JavaDoc<T>();
103     boolean sawDelim = st.hasMoreTokens();
104
105     while(st.hasMoreTokens()) {
106       String JavaDoc token = st.nextToken();
107       boolean isDelim = token.equals(d);
108
109       if (!isDelim) {
110         res.add(parser.parse(token));
111       } else if (sawDelim) { // isDelim & sawDelim (two delims in a row)
112
throw new OptionParseException(name, s, "Argument contains delimiter with no preceding list element.");
113       }
114       sawDelim = isDelim;
115     }
116     if (sawDelim) throw new OptionParseException(name, s, "Value shouldn't end with a delimiter.");
117     return res;
118   }
119
120   /** Formats the Vector v. The overall String format is determined by the method Vector<T>.tString(), but each
121     * element of the vector is formatted by calling formatElement().
122     * @param v The Vector to be formatted.
123     * @return A String representing "v".
124     */

125   public String JavaDoc format(Vector JavaDoc<T> v) {
126     final StringBuilder JavaDoc res = new StringBuilder JavaDoc(header);
127
128     int size = v.size();
129     int i = 0;
130     while (i < size) {
131       res.append(formatter.format(v.get(i)));
132       i++;
133       if (i < size) res.append(delim);
134     }
135     return res.append(footer).toString();
136   }
137 }
138
139
Popular Tags