1 21 22 package org.armedbear.j.mail; 23 24 import java.util.ArrayList ; 25 import java.util.List ; 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 from; 33 private int lineCount; 34 35 private NewsGroupSummaryEntry() 36 { 37 } 38 39 public static NewsGroupSummaryEntry parseOverviewEntry(String 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 token = s.substring(begin, end); 48 try { 49 entry.messageNumber = Integer.parseInt(token); 50 } 51 catch (NumberFormatException 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)); begin = end + 1; 71 end = s.indexOf('\t', begin); 72 if (end < 0) 73 break; 74 entry.messageId = s.substring(begin, end); begin = end + 1; 76 end = s.indexOf('\t', begin); 77 if (end < 0) 78 break; 79 String refs = s.substring(begin, end); 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); try { 89 entry.size = Integer.parseInt(token); 90 } 91 catch (NumberFormatException 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(); if (end > begin) { 100 token = s.substring(begin, end); try { 102 entry.lineCount = Integer.parseInt(token); 103 } 104 catch (NumberFormatException e) { 105 Log.error(e); 106 } 107 } 108 break; 110 } 111 return entry; 112 } 113 114 public final int getArticleNumber() 115 { 116 return messageNumber; 117 } 118 119 public String toString() 120 { 121 return toString(1); 122 } 123 124 public String 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(" "); 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 formatFrom(int fieldWidth) 146 { 147 String 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 |