001: /*
002: * ServletParameter.java
003: *
004: * Copyright (c) 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package org.pnuts.servlet;
010:
011: import java.io.UnsupportedEncodingException;
012: import java.util.Collection;
013: import java.util.Iterator;
014: import java.util.Map;
015: import java.util.Set;
016: import java.util.AbstractSet;
017: import org.pnuts.net.URLEncoding;
018: import pnuts.lang.Pnuts;
019:
020: class ServletParameter implements Map {
021: private Map map;
022:
023: public ServletParameter(Map map) {
024: this .map = map;
025: }
026:
027: public int size() {
028: return map.size();
029: }
030:
031: public boolean isEmpty() {
032: return map.isEmpty();
033: }
034:
035: public boolean containsKey(Object key) {
036: return map.containsKey(key);
037: }
038:
039: public boolean containsValue(Object value) {
040: throw new UnsupportedOperationException();
041: }
042:
043: public Set keySet() {
044: return map.keySet();
045: }
046:
047: public Set entrySet() {
048: return new ParameterSet(map.entrySet());
049: }
050:
051: public Object[] getAll(String name) {
052: return (Object[]) map.get(name);
053: }
054:
055: public Object get(Object name) {
056: return get(name, null);
057: }
058:
059: public Object get(Object name, String defaultValue) {
060: Object[] array = (Object[]) map.get(name);
061: if (array == null) {
062: return defaultValue;
063: } else {
064: return array[0];
065: }
066: }
067:
068: public Object put(Object key, Object value) {
069: if (value instanceof Object[]) {
070: return map.put(key, (Object[]) value);
071: } else {
072: return map.put(key, new Object[] { value });
073: }
074: }
075:
076: public void putAll(Map t) {
077: map.putAll(t);
078: }
079:
080: public Object remove(Object key) {
081: return map.remove(key);
082: }
083:
084: public void clear() {
085: map.clear();
086: }
087:
088: public Collection values() {
089: return new ValueSet(map.values());
090: }
091:
092: public void copyInto(Map m) {
093: Iterator it = map.keySet().iterator();
094: if (it.hasNext()) {
095: Object key = it.next();
096: Object value = map.get(key);
097: if (value instanceof Object[]) {
098: value = ((Object[]) value)[0];
099: }
100: m.put(key, value);
101: }
102: }
103:
104: public String toQueryString(String encoding)
105: throws UnsupportedEncodingException {
106: StringBuffer sbuf = new StringBuffer();
107: Iterator it = map.entrySet().iterator();
108: boolean first = true;
109: if (it.hasNext()) {
110: Map.Entry entry = (Map.Entry) it.next();
111: String key = (String) entry.getKey();
112: Object value = entry.getValue();
113: if (value instanceof Object[]) {
114: Object[] array = (Object[]) value;
115: for (int i = 0; i < array.length; i++) {
116: if (!first) {
117: sbuf.append('&');
118: first = false;
119: }
120: sbuf.append(URLEncoding.encode(key, encoding));
121: sbuf.append('=');
122: sbuf.append(URLEncoding.encode((String) array[i],
123: encoding));
124: }
125: } else {
126: first = false;
127: sbuf.append(URLEncoding.encode(key, encoding));
128: sbuf.append('=');
129: sbuf.append(URLEncoding
130: .encode((String) value, encoding));
131: }
132: }
133: while (it.hasNext()) {
134: Map.Entry entry = (Map.Entry) it.next();
135: String key = (String) entry.getKey();
136: Object value = entry.getValue();
137: if (value instanceof Object[]) {
138: Object[] array = (Object[]) value;
139: for (int i = 0; i < array.length; i++) {
140: sbuf.append('&');
141: sbuf.append(URLEncoding.encode(key, encoding));
142: sbuf.append('=');
143: sbuf.append(URLEncoding.encode((String) array[i],
144: encoding));
145: }
146: } else {
147: sbuf.append('&');
148: sbuf.append(URLEncoding.encode(key, encoding));
149: sbuf.append('=');
150: sbuf.append(URLEncoding
151: .encode((String) value, encoding));
152: }
153: }
154: return sbuf.toString();
155: }
156:
157: public String toString() {
158: StringBuffer sbuf = new StringBuffer();
159: sbuf.append('{');
160: Iterator it = map.keySet().iterator();
161: Object key, value;
162: if (it.hasNext()) {
163: sbuf.append(key = it.next());
164: sbuf.append('=');
165: sbuf.append(Pnuts.format(map.get(key)));
166: }
167: while (it.hasNext()) {
168: sbuf.append(',');
169: sbuf.append(key = it.next());
170: sbuf.append('=');
171: sbuf.append(Pnuts.format(map.get(key)));
172: }
173: sbuf.append('}');
174: return sbuf.toString();
175: }
176:
177: static class ValueSet extends AbstractSet {
178: private Collection col;
179:
180: ValueSet(Collection col) {
181: this .col = col;
182: }
183:
184: public Iterator iterator() {
185: return new ParameterIterator(this .col.iterator(), 1);
186: }
187:
188: public int size() {
189: return col.size();
190: }
191: }
192:
193: static class ParameterSet extends AbstractSet {
194: private Set set;
195:
196: ParameterSet(Set set) {
197: this .set = set;
198: }
199:
200: public Iterator iterator() {
201: return new ParameterIterator(this .set.iterator(), 0);
202: }
203:
204: public int size() {
205: return set.size();
206: }
207: }
208:
209: static class ParameterIterator implements Iterator {
210: private Iterator it;
211: private ParameterEntry entry;
212: private int type; // 0==Entry,1==Value,2==Key
213:
214: ParameterIterator(Iterator it, int type) {
215: this .it = it;
216: this .type = type;
217: if (type == 0) {
218: this .entry = new ParameterEntry();
219: }
220: }
221:
222: public boolean hasNext() {
223: return it.hasNext();
224: }
225:
226: public Object next() {
227: if (type == 0) {
228: Map.Entry n = (Map.Entry) it.next();
229: entry.attach(n);
230: return entry;
231: } else if (type == 1) {
232: Object[] n = (Object[]) it.next();
233: if (n != null && n.length > 0) {
234: return n[0];
235: } else {
236: return null;
237: }
238: } else {
239: return it.next();
240: }
241: }
242:
243: public void remove() {
244: this .it.remove();
245: }
246: }
247:
248: static class ParameterEntry implements Map.Entry {
249: private Map.Entry entry;
250:
251: ParameterEntry() {
252: }
253:
254: void attach(Map.Entry entry) {
255: this .entry = entry;
256: }
257:
258: public Object getKey() {
259: return this .entry.getKey();
260: }
261:
262: public Object getValue() {
263: return ((Object[]) entry.getValue())[0];
264: }
265:
266: public Object[] getValues() {
267: return (Object[]) entry.getValue();
268: }
269:
270: public Object setValue(Object value) {
271: return (Object[]) entry.setValue(new Object[] { value });
272: }
273:
274: public int hashCode() {
275: return entry.getKey().hashCode()
276: ^ entry.getValue().hashCode();
277: }
278:
279: public boolean equals(Object obj) {
280: if (obj instanceof ParameterEntry) {
281: ParameterEntry that = (ParameterEntry) obj;
282: Object k1 = getKey();
283: Object k2 = that.getKey();
284: Object v1 = getValue();
285: Object v2 = that.getValue();
286: return (k1 == k2 || (k1 != null && k1.equals(k2)))
287: && (v1 == v2 || (v1 != null && v1.equals(v2)));
288: } else {
289: return false;
290: }
291: }
292: }
293: }
|