001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/message/BasicNameValuePair.java $
003: * $Revision: 604625 $
004: * $Date: 2007-12-16 15:11:11 +0100 (Sun, 16 Dec 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.message;
033:
034: import org.apache.http.NameValuePair;
035: import org.apache.http.util.CharArrayBuffer;
036: import org.apache.http.util.LangUtils;
037:
038: /**
039: * A simple class encapsulating an attribute/value pair.
040: * <p>
041: * This class comforms to the generic grammar and formatting rules outlined in the
042: * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2.2">Section 2.2</a>
043: * and
044: * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6">Section 3.6</a>
045: * of <a href="http://www.w3.org/Protocols/rfc2616/rfc2616.txt">RFC 2616</a>
046: * </p>
047: * <h>2.2 Basic Rules</h>
048: * <p>
049: * The following rules are used throughout this specification to describe basic parsing constructs.
050: * The US-ASCII coded character set is defined by ANSI X3.4-1986.
051: * </p>
052: * <pre>
053: * OCTET = <any 8-bit sequence of data>
054: * CHAR = <any US-ASCII character (octets 0 - 127)>
055: * UPALPHA = <any US-ASCII uppercase letter "A".."Z">
056: * LOALPHA = <any US-ASCII lowercase letter "a".."z">
057: * ALPHA = UPALPHA | LOALPHA
058: * DIGIT = <any US-ASCII digit "0".."9">
059: * CTL = <any US-ASCII control character
060: * (octets 0 - 31) and DEL (127)>
061: * CR = <US-ASCII CR, carriage return (13)>
062: * LF = <US-ASCII LF, linefeed (10)>
063: * SP = <US-ASCII SP, space (32)>
064: * HT = <US-ASCII HT, horizontal-tab (9)>
065: * <"> = <US-ASCII double-quote mark (34)>
066: * </pre>
067: * <p>
068: * Many HTTP/1.1 header field values consist of words separated by LWS or special
069: * characters. These special characters MUST be in a quoted string to be used within
070: * a parameter value (as defined in section 3.6).
071: * <p>
072: * <pre>
073: * token = 1*<any CHAR except CTLs or separators>
074: * separators = "(" | ")" | "<" | ">" | "@"
075: * | "," | ";" | ":" | "\" | <">
076: * | "/" | "[" | "]" | "?" | "="
077: * | "{" | "}" | SP | HT
078: * </pre>
079: * <p>
080: * A string of text is parsed as a single word if it is quoted using double-quote marks.
081: * </p>
082: * <pre>
083: * quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
084: * qdtext = <any TEXT except <">>
085: * </pre>
086: * <p>
087: * The backslash character ("\") MAY be used as a single-character quoting mechanism only
088: * within quoted-string and comment constructs.
089: * </p>
090: * <pre>
091: * quoted-pair = "\" CHAR
092: * </pre>
093: * <h>3.6 Transfer Codings</h>
094: * <p>
095: * Parameters are in the form of attribute/value pairs.
096: * </p>
097: * <pre>
098: * parameter = attribute "=" value
099: * attribute = token
100: * value = token | quoted-string
101: * </pre>
102: *
103: * @author <a href="mailto:oleg at ural.com">Oleg Kalnichevski</a>
104: *
105: */
106: public class BasicNameValuePair implements NameValuePair, Cloneable {
107:
108: private final String name;
109: private final String value;
110:
111: /**
112: * Default Constructor taking a name and a value. The value may be null.
113: *
114: * @param name The name.
115: * @param value The value.
116: */
117: public BasicNameValuePair(final String name, final String value) {
118: super ();
119: if (name == null) {
120: throw new IllegalArgumentException("Name may not be null");
121: }
122: this .name = name;
123: this .value = value;
124: }
125:
126: /**
127: * Returns the name.
128: *
129: * @return String name The name
130: */
131: public String getName() {
132: return this .name;
133: }
134:
135: /**
136: * Returns the value.
137: *
138: * @return String value The current value.
139: */
140: public String getValue() {
141: return this .value;
142: }
143:
144: /**
145: * Get a string representation of this pair.
146: *
147: * @return A string representation.
148: */
149: public String toString() {
150: // don't call complex default formatting for a simple toString
151:
152: int len = this .name.length();
153: if (this .value != null)
154: len += 1 + this .value.length();
155: CharArrayBuffer buffer = new CharArrayBuffer(len);
156:
157: buffer.append(this .name);
158: if (this .value != null) {
159: buffer.append("=");
160: buffer.append(this .value);
161: }
162: return buffer.toString();
163: }
164:
165: public boolean equals(final Object object) {
166: if (object == null)
167: return false;
168: if (this == object)
169: return true;
170: if (object instanceof NameValuePair) {
171: BasicNameValuePair that = (BasicNameValuePair) object;
172: return this .name.equals(that.name)
173: && LangUtils.equals(this .value, that.value);
174: } else {
175: return false;
176: }
177: }
178:
179: public int hashCode() {
180: int hash = LangUtils.HASH_SEED;
181: hash = LangUtils.hashCode(hash, this .name);
182: hash = LangUtils.hashCode(hash, this .value);
183: return hash;
184: }
185:
186: public Object clone() throws CloneNotSupportedException {
187: return super.clone();
188: }
189:
190: }
|