1 /*
2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicNameValuePair.java $
3  * $Revision: 604625 $
4  * $Date: 2007-12-16 06:11:11 -0800 (Sun, 16 Dec 2007) $
5  *
6  * ====================================================================
7  * Licensed to the Apache Software Foundation (ASF) under one
8  * or more contributor license agreements.  See the NOTICE file
9  * distributed with this work for additional information
10  * regarding copyright ownership.  The ASF licenses this file
11  * to you under the Apache License, Version 2.0 (the
12  * "License"); you may not use this file except in compliance
13  * with the License.  You may obtain a copy of the License at
14  *
15  *   http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing,
18  * software distributed under the License is distributed on an
19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20  * KIND, either express or implied.  See the License for the
21  * specific language governing permissions and limitations
22  * under the License.
23  * ====================================================================
24  *
25  * This software consists of voluntary contributions made by many
26  * individuals on behalf of the Apache Software Foundation.  For more
27  * information on the Apache Software Foundation, please see
28  * <http://www.apache.org/>.
29  *
30  */
31 
32 package org.apache.http.message;
33 
34 import org.apache.http.NameValuePair;
35 import org.apache.http.util.CharArrayBuffer;
36 import org.apache.http.util.LangUtils;
37 
38 /**
39  * A simple class encapsulating an attribute/value pair.
40  * <p>
41  *  This class comforms to the generic grammar and formatting rules outlined in the
42  *  <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2.2">Section 2.2</a>
43  *  and
44  *  <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6">Section 3.6</a>
45  *  of <a href="http://www.w3.org/Protocols/rfc2616/rfc2616.txt">RFC 2616</a>
46  * </p>
47  * <h>2.2 Basic Rules</h>
48  * <p>
49  *  The following rules are used throughout this specification to describe basic parsing constructs.
50  *  The US-ASCII coded character set is defined by ANSI X3.4-1986.
51  * </p>
52  * <pre>
53  *     OCTET          = <any 8-bit sequence of data>
54  *     CHAR           = <any US-ASCII character (octets 0 - 127)>
55  *     UPALPHA        = <any US-ASCII uppercase letter "A".."Z">
56  *     LOALPHA        = <any US-ASCII lowercase letter "a".."z">
57  *     ALPHA          = UPALPHA | LOALPHA
58  *     DIGIT          = <any US-ASCII digit "0".."9">
59  *     CTL            = <any US-ASCII control character
60  *                      (octets 0 - 31) and DEL (127)>
61  *     CR             = <US-ASCII CR, carriage return (13)>
62  *     LF             = <US-ASCII LF, linefeed (10)>
63  *     SP             = <US-ASCII SP, space (32)>
64  *     HT             = <US-ASCII HT, horizontal-tab (9)>
65  *     <">            = <US-ASCII double-quote mark (34)>
66  * </pre>
67  * <p>
68  *  Many HTTP/1.1 header field values consist of words separated by LWS or special
69  *  characters. These special characters MUST be in a quoted string to be used within
70  *  a parameter value (as defined in section 3.6).
71  * <p>
72  * <pre>
73  * token          = 1*<any CHAR except CTLs or separators>
74  * separators     = "(" | ")" | "<" | ">" | "@"
75  *                | "," | ";" | ":" | "\" | <">
76  *                | "/" | "[" | "]" | "?" | "="
77  *                | "{" | "}" | SP | HT
78  * </pre>
79  * <p>
80  *  A string of text is parsed as a single word if it is quoted using double-quote marks.
81  * </p>
82  * <pre>
83  * quoted-string  = ( <"> *(qdtext | quoted-pair ) <"> )
84  * qdtext         = <any TEXT except <">>
85  * </pre>
86  * <p>
87  *  The backslash character ("\") MAY be used as a single-character quoting mechanism only
88  *  within quoted-string and comment constructs.
89  * </p>
90  * <pre>
91  * quoted-pair    = "\" CHAR
92  * </pre>
93  * <h>3.6 Transfer Codings</h>
94  * <p>
95  *  Parameters are in the form of attribute/value pairs.
96  * </p>
97  * <pre>
98  * parameter               = attribute "=" value
99  * 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  * @deprecated Please use {@link java.net.URL#openConnection} instead.
107  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
108  *     for further details.
109  */
110 @Deprecated
111 public class BasicNameValuePair implements NameValuePair, Cloneable {
112 
113     private final String name;
114     private final String value;
115 
116     /**
117      * Default Constructor taking a name and a value. The value may be null.
118      *
119      * @param name The name.
120      * @param value The value.
121      */
BasicNameValuePair(final String name, final String value)122     public BasicNameValuePair(final String name, final String value) {
123         super();
124         if (name == null) {
125             throw new IllegalArgumentException("Name may not be null");
126         }
127         this.name = name;
128         this.value = value;
129     }
130 
131     /**
132      * Returns the name.
133      *
134      * @return String name The name
135      */
getName()136     public String getName() {
137         return this.name;
138     }
139 
140     /**
141      * Returns the value.
142      *
143      * @return String value The current value.
144      */
getValue()145     public String getValue() {
146         return this.value;
147     }
148 
149 
150     /**
151      * Get a string representation of this pair.
152      *
153      * @return A string representation.
154      */
toString()155     public String toString() {
156         // don't call complex default formatting for a simple toString
157 
158         int len = this.name.length();
159         if (this.value != null)
160             len += 1 + this.value.length();
161         CharArrayBuffer buffer = new CharArrayBuffer(len);
162 
163         buffer.append(this.name);
164         if (this.value != null) {
165             buffer.append("=");
166             buffer.append(this.value);
167         }
168         return buffer.toString();
169     }
170 
equals(final Object object)171     public boolean equals(final Object object) {
172         if (object == null) return false;
173         if (this == object) return true;
174         if (object instanceof NameValuePair) {
175             BasicNameValuePair that = (BasicNameValuePair) object;
176             return this.name.equals(that.name)
177                   && LangUtils.equals(this.value, that.value);
178         } else {
179             return false;
180         }
181     }
182 
hashCode()183     public int hashCode() {
184         int hash = LangUtils.HASH_SEED;
185         hash = LangUtils.hashCode(hash, this.name);
186         hash = LangUtils.hashCode(hash, this.value);
187         return hash;
188     }
189 
clone()190     public Object clone() throws CloneNotSupportedException {
191         return super.clone();
192     }
193 
194 }
195