1 36 package org.columba.ristretto.imap.parser; 37 38 import java.util.ArrayList ; 39 import java.util.List ; 40 import java.util.regex.Matcher ; 41 import java.util.regex.Pattern ; 42 43 import org.columba.ristretto.imap.IMAPResponse; 44 import org.columba.ristretto.imap.Namespace; 45 import org.columba.ristretto.imap.NamespaceCollection; 46 import org.columba.ristretto.parser.ParserException; 47 48 53 public class NamespaceParser { 54 55 private static final Pattern namespacePattern = Pattern.compile("\\(" + "\"([^\"]*)\"" + "\\s" + "\"([^\"]*)\"" + "(\\s\"([^\"]*)\"" + "\\s\\(([^\\)]*)\\))?" + "\\)"); 66 private static final Pattern quotedPattern = Pattern 67 .compile("\"([^\"]*)\"\\s?"); 69 76 public static final NamespaceCollection parse(IMAPResponse response) 77 throws ParserException { 78 return parse(response.getResponseMessage()); 79 } 80 81 88 public static final NamespaceCollection parse(String input) 89 throws ParserException { 90 NamespaceCollection result = new NamespaceCollection(); 91 92 String [] parts = tokenizeParts(input); 95 96 for (int i = 0; i < 3; i++) { 98 String part = parts[i]; 99 100 if (part.equalsIgnoreCase("nil")) { 102 result.addNamespace(i, new Namespace(null, null)); 103 } else { 104 Matcher namespaceMatcher = namespacePattern.matcher(part); 106 107 while (namespaceMatcher.find()) { 108 Namespace ns = new Namespace(namespaceMatcher.group(1), 109 namespaceMatcher.group(2)); 110 111 if (namespaceMatcher.group(4) != null) { 113 ns.setExtensionName(namespaceMatcher.group(4)); 114 115 List parameterList = new ArrayList (); 117 Matcher parameterMatcher = quotedPattern 118 .matcher(namespaceMatcher.group(5)); 119 while (parameterMatcher.find()) { 120 parameterList.add(parameterMatcher.group(1)); 121 } 122 123 ns.setExtensionParameter((String []) parameterList 125 .toArray(new String [0])); 126 } 127 128 result.addNamespace(i, ns); 129 } 130 } 131 } 132 133 return result; 134 } 135 136 private static String [] tokenizeParts(String input) throws ParserException { 137 String [] result = new String [3]; 138 int start = 0; 139 int end = -1; 140 141 for (int i = 0; i < 3; i++) { 142 if (input.charAt(start) == '(') { 143 end = ParenthesisParser.getClosingPos(input, start); 144 145 result[i] = input.substring(start + 1, end); 147 } else { 148 end = start + 2; 149 result[i] = "NIL"; 150 } 151 start = end + 1; 152 153 if( input.length() > start && input.charAt(start) == ' ') { 155 start ++; 156 } 157 } 158 159 return result; 160 } 161 162 } | Popular Tags |