KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > HTTPClient > HttpHeaderElement


1 /*
2  * @(#)HttpHeaderElement.java 0.3-2 18/06/1999
3  *
4  * This file is part of the HTTPClient package
5  * Copyright (C) 1996-1999 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  */

28
29 package HTTPClient;
30
31
32 /**
33  * This class holds a description of an http header element. It is used
34  * by <code>HTTPClient.Util.parseHeader()</code>.
35  *
36  * @see Util#parseHeader(java.lang.String)
37  * @see Util#getElement(java.util.Vector, java.lang.String)
38  * @see Util#assembleHeader(java.util.Vector)
39  * @version 0.3-2 18/06/1999
40  * @author Ronald Tschalär
41  */

42
43 public class HttpHeaderElement
44 {
45     /** element name */
46     private String JavaDoc name;
47
48     /** element value */
49     private String JavaDoc value;
50
51     /** element parameters */
52     private NVPair[] parameters;
53
54
55     // Constructors
56

57     /**
58      * Construct an element with the given name. The value and parameters
59      * are set to null. This can be used when a dummy element is constructed
60      * for comparison or retrieval purposes.
61      *
62      * @param name the name of the element
63      */

64     public HttpHeaderElement(String JavaDoc name)
65     {
66     this.name = name;
67     this.value = null;
68     parameters = new NVPair[0];
69     }
70
71
72     /**
73      * @param name the first token in the element
74      * @param value the value part, or null
75      * @param params the parameters
76      */

77     public HttpHeaderElement(String JavaDoc name, String JavaDoc value, NVPair[] params)
78     {
79     this.name = name;
80     this.value = value;
81     if (params != null)
82     {
83         parameters = new NVPair[params.length];
84         System.arraycopy(params, 0, parameters, 0, params.length);
85     }
86     else
87         parameters = new NVPair[0];
88     }
89
90
91     // Methods
92

93     /**
94      * @return the name
95      */

96     public String JavaDoc getName()
97     {
98     return name;
99     }
100
101
102     /**
103      * @return the value
104      */

105     public String JavaDoc getValue()
106     {
107     return value;
108     }
109
110
111     /**
112      * @return the parameters
113      */

114     public NVPair[] getParams()
115     {
116     return parameters;
117     }
118
119
120     /**
121      * Two elements are equal if they have the same name. The comparison is
122      * <em>case-insensitive</em>.
123      *
124      * @param obj the object to compare with
125      * @return true if <var>obj</var> is an HttpHeaderElement with the same
126      * name as this element.
127      */

128     public boolean equals(Object JavaDoc obj)
129     {
130     if ((obj != null) && (obj instanceof HttpHeaderElement))
131     {
132         String JavaDoc other = ((HttpHeaderElement) obj).name;
133         return name.equalsIgnoreCase(other);
134     }
135
136     return false;
137     }
138
139
140     /**
141      * @return a string containing the HttpHeaderElement formatted as it
142      * would appear in a header
143      */

144     public String JavaDoc toString()
145     {
146     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
147     appendTo(buf);
148     return buf.toString();
149     }
150
151
152     /**
153      * Append this header element to the given buffer. This is basically a
154      * more efficient version of <code>toString()</code> for assembling
155      * multiple elements.
156      *
157      * @param buf the StringBuffer to append this header to
158      * @see #toString()
159      */

160     public void appendTo(StringBuffer JavaDoc buf)
161     {
162     buf.append(name);
163
164     if (value != null)
165     {
166         if (Util.needsQuoting(value))
167         {
168         buf.append("=\"");
169         buf.append(Util.quoteString(value, "\\\""));
170         buf.append('"');
171         }
172         else
173         {
174         buf.append('=');
175         buf.append(value);
176         }
177     }
178
179     for (int idx=0; idx<parameters.length; idx++)
180     {
181         buf.append(";");
182         buf.append(parameters[idx].getName());
183         String JavaDoc pval = parameters[idx].getValue();
184         if (pval != null)
185         {
186         if (Util.needsQuoting(pval))
187         {
188             buf.append("=\"");
189             buf.append(Util.quoteString(pval, "\\\""));
190             buf.append('"');
191         }
192         else
193         {
194             buf.append('=');
195             buf.append(pval);
196         }
197         }
198     }
199     }
200 }
201
202
Popular Tags