001 /* 002 * Copyright 2010 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package com.hs.mail.imap.message.builder; 017 018 import java.util.ArrayList; 019 import java.util.LinkedList; 020 import java.util.List; 021 022 import javax.mail.Flags; 023 024 import com.hs.mail.imap.message.SequenceRange; 025 import com.hs.mail.imap.message.request.ImapRequest; 026 import com.hs.mail.imap.parser.Token; 027 import com.hs.mail.imap.server.codec.DecoderUtils; 028 import com.hs.mail.imap.server.codec.ImapMessage; 029 030 /** 031 * 032 * @author Won Chul Doh 033 * @since Jan 28, 2010 034 * 035 */ 036 public abstract class ImapRequestBuilder { 037 038 public abstract ImapRequest createRequest(String tag, String command, 039 ImapMessage message); 040 041 protected SequenceRange[] parseSequenceSet(LinkedList<Token> tokens) { 042 Token token = tokens.remove(); 043 List<SequenceRange> rangeList = new ArrayList<SequenceRange>(); 044 do { 045 rangeList.add(DecoderUtils.parseSeqRange(token.value)); 046 token = tokens.peek(); 047 if (token != null && ",".equals(token.value)) { 048 tokens.remove(); // remove a comma 049 token = tokens.remove(); 050 } else { 051 break; 052 } 053 } while (true); 054 return (SequenceRange[]) rangeList.toArray(new SequenceRange[rangeList 055 .size()]); 056 } 057 058 protected Flags parseFlagList(LinkedList<Token> tokens) { 059 Flags flags = null; 060 Token token = tokens.peek(); 061 if (token.type == Token.Type.LPAREN) { 062 tokens.remove(); // remove left parenthesis 063 flags = new Flags(); 064 do { 065 if ((token = tokens.remove()).type == Token.Type.RPAREN) 066 break; 067 DecoderUtils.decodeFlagList(token.value, flags); 068 } while (true); 069 } 070 return flags; 071 } 072 073 }