KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > parser > HeaderParser


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package org.columba.ristretto.parser;
37
38 import java.io.IOException JavaDoc;
39 import java.util.regex.Matcher JavaDoc;
40 import java.util.regex.Pattern JavaDoc;
41
42 import org.columba.ristretto.io.Source;
43 import org.columba.ristretto.message.Header;
44
45 /**
46  * Parser for headers as defined in RFC 2822 and RFC 2045.
47  *
48  * @author Timo Stich <tstich@users.sourceforge.net>
49  */

50 public class HeaderParser {
51     
52     private static final Pattern JavaDoc linePattern = Pattern.compile("([^\r\n]*)(\r?\n)|(\r\n?)");
53     private static final Pattern JavaDoc keyValuePattern = Pattern.compile("([^:\\s]+): (.*)");
54     private static final Pattern JavaDoc lineWrapPattern = Pattern.compile("\\s(.*)");
55     
56     private HeaderParser() {
57     }
58     /**
59      * Parses the headers of a RFC 2822 or 2045 compliant message. The parser
60      * starts at the actual position of the Source. After the parsing process
61      * the Source is positioned after the headers.
62      *
63      * @see BodyParser
64      *
65      * @param source
66      * @return the Header
67      * @throws ParserException
68      */

69     public static Header parse( Source source ) throws ParserException {
70         Header header = new Header();
71         Matcher JavaDoc lineMatcher = linePattern.matcher(source);
72         Matcher JavaDoc keyValueMatcher = keyValuePattern.matcher("");
73         Matcher JavaDoc lineWrapMatcher = lineWrapPattern.matcher("");
74         
75         String JavaDoc lastKey = null;
76         String JavaDoc lastValue = null;
77         
78         while(lineMatcher.find()) {
79             // Is this the end of the header?
80
if( lineMatcher.group(1) == null) {
81                 // Store a previously found key value pair
82
if( lastValue != null) {
83                     header.append(lastKey, lastValue);
84                 }
85                 
86                 // we are done
87
return header;
88             }
89             
90             // Is this the end of the header?
91
if( lineMatcher.group(1).equals("")) {
92                 // Store a previously found key value pair
93
if( lastValue != null) {
94                     header.append(lastKey, lastValue);
95                 }
96                 
97                 // Go to the end of the header
98
try {
99                     source.seek(lineMatcher.end());
100                 } catch (IOException JavaDoc e) {
101                     e.printStackTrace();
102                 }
103                 
104                 // we are done
105
return header;
106             }
107             
108             // Check first for the most possible type:
109
// a simple key value line
110
keyValueMatcher.reset(lineMatcher.group(1));
111             if( keyValueMatcher.matches()) {
112                 // Store a previously found key value pair
113
if( lastValue != null) {
114                     header.append(lastKey, lastValue);
115                 }
116                 
117                 // This is a normal key value headerline
118
lastKey = normalizeKey( keyValueMatcher.group(1) );
119                 lastValue = keyValueMatcher.group(2);
120             } else {
121                 // Do we have a folding WS?
122
lineWrapMatcher.reset(lineMatcher.group(1));
123                 if( lineWrapMatcher.matches()) {
124                     if(lastValue != null) {
125                         // Append this to the last value
126
lastValue += lineWrapMatcher.group(1);
127                     }
128                 }
129                 
130             }
131         }
132
133         // We detected no end of header but ran out of lines
134
// Store a previously found key value pair
135
if( lastValue != null) {
136             header.append(lastKey, lastValue);
137         }
138         try {
139             // Seek source to its end
140
source.seek(source.length()-1);
141         } catch (IOException JavaDoc e) {
142             e.printStackTrace();
143         }
144         
145         return header;
146     }
147     
148     /**
149      * Formats the keys of the headers as defined in RFC 2822 and 2045.
150      * This is used to e.g. transform "from" -> "From".
151      *
152      * @param key unformated key.
153      * @return correctly formated key.
154      */

155     public static String JavaDoc normalizeKey( String JavaDoc key ) {
156         StringBuffer JavaDoc normalizedKey = new StringBuffer JavaDoc( key.length() );
157         char last = key.charAt(0);
158         char act;
159         normalizedKey.append(Character.toUpperCase(key.charAt(0)));
160
161         for( int i=1; i<key.length(); i++) {
162             act = key.charAt(i);
163             if( last == '-' || (( last == 'I' || last == 'i') && act == 'd') ) {
164                 normalizedKey.append(Character.toUpperCase(act));
165             } else {
166                 normalizedKey.append(Character.toLowerCase(act) );
167             }
168             last = act;
169         }
170         
171         return normalizedKey.toString();
172     }
173 }
174
Free Books   Free Magazines  
Popular Tags