// -*- c++ -*-

/*
 * Jakelib2 - General purpose C++ library
 * Copyright (C) 2001 Florian Wolff  (florian@donuz.de)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: SimpleDateFormat.h,v 1.3 2003/09/20 12:38:02 florian Exp $
 */

#ifndef INCLUDE_JAKELIB2_TEXT_SIMPLEDATEFORMAT_H
# define INCLUDE_JAKELIB2_TEXT_SIMPLEDATEFORMAT_H

#include "DateFormat.h"
#include "DateFormatSymbols.h"

namespace jakelib {
    namespace util {
      class ArrayList;
      class Date;
      class Locale;
    }
    
    namespace text  {
	class DateFormat;
	
	/**
	 * SimpleDateFormat is a sample implementation of the abstract DateFormat class.
	 *
	 * @class SimpleDateFormat
	 * @version $Id: SimpleDateFormat.h,v 1.3 2003/09/20 12:38:02 florian Exp $
	 * @author Heiko Feldker (heikofkr@cs.tu-berlin.de)
	 */
	
	class SimpleDateFormat : public jakelib::text::DateFormat
	{
	public:
	    /**
	     * Initialization (construction) of a new SimpleDateFormat object. 
	     */
	    SimpleDateFormat();

	    SimpleDateFormat(jakelib::lang::String * pattern);
	    SimpleDateFormat(jakelib::lang::String * pattern, jakelib::text::DateFormatSymbols * syms);
	    SimpleDateFormat(jakelib::lang::String * pattern, jakelib::util::Locale * locale);
	    
	    /**
	     * This method formats an \c Object into a \c StringBuffer.
	     * Implements the abstract format method in DateFormat.
	     * All created text is appended to the given StringBuffer.
             * For more information about the FieldPosition,
	     * please refer to the original Java-API doc.
	     *
	     * @param date The \c Date to format. 
	     * @param sbuf The \c StringBuffer to append the formatted date to. 
	     * @param fp The \c FieldPosition to use. 
	     *
	     * @return The formatted \c StringBuffer.
	     */
	    virtual jakelib::lang::StringBuffer* format(jakelib::util::Date *date,
                                                        jakelib::lang::StringBuffer *sbuf, 
							jakelib::text::FieldPosition *fp);

	    virtual jakelib::lang::Object * parseObject(jakelib::lang::String * str,
                                                        jakelib::text::ParsePosition * pos);
	
          /**
           * This method formats a \c Date into a \c String.
           * 
           * @param date The \c Date to format.
           *
           * @return The formatted \c String.
           */
          jakelib::lang::String* format(jakelib::util::Date *date);
          
	private:
	    jakelib::lang::String* pattern;
	    jakelib::util::Locale* locale;
	    jakelib::text::DateFormatSymbols * syms;
	    jakelib::util::ArrayList* compiledPattern;

	    void compilePattern();
	    jint SimpleDateFormat::countEqualChars(jakelib::lang::String *str,
                                                   jint startIndex,
                                                   jchar myChar);
	    
	    /** inner class representing parsed tokens */
	    class ParseToken : public jakelib::lang::Object 
	    {
	    public :
		typedef enum {
		    STRING,
		    ERA,
		    YEAR,
		    MONTH_IN_YEAR,
		    WEEK_IN_YEAR,
		    WEEK_IN_MONTH,
		    DAY_IN_YEAR,
		    DAY_IN_MONTH,
		    // Nummer des Tages der Woche. ;)
		    DAY_OF_WEEK_IN_MONTH,
		    DAY_IN_WEEK,
		    AM_PM_MARKER,
		    HOUR_0_23,
		    HOUR_1_24,
		    HOUR_0_11,
		    HOUR_1_12,
		    MINUTE,
		    SECOND,
		    MILLISECOND,
		    TIMEZONE,
		    TIMEZONE_OFFSET
		} PatternElement;
		    
		PatternElement element;
		int length;
		jakelib::lang::String * strVal;

		ParseToken(PatternElement pe, int length, jakelib::lang::String * str = null);
	    };
	
	    
	    JAKELIB_DECLARE_CLASS(SimpleDateFormat);
	};
	
	JAKELIB_DECLARE_ARRAY(SimpleDateFormat);
    }
}
#endif
    
