KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > HeaderTokenizer


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
//
17
// This code has been copied from package org.columba.mail.parser for use in the
18
// EudoraMailImportFilter plugin (with minor modifications) by Karl Peder Olesen, 2003-05-15
19

20 package org.columba.mail;
21
22 /**
23  * Tokenizer used for parsing RFC822 compliant headers
24  *
25  * @author Frederik Dietz and Time Stich
26  * @author Karl Peder Olesen (only minor modifications)
27  * @version 1.0
28  */

29 class HeaderTokenizer
30 {
31
32     private String JavaDoc input;
33     private int actPos;
34     
35     public HeaderTokenizer( String JavaDoc input )
36         {
37             this.input = input;
38             actPos = 0;
39         }
40
41 // This Method frees a given String from Comments defined in Rfc822
42

43     private String JavaDoc killComments( String JavaDoc commented )
44         {
45             int end;
46             int start = commented.indexOf("(");
47             int quoted;
48
49             if( start == -1 ) return commented;
50
51             StringBuffer JavaDoc output = new StringBuffer JavaDoc( commented );
52             
53             while( start != -1 )
54             {
55                 end = output.toString().indexOf(")",start);
56
57                 if( end == -1 ) return output.toString();
58
59                 quoted = commented.indexOf( "?=", start );
60                 if( (quoted == -1) | (quoted > end) ) {
61                     output = output.delete(start,end+1);
62                     start = output.toString().indexOf("(");
63                 }
64                 else {
65                     start = output.toString().indexOf("(", end);
66                 }
67             }
68
69             return output.toString();
70         }
71     
72 // This Method delivers the next line
73

74     public String JavaDoc nextLine()
75         {
76             StringBuffer JavaDoc output;
77             int crlfPos;
78
79             if(actPos >= input.length()) return null;
80
81             if( (input.charAt(actPos)=='\n') ) return null;
82             
83             crlfPos = input.indexOf('\n',actPos);
84             if( crlfPos == -1 ) return null;
85
86             output = new StringBuffer JavaDoc( killComments( input.substring(actPos,crlfPos)) );
87             actPos = crlfPos + 1;
88             if(actPos >= input.length()) return output.toString().trim();
89             
90             while( input.charAt(actPos) == '\t' | input.charAt(actPos) == ' ' )
91             {
92                 crlfPos = input.indexOf('\n',actPos);
93                 output.append( '\n' + killComments( input.substring(actPos,crlfPos)) ); // *20030515, kpo* '\n' appended
94
actPos = crlfPos + 1;
95                 if( actPos >= input.length() ) return output.toString().trim();
96             }
97                 
98             return output.toString().trim();
99                 
100         }
101     
102 }
103
Popular Tags