/* -*- 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: CMLImporter.cpp,v 1.3 2003/06/19 17:53:08 rjesse Exp $
 */

#include "CMLImporter.h"
#include "CMLImportHandler.h"
#include <qfileinfo.h>

/*!
  Constructs an empty importer object for CookML documents.
  \sa apply()
*/
CMLImporter::CMLImporter()
   : Importer(), QXmlSimpleReader()
{
}

RecipeModel&
CMLImporter::apply()
   throw(Error)
{
    QXmlInputSource* source = new QXmlInputSource();
    if (m_type == FILE) {
        try {
            file2String( QTextStream::UnicodeUTF8);
        }
        catch (Error& e) {
            throw e;
        }
    }
    source->setData( m_rawStringL.join( "\n"));

    CMLImportHandler handler( &m_cookbook);
    setContentHandler( &handler);
    if (!parse( source, false)) {
        switch (m_type) {
        case FILE: {
            QString s( tr( "Could not find any recipes in %1.") .arg( m_filename));
            throw Error( s);
            break;
        }
        case STRING: {
            QString s( tr( "Could not find any recipes in the given string."));
            throw Error( s);
            break;
        }
        default:
            throw Error( tr( "Undefined parse error during CookML import."));
            break;
        }
    }
    delete source;

    if (m_cookbook.getName().isEmpty() && m_type == FILE) {
       QFileInfo fi( m_filename);
       m_cookbook.setName( fi.baseName( false));
    }
    
    return m_cookbook;
}

/*!
  Unused as the whole document is passed at once by the CMLImportHandler.
*/
void
CMLImporter::readCookbookHeader() // protected
{
}

/*!
  Unused as the whole document is passed at once by the CMLImportHandler.
*/
void
CMLImporter::readAllRecipes() // protected
{
}

/*!
  Unused as the whole document is passed at once by the CMLImportHandler.
*/
void
CMLImporter::readRecipe()	// protected
{
} // readRecipe()

/*!
  Unused as the whole document is passed at once by the CMLImportHandler.
*/
void
CMLImporter::readRecipeHeader() // protected
{
}

/*!
  Unused as the whole document is passed at once by the CMLImportHandler.
*/
void
CMLImporter::readRecipeBody() // protected
{
}
