1 36 package org.columba.ristretto.imap; 37 38 import java.util.LinkedList ; 39 import java.util.List ; 40 import java.util.regex.Matcher ; 41 import java.util.regex.Pattern ; 42 43 import org.columba.ristretto.io.CharSequenceSource; 44 import org.columba.ristretto.io.Source; 45 46 51 public class IMAPResponse { 52 53 private static final Pattern literalPattern = Pattern.compile("^\\{(\\d+)\\}$"); 54 private static final Matcher literalMatcher = literalPattern.matcher(""); 55 56 protected List literals; 57 58 protected int preNumber; 59 60 protected String tag; 61 62 protected String source; 63 64 protected String responseMessage; 65 66 protected int responseType; 67 68 protected String responseSubType; 69 70 protected ResponseTextCode responseTextCode; 71 72 75 public static final int RESPONSE_STATUS = 0; 76 77 80 public static final int RESPONSE_MAILBOX_DATA = 1; 81 82 85 public static final int RESPONSE_MESSAGE_DATA = 2; 86 87 90 public static final int RESPONSE_CONTINUATION = 3; 91 92 97 public IMAPResponse(String source) { 98 this.source = source; 99 100 literals = new LinkedList (); 101 preNumber = -1; 102 } 103 104 112 public String getTag() { 113 return tag; 114 } 115 116 117 118 123 public String getSource() { 124 StringBuffer cleanedup = new StringBuffer (source.length()); 125 literalMatcher.reset(source); 126 127 while (literalMatcher.find()) { 128 literalMatcher.appendReplacement(cleanedup, getData(literalMatcher.group()).toString()); 129 } 130 literalMatcher.appendTail(cleanedup); 131 132 return cleanedup.toString(); 133 } 134 135 136 137 142 public boolean isTagged() { 143 return tag != null; 144 } 145 146 152 public boolean isOK() { 153 return responseSubType.equals("OK"); 154 } 155 156 162 public boolean isNO() { 163 return responseSubType.equals("NO"); 164 } 165 166 172 public boolean isBYE() { 173 return responseSubType.equals("BYE"); 174 } 175 176 182 public boolean isBAD() { 183 return responseSubType.equals("BAD"); 184 } 185 186 191 public void setTag(String string) { 192 tag = string; 193 } 194 195 210 public String getResponseMessage() { 211 return responseMessage; 212 } 213 214 221 public void setResponseMessage(String responseText) { 222 this.responseMessage = responseText; 223 } 224 225 236 public int getResponseType() { 237 return responseType; 238 } 239 240 247 public void setResponseType(int responseType) { 248 this.responseType = responseType; 249 } 250 251 258 public String getResponseSubType() { 259 return responseSubType; 260 } 261 262 269 public void setResponseSubType(String responseSubType) { 270 this.responseSubType = responseSubType; 271 } 272 273 282 public ResponseTextCode getResponseTextCode() { 283 return responseTextCode; 284 } 285 286 293 public void setResponseTextCode(ResponseTextCode responseTextCode) { 294 this.responseTextCode = responseTextCode; 295 } 296 297 300 public void appendResponseText(String restresponse) { 301 source += restresponse; 302 responseMessage += restresponse; 303 } 304 305 311 public Source getLiteral(int index) { 312 return (Source) literals.get(index); 313 } 314 315 320 public void addLiteral(Source literal) { 321 literals.add(literal); 322 } 323 324 333 public int getPreNumber() { 334 return preNumber; 335 } 336 337 342 public void setPreNumber(int preNumber) { 343 this.preNumber = preNumber; 344 } 345 346 355 public Source getData(CharSequence data) { 356 if( data.length() == 0 ) return new CharSequenceSource(data); 357 literalMatcher.reset(data); 358 if( literalMatcher.matches() ) { 359 return getLiteral(Integer.parseInt(literalMatcher.group(1))); 360 } else { 361 if( data.charAt(0) == '"') { 363 return new CharSequenceSource( data.subSequence(1,data.length()-1) ); 364 } else { 365 return new CharSequenceSource( data ); 366 } 367 368 } 369 } 370 371 } 372 | Popular Tags |