001: // HttpAcceptCharset.java
002: // $Id: HttpAcceptCharset.java,v 1.7 1998/08/13 16:27:16 bmahe 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: public class HttpAcceptCharset extends BasicValue {
009: String charset = null;
010: double quality = 1.0;
011: HttpAcceptCharsetList list = null;
012:
013: /**
014: * parse.
015: * @exception HttpParserException if parsing failed.
016: */
017: protected void parse() throws HttpParserException {
018: ParseState ps = new ParseState(roff, rlen);
019: ps.separator = (byte) ';';
020: ps.spaceIsSep = false;
021: // Get the charset:
022: if (HttpParser.nextItem(raw, ps) < 0)
023: error("Invalid Accept-Charset: no charset.");
024: this .charset = new String(raw, 0, ps.start, ps.end - ps.start);
025: // And the optional quality:
026: ps.prepare();
027: ps.separator = (byte) '=';
028: if (HttpParser.nextItem(raw, ps) < 0) {
029: this .quality = 1.0;
030: } else {
031: ps.prepare();
032: this .quality = HttpParser.parseQuality(raw, ps);
033: }
034: }
035:
036: protected void invalidateByteValue() {
037: super .invalidateByteValue();
038: if (list != null)
039: list.invalidateByteValue();
040: }
041:
042: protected void updateByteValue() {
043: HttpBuffer buf = new HttpBuffer();
044: buf.append(charset);
045: buf.append(';');
046: buf.append(quality);
047: raw = buf.getByteCopy();
048: roff = 0;
049: rlen = raw.length;
050: }
051:
052: public Object getValue() {
053: validate();
054: return this ;
055: }
056:
057: /**
058: * Get this accept charset clause charset.
059: * @return A String encoding the charset token.
060: */
061:
062: public String getCharset() {
063: validate();
064: return charset;
065: }
066:
067: /**
068: * Set the charset accepted by this clause.
069: * @param charset The accepted charset.
070: */
071:
072: public void setCharset(String charset) {
073: if (this .charset.equals(charset))
074: return;
075: invalidateByteValue();
076: this .charset = charset;
077: }
078:
079: /**
080: * Get the quality at which this charset is accepted.
081: * @return A double value, encoding the quality, or <strong>1.0</strong>
082: * if undefined.
083: */
084:
085: public double getQuality() {
086: validate();
087: return quality;
088: }
089:
090: /**
091: * Set the quality under which this charset is accepted.
092: * @param q The quality for this charset.
093: */
094:
095: public void setQuality(double quality) {
096: if (this .quality != quality)
097: invalidateByteValue();
098: this .quality = quality;
099: }
100:
101: HttpAcceptCharset(HttpAcceptCharsetList list, byte raw[], int o,
102: int l) {
103: this .list = list;
104: this .raw = raw;
105: this .roff = o;
106: this .rlen = l;
107: this .isValid = false;
108: }
109:
110: HttpAcceptCharset(boolean isValid, String charset, double quality) {
111: this .isValid = isValid;
112: setCharset(charset);
113: setQuality(quality);
114: }
115:
116: public HttpAcceptCharset() {
117: this .isValid = false;
118: }
119:
120: }
|