• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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      */
BasicNameValuePair(final String name, final String value)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      */
getName()131     public String getName() {
132         return this.name;
133     }
134 
135     /**
136      * Returns the value.
137      *
138      * @return String value The current value.
139      */
getValue()140     public String getValue() {
141         return this.value;
142     }
143 
144 
145     /**
146      * Get a string representation of this pair.
147      *
148      * @return A string representation.
149      */
toString()150     public String toString() {
151         // don't call complex default formatting for a simple toString
152 
153         int len = this.name.length();
154         if (this.value != null)
155             len += 1 + this.value.length();
156         CharArrayBuffer buffer = new CharArrayBuffer(len);
157 
158         buffer.append(this.name);
159         if (this.value != null) {
160             buffer.append("=");
161             buffer.append(this.value);
162         }
163         return buffer.toString();
164     }
165 
equals(final Object object)166     public boolean equals(final Object object) {
167         if (object == null) return false;
168         if (this == object) return true;
169         if (object instanceof NameValuePair) {
170             BasicNameValuePair that = (BasicNameValuePair) object;
171             return this.name.equals(that.name)
172                   && LangUtils.equals(this.value, that.value);
173         } else {
174             return false;
175         }
176     }
177 
hashCode()178     public int hashCode() {
179         int hash = LangUtils.HASH_SEED;
180         hash = LangUtils.hashCode(hash, this.name);
181         hash = LangUtils.hashCode(hash, this.value);
182         return hash;
183     }
184 
clone()185     public Object clone() throws CloneNotSupportedException {
186         return super.clone();
187     }
188 
189 }
190