KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > mail > NewsGroupSummaryEntry


1 /*
2  * NewsGroupSummaryEntry.java
3  *
4  * Copyright (C) 2000-2002 Peter Graves
5  * $Id: NewsGroupSummaryEntry.java,v 1.3 2003/01/10 16:18:39 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.mail;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26 import org.armedbear.j.FastStringBuffer;
27 import org.armedbear.j.Log;
28 import org.armedbear.j.Utilities;
29
30 public final class NewsGroupSummaryEntry extends MailboxEntry
31 {
32     private String JavaDoc from;
33     private int lineCount;
34
35     private NewsGroupSummaryEntry()
36     {
37     }
38
39     public static NewsGroupSummaryEntry parseOverviewEntry(String JavaDoc s)
40     {
41         NewsGroupSummaryEntry entry = new NewsGroupSummaryEntry();
42         while (true) {
43             int begin = 0;
44             int end = s.indexOf('\t', begin);
45             if (end < 0)
46                 return null;
47             String JavaDoc token = s.substring(begin, end);
48             try {
49                 entry.messageNumber = Integer.parseInt(token);
50             }
51             catch (NumberFormatException JavaDoc e) {
52                 Log.error(e);
53                 return null;
54             }
55             begin = end + 1;
56             end = s.indexOf('\t', begin);
57             if (end < 0)
58                 return null;
59             entry.subject = s.substring(begin, end);
60             begin = end + 1;
61             end = s.indexOf('\t', begin);
62             if (end < 0)
63                 break;
64             entry.from = s.substring(begin, end);
65             begin = end + 1;
66             end = s.indexOf('\t', begin);
67             if (end < 0)
68                 break;
69             entry.date = RFC822Date.parseDate(s.substring(begin, end)); // Date.
70
begin = end + 1;
71             end = s.indexOf('\t', begin);
72             if (end < 0)
73                 break;
74             entry.messageId = s.substring(begin, end); // Message ID.
75
begin = end + 1;
76             end = s.indexOf('\t', begin);
77             if (end < 0)
78                 break;
79             String JavaDoc refs = s.substring(begin, end); // References.
80
if (refs != null)
81                 entry.references = parseReferences(refs);
82             begin = end + 1;
83             end = s.indexOf('\t', begin);
84             if (end < 0)
85                 break;
86             if (end > begin) {
87                 token = s.substring(begin, end); // Byte count.
88
try {
89                     entry.size = Integer.parseInt(token);
90                 }
91                 catch (NumberFormatException JavaDoc e) {
92                     Log.error(e);
93                 }
94             }
95             begin = end + 1;
96             end = s.indexOf('\t', begin);
97             if (end < 0)
98                 end = s.length(); // This might be the last token.
99
if (end > begin) {
100                 token = s.substring(begin, end); // Line count.
101
try {
102                     entry.lineCount = Integer.parseInt(token);
103                 }
104                 catch (NumberFormatException JavaDoc e) {
105                     Log.error(e);
106                 }
107             }
108             // Done with this entry.
109
break;
110         }
111         return entry;
112     }
113
114     public final int getArticleNumber()
115     {
116         return messageNumber;
117     }
118
119     public String JavaDoc toString()
120     {
121         return toString(1);
122     }
123
124     public String JavaDoc toString(int depth)
125     {
126         FastStringBuffer sb = new FastStringBuffer();
127         if (SHOW_MESSAGE_NUMBERS) {
128             sb.append(Utilities.rightJustify(getSequenceNumber(), 4));
129             sb.append(' ');
130         }
131         sb.append(" "); // to
132
sb.append(formatFlags());
133         sb.append(" ");
134         sb.append(formatDate());
135         sb.append(" ");
136         sb.append(formatFrom(20));
137         sb.append(" ");
138         sb.append(formatSize());
139         sb.append(Utilities.spaces(depth+1));
140         if (subject != null)
141             sb.append(subject);
142         return sb.toString();
143     }
144
145     protected String JavaDoc formatFrom(int fieldWidth)
146     {
147         String JavaDoc s = from;
148         int index = s.indexOf('<');
149         if (index > 0)
150             s = s.substring(0, index).trim();
151         else {
152             index = s.indexOf('(');
153             if (index > 0)
154                 s = s.substring(0, index).trim();
155         }
156         if (s.length() >= 2) {
157             if (s.charAt(0) == '"' && s.charAt(s.length()-1) == '"') {
158                 s = s.substring(1, s.length()-1);
159             }
160         }
161         if (fieldWidth > 0) {
162             int length = s.length();
163             if (length > fieldWidth)
164                 s = s.substring(0, fieldWidth);
165             else if (length < fieldWidth)
166                 s = s.concat(Utilities.spaces(fieldWidth - length));
167         }
168         return s;
169     }
170 }
171
Popular Tags