1 36 package org.columba.ristretto.imap.parser; 37 38 import java.util.regex.Matcher ; 39 import java.util.regex.Pattern ; 40 41 import org.columba.ristretto.imap.IMAPResponse; 42 import org.columba.ristretto.imap.MailboxStatus; 43 import org.columba.ristretto.parser.ParserException; 44 45 51 public class MailboxStatusParser { 52 53 private static final Pattern statusPattern = Pattern.compile("([^\\s]+)\\s\\(([^\\)]*)\\)"); 54 private static final Pattern attributePattern = Pattern.compile("\\s?([^\\s]+)\\s(\\d+)"); 55 56 63 public static final MailboxStatus parse(IMAPResponse response) throws ParserException { 64 MailboxStatus result = new MailboxStatus(); 65 Matcher statusMatcher = statusPattern.matcher(response.getResponseMessage()); 66 67 if( statusMatcher.find()) { 68 result.setName(removeQuotes(statusMatcher.group(1))); 69 Matcher attributeMatcher = attributePattern.matcher(statusMatcher.group(2)); 70 while( attributeMatcher.find()) { 71 if( attributeMatcher.group(1).equals("MESSAGES")) { 72 result.setMessages( Integer.parseInt(attributeMatcher.group(2))); 73 } else if( attributeMatcher.group(1).equals("RECENT")) { 74 result.setRecent( Integer.parseInt(attributeMatcher.group(2))); 75 } else if( attributeMatcher.group(1).equals("UNSEEN")) { 76 result.setUnseen( Integer.parseInt(attributeMatcher.group(2))); 77 } else if( attributeMatcher.group(1).equals("UIDVALIDITY")) { 78 result.setUidValidity( Long.parseLong(attributeMatcher.group(2))); 79 } else if( attributeMatcher.group(1).equals("UIDNEXT")) { 80 result.setUidNext( Long.parseLong(attributeMatcher.group(2))); 81 } 82 } 83 84 return result; 85 } 86 throw new ParserException(response.getResponseMessage()); 87 } 88 89 private static String removeQuotes(String string) { 90 String result = string; 91 if( result.startsWith("\"")) { 92 result = result.substring(1); 93 } 94 if( result.endsWith("\"")) { 95 result = result.substring(0,result.length()-1); 96 } 97 98 return result; 99 } 100 101 102 103 } 104 | Popular Tags |