001: // HttpExt.java
002: // $Id: HttpExtList.java,v 1.10 2007/02/09 22:20:53 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1999.
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: import java.util.Enumeration;
010:
011: /**
012: * @version $Revision: 1.10 $
013: * @author Benoît Mahé (bmahe@w3.org)
014: * Parse a comma separated list of Http extension headers.
015: */
016:
017: public class HttpExtList extends BasicValue {
018:
019: //
020: // Is this list a Mandatory, Optionnal, hop-by-Hop Mandatory or hop-by-Hop
021: // Optionnal?
022:
023: public static final int MAN = 0;
024: public static final int CMAN = 1;
025: public static final int OPT = 2;
026: public static final int COPT = 3;
027:
028: Vector httpexts = null;
029: int manopt = MAN;
030:
031: public int getManOptFlag() {
032: return manopt;
033: }
034:
035: protected void setManOptFlag(int manopt) {
036: this .manopt = manopt;
037: }
038:
039: /**
040: * Parse this header value into its various components.
041: * @exception HttpParserException if unable to parse.
042: */
043: protected void parse() throws HttpParserException {
044: //extension declaration list parser
045: ParseState edl = new ParseState(roff, rlen);
046: //attr-value pair
047: ParseState av = new ParseState(0, 0);
048: //item parser
049: ParseState it = new ParseState(0, 0);
050:
051: edl.separator = (byte) ',';
052: edl.spaceIsSep = false;
053: av.separator = (byte) ';';
054: av.spaceIsSep = false;
055: it.separator = (byte) '=';
056: it.spaceIsSep = false;
057:
058: while (HttpParser.nextItem(raw, edl) >= 0) {
059: av.ioff = edl.start;
060: av.bufend = edl.end;
061: HttpExt ext = new HttpExt();
062:
063: while (HttpParser.nextItem(raw, av) >= 0) {
064: it.ioff = av.start;
065: it.bufend = av.end;
066: // attr = value or "absoluteURI" or "field-name"
067: boolean unquoted = HttpParser.unquote(raw, it);
068: if (HttpParser.nextItem(raw, it) < 0)
069: error("Invalid extension item [" + av.toString(raw)
070: + "]");
071: String itemNaturalCase = it.toString(raw);
072: String item = it.toString(raw, true);
073: // if (item.charAt(0) == '"') {
074: if (unquoted) {
075: // "absoluteURI" or "field-name"
076: ext.setName(itemNaturalCase);
077: } else {
078: it.prepare();
079: HttpParser.unquote(raw, it);
080: if (HttpParser.nextItem(raw, it) < 0)
081: error("No value for attribute [" + item + "]");
082: if (item.equals("ns")) {
083: ext.setNamespace(it.toString(raw));
084: } else {
085: ext.addDeclExt(itemNaturalCase, it
086: .toString(raw));
087: }
088: }
089: av.prepare();
090: }
091: edl.prepare();
092: httpexts.addElement(ext);
093: }
094: }
095:
096: protected void updateByteValue() {
097: HttpBuffer buf = new HttpBuffer();
098: //Dump all extensions declaration
099: int len = httpexts.size();
100: for (int i = 0; i < len; i++) {
101: HttpExt ext = (HttpExt) httpexts.elementAt(i);
102: if (i != 0)
103: buf.append(", ");
104: buf.appendQuoted(ext.getName());
105: if (ext.needsHeaders()) {
106: buf.append(";");
107: buf.append("ns", (byte) '=', ext.getNamespace());
108: }
109: Enumeration e = ext.getDeclExtNames();
110: while (e.hasMoreElements()) {
111: String name = (String) e.nextElement();
112: buf.append("; ");
113: buf.append(name, (byte) '=', ext.getDeclExt(name));
114: }
115: }
116: raw = buf.getByteCopy();
117: roff = 0;
118: rlen = raw.length;
119: }
120:
121: /**
122: * Add an Http extension header.
123: * @param ext an HttpExt.
124: */
125: public void addHttpExt(HttpExt ext) {
126: validate();
127: httpexts.addElement(ext);
128: }
129:
130: /**
131: * Get all Http extensions header.
132: * @return an HttpExt array.
133: */
134: public HttpExt[] getHttpExts() {
135: validate();
136: HttpExt exts[] = new HttpExt[httpexts.size()];
137: httpexts.copyInto(exts);
138: return exts;
139: }
140:
141: public int getLength() {
142: validate();
143: return httpexts.size();
144: }
145:
146: /**
147: * Get an Http extension header.
148: * @param name The extension identifier (AbsoluteURI or field name)
149: * @return an HttpExt or <strong>null</strong>.
150: */
151: public HttpExt getHttpExt(String name) {
152: validate();
153: for (int i = 0; i < httpexts.size(); i++) {
154: HttpExt ext = (HttpExt) httpexts.elementAt(i);
155: if (ext.getName().equals(name))
156: return ext;
157: }
158: return null;
159: }
160:
161: public Object getValue() {
162: return this ;
163: }
164:
165: /**
166: * for user.
167: * @param exts the HttpExt array.
168: */
169: public HttpExtList(HttpExt exts[]) {
170: this .isValid = true;
171: int len = exts.length;
172: this .httpexts = new Vector(len);
173: if (exts != null) {
174: for (int i = 0; i < len; i++)
175: httpexts.addElement(exts[i]);
176: }
177: }
178:
179: /**
180: * Constructor, for User
181: * @param old the old Http extension declaration list
182: * If you want to reply the same extensions, use this
183: * contructor.
184: */
185: public HttpExtList(HttpExtList old) {
186: this .isValid = true;
187: old.validate();
188: int len = old.httpexts.size();
189: this .httpexts = new Vector(len);
190: for (int i = 0; i < len; i++) {
191: HttpExt newext = new HttpExt((HttpExt) old.httpexts
192: .elementAt(i));
193: this .httpexts.addElement(newext);
194: }
195: this .manopt = old.manopt;
196: }
197:
198: /**
199: * for parser only
200: */
201: protected HttpExtList() {
202: this .isValid = false;
203: this .httpexts = new Vector(2);
204: }
205:
206: }
|