001: /*
002: * @(#)HttpHeaderElement.java 0.3-2 18/06/1999
003: *
004: * This file is part of the HTTPClient package
005: * Copyright (C) 1996-1999 Ronald Tschalär
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free
019: * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
020: * MA 02111-1307, USA
021: *
022: * For questions, suggestions, bug-reports, enhancement-requests etc.
023: * I may be contacted at:
024: *
025: * ronald@innovation.ch
026: *
027: */
028:
029: package HTTPClient;
030:
031: /**
032: * This class holds a description of an http header element. It is used
033: * by <code>HTTPClient.Util.parseHeader()</code>.
034: *
035: * @see Util#parseHeader(java.lang.String)
036: * @see Util#getElement(java.util.Vector, java.lang.String)
037: * @see Util#assembleHeader(java.util.Vector)
038: * @version 0.3-2 18/06/1999
039: * @author Ronald Tschalär
040: */
041:
042: public class HttpHeaderElement {
043: /** element name */
044: private String name;
045:
046: /** element value */
047: private String value;
048:
049: /** element parameters */
050: private NVPair[] parameters;
051:
052: // Constructors
053:
054: /**
055: * Construct an element with the given name. The value and parameters
056: * are set to null. This can be used when a dummy element is constructed
057: * for comparison or retrieval purposes.
058: *
059: * @param name the name of the element
060: */
061: public HttpHeaderElement(String name) {
062: this .name = name;
063: this .value = null;
064: parameters = new NVPair[0];
065: }
066:
067: /**
068: * @param name the first token in the element
069: * @param value the value part, or null
070: * @param params the parameters
071: */
072: public HttpHeaderElement(String name, String value, NVPair[] params) {
073: this .name = name;
074: this .value = value;
075: if (params != null) {
076: parameters = new NVPair[params.length];
077: System.arraycopy(params, 0, parameters, 0, params.length);
078: } else
079: parameters = new NVPair[0];
080: }
081:
082: // Methods
083:
084: /**
085: * @return the name
086: */
087: public String getName() {
088: return name;
089: }
090:
091: /**
092: * @return the value
093: */
094: public String getValue() {
095: return value;
096: }
097:
098: /**
099: * @return the parameters
100: */
101: public NVPair[] getParams() {
102: return parameters;
103: }
104:
105: /**
106: * Two elements are equal if they have the same name. The comparison is
107: * <em>case-insensitive</em>.
108: *
109: * @param obj the object to compare with
110: * @return true if <var>obj</var> is an HttpHeaderElement with the same
111: * name as this element.
112: */
113: public boolean equals(Object obj) {
114: if ((obj != null) && (obj instanceof HttpHeaderElement)) {
115: String other = ((HttpHeaderElement) obj).name;
116: return name.equalsIgnoreCase(other);
117: }
118:
119: return false;
120: }
121:
122: /**
123: * @return a string containing the HttpHeaderElement formatted as it
124: * would appear in a header
125: */
126: public String toString() {
127: StringBuffer buf = new StringBuffer();
128: appendTo(buf);
129: return buf.toString();
130: }
131:
132: /**
133: * Append this header element to the given buffer. This is basically a
134: * more efficient version of <code>toString()</code> for assembling
135: * multiple elements.
136: *
137: * @param buf the StringBuffer to append this header to
138: * @see #toString()
139: */
140: public void appendTo(StringBuffer buf) {
141: buf.append(name);
142:
143: if (value != null) {
144: if (Util.needsQuoting(value)) {
145: buf.append("=\"");
146: buf.append(Util.quoteString(value, "\\\""));
147: buf.append('"');
148: } else {
149: buf.append('=');
150: buf.append(value);
151: }
152: }
153:
154: for (int idx = 0; idx < parameters.length; idx++) {
155: buf.append(";");
156: buf.append(parameters[idx].getName());
157: String pval = parameters[idx].getValue();
158: if (pval != null) {
159: if (Util.needsQuoting(pval)) {
160: buf.append("=\"");
161: buf.append(Util.quoteString(pval, "\\\""));
162: buf.append('"');
163: } else {
164: buf.append('=');
165: buf.append(pval);
166: }
167: }
168: }
169: }
170: }
|