001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.config;
038:
039: import java.util.Vector;
040: import java.util.StringTokenizer;
041:
042: /**
043: * Abstract class defining behavior shared by all
044: * configuration options with values of type
045: * Vector<T>.
046: * @version $Id: VectorOption.java 4255 2007-08-28 19:17:37Z mgricken $
047: */
048: public class VectorOption<T> extends Option<Vector<T>> {
049:
050: protected ParseStrategy<T> parser;
051: protected FormatStrategy<T> formatter;
052: public final String header;
053: public final char delim;
054: public final String footer;
055:
056: /**
057: * @param key The name of this option.
058: * @param parser the parsing strategy for an element in this option
059: * @param formatter the formatting strategy for an element in this option
060: */
061: private VectorOption(String key, ParseStrategy<T> parser,
062: FormatStrategy<T> formatter, String header, char delim,
063: String footer, Vector<T> def) {
064: super (key, def);
065: this .parser = parser;
066: this .formatter = formatter;
067: this .header = header;
068: this .delim = delim;
069: this .footer = footer;
070: }
071:
072: public VectorOption(String key, Option<T> strategy, String header,
073: char delim, String footer, Vector<T> def) {
074: this (key, strategy, strategy, header, delim, footer, def);
075: }
076:
077: /**
078: * Defaults the "header", "footer", and "delim" fields
079: * to open bracket, close bracket, and comma, repsectively.
080: * @param key The name of this option.
081: * @param option The object that knows how to parse and format
082: * an element of type T.
083: */
084: public VectorOption(String key, Option<T> option, Vector<T> def) {
085: this (key, option, option, "[", ',', "]", def);
086: }
087:
088: /**
089: * @param s The String to be parsed.
090: * @return An instance of Vector<T> represented by "s".
091: * @exception IllegalArgumentException if "s" is not formatted
092: * according to the method Vector<T>.toString().
093: */
094: public Vector<T> parse(String s) {
095: s = s.trim();
096: int startFirstElement = header.length();
097: int startFooter = s.length() - footer.length();
098:
099: if (startFooter < startFirstElement || !s.startsWith(header)
100: || !s.endsWith(footer)) {
101: throw new OptionParseException(name, s,
102: "Value must start with " + header + " and end "
103: + "with " + footer
104: + " to be a valid vector.");
105: }
106: s = s.substring(startFirstElement, startFooter);
107: String d = String.valueOf(delim);
108: StringTokenizer st = new StringTokenizer(s, d, true);
109:
110: Vector<T> res = new Vector<T>();
111: boolean sawDelim = st.hasMoreTokens();
112:
113: while (st.hasMoreTokens()) {
114: String token = st.nextToken();
115: boolean isDelim = token.equals(d);
116:
117: if (!isDelim) {
118: res.add(parser.parse(token));
119: } else if (sawDelim) { // isDelim & sawDelim (two delims in a row)
120: throw new OptionParseException(name, s,
121: "Argument contains delimiter with no preceding list element.");
122: }
123: sawDelim = isDelim;
124: }
125: if (sawDelim)
126: throw new OptionParseException(name, s,
127: "Value shouldn't end with a delimiter.");
128: return res;
129: }
130:
131: /** Formats the Vector v. The overall String format is determined by the method Vector<T>.tString(), but each
132: * element of the vector is formatted by calling formatElement().
133: * @param v The Vector to be formatted.
134: * @return A String representing "v".
135: */
136: public String format(Vector<T> v) {
137: final StringBuilder res = new StringBuilder(header);
138:
139: int size = v.size();
140: int i = 0;
141: while (i < size) {
142: res.append(formatter.format(v.get(i)));
143: i++;
144: if (i < size)
145: res.append(delim);
146: }
147: return res.append(footer).toString();
148: }
149: }
|