KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > caramel > util > UrlQueryTokenizer


1 /*
2 ** Caramel - Non-GUI Java Addons
3 ** Copyright (c) 2001, 2002, 2003 by Gerald Bauer
4 **
5 ** This program is free software.
6 **
7 ** You may redistribute it and/or modify it under the terms of the GNU
8 ** Lesser General Public License as published by the Free Software Foundation.
9 ** Version 2.1 of the license should be included with this distribution in
10 ** the file LICENSE, as well as License.html. If the license is not
11 ** included with this distribution, you may find a copy at the FSF web
12 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14 **
15 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19 ** REDISTRIBUTION OF THIS SOFTWARE.
20 **
21 */

22
23 package caramel.util;
24
25 import java.net.*;
26 import houston.*;
27
28 public class UrlQueryTokenizer
29 {
30    static Logger T = Logger.getLogger( UrlQueryTokenizer.class );
31    private StringBuffer JavaDoc _key;
32    private int _pos;
33
34    private String JavaDoc _query;
35    private StringBuffer JavaDoc _value;
36
37    public UrlQueryTokenizer( String JavaDoc query )
38    {
39       _query = query;
40       _pos = 0;
41    }
42
43    public String JavaDoc getKey()
44    {
45       return URLDecoder.decode( _key.toString() );
46    }
47
48    public String JavaDoc getValue()
49    {
50       return URLDecoder.decode( _value.toString() );
51    }
52
53    public boolean advance()
54    {
55       // is more data available?
56
if( _pos >= _query.length() )
57          return false;
58       // no more arguments available
59

60       // returns true if there are more arguments availabe
61

62       _key = new StringBuffer JavaDoc();
63       _value = new StringBuffer JavaDoc();
64
65       // get key/name
66
while( _pos < _query.length() )
67       {
68          char ch = _query.charAt( _pos++ );
69          if( ch == '=' )
70             break;
71          else
72             _key.append( ch );
73       }
74
75       // get value
76
while( _pos < _query.length() )
77       {
78          char ch = _query.charAt( _pos++ );
79          if( ch == '&' )
80             break;
81          else
82             _value.append( ch );
83       }
84
85       T.debug( "key=" + getKey() );
86       T.debug( "value=" + getValue() );
87
88       return true;
89    }
90 }
91
Popular Tags