/*
** Caramel - Core Java Toolbox
** Copyright (c) 2001, 2002, 2003 by Gerald Bauer
**
** This program is free software.
**
** You may redistribute it and/or modify it under the terms of the GNU
** General Public License as published by the Free Software Foundation.
** Version 2 of the license should be included with this distribution in
** the file LICENSE, as well as License.html. If the license is not
** included with this distribution, you may find a copy at the FSF web
** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE.
**
*/

package caramel.util;

import java.net.*;
import houston.*;

public class UrlQueryTokenizer
{
   static Logger T = Logger.getLogger( UrlQueryTokenizer.class );
   private StringBuffer _key;
   private int _pos;

   private String _query;
   private StringBuffer _value;

   public UrlQueryTokenizer( String query )
   {
      _query = query;
      _pos = 0;
   }

   public String getKey()
   {
      return URLDecoder.decode( _key.toString() );
   }

   public String getValue()
   {
      return URLDecoder.decode( _value.toString() );
   }

   public boolean advance()
   {
      // is more data available?
      if( _pos >= _query.length() )
         return false;
      // no more arguments available

      // returns true if there are more arguments availabe

      _key = new StringBuffer();
      _value = new StringBuffer();

      // get key/name
      while( _pos < _query.length() )
      {
         char ch = _query.charAt( _pos++ );
         if( ch == '=' )
            break;
         else
            _key.append( ch );
      }

      // get value
      while( _pos < _query.length() )
      {
         char ch = _query.charAt( _pos++ );
         if( ch == '&' )
            break;
         else
            _value.append( ch );
      }

      T.debug( "key=" + getKey() );
      T.debug( "value=" + getValue() );

      return true;
   }
}