001 // Copyright 2004, 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry.multipart;
016
017 import java.util.ArrayList;
018 import java.util.List;
019
020 /**
021 * A portion of a multipart request that stores a value, or values, for a
022 * parameter.
023 *
024 * @author Howard Lewis Ship
025 * @since 2.0.1
026 */
027
028 public class ValuePart
029 {
030
031 private int _count;
032
033 // Stores either String or List of String
034 private Object _value;
035
036 public ValuePart(String value)
037 {
038 _count = 1;
039 _value = value;
040 }
041
042 public int getCount()
043 {
044 return _count;
045 }
046
047 /**
048 * Returns the value, or the first value (if multi-valued).
049 */
050
051 public String getValue()
052 {
053 if (_count == 1) return (String) _value;
054
055 List l = (List) _value;
056
057 return (String) l.get(0);
058 }
059
060 /**
061 * Returns the values as an array of strings. If there is only one value, it
062 * is returned wrapped as a single element array.
063 */
064
065 public String[] getValues()
066 {
067 if (_count == 1) return new String[] { (String) _value };
068
069 List l = (List) _value;
070
071 return (String[]) l.toArray(new String[_count]);
072 }
073
074 public void add(String newValue)
075 {
076 if (_count == 1)
077 {
078 List l = new ArrayList();
079 l.add(_value);
080 l.add(newValue);
081
082 _value = l;
083 _count++;
084 return;
085 }
086
087 List l = (List) _value;
088 l.add(newValue);
089 _count++;
090 }
091
092 /**
093 * Does nothing.
094 */
095
096 public void cleanup()
097 {
098 }
099 }