001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.beanutils.converters;
019:
020: import java.io.IOException;
021: import java.io.StreamTokenizer;
022: import java.io.StringReader;
023: import java.util.ArrayList;
024: import java.util.List;
025: import org.apache.commons.beanutils.ConversionException;
026: import org.apache.commons.beanutils.Converter;
027:
028: /**
029: * <p>Convenience base class for converters that translate the String
030: * representation of an array into a corresponding array of primitives
031: * object. This class encapsulates the functionality required to parse
032: * the String into a list of String elements that can later be
033: * individually converted to the appropriate primitive type.</p>
034: *
035: * <p>The input syntax accepted by the <code>parseElements()</code> method
036: * is designed to be compatible with the syntax used to initialize arrays
037: * in a Java source program, except that only String literal values are
038: * supported. For maximum flexibility, the surrounding '{' and '}'
039: * characters are optional, and individual elements may be separated by
040: * any combination of whitespace and comma characters.</p>
041: *
042: * @author Craig R. McClanahan
043: * @version $Revision: 557808 $ $Date: 2007-07-20 00:05:03 +0100 (Fri, 20 Jul 2007) $
044: * @since 1.4
045: * @deprecated Replaced by the new {@link ArrayConverter} implementation
046: */
047:
048: public abstract class AbstractArrayConverter implements Converter {
049:
050: // ----------------------------------------------------------- Constructors
051:
052: /**
053: * Create a {@link Converter} that will throw a {@link ConversionException}
054: * if a conversion error occurs.
055: */
056: public AbstractArrayConverter() {
057:
058: this .defaultValue = null;
059: this .useDefault = false;
060:
061: }
062:
063: /**
064: * Create a {@link Converter} that will return the specified default value
065: * if a conversion error occurs.
066: *
067: * @param defaultValue The default value to be returned
068: */
069: public AbstractArrayConverter(Object defaultValue) {
070:
071: if (defaultValue == NO_DEFAULT) {
072: this .useDefault = false;
073: } else {
074: this .defaultValue = defaultValue;
075: this .useDefault = true;
076: }
077:
078: }
079:
080: // ------------------------------------------------------- Static Variables
081:
082: /**
083: * This is a special reference that can be passed as the "default object"
084: * to the constructor to indicate that no default is desired. Note that
085: * the value 'null' cannot be used for this purpose, as the caller may
086: * want a null to be returned as the default.
087: */
088: public static final Object NO_DEFAULT = new Object();
089:
090: // ----------------------------------------------------- Instance Variables
091:
092: /**
093: * <p>Model object for string arrays.</p>
094: */
095: protected static String[] strings = new String[0];
096:
097: /**
098: * The default value specified to our Constructor, if any.
099: */
100: protected Object defaultValue = null;
101:
102: /**
103: * Should we return the default value on conversion errors?
104: */
105: protected boolean useDefault = true;
106:
107: // --------------------------------------------------------- Public Methods
108:
109: /**
110: * Convert the specified input object into an output object of the
111: * specified type. This method must be implemented by a concrete
112: * subclass.
113: *
114: * @param type Data type to which this value should be converted
115: * @param value The input value to be converted
116: * @return The converted value
117: *
118: * @exception ConversionException if conversion cannot be performed
119: * successfully
120: */
121: public abstract Object convert(Class type, Object value);
122:
123: // ------------------------------------------------------ Protected Methods
124:
125: /**
126: * <p>Parse an incoming String of the form similar to an array initializer
127: * in the Java language into a <code>List</code> individual Strings
128: * for each element, according to the following rules.</p>
129: * <ul>
130: * <li>The string is expected to be a comma-separated list of values.</li>
131: * <li>The string may optionally have matching '{' and '}' delimiters
132: * around the list.</li>
133: * <li>Whitespace before and after each element is stripped.</li>
134: * <li>Elements in the list may be delimited by single or double quotes.
135: * Within a quoted elements, the normal Java escape sequences are valid.</li>
136: * </ul>
137: *
138: * @param svalue String value to be parsed
139: * @return The parsed list of String values
140: *
141: * @exception ConversionException if the syntax of <code>svalue</code>
142: * is not syntactically valid
143: * @exception NullPointerException if <code>svalue</code>
144: * is <code>null</code>
145: */
146: protected List parseElements(String svalue) {
147:
148: // Validate the passed argument
149: if (svalue == null) {
150: throw new NullPointerException();
151: }
152:
153: // Trim any matching '{' and '}' delimiters
154: svalue = svalue.trim();
155: if (svalue.startsWith("{") && svalue.endsWith("}")) {
156: svalue = svalue.substring(1, svalue.length() - 1);
157: }
158:
159: try {
160:
161: // Set up a StreamTokenizer on the characters in this String
162: StreamTokenizer st = new StreamTokenizer(new StringReader(
163: svalue));
164: st.whitespaceChars(',', ','); // Commas are delimiters
165: st.ordinaryChars('0', '9'); // Needed to turn off numeric flag
166: st.ordinaryChars('.', '.');
167: st.ordinaryChars('-', '-');
168: st.wordChars('0', '9'); // Needed to make part of tokens
169: st.wordChars('.', '.');
170: st.wordChars('-', '-');
171:
172: // Split comma-delimited tokens into a List
173: ArrayList list = new ArrayList();
174: while (true) {
175: int ttype = st.nextToken();
176: if ((ttype == StreamTokenizer.TT_WORD) || (ttype > 0)) {
177: list.add(st.sval);
178: } else if (ttype == StreamTokenizer.TT_EOF) {
179: break;
180: } else {
181: throw new ConversionException(
182: "Encountered token of type " + ttype);
183: }
184: }
185:
186: // Return the completed list
187: return (list);
188:
189: } catch (IOException e) {
190:
191: throw new ConversionException(e);
192:
193: }
194:
195: }
196:
197: }
|