/* -*- indent-tabs-mode: nil; -*-
 *
 * This file is part of the Mango recipe management distribution.
 * Copyright (c) 2000-2003, Roland Jesse
 * All rights reserved.
 *
 * For license and copyright information see the file COPYRIGHT or 
 * <http://www.opensource.org/licenses/bsd-license.html>.
 *
 * $Id: RecipeMLImportHandler.cpp,v 1.3 2003/05/11 16:13:30 rjesse Exp $
 */

#include "RecipeMLImportHandler.h"
#include "RecipeModel.h"

/*!
  Constructs an instance of the class.
*/
RecipeMLImportHandler::
RecipeMLImportHandler( RecipeModel* rm)
   : m_model( rm)
{
}

/*!
  This function is invoked whenever there is an element to be parsed.
  \param qName is the qualified name of the current element to parse.
  \param atts are the attributes of the element. If there are no attributes, it is empty.
*/
bool RecipeMLImportHandler::
startElement( const QString&, const QString&, const QString& qName, const QXmlAttributes& atts)
{
   m_curElement.truncate( 0);
   if ("recipeml" == qName) {
      QString name( atts.value( "name"));
      if (!name.isEmpty()) m_model->setName( name);
   } // recipeml
   else if ("recipe" == qName) m_curRecipe.clear();
   else if ("meta" == qName) {  // recipe : meta
      QString s( atts.value( "DC.Date"));
      if (!s.isEmpty()) m_curRecipe.setCreationDate( QDate::fromString( s));
      s = atts.value( "DC.Creator");
      m_curRecipe.setTypist( s);
   } // recipe : meta
   else if (("title" == qName) // recipe : head : (title,source,yield,preptime,cat)
            || ("source" == qName)
            || ("yield" == qName)
            || ("preptime" == qName)
            || ("cat" == qName))
   {
      m_curElement = qName;
   } // recipe : head : title
   else if ("ing" == qName) m_curIngredient.clear();
   else if (("qty" == qName)
            || ("unit" == qName)
            || ("item" == qName))
   {
      m_curElement = qName;
   }
   else if ("step" == qName)    // recipe : directions : step
      m_curElement = qName;
   
   return true;
}

/*!
  This function is called whenever a closing element was found.
  \param
*/
bool RecipeMLImportHandler::
endElement( const QString&, const QString&, const QString& qName)
{
   if ("title" == qName)        // recipe : head : title
      m_curRecipe.setTitle( m_data[qName]);
   else if ("source" == qName)  // recipe : head : source
      m_curRecipe.setSource( m_data[qName]);
   else if ("yield" == qName) // recipe : head : yield
      m_curRecipe.setYieldQty( m_data[qName].toInt());
   else if ("preptime" == qName) // recipe : head : preptime
      m_curRecipe.setPreparationTime( "preptime", QTime::fromString( m_data[qName]));
   else if ("cat" == qName) // recipe : head : categories : cat
      m_curRecipe.addCategory( m_data[qName]);
   else if ("recipe" == qName)  // recipe
      m_model->add( m_curRecipe);
   else if ("qty" == qName)  // recipe : ingredients : ing : amt : qty
      m_curIngredient.setAmountQty( m_data[qName].toFloat());
   else if (("unit" == qName) // recipe : ingredients : ing : amt : unit
            && (m_data.contains( qName))) //  just ignore empty unit elements
   {
      QString s( m_data[qName]);
      Measurement m( m_data[qName]);
      m_curIngredient.setMeasurement( m);
   }
   else if ("item" == qName)    // recipe : ingredients : ing : item
      m_curIngredient.setName( m_data[qName]);
   else if ("ing" == qName)     // recipe : ingredients : ing
      m_curRecipe.addIngredient( m_curIngredient);
   else if ("step" == qName)    // recipe : directions : step
      m_curRecipe.add2Directions( m_data[qName]);
   
   if (m_data.contains( qName))
      m_data.remove( qName);
   return true;
}

/*!
  Reads the data of the current element and stores it in m_data.
*/
bool RecipeMLImportHandler::
characters( const QString& ch)
{
   QString s;
   if ("step" == ch) s = ch;
   else s = ch.stripWhiteSpace();
   m_data[m_curElement] += s;
   return true;
}
