001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: //
017: // This code has been copied from package org.columba.mail.parser for use in the
018: // EudoraMailImportFilter plugin (with minor modifications) by Karl Peder Olesen, 2003-05-15
019:
020: package org.columba.mail;
021:
022: /**
023: * Tokenizer used for parsing RFC822 compliant headers
024: *
025: * @author Frederik Dietz and Time Stich
026: * @author Karl Peder Olesen (only minor modifications)
027: * @version 1.0
028: */
029: class HeaderTokenizer {
030:
031: private String input;
032: private int actPos;
033:
034: public HeaderTokenizer(String input) {
035: this .input = input;
036: actPos = 0;
037: }
038:
039: // This Method frees a given String from Comments defined in Rfc822
040:
041: private String killComments(String commented) {
042: int end;
043: int start = commented.indexOf("(");
044: int quoted;
045:
046: if (start == -1)
047: return commented;
048:
049: StringBuffer output = new StringBuffer(commented);
050:
051: while (start != -1) {
052: end = output.toString().indexOf(")", start);
053:
054: if (end == -1)
055: return output.toString();
056:
057: quoted = commented.indexOf("?=", start);
058: if ((quoted == -1) | (quoted > end)) {
059: output = output.delete(start, end + 1);
060: start = output.toString().indexOf("(");
061: } else {
062: start = output.toString().indexOf("(", end);
063: }
064: }
065:
066: return output.toString();
067: }
068:
069: // This Method delivers the next line
070:
071: public String nextLine() {
072: StringBuffer output;
073: int crlfPos;
074:
075: if (actPos >= input.length())
076: return null;
077:
078: if ((input.charAt(actPos) == '\n'))
079: return null;
080:
081: crlfPos = input.indexOf('\n', actPos);
082: if (crlfPos == -1)
083: return null;
084:
085: output = new StringBuffer(killComments(input.substring(actPos,
086: crlfPos)));
087: actPos = crlfPos + 1;
088: if (actPos >= input.length())
089: return output.toString().trim();
090:
091: while (input.charAt(actPos) == '\t'
092: | input.charAt(actPos) == ' ') {
093: crlfPos = input.indexOf('\n', actPos);
094: output.append('\n' + killComments(input.substring(actPos,
095: crlfPos))); // *20030515, kpo* '\n' appended
096: actPos = crlfPos + 1;
097: if (actPos >= input.length())
098: return output.toString().trim();
099: }
100:
101: return output.toString().trim();
102:
103: }
104:
105: }
|