KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > imap > parser > FlagsParser


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package org.columba.ristretto.imap.parser;
37
38 import java.util.List JavaDoc;
39 import java.util.Vector JavaDoc;
40 import java.util.regex.Matcher JavaDoc;
41 import java.util.regex.Pattern JavaDoc;
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 /**
49  * @author fdietz
50  *
51  * See RFC 2060 IMAP4 (http://rfc-editor.org)
52  *
53  * fetch list of message flags
54  *
55  * example:
56  *
57  * C: A999 UID FETCH 4827313:4828442 FLAGS
58  * S: * 23 FETCH (FLAGS (\Seen) UID 4827313)
59  * S: * 24 FETCH (FLAGS (\Seen) UID 4827943)
60  * S: * 25 FETCH (FLAGS (\Seen) UID 4828442)
61  * S: A999 UID FETCH completed
62  *
63  */

64
65
66 //7.2.6. FLAGS Response
67
//
68
// Contents: flag parenthesized list
69
//
70
// The FLAGS response occurs as a result of a SELECT or EXAMINE
71
// command. The flag parenthesized list identifies the flags (at a
72
// minimum, the system-defined flags) that are applicable for this
73
// mailbox. Flags other than the system flags can also exist,
74
// depending on server implementation.
75
//
76
// The update from the FLAGS response MUST be recorded by the client.
77
//
78
// Example: S: * FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
79
//
80
public class FlagsParser {
81
82
83     private static final Pattern JavaDoc flagsPattern = Pattern.compile("(\\\\|$)?(Answered)|(Flagged)|(Deleted)|(Seen)|(Draft)|(Recent)|(Junk) ?", Pattern.CASE_INSENSITIVE);
84     
85
86     /**
87      * Parse the Flags of the IMAP response.
88      *
89      * @param response
90      * @return the Flags.
91      */

92     public static IMAPFlags parse(IMAPResponse response) {
93         IMAPFlags result = new IMAPFlags();
94
95         result.setIndex(response.getPreNumber());
96         
97         // first parse the message attributes
98
Attributes attributes = MessageAttributeParser.parse( response.getResponseMessage() );
99         
100         // parse the flags
101
if( attributes.get("FLAGS") != null ) {
102             Matcher JavaDoc matcher = flagsPattern.matcher((String JavaDoc)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         // add uid
123
if( attributes.get("UID") != null ) {
124             result.setUid(new Integer JavaDoc((String JavaDoc)attributes.get("UID")));
125         }
126         
127         return result;
128     }
129
130     /**
131      * Parse the Flags of the IMAP repsonses.
132      *
133      * @param responses
134      * @return the Flags
135      */

136     public static Flags[] parseFlags(IMAPResponse[] responses) {
137         List JavaDoc v = new Vector JavaDoc();
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                 // consumes this line
146
responses[i] = null;
147             }
148         }
149
150         Flags[] flags = new Flags[v.size()];
151         ((Vector JavaDoc)v).copyInto(flags);
152
153         return flags;
154     }
155
156     protected static IMAPFlags parseFlagsLine(String JavaDoc str) {
157         IMAPFlags flags = new IMAPFlags();
158
159         if (str.indexOf("Seen") != -1) {
160             //System.out.println("seen is true ");
161
flags.setSeen(true);
162         }
163         if (str.indexOf("Answered") != -1) {
164             //System.out.println("answered is true ");
165
flags.setAnswered(true);
166         }
167         if (str.indexOf("Flagged") != -1) {
168             //System.out.println("flagged is true ");
169
flags.setFlagged(true);
170         }
171         if (str.indexOf("Deleted") != -1) {
172             //System.out.println("deleted is true ");
173
flags.setDeleted(true);
174         }
175
176         if (str.indexOf("Recent") != -1) {
177             //System.out.println("deleted is true ");
178
flags.setRecent(true);
179         }
180
181         return flags;
182     }
183
184     protected static String JavaDoc parseUidsLine(String JavaDoc data) {
185
186       // Find the start of the UID portion. Look for UID... as the UID
187
// portion isn't guarenteed to be at the beginning of the line.
188
int leftIndex = data.indexOf("UID ") + 4;
189       int rightIndex = data.indexOf(" ", leftIndex);
190
191       if(rightIndex == -1){
192         // No rightIndex, therefore you went to the end of line, so just
193
// return from the left index to the end of the line
194
return data.substring(leftIndex);
195       }else{
196         // Return the sub string you found
197
return data.substring(leftIndex, rightIndex);
198       }
199     }
200
201 }
202
Popular Tags