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.ListInfo; 43 import org.columba.ristretto.imap.MailboxNameUTF7Converter; 44 import org.columba.ristretto.parser.ParserException; 45 46 52 public class ListInfoParser { 53 54 private static final Pattern listResponsePattern = 56 Pattern.compile("^\\(([^)]*)\\) " + "((\"([^\"]+)\")|(NIL)) " + "\"?([^\"]*)\"?$"); 60 private static final Pattern parameterPattern = 61 Pattern.compile( 62 "((\\\\Noinferiors)|(\\\\Noselect)|(\\\\Marked)|(\\\\UnMarked)) ?", 63 Pattern.CASE_INSENSITIVE); 64 65 72 public static ListInfo parse(IMAPResponse response) 73 throws ParserException { 74 Matcher matcher = 75 listResponsePattern.matcher(response.getResponseMessage()); 76 if (matcher.matches()) { 77 String parameterString = matcher.group(1); 78 int parameters = 0; 79 if (parameterString != null) { 80 parameters = parseParameters(parameterString); 81 } 82 String delimiter = matcher.group(4); String name = MailboxNameUTF7Converter.decode(matcher.group(6)); 84 85 name = response.getData(name).toString(); 86 87 ListInfo result = new ListInfo(name, delimiter, parameters); 88 89 return result; 90 } 91 92 throw new ParserException( 93 "Invalid List/Lsub response : " + response.getSource()); 94 } 95 96 private static int parseParameters(String parameterString) { 97 Matcher matcher = parameterPattern.matcher(parameterString); 98 int result = 0; 99 100 while (matcher.find()) { 101 if (matcher.group(2) != null) { 103 result |= ListInfo.NOINFERIORS; 104 continue; 105 } 106 if (matcher.group(3) != null) { 108 result |= ListInfo.NOSELECT; 109 continue; 110 } 111 if (matcher.group(4) != null) { 113 result |= ListInfo.MARKED; 114 continue; 115 } 116 if (matcher.group(5) != null) { 118 result |= ListInfo.UNMARKED; 119 continue; 120 } 121 } 122 123 return result; 124 } 125 126 } 127 | Popular Tags |