00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
00071 int version = getVersion();
00072
00073
00074 ERRORMACRO( version >= 13, Error, ,
00075 i18n( "Update for database versions 0.1 to 0.12 is not "
00076 "supported." ) );
00077
00078
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
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 }