1 package SnowMailClient.model; 2 3 4 import SnowMailClient.utils.MailMessageUtils; 5 import SnowMailClient.utils.NumberedLineReader; 6 7 8 import java.util.*; 9 import java.text.*; 10 import java.io.*; 11 12 13 15 public class Header 16 { 17 public static boolean debug = false; 18 19 21 public final static String GPG_IS_SIGNED_BY = "gpg-snowmail-is-signed-by"; 22 23 25 public final static String GPG_HAS_BEEN_SIGNED_BY = "gpg-snowmail-has-been-signed-by"; 26 27 private final List<HeaderEntry> entries = new ArrayList<HeaderEntry>(); 28 29 30 public Header() 31 { 32 } 34 public int getEntriesCount() { return entries.size(); } 35 public HeaderEntry getEntryAt(int pos) { return entries.get(pos); } 36 37 public void removeAllEntries() 38 { 39 entries.clear(); 40 } 41 42 public void addEntry(HeaderEntry he) 43 { 44 entries.add(he); 45 } 46 47 public void removeEntry(HeaderEntry he) 48 { 49 entries.remove(he); 50 } 51 52 public void remove(String key) 53 { 54 HeaderEntry he = this.getEntry(key); 55 if(he!=null) removeEntry(he); 56 } 57 58 59 60 62 public void addEntry(String key, String value) 63 { 64 HeaderEntry he = new HeaderEntry(key, value); 65 this.addEntry(he); 66 } 67 68 70 public void setEntryOverwrite(String key, String value) 71 { 72 HeaderEntry he = getEntry(key); 73 if(he!=null) 74 { 75 this.removeEntry(he); 76 } 77 78 he = new HeaderEntry(key, value); 79 this.addEntry(he); 80 } 81 82 84 public HeaderEntry getEntry(String key) 85 { 86 for(HeaderEntry he: entries) 87 { 88 if(he.isEquals(key)) return he; 89 } 90 return null; 91 } 92 93 public boolean hasEntry(String key) 94 { 95 for(HeaderEntry he: entries) 96 { 97 if(he.isEquals(key)) return true; 98 } 99 return false; 100 } 101 102 104 public Vector<HeaderEntry> getAllEntries(String key) 105 { 106 Vector<HeaderEntry> found = new Vector<HeaderEntry>(); 107 for(HeaderEntry he: entries) 108 { 109 if(he.isEquals(key)) found.addElement(he); 110 } 111 return found; 112 } 113 114 public String getEntryValue(String key, String def) 115 { 116 return getRawEntryValue(key, def); 117 } 118 119 120 public String getRawEntryValue(String key, String def) 121 { 122 for(HeaderEntry he: entries) 123 { 124 if(he.isEquals(key)) return he.getValue(); 125 } 126 return def; 127 } 128 129 130 132 public String toString() 133 { 134 StringBuffer sb = new StringBuffer (); 135 for(HeaderEntry he: entries) 136 { 137 sb.append(he.toString()+"\r\n"); 138 } 139 return sb.toString(); 140 } 141 142 public String to_ASCII_String() 143 { 144 StringBuffer sb = new StringBuffer (); 145 for(HeaderEntry he: entries) 146 { 147 sb.append(he.to_ASCII_String()+"\r\n"); 148 } 149 return sb.toString(); 150 } 151 152 153 156 public static void parseHeader(final NumberedLineReader in, final Header header) throws Exception 157 { 158 String actualTag = null; StringBuffer actualArgBuffer = new StringBuffer (); 160 boolean nextLineSameTag = false; 161 162 String actualLine = in.readLine(); 163 while(actualLine != null && !actualLine.trim().equals("")) 165 { 166 actualLine = actualLine.trim(); 167 if(debug) System.out.println("Header line = " + actualLine); 168 169 int pos = actualLine.indexOf(':'); 172 int posSp = actualLine.indexOf(' '); 173 174 if(pos==-1 || (posSp>0 && posSp<pos)) 176 { 177 if(actualTag == null) 178 { 179 if(actualLine.startsWith("--")) 181 { 182 in.undoRead(); return; 184 } 185 throw new Exception ("First tag empty '"+actualLine+"'"); 186 } 187 else 188 { 189 actualArgBuffer.append("\n "+actualLine.trim()); 191 } 192 } 193 else 194 { 195 if( actualTag !=null) 197 { 198 actualTag = actualTag.trim(); 199 if(actualTag.equals("subject") || actualTag.equals("from") || actualTag.equals("to")) 200 { 201 String val = MailMessageUtils.decodeCharsetCodedArgumentFully( 202 actualArgBuffer.toString()); 203 header.addEntry(actualTag, val); 204 } 205 else 206 { 207 header.addEntry(actualTag, actualArgBuffer.toString()); 208 } 209 } 210 actualTag = actualLine.substring(0,pos).toLowerCase().trim(); 212 actualArgBuffer.delete(0, actualArgBuffer.length()); 213 actualArgBuffer.append(actualLine.substring(pos+1).trim()); 214 } 215 216 actualLine = in.readLine(); 218 } 219 220 if( actualTag !=null) 222 { 223 header.addEntry(actualTag, actualArgBuffer.toString()); 224 } 225 } 226 227 228 229 } | Popular Tags |