KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > imapserver > commands > SetArgument


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.imapserver.commands;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23
24 final class SetArgument implements ImapArgument
25 {
26     public Object JavaDoc parse( StringTokenizer JavaDoc tokens ) throws Exception JavaDoc
27     {
28         if ( !tokens.hasMoreTokens() ) {
29             throw new Exception JavaDoc( "Missing argument " + format() );
30         }
31
32         return tokens.nextToken();
33     }
34
35     private List JavaDoc parseSet( String JavaDoc rawSet )
36     {
37         List JavaDoc response = new ArrayList JavaDoc();
38
39         int checkComma = rawSet.indexOf( "," );
40         if ( checkComma == -1 ) {
41             // No comma present
42
int checkColon = rawSet.indexOf( ":" );
43             if ( checkColon == -1 ) {
44                 // No colon present (single integer)
45
Integer JavaDoc seqNum;
46                 if ( rawSet.equals( "*" ) ) {
47                     seqNum = new Integer JavaDoc( -1 );
48                 }
49                 else {
50                     seqNum = new Integer JavaDoc( rawSet.trim() );
51                     if ( seqNum.intValue() < 1 ) {
52                         throw new IllegalArgumentException JavaDoc( "Not a positive integer" );
53                     }
54                 }
55                 response.add( seqNum );
56             }
57             else {
58                 // Simple sequence
59

60                 // Add the first number in the range.
61
Integer JavaDoc firstNum = new Integer JavaDoc( rawSet.substring( 0, checkColon ) );
62                 int first = firstNum.intValue();
63                 if ( first < 1 ) {
64                     throw new IllegalArgumentException JavaDoc( "Not a positive integer" );
65                 }
66                 response.add( firstNum );
67
68                 Integer JavaDoc lastNum;
69                 int last;
70                 if ( rawSet.indexOf( "*" ) != -1 ) {
71                     // Range from firstNum to '*'
72
// Add -1, to indicate unended range.
73
lastNum = new Integer JavaDoc( -1 );
74                 }
75                 else {
76                     // Get the final num, and add all numbers in range.
77
lastNum = new Integer JavaDoc( rawSet.substring( checkColon + 1 ) );
78                     last = lastNum.intValue();
79                     if ( last < 1 ) {
80                         throw new IllegalArgumentException JavaDoc( "Not a positive integer" );
81                     }
82                     if ( last < first ) {
83                         throw new IllegalArgumentException JavaDoc( "Not an increasing range" );
84                     }
85
86                     for ( int i = (first + 1); i <= last; i++ ) {
87                         response.add( new Integer JavaDoc( i ) );
88                     }
89                 }
90             }
91
92         }
93         else {
94             // Comma present, compound range.
95
try {
96                 String JavaDoc firstRawSet = rawSet.substring( 0, checkComma );
97                 String JavaDoc secondRawSet = rawSet.substring( checkComma + 1 );
98                 response.addAll( parseSet( firstRawSet ) );
99                 response.addAll( parseSet( secondRawSet ) );
100             }
101             catch ( IllegalArgumentException JavaDoc e ) {
102                 throw e;
103             }
104         }
105         return response;
106
107     }
108
109     public String JavaDoc format()
110     {
111         return "<set>";
112     }
113
114 }
115
Popular Tags