Main | License | Installation | FAQ | Screenshots | Contact | Links | Sf.net | Freshmeat.net | KDE-Apps.org
Requirements | Design | Modules | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

cookBook.cpp

Go to the documentation of this file.
00001 /* Copyright (C) 2002, 2003, 2004, 2005 Jan Wedekind.
00002    This file is part of the recipe database application AnyMeal.
00003 
00004    AnyMeal is free software; you can redistribute it and/or modify it under
00005    the terms of the GNU GENERAL PUBLIC LICENSE as published by the Free
00006    Software Foundation; either version 3 of the License, or (at your option)
00007    any later version.
00008 
00009    AnyMeal is distributed in the hope that it will be useful, but WITHOUT ANY
00010    WARRANTY; without even the implied warranty of MERCHANTIBILITY or FITNESS
00011    FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
00012    details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
00016 #include "include.hpp"
00017 #include <fstream>
00018 #include <klocale.h>
00019 #include <sstream>
00020 #include "chainedCompiler.hpp"
00021 #include "cookBook.hpp"
00022 #include "cookBookDeprecated.hpp"
00023 #include "utils.hpp"
00024 #include "xmlDocument.hpp"
00025 #include "xsu.hpp"
00026 
00027 using namespace std;
00028 
00029 CookBook::CookBook( DatabasePtr _database,
00030                     const string &inputXSLFileName,
00031                     const string &outputXSLFileName,
00032                     bool performVersionCheck ) throw (Error):
00033   database(_database),
00034   xmlLayer( createXMLLayer( _database, inputXSLFileName, outputXSLFileName ) )
00035 {
00036   if ( performVersionCheck ) {
00037     int version = getVersion();
00038     ERRORMACRO( version >= 13, Error, ,
00039                 i18n( "Update for database versions 0.1 to 0.12 is not "
00040                       "supported." ) );
00041     ERRORMACRO( version <= DATABASEVERSION, Error, ,
00042                 i18n( "Recipe database is of version %1. This software can "
00043                       "only handle databases up to version %2. You need to "
00044                       "update AnyMeal to access this database." ).
00045                 arg( versionText( version ) ).
00046                 arg( versionText( DATABASEVERSION ) ) );
00047     ERRORMACRO( version == DATABASEVERSION, CookBookDeprecated, ( version ),
00048                 i18n( "Recipe database appears to be of "
00049                       "version %1. Client requires database "
00050                       "to be of version %2." ).
00051                 arg( versionText( version ) ).
00052                 arg( versionText( DATABASEVERSION ) ) );
00053   };
00054 }
00055 
00056 int CookBook::getVersion(void) const throw (Error)
00057 {
00058   XMLDocument xmlDocument( "" );
00059   xmlDocument.fromString
00060     ( xmlLayer->translate
00061       ( "<?xml version='1.0' encoding='UTF-8'?><version/>" ) );
00062   int
00063     version = atoi( xmlDocument.getDocumentElement().selectNode( "version" ).
00064                     getNodeText().c_str() );
00065   return version;
00066 }
00067 
00068 void CookBook::update(void) throw (Error)
00069 {
00070   // Read version number.
00071   int version = getVersion();
00072   
00073   // Check for minimum version requirement.
00074   ERRORMACRO( version >= 13, Error, ,
00075               i18n( "Update for database versions 0.1 to 0.12 is not "
00076                     "supported." ) );
00077   
00078   // Check against maximum version constraint.
00079   ERRORMACRO( version <= DATABASEVERSION, Error, ,
00080               i18n( "Recipe database is of version %1. This software can "
00081                     "only handle databases up to version %2. You need to "
00082                     "update AnyMeal to access this database." ).
00083               arg( versionText( version ) ).
00084               arg( versionText( DATABASEVERSION ) ) );
00085 
00086   // Update database.
00087   stringstream query;
00088   query << "<?xml version='1.0' encoding='UTF-8'?>" << endl
00089         << "<update><version>" << version << "</version></update>"
00090         << endl;
00091   ofstream nullDevice( "/dev/null" );
00092   xmlLayer->translate( query, nullDevice );
00093 }
00094 
00095 CompilerPtr CookBook::createXMLLayer( DatabasePtr _database,
00096                                       const string &inputXSLFileName,
00097                                       const string &outputXSLFileName )
00098 {
00099 
00100   XSLCompilerPtr
00101     sqlIn( new XSLCompiler( inputXSLFileName,
00102                             FormatterListener::OUTPUT_METHOD_TEXT,
00103                             SQLENCODING, "" ) ),
00104     sqlOut( new XSLCompiler( outputXSLFileName,
00105                              FormatterListener::OUTPUT_METHOD_XML,
00106                              "UTF-8", "" ) );
00107   CompilerPtr xmlLayer
00108     ( new ChainedCompiler( sqlIn,
00109                            CompilerPtr( new ChainedCompiler
00110                                         ( CompilerPtr
00111                                           ( new XSU( _database ) ),
00112                                           sqlOut ) ) ) );
00113   return xmlLayer;
00114 }


anymeal 0.30 - recipe management software - Make the most of your food! - © Jan Wedekind Sun Sep 14 16:01:26 2008 - GNU Free Documentation License