KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > io > HeaderEnumeration


1 package com.quadcap.io;
2
3 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.util.Enumeration JavaDoc;
42 import java.util.Map JavaDoc;
43 import java.util.NoSuchElementException JavaDoc;
44
45 import java.io.IOException JavaDoc;
46 import java.io.InputStream JavaDoc;
47 import java.io.PushbackInputStream JavaDoc;
48
49 import com.quadcap.util.Debug;
50
51 /**
52  * Return an enumeration of the headers of an RFC-822 style message.
53  * This implementation assumes that the headers have already been separated
54  * from the body, so that the input stream passed to the HeaderEnumeration
55  * constructor returns only the header portion of the message.
56  *
57  * <p><i>XXX It would be pretty easy to have this class detect the
58  * CRLF, CRLF sequence that ends the headers, as well....</i>
59  *
60  * @author Stan Bailes
61  */

62 public class HeaderEnumeration implements Enumeration JavaDoc {
63     String JavaDoc next;
64     PushbackInputStream JavaDoc is;
65     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
66
67     static final byte CR = 0x0d;
68     static final byte LF = 0x0a;
69
70     public HeaderEnumeration(InputStream JavaDoc is) {
71     this.is = new PushbackInputStream JavaDoc(is);
72     }
73
74     /**
75      * Tests if this enumeration contains more elements.
76      *
77      * @return <code>true</code> if this enumeration contains more elements;
78      * <code>false</code> otherwise.
79      */

80     public boolean hasMoreElements() {
81     if (next == null) getNext();
82     return (next != null);
83     }
84
85     /**
86      * Returns the next element of this enumeration.
87      *
88      * @return the next element of this enumeration.
89      * @exception NoSuchElementException if no more elements exist.
90      */

91     public Object JavaDoc nextElement() {
92     if (next == null) getNext();
93     if (next == null) throw new NoSuchElementException JavaDoc("no more elements");
94     Object JavaDoc ret = next;
95     next = null;
96     return ret;
97     }
98
99     /**
100      * Run the state machine to produce the next header.
101      */

102     void getNext() {
103     sb.setLength(0);
104
105     int c;
106     int state = 0;
107
108     try {
109         while ((c = is.read()) >= 0) {
110         switch (state) {
111         case 0:
112             if (c == CR) {
113             state = 1;
114             } else {
115             sb.append((char)c);
116             }
117             break;
118         case 1:
119             if (c == LF) {
120             state = 2;
121             } else if (c != CR) {
122             sb.append((char)c);
123             state = 0;
124             }
125             break;
126         case 2:
127             state = 0;
128             if (c == ' ' || c == '\t') {
129             sb.append(' ');
130             } else {
131             is.unread(c);
132             next = sb.toString();
133             sb.setLength(0);
134             return;
135             }
136             break;
137         }
138         }
139             if (state == 2 && sb.toString().trim().length() > 0) {
140                 next = sb.toString().trim();
141                 sb.setLength(0);
142             }
143     } catch (IOException JavaDoc e) {
144         Debug.print(e);
145     }
146     }
147
148     /**
149      * Helper function to build map for header (name -> value).
150      *
151      * @exception IOException may be thrown.
152      */

153     public void getHeaderMap(Map JavaDoc headerMap) throws IOException JavaDoc {
154     while (hasMoreElements()) {
155         String JavaDoc header = (String JavaDoc)nextElement();
156         int idx = header.indexOf(':');
157         if (idx >= 0) {
158         String JavaDoc headerName = header.substring(0, idx).trim();
159         String JavaDoc headerVal = header.substring(idx+1).trim();
160         headerMap.put(headerName.toLowerCase(), headerVal);
161         } else {
162         throw new IOException JavaDoc("Bad header: '" + header + "'");
163         }
164     }
165     }
166
167 }
168
Popular Tags