1 36 package org.columba.ristretto.imap.parser; 37 38 import java.util.List ; 39 import java.util.Vector ; 40 import java.util.regex.Matcher ; 41 import java.util.regex.Pattern ; 42 43 import org.columba.ristretto.imap.IMAPFlags; 44 import org.columba.ristretto.imap.IMAPResponse; 45 import org.columba.ristretto.message.Attributes; 46 import org.columba.ristretto.message.Flags; 47 48 64 65 66 public class FlagsParser { 81 82 83 private static final Pattern flagsPattern = Pattern.compile("(\\\\|$)?(Answered)|(Flagged)|(Deleted)|(Seen)|(Draft)|(Recent)|(Junk) ?", Pattern.CASE_INSENSITIVE); 84 85 86 92 public static IMAPFlags parse(IMAPResponse response) { 93 IMAPFlags result = new IMAPFlags(); 94 95 result.setIndex(response.getPreNumber()); 96 97 Attributes attributes = MessageAttributeParser.parse( response.getResponseMessage() ); 99 100 if( attributes.get("FLAGS") != null ) { 102 Matcher matcher = flagsPattern.matcher((String )attributes.get("FLAGS")); 103 while( matcher.find()) { 104 if( matcher.group(1) != null ) { 105 result.set(Flags.ANSWERED); 106 } else if( matcher.group(3) != null ) { 107 result.set(Flags.FLAGGED); 108 } else if( matcher.group(4) != null ) { 109 result.set(Flags.DELETED); 110 } else if( matcher.group(5) != null ) { 111 result.set(Flags.SEEN); 112 } else if( matcher.group(6) != null ) { 113 result.set(Flags.DRAFT); 114 } else if( matcher.group(7) != null ) { 115 result.set(Flags.RECENT); 116 } else if( matcher.group(8) != null ) { 117 result.set(IMAPFlags.JUNK); 118 } 119 } 120 } 121 122 if( attributes.get("UID") != null ) { 124 result.setUid(new Integer ((String )attributes.get("UID"))); 125 } 126 127 return result; 128 } 129 130 136 public static Flags[] parseFlags(IMAPResponse[] responses) { 137 List v = new Vector (); 138 139 for (int i = 0; i < responses.length - 1; i++) { 140 if (responses[i] == null) 141 continue; 142 143 if( responses[i].getResponseSubType().equals("FETCH")) { 144 v.add( parse(responses[i])); 145 responses[i] = null; 147 } 148 } 149 150 Flags[] flags = new Flags[v.size()]; 151 ((Vector )v).copyInto(flags); 152 153 return flags; 154 } 155 156 protected static IMAPFlags parseFlagsLine(String str) { 157 IMAPFlags flags = new IMAPFlags(); 158 159 if (str.indexOf("Seen") != -1) { 160 flags.setSeen(true); 162 } 163 if (str.indexOf("Answered") != -1) { 164 flags.setAnswered(true); 166 } 167 if (str.indexOf("Flagged") != -1) { 168 flags.setFlagged(true); 170 } 171 if (str.indexOf("Deleted") != -1) { 172 flags.setDeleted(true); 174 } 175 176 if (str.indexOf("Recent") != -1) { 177 flags.setRecent(true); 179 } 180 181 return flags; 182 } 183 184 protected static String parseUidsLine(String data) { 185 186 int leftIndex = data.indexOf("UID ") + 4; 189 int rightIndex = data.indexOf(" ", leftIndex); 190 191 if(rightIndex == -1){ 192 return data.substring(leftIndex); 195 }else{ 196 return data.substring(leftIndex, rightIndex); 198 } 199 } 200 201 } 202 | Popular Tags |