001: /*
002: * NewsGroupSummaryEntry.java
003: *
004: * Copyright (C) 2000-2002 Peter Graves
005: * $Id: NewsGroupSummaryEntry.java,v 1.3 2003/01/10 16:18:39 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j.mail;
023:
024: import java.util.ArrayList;
025: import java.util.List;
026: import org.armedbear.j.FastStringBuffer;
027: import org.armedbear.j.Log;
028: import org.armedbear.j.Utilities;
029:
030: public final class NewsGroupSummaryEntry extends MailboxEntry {
031: private String from;
032: private int lineCount;
033:
034: private NewsGroupSummaryEntry() {
035: }
036:
037: public static NewsGroupSummaryEntry parseOverviewEntry(String s) {
038: NewsGroupSummaryEntry entry = new NewsGroupSummaryEntry();
039: while (true) {
040: int begin = 0;
041: int end = s.indexOf('\t', begin);
042: if (end < 0)
043: return null;
044: String token = s.substring(begin, end);
045: try {
046: entry.messageNumber = Integer.parseInt(token);
047: } catch (NumberFormatException e) {
048: Log.error(e);
049: return null;
050: }
051: begin = end + 1;
052: end = s.indexOf('\t', begin);
053: if (end < 0)
054: return null;
055: entry.subject = s.substring(begin, end);
056: begin = end + 1;
057: end = s.indexOf('\t', begin);
058: if (end < 0)
059: break;
060: entry.from = s.substring(begin, end);
061: begin = end + 1;
062: end = s.indexOf('\t', begin);
063: if (end < 0)
064: break;
065: entry.date = RFC822Date.parseDate(s.substring(begin, end)); // Date.
066: begin = end + 1;
067: end = s.indexOf('\t', begin);
068: if (end < 0)
069: break;
070: entry.messageId = s.substring(begin, end); // Message ID.
071: begin = end + 1;
072: end = s.indexOf('\t', begin);
073: if (end < 0)
074: break;
075: String refs = s.substring(begin, end); // References.
076: if (refs != null)
077: entry.references = parseReferences(refs);
078: begin = end + 1;
079: end = s.indexOf('\t', begin);
080: if (end < 0)
081: break;
082: if (end > begin) {
083: token = s.substring(begin, end); // Byte count.
084: try {
085: entry.size = Integer.parseInt(token);
086: } catch (NumberFormatException e) {
087: Log.error(e);
088: }
089: }
090: begin = end + 1;
091: end = s.indexOf('\t', begin);
092: if (end < 0)
093: end = s.length(); // This might be the last token.
094: if (end > begin) {
095: token = s.substring(begin, end); // Line count.
096: try {
097: entry.lineCount = Integer.parseInt(token);
098: } catch (NumberFormatException e) {
099: Log.error(e);
100: }
101: }
102: // Done with this entry.
103: break;
104: }
105: return entry;
106: }
107:
108: public final int getArticleNumber() {
109: return messageNumber;
110: }
111:
112: public String toString() {
113: return toString(1);
114: }
115:
116: public String toString(int depth) {
117: FastStringBuffer sb = new FastStringBuffer();
118: if (SHOW_MESSAGE_NUMBERS) {
119: sb.append(Utilities.rightJustify(getSequenceNumber(), 4));
120: sb.append(' ');
121: }
122: sb.append(" "); // to
123: sb.append(formatFlags());
124: sb.append(" ");
125: sb.append(formatDate());
126: sb.append(" ");
127: sb.append(formatFrom(20));
128: sb.append(" ");
129: sb.append(formatSize());
130: sb.append(Utilities.spaces(depth + 1));
131: if (subject != null)
132: sb.append(subject);
133: return sb.toString();
134: }
135:
136: protected String formatFrom(int fieldWidth) {
137: String s = from;
138: int index = s.indexOf('<');
139: if (index > 0)
140: s = s.substring(0, index).trim();
141: else {
142: index = s.indexOf('(');
143: if (index > 0)
144: s = s.substring(0, index).trim();
145: }
146: if (s.length() >= 2) {
147: if (s.charAt(0) == '"' && s.charAt(s.length() - 1) == '"') {
148: s = s.substring(1, s.length() - 1);
149: }
150: }
151: if (fieldWidth > 0) {
152: int length = s.length();
153: if (length > fieldWidth)
154: s = s.substring(0, fieldWidth);
155: else if (length < fieldWidth)
156: s = s.concat(Utilities.spaces(fieldWidth - length));
157: }
158: return s;
159: }
160: }
|