001: // MimeHeaders.java
002: // $Id: MimeHeaders.java,v 1.4 2000/08/16 21:38:01 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.www.mime;
007:
008: import java.io.IOException;
009: import java.io.InputStream;
010: import java.io.PrintStream;
011:
012: import java.util.Enumeration;
013: import java.util.Hashtable;
014:
015: /**
016: * The most stupid MIME header holder.
017: * This class uses a hashtable mapping header names (as String), to header
018: * values (as String). Header names are lowered before entering the hashtable.
019: */
020:
021: public class MimeHeaders implements MimeHeaderHolder {
022: Hashtable headers = null;
023: MimeParser parser = null;
024:
025: /**
026: * A new header has been parsed.
027: * @param name The name of the encountered header.
028: * @param buf The byte buffer containing the value.
029: * @param off Offset of the header value in the above buffer.
030: * @param len Length of the value in the above header.
031: * @exception MimeParserException if the parsing failed
032: */
033:
034: public void notifyHeader(String name, byte buf[], int off, int len)
035: throws MimeParserException {
036: String lname = name.toLowerCase();
037: String oldval = null;
038: if (headers == null) {
039: headers = new Hashtable(5);
040: } else {
041: oldval = (String) headers.get(lname);
042: }
043: String newval = ((oldval != null) ? oldval + ","
044: + new String(buf, 0, off, len) : new String(buf, 0,
045: off, len));
046: headers.put(lname, newval);
047: }
048:
049: /**
050: * The parsing is now about to start, take any appropriate action.
051: * This hook can return a <strong>true</strong> boolean value to enforce
052: * the MIME parser into transparent mode (eg the parser will <em>not</em>
053: * try to parse any headers.
054: * <p>This hack is primarily defined for HTTP/0.9 support, it might
055: * also be usefull for other hacks.
056: * @param parser The Mime parser.
057: * @return A boolean <strong>true</strong> if the MimeParser shouldn't
058: * continue the parsing, <strong>false</strong> otherwise.
059: * @exception IOException if an IO error occurs.
060: */
061:
062: public boolean notifyBeginParsing(MimeParser parser)
063: throws IOException {
064: return false;
065: }
066:
067: /**
068: * All the headers have been parsed, take any appropriate actions.
069: * @param parser The Mime parser.
070: * @exception IOException if an IO error occurs.
071: */
072:
073: public void notifyEndParsing(MimeParser parser) throws IOException {
074: return;
075: }
076:
077: /**
078: * Set a header value.
079: * @param name The header name.
080: * @param value The header value.
081: */
082:
083: public void setValue(String name, String value) {
084: if (headers == null)
085: headers = new Hashtable(5);
086: headers.put(name.toLowerCase(), value);
087: }
088:
089: /**
090: * Retreive a header value.
091: * @param name The name of the header.
092: * @return The value for this header, or <strong>null</strong> if
093: * undefined.
094: */
095:
096: public String getValue(String name) {
097: return ((headers != null) ? (String) headers.get(name
098: .toLowerCase()) : null);
099: }
100:
101: /**
102: * Enumerate the headers defined by the holder.
103: * @return A enumeration of header names, or <strong>null</strong> if no
104: * header is defined.
105: */
106:
107: public Enumeration enumerateHeaders() {
108: if (headers == null)
109: return null;
110: return headers.keys();
111: }
112:
113: /**
114: * Get the entity stream attached to these headers, if any.
115: * @return An InputStream instance, or <strong>null</strong> if no
116: * entity available.
117: */
118:
119: public InputStream getInputStream() {
120: return ((parser != null) ? parser.getInputStream() : null);
121: }
122:
123: /**
124: * Dump all headers to the given stream.
125: * @param out The stream to dump to.
126: */
127:
128: public void dump(PrintStream out) {
129: Enumeration names = enumerateHeaders();
130: if (names != null) {
131: while (names.hasMoreElements()) {
132: String name = (String) names.nextElement();
133: out.println(name + ": " + headers.get(name));
134: }
135: }
136: }
137:
138: public MimeHeaders(MimeParser parser) {
139: this .parser = parser;
140: }
141:
142: public MimeHeaders() {
143: }
144:
145: }
|