1 17 18 package org.apache.james.imapserver.commands; 19 20 import java.util.ArrayList ; 21 import java.util.List ; 22 import java.util.StringTokenizer ; 23 24 final class SetArgument implements ImapArgument 25 { 26 public Object parse( StringTokenizer tokens ) throws Exception 27 { 28 if ( !tokens.hasMoreTokens() ) { 29 throw new Exception ( "Missing argument " + format() ); 30 } 31 32 return tokens.nextToken(); 33 } 34 35 private List parseSet( String rawSet ) 36 { 37 List response = new ArrayList (); 38 39 int checkComma = rawSet.indexOf( "," ); 40 if ( checkComma == -1 ) { 41 int checkColon = rawSet.indexOf( ":" ); 43 if ( checkColon == -1 ) { 44 Integer seqNum; 46 if ( rawSet.equals( "*" ) ) { 47 seqNum = new Integer ( -1 ); 48 } 49 else { 50 seqNum = new Integer ( rawSet.trim() ); 51 if ( seqNum.intValue() < 1 ) { 52 throw new IllegalArgumentException ( "Not a positive integer" ); 53 } 54 } 55 response.add( seqNum ); 56 } 57 else { 58 60 Integer firstNum = new Integer ( rawSet.substring( 0, checkColon ) ); 62 int first = firstNum.intValue(); 63 if ( first < 1 ) { 64 throw new IllegalArgumentException ( "Not a positive integer" ); 65 } 66 response.add( firstNum ); 67 68 Integer lastNum; 69 int last; 70 if ( rawSet.indexOf( "*" ) != -1 ) { 71 lastNum = new Integer ( -1 ); 74 } 75 else { 76 lastNum = new Integer ( rawSet.substring( checkColon + 1 ) ); 78 last = lastNum.intValue(); 79 if ( last < 1 ) { 80 throw new IllegalArgumentException ( "Not a positive integer" ); 81 } 82 if ( last < first ) { 83 throw new IllegalArgumentException ( "Not an increasing range" ); 84 } 85 86 for ( int i = (first + 1); i <= last; i++ ) { 87 response.add( new Integer ( i ) ); 88 } 89 } 90 } 91 92 } 93 else { 94 try { 96 String firstRawSet = rawSet.substring( 0, checkComma ); 97 String secondRawSet = rawSet.substring( checkComma + 1 ); 98 response.addAll( parseSet( firstRawSet ) ); 99 response.addAll( parseSet( secondRawSet ) ); 100 } 101 catch ( IllegalArgumentException e ) { 102 throw e; 103 } 104 } 105 return response; 106 107 } 108 109 public String format() 110 { 111 return "<set>"; 112 } 113 114 } 115 | Popular Tags |