KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > Headers


1 /*
2  * Headers.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: Headers.java,v 1.1.1.1 2002/09/24 16:09:10 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program 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
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 public final class Headers
25 {
26     // These are indexes into the array of values. We only store values for the
27
// headers we're interested in.
28
public static final int CC = 0;
29     public static final int CONTENT_DISPOSITION = 1;
30     public static final int CONTENT_TRANSFER_ENCODING = 2;
31     public static final int CONTENT_TYPE = 3;
32     public static final int DATE = 4;
33     public static final int FROM = 5;
34     public static final int IN_REPLY_TO = 6;
35     public static final int MESSAGE_ID = 7;
36     public static final int REFERENCES = 8;
37     public static final int REPLY_TO = 9;
38     public static final int SET_COOKIE = 10;
39     public static final int SUBJECT = 11;
40     public static final int TO = 12;
41     public static final int X_J_STATUS = 13;
42     public static final int X_UIDL = 14;
43
44     private static final int MAX_HEADERS = 15;
45
46     private String JavaDoc[] values = new String JavaDoc[MAX_HEADERS];
47
48     private Headers()
49     {
50     }
51
52     public String JavaDoc getValue(String JavaDoc name)
53     {
54         int index = getIndex(name);
55         if (index >= 0)
56             return values[index];
57         return null;
58     }
59
60     public String JavaDoc getValue(int index)
61     {
62         if (index < MAX_HEADERS)
63             return values[index];
64         Debug.bug();
65         return null;
66     }
67
68     private void setValue(String JavaDoc name, String JavaDoc value)
69     {
70         int index = getIndex(name);
71         if (index >= 0)
72             values[index] = value;
73     }
74
75     private int getIndex(String JavaDoc name)
76     {
77         if (name.length() == 0)
78             return -1;
79         name = name.toLowerCase();
80         switch (name.charAt(0)) {
81             case 'c':
82                 if (name.equals("cc"))
83                     return CC;
84                 if (name.equals("content-disposition"))
85                     return CONTENT_DISPOSITION;
86                 if (name.equals("content-transfer-encoding"))
87                     return CONTENT_TRANSFER_ENCODING;
88                 if (name.equals("content-type"))
89                     return CONTENT_TYPE;
90                 break;
91             case 'd':
92                 if (name.equals("date"))
93                     return DATE;
94                 break;
95             case 'f':
96                 if (name.equals("from"))
97                     return FROM;
98                 break;
99             case 'i':
100                 if (name.equals("in-reply-to"))
101                     return IN_REPLY_TO;
102                 break;
103             case 'm':
104                 if (name.equals("message-id"))
105                     return MESSAGE_ID;
106                 break;
107             case 'r':
108                 if (name.equals("references"))
109                     return REFERENCES;
110                 if (name.equals("reply-to"))
111                     return REPLY_TO;
112                 break;
113             case 's':
114                 if (name.equals("set-cookie"))
115                     return SET_COOKIE;
116                 if (name.equals("subject"))
117                     return SUBJECT;
118                 break;
119             case 't':
120                 if (name.equals("to"))
121                     return TO;
122                 break;
123             case 'x':
124                 if (name.equals("x-j-status"))
125                     return X_J_STATUS;
126                 if (name.equals("x-uidl"))
127                     return X_UIDL;
128                 break;
129             default:
130                 break;
131         }
132         return -1;
133     }
134
135     public static Headers parse(String JavaDoc s)
136     {
137         Headers headers = new Headers();
138         FastStringBuffer sb = new FastStringBuffer();
139         String JavaDoc name = null;
140         int begin = 0;
141         int end;
142         while (true) {
143             end = s.indexOf('\n', begin);
144             if (end < 0) {
145                 // Done.
146
if (name != null && sb.length() > 0)
147                     headers.setValue(name, sb.toString());
148                 break;
149             }
150             String JavaDoc line = s.substring(begin, end);
151             // Trim trailing '\r' if any.
152
if (line.length() > 0 && line.charAt(line.length()-1) == '\r')
153                 line = line.substring(0, line.length()-1);
154             if (line.length() == 0) {
155                 // Done.
156
if (name != null && sb.length() > 0)
157                     headers.setValue(name, sb.toString());
158                 break;
159             }
160             begin = end + 1;
161             char c = line.charAt(0);
162             if (c == ' ' || c == '\t') {
163                 // Continuation.
164
sb.append(line);
165                 continue;
166             }
167             if (name != null && sb.length() > 0)
168                 headers.setValue(name, sb.toString());
169             // New header.
170
name = null;
171             sb.setLength(0);
172             int i = line.indexOf(':');
173             if (i < 0) {
174                 Log.error("can't parse header |" + s + "|");
175                 return headers;
176             }
177             // Store names in lower case.
178
name = line.substring(0, i).trim();
179             // Field value.
180
sb.append(line.substring(i+1).trim());
181         }
182         return headers;
183     }
184 }
185
Popular Tags