00001 #ifndef _PHPFILES_H_ 00002 #define _PHPFILES_H_ 00003 00004 #include "rulesystem.h" 00005 #include "htmlbox.h" 00006 #include "transformerset.h" 00007 #include "treeparser.h" 00008 #include <fstream> 00009 00011 static string phpName2tplName(const string phpName){ 00012 string x = phpName; 00013 return x.replace(phpName.length()-3,phpName.length(),"tpl"); 00014 }; 00015 00017 class PHPFiles{ 00018 private: 00019 00021 class PHPFileInfo{ 00022 public: 00024 const string phpName; 00026 StatementList *phpOut; 00028 StatementList *tplOut; 00030 PHPFileInfo(const string iphpName) : phpName(iphpName), 00031 phpOut(NULL), tplOut(NULL) {}; 00032 }; 00033 00035 RuleSystem factor; 00036 00038 string inDir; 00040 string outDir; 00041 00043 typedef list<PHPFileInfo> PHPFileInfoList; 00045 typedef PHPFileInfoList::iterator iterator; 00047 typedef PHPFileInfoList::const_iterator const_iterator; 00048 00050 PHPFileInfoList phpFileInfos; 00051 00053 HTMLBox htmlBox; 00054 00056 int yydebug; 00057 00059 void phpHeader(StatementList *phpOut) const; 00060 00062 void phpFooter(StatementList *phpOut, const string tplName) const; 00063 00065 void executeRuleSystemAtTime(){ 00066 iterator it = phpFileInfos.begin(); 00067 iterator eit = phpFileInfos.end(); 00068 while (it!=eit){ 00069 StatementList *phpIn = it->phpOut; 00070 it->phpOut = new StatementList(); 00071 cout << "Executing rules at time " << factor.getRuleRunTime() << " on " << it->phpName << endl; 00072 factor.execute(it->phpName, phpIn, it->phpOut, it->tplOut); 00073 it++; 00074 }; 00075 }; 00076 00078 void executeRuleSystem(){ 00079 do { 00080 executeRuleSystemAtTime(); 00081 } while (factor.stepTime()); 00082 }; 00083 00085 void loadIn(){ 00086 iterator it = phpFileInfos.begin(); 00087 iterator eit = phpFileInfos.end(); 00088 while (it!=eit){ 00089 it->tplOut = new StatementList(); 00090 cout << "Reading tree of: " << inDir + it->phpName << endl; 00091 it->phpOut = phpParser->parse(inDir + it->phpName, yydebug); 00092 cout << "Normalizing" << endl; 00093 phpPreNormalizers.apply(it->phpOut); 00094 it++; 00095 }; 00096 }; 00097 00099 void saveOut(){ 00100 iterator it = phpFileInfos.begin(); 00101 iterator eit = phpFileInfos.end(); 00102 while (it!=eit){ 00103 phpHeader(it->phpOut); 00104 phpFooter(it->phpOut,phpName2tplName(it->phpName)); 00105 00106 phpPostNormalizers.apply(it->phpOut); 00107 tplPostNormalizers.apply(it->tplOut); 00108 00109 ofstream phpOutOfs((outDir+it->phpName).c_str()); 00110 phpOutOfs << it->phpOut; 00111 ofstream tplOutOfs((outDir+phpName2tplName(it->phpName)).c_str()); 00112 tplOutOfs << it->tplOut; 00113 it++; 00114 }; 00115 }; 00116 00118 void processHTML(){ 00119 iterator it = phpFileInfos.begin(); 00120 iterator eit = phpFileInfos.end(); 00121 while (it!=eit){ 00122 it->tplOut->transform(&htmlBox.putter); 00123 it++; 00124 }; 00125 00126 it = phpFileInfos.begin(); 00127 eit = phpFileInfos.end(); 00128 while (it!=eit){ 00129 it->tplOut->transform(&htmlBox.replacer); 00130 it++; 00131 }; 00132 }; 00133 00134 00135 public: 00136 00138 00141 PHPFiles(istream &phpConfig, int iyydebug = 0) : yydebug(iyydebug) { 00142 phpConfig >> inDir; 00143 phpConfig >> outDir; 00144 00145 htmlBox.setOutDir(outDir); 00146 00147 while (!phpConfig.eof()){ 00148 string phpName; 00149 phpConfig >> phpName; 00150 if (phpName != "") phpFileInfos.push_back(PHPFileInfo(phpName)); 00151 }; 00152 }; 00153 00155 void execute(){ 00156 cout << "Stage 1: reading files" << endl; 00157 loadIn(); 00158 cout << "Stage 2: executing rule system" << endl; 00159 executeRuleSystem(); 00160 cout << "Stage 3: processing HTML" << endl; 00161 processHTML(); 00162 cout << "Stage 4: saving results" << endl; 00163 saveOut(); 00164 cout << "DONE!!!" << endl; 00165 }; 00166 00168 ~PHPFiles(){ 00169 iterator it = phpFileInfos.begin(); 00170 iterator eit = phpFileInfos.end(); 00171 while (it!=eit){ 00172 delete it->phpOut; 00173 delete it->tplOut; 00174 it++; 00175 }; 00176 }; 00177 00178 }; 00179 00180 #endif