001: // HttpTokenList.java
002: // $Id: HttpTokenList.java,v 1.13 2000/08/16 21:38:01 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.www.http;
007:
008: import java.util.Vector;
009:
010: /**
011: * Parse a comma separated list of tokens.
012: */
013:
014: public class HttpTokenList extends BasicValue {
015: /**
016: * Convert tokens to lower case.
017: */
018: protected static final int CASE_LOWER = 0;
019: /**
020: * Don't touch cases of tokens.
021: */
022: protected static final int CASE_ASIS = 1;
023: /**
024: * Convert case to upper case.
025: */
026: protected static final int CASE_UPPER = 2;
027:
028: protected String tokens[] = null;
029: protected int casemode = 0;
030:
031: /**
032: * Parse the byte buffer to build the token list.
033: */
034:
035: protected void parse() {
036: Vector toks = new Vector(8);
037: ParseState ps = new ParseState();
038: ParseState it = new ParseState();
039:
040: ps.ioff = roff;
041: ps.bufend = rlen;
042: ps.spaceIsSep = false;
043: while (HttpParser.nextItem(raw, ps) >= 0) {
044: it.ioff = ps.start;
045: it.bufend = ps.end;
046: HttpParser.unquote(raw, it);
047: switch (casemode) {
048: case CASE_LOWER:
049: toks.addElement(it.toString(raw, true));
050: break;
051: case CASE_ASIS:
052: toks.addElement(it.toString(raw));
053: break;
054: case CASE_UPPER:
055: toks.addElement(it.toString(raw, false));
056: break;
057: }
058: ps.prepare();
059: }
060: tokens = new String[toks.size()];
061: toks.copyInto(tokens);
062: }
063:
064: protected void updateByteValue() {
065: HttpBuffer buf = new HttpBuffer();
066: if (tokens != null) {
067: for (int i = 0; i < tokens.length; i++) {
068: if (i > 0)
069: buf.append(',');
070: buf.append(tokens[i]);
071: }
072: }
073: raw = buf.getByteCopy();
074: roff = 0;
075: rlen = raw.length;
076: }
077:
078: /**
079: * Get this token list value.
080: * @return A list of tokens, encoded as a String array, or <strong>null
081: * </strong> if undefined.
082: */
083:
084: public Object getValue() {
085: validate();
086: return tokens;
087: }
088:
089: public void setValue(String tokens[]) {
090: invalidateByteValue();
091: this .tokens = tokens;
092: isValid = true;
093: }
094:
095: /**
096: * Add a token to this token list.
097: * @param token The token to add.
098: * @param always Always add to the list, even if the token us already
099: * present in the list.
100: */
101:
102: public void addToken(String token, boolean always) {
103: validate();
104: // Check if already set:
105: if (!always) {
106: if (tokens != null) {
107: for (int i = 0; i < tokens.length; i++)
108: if (tokens[i].equals(token))
109: return;
110: }
111: }
112: // Add it to the token list:
113: invalidateByteValue();
114: if (tokens == null) {
115: tokens = new String[1];
116: tokens[0] = token;
117: } else {
118: String newtoks[] = new String[tokens.length + 1];
119: System.arraycopy(tokens, 0, newtoks, 0, tokens.length);
120: newtoks[tokens.length] = token;
121: tokens = newtoks;
122: }
123: }
124:
125: /**
126: * Does this token list includes that token ?
127: * @param token The token to look for.
128: * @return A boolean, <strong>true</strong> if found,
129: * <strong>false</strong> otherwise.
130: */
131:
132: public boolean hasToken(String token, boolean caseSensitive) {
133: validate();
134: if (tokens == null)
135: return false;
136: if (caseSensitive) {
137: for (int i = 0; i < tokens.length; i++)
138: if (tokens[i].equals(token))
139: return true;
140: } else {
141: for (int i = 0; i < tokens.length; i++)
142: if (tokens[i].equalsIgnoreCase(token))
143: return true;
144: }
145: return false;
146: }
147:
148: /**
149: * Create a parsed token list, for emitting.
150: */
151:
152: protected HttpTokenList(String tokens[]) {
153: this .isValid = true;
154: this .tokens = tokens;
155: }
156:
157: /**
158: * Create a token list from a comma separated list of tokens.
159: */
160:
161: protected HttpTokenList(String tokens) {
162: this .isValid = false;
163: this .raw = new byte[tokens.length()];
164: tokens.getBytes(0, raw.length, raw, 0);
165: this .roff = 0;
166: this .rlen = raw.length;
167: }
168:
169: /**
170: * Create an empty token list for parsing.
171: */
172:
173: protected HttpTokenList() {
174: this .isValid = false;
175: }
176:
177: }
|