1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.net.nntp;
19
20 import java.util.ArrayList;
21
22
23
24
25
26
27 public class Article implements Threadable {
28 private long articleNumber;
29 private String subject;
30 private String date;
31 private String articleId;
32 private String simplifiedSubject;
33 private String from;
34 private ArrayList<String> references;
35 private boolean isReply = false;
36
37 public Article kid, next;
38
39 public Article() {
40 articleNumber = -1;
41 }
42
43
44
45
46
47 public void addReference(String msgId) {
48 if (msgId == null || msgId.length() == 0) {
49 return;
50 }
51 if (references == null) {
52 references = new ArrayList<String>();
53 }
54 isReply = true;
55 for(String s : msgId.split(" ")) {
56 references.add(s);
57 }
58 }
59
60
61
62
63
64 public String[] getReferences() {
65 if (references == null) {
66 return new String[0];
67 }
68 return references.toArray(new String[references.size()]);
69 }
70
71
72
73
74
75 private void simplifySubject() {
76 int start = 0;
77 String subject = getSubject();
78 int len = subject.length();
79
80 boolean done = false;
81
82 while (!done) {
83 done = true;
84
85
86
87 while (start < len && subject.charAt(start) == ' ') {
88 start++;
89 }
90
91 if (start < (len - 2)
92 && (subject.charAt(start) == 'r' || subject.charAt(start) == 'R')
93 && (subject.charAt(start + 1) == 'e' || subject.charAt(start + 1) == 'E')) {
94
95 if (subject.charAt(start + 2) == ':') {
96 start += 3;
97 done = false;
98 } else if (
99 start < (len - 2)
100 &&
101 (subject.charAt(start + 2) == '[' || subject.charAt(start + 2) == '(')) {
102
103 int i = start + 3;
104
105 while (i < len && subject.charAt(i) >= '0' && subject.charAt(i) <= '9') {
106 i++;
107 }
108
109 if (i < (len - 1)
110 && (subject.charAt(i) == ']' || subject.charAt(i) == ')')
111 && subject.charAt(i + 1) == ':')
112 {
113 start = i + 2;
114 done = false;
115 }
116 }
117 }
118
119 if ("(no subject)".equals(simplifiedSubject)) {
120 simplifiedSubject = "";
121 }
122
123 int end = len;
124
125 while (end > start && subject.charAt(end - 1) < ' ') {
126 end--;
127 }
128
129 if (start == 0 && end == len) {
130 simplifiedSubject = subject;
131 } else {
132 simplifiedSubject = subject.substring(start, end);
133 }
134 }
135 }
136
137
138
139
140
141
142
143 public static void printThread(Article article, int depth) {
144 for (int i = 0; i < depth; ++i) {
145 System.out.print("==>");
146 }
147 System.out.println(article.getSubject() + "\t" + article.getFrom()+"\t"+article.getArticleId());
148 if (article.kid != null) {
149 printThread(article.kid, depth + 1);
150 }
151 if (article.next != null) {
152 printThread(article.next, depth);
153 }
154 }
155
156 public String getArticleId() {
157 return articleId;
158 }
159
160 public long getArticleNumberLong() {
161 return articleNumber;
162 }
163
164 public String getDate() {
165 return date;
166 }
167
168 public String getFrom() {
169 return from;
170 }
171
172 public String getSubject() {
173 return subject;
174 }
175
176 public void setArticleId(String string) {
177 articleId = string;
178 }
179
180 public void setArticleNumber(long l) {
181 articleNumber = l;
182 }
183
184 public void setDate(String string) {
185 date = string;
186 }
187
188 public void setFrom(String string) {
189 from = string;
190 }
191
192 public void setSubject(String string) {
193 subject = string;
194 }
195
196
197
198 public boolean isDummy() {
199 return (articleNumber == -1);
200 }
201
202
203 public String messageThreadId() {
204 return articleId;
205 }
206
207
208 public String[] messageThreadReferences() {
209 return getReferences();
210 }
211
212
213 public String simplifiedSubject() {
214 if(simplifiedSubject == null) {
215 simplifySubject();
216 }
217 return simplifiedSubject;
218 }
219
220
221
222 public boolean subjectIsReply() {
223 return isReply;
224 }
225
226
227
228 public void setChild(Threadable child) {
229 this.kid = (Article) child;
230 flushSubjectCache();
231 }
232
233 private void flushSubjectCache() {
234 simplifiedSubject = null;
235 }
236
237
238
239 public void setNext(Threadable next) {
240 this.next = (Article)next;
241 flushSubjectCache();
242 }
243
244
245
246 public Threadable makeDummy() {
247 return new Article();
248 }
249
250 @Override
251 public String toString(){
252 return articleNumber + " " +articleId + " " + subject;
253 }
254
255
256
257 @Deprecated
258 public int getArticleNumber() {
259 return (int) articleNumber;
260 }
261
262 @Deprecated
263 public void setArticleNumber(int a) {
264 articleNumber = a;
265 }
266 @Deprecated
267
268 public void addHeaderField(String name, String val) {
269 }
270
271 }