KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.james.util.Assert;
21
22 import java.util.StringTokenizer JavaDoc;
23
24 class AstringArgument implements ImapArgument
25 {
26     private String JavaDoc name;
27     private boolean isFinal;
28
29     public AstringArgument( String JavaDoc name )
30     {
31         this.name = name;
32     }
33
34     public Object JavaDoc parse( StringTokenizer JavaDoc tokens )
35             throws Exception JavaDoc
36     {
37         // TODO: do this properly - need to check for disallowed characters.
38

39         if ( ! tokens.hasMoreTokens() ) {
40             throw new Exception JavaDoc( "Missing argument <" + this.name + ">");
41         }
42         String JavaDoc token = tokens.nextToken();
43         Assert.isTrue( token.length() > 0 );
44
45         StringBuffer JavaDoc astring = new StringBuffer JavaDoc( token );
46
47         if ( astring.charAt(0) == '\"' ) {
48             while ( astring.length() == 1 ||
49                     astring.charAt( astring.length() - 1 ) != '\"' ) {
50                 if ( tokens.hasMoreTokens() ) {
51                     astring.append( " " );
52                     astring.append( tokens.nextToken() );
53                 }
54                 else {
55                     throw new Exception JavaDoc( "Missing closing quote for <" + this.name + ">" );
56                 }
57             }
58             astring.deleteCharAt( 0 );
59             astring.deleteCharAt( astring.length() - 1 );
60         }
61
62         return astring.toString();
63     }
64
65     public String JavaDoc format()
66     {
67         return "<" + this.name + ">";
68     }
69
70 }
71
Popular Tags