001: // HttpParamList.java
002: // $Id: HttpParamList.java,v 1.3 2000/08/16 21:38:00 ylafon Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 1999.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.www.http;
007:
008: import org.w3c.util.ArrayDictionary;
009:
010: /**
011: * This class parses name->value pairs, used in Authentication Info
012: */
013:
014: public class HttpParamList extends BasicValue {
015: ArrayDictionary params = null;
016:
017: /**
018: * parse.
019: * @exception HttpParserException if parsing failed.
020: */
021: protected void parse() throws HttpParserException {
022: ParseState ps = new ParseState(roff, rlen);
023:
024: this .params = new ArrayDictionary(4, 4);
025: ps.prepare();
026:
027: // Parameters parsing
028: ParseState it = new ParseState();
029: it.separator = (byte) '=';
030: ps.separator = (byte) ',';
031: while (HttpParser.nextItem(raw, ps) >= 0) {
032: // Get the param name:
033: it.prepare(ps);
034: if (HttpParser.nextItem(raw, it) < 0)
035: error("Invalid param list: bad param name.");
036: String key = it.toString(raw, true);
037: // Get the param value:
038: it.prepare();
039: if (HttpParser.nextItem(raw, it) < 0)
040: error("Invalid param list: no param value.");
041: it.ioff = it.start;
042: HttpParser.unquote(raw, it);
043: params.put(key, it.toString(raw));
044: ps.prepare();
045: }
046: }
047:
048: protected void updateByteValue() {
049: HttpBuffer buf = new HttpBuffer();
050: int len = params.size();
051: for (int i = 0; len > 0; i++) {
052: String key = (String) params.keyAt(i);
053: if (key == null)
054: continue;
055: buf.appendQuoted(key, (byte) '=', (String) params
056: .elementAt(i));
057: len--;
058: if (len > 0) {
059: buf.append((byte) ',');
060: }
061: }
062: raw = buf.getByteCopy();
063: roff = 0;
064: rlen = raw.length;
065: }
066:
067: public Object getValue() {
068: validate();
069: return this ;
070: }
071:
072: /**
073: * Get a parameter.
074: * @param name The name of the parameter to fetch.
075: * @return The String value, or <strong>null</strong> if undefined.
076: */
077:
078: public String getParameter(String name) {
079: validate();
080: return (params == null) ? null : (String) params.get(name);
081: }
082:
083: /**
084: * Set an authentication parameter.
085: * @param name The name of the authentication parameter.
086: * @param value The value of the authentication parameter.
087: */
088:
089: public void setParameter(String name, String value) {
090: validate();
091: if (params == null)
092: params = new ArrayDictionary(4, 4);
093: params.put(name, value);
094: }
095:
096: public HttpParamList(boolean isValid) {
097: this .isValid = isValid;
098: }
099:
100: public HttpParamList() {
101: this .isValid = false;
102: }
103: }
|