1 /*
2 * @(#)HttpHeaderElement.java 0.3-3 06/05/2001
3 *
4 * This file is part of the HTTPClient package
5 * Copyright (C) 1996-2001 Ronald Tschalär
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * MA 02111-1307, USA
21 *
22 * For questions, suggestions, bug-reports, enhancement-requests etc.
23 * I may be contacted at:
24 *
25 * ronald@innovation.ch
26 *
27 * The HTTPClient's home page is located at:
28 *
29 * http://www.innovation.ch/java/HTTPClient/
30 *
31 */
32
33 package HTTPClient;
34
35
36 /**
37 * This class holds a description of an http header element. It is used
38 * by {@link Util#parseHeader(java.lang.String) Util.parseHeader}.
39 *
40 * @see Util#parseHeader(java.lang.String)
41 * @see Util#getElement(java.util.Vector, java.lang.String)
42 * @see Util#assembleHeader(java.util.Vector)
43 * @version 0.3-3 06/05/2001
44 * @author Ronald Tschalär
45 */
46 public class HttpHeaderElement
47 {
48 /** element name */
49 private String name;
50
51 /** element value */
52 private String value;
53
54 /** element parameters */
55 private NVPair[] parameters;
56
57
58 // Constructors
59
60 /**
61 * Construct an element with the given name. The value and parameters
62 * are set to null. This can be used when a dummy element is constructed
63 * for comparison or retrieval purposes.
64 *
65 * @param name the name of the element
66 */
67 public HttpHeaderElement(String name)
68 {
69 this.name = name;
70 this.value = null;
71 parameters = new NVPair[0];
72 }
73
74
75 /**
76 * @param name the first token in the element
77 * @param value the value part, or null
78 * @param params the parameters
79 */
80 public HttpHeaderElement(String name, String value, NVPair[] params)
81 {
82 this.name = name;
83 this.value = value;
84 if (params != null)
85 {
86 parameters = new NVPair[params.length];
87 System.arraycopy(params, 0, parameters, 0, params.length);
88 }
89 else
90 parameters = new NVPair[0];
91 }
92
93
94 // Methods
95
96 /**
97 * @return the name
98 */
99 public String getName()
100 {
101 return name;
102 }
103
104
105 /**
106 * @return the value
107 */
108 public String getValue()
109 {
110 return value;
111 }
112
113
114 /**
115 * @return the parameters
116 */
117 public NVPair[] getParams()
118 {
119 return parameters;
120 }
121
122
123 /**
124 * Two elements are equal if they have the same name. The comparison is
125 * <em>case-insensitive</em>.
126 *
127 * @param obj the object to compare with
128 * @return true if <var>obj</var> is an HttpHeaderElement with the same
129 * name as this element.
130 */
131 public boolean equals(Object obj)
132 {
133 if ((obj != null) && (obj instanceof HttpHeaderElement))
134 {
135 String other = ((HttpHeaderElement) obj).name;
136 return name.equalsIgnoreCase(other);
137 }
138
139 return false;
140 }
141
142
143 /**
144 * @return a string containing the HttpHeaderElement formatted as it
145 * would appear in a header
146 */
147 public String toString()
148 {
149 StringBuffer buf = new StringBuffer();
150 appendTo(buf);
151 return buf.toString();
152 }
153
154
155 /**
156 * Append this header element to the given buffer. This is basically a
157 * more efficient version of <code>toString()</code> for assembling
158 * multiple elements.
159 *
160 * @param buf the StringBuffer to append this header to
161 * @see #toString()
162 */
163 public void appendTo(StringBuffer buf)
164 {
165 buf.append(name);
166
167 if (value != null)
168 {
169 if (Util.needsQuoting(value))
170 {
171 buf.append("=\"");
172 buf.append(Util.quoteString(value, "\\\""));
173 buf.append('"');
174 }
175 else
176 {
177 buf.append('=');
178 buf.append(value);
179 }
180 }
181
182 for (int idx=0; idx<parameters.length; idx++)
183 {
184 buf.append(";");
185 buf.append(parameters[idx].getName());
186 String pval = parameters[idx].getValue();
187 if (pval != null)
188 {
189 if (Util.needsQuoting(pval))
190 {
191 buf.append("=\"");
192 buf.append(Util.quoteString(pval, "\\\""));
193 buf.append('"');
194 }
195 else
196 {
197 buf.append('=');
198 buf.append(pval);
199 }
200 }
201 }
202 }
203 }