00001 #include "rtreecontextrule.h"
00002 #include "streevariable.h"
00003
00004 ContextRule::ContextRule(List* iphpInPattern, List* itplOutPattern, List* iphpOutPattern) :
00005 phpInPattern(iphpInPattern), tplOutPattern(itplOutPattern),phpOutPattern(iphpOutPattern) {}
00006
00007 ContextRule::~ContextRule(){
00008 delete phpInPattern;
00009 delete phpOutPattern;
00010 delete tplOutPattern;
00011 };
00012
00013 Rule* ContextRule::substitute(const Node::Assignment &assign){
00014 phpInPattern->substitute(assign);
00015 tplOutPattern->substitute(assign);
00016 phpOutPattern->substitute(assign);
00017 return this;
00018 };
00019
00020
00021 bool ContextRule::execute(RuleContext* context, List* phpIn, List* phpOut, List* tplOut, Node::Assignment& assign)const {
00022 if (phpInPattern->matchToBegining(phpIn,assign, context->getNameSpace())){
00023 RuleContext* subcontext = generateSubcontext(context,assign);
00024
00025 StatementList *tplOutMatched = new StatementList();
00026
00027 bool tplOutIsEmpty = true;
00028
00029 List::iterator it = tplOutPattern->begin();
00030 List::iterator eit = tplOutPattern->end();
00031 while (it!=eit){
00032 if ((*it)->getName()==CVariable){
00033 Node* phpContextMatched = (*it)->copy();
00034 substituteNode(phpContextMatched,assign);
00035
00036 StatementList* phpContextOut = new StatementList();
00037
00038 unsigned int tplOutMatchedLenght = tplOutMatched->size();
00039 subcontext->execute(phpContextMatched,phpContextOut,tplOutMatched);
00040 if (tplOutMatchedLenght != tplOutMatched->size())
00041 tplOutIsEmpty = false;
00042
00043 assign[dynamic_cast<Variable*>(*it)->getId()] = phpContextOut;
00044
00045 }else if ((*it)->getName()==CTemplate){
00046 Node* tplMatched = (*it)->copy();
00047 substituteNode(tplMatched,assign);
00048 subcontext->processTemplate(tplMatched);
00049 tplOutMatched->push_back(tplMatched);
00050 }else throw "ERROR: ContextRule::execute - Wrong element in template list";
00051 it++;
00052 }
00053
00054 StatementList *phpOutMatched = dynamic_cast<StatementList*>(phpOutPattern->copy());
00055 phpOutMatched->substitute(assign);
00056
00057 for (int i=0 ; i<phpInPattern->getLength() ; i++){
00058 delete phpIn->front();
00059 phpIn->pop_front();
00060 };
00061
00062 subcontext->assignVariableNames(phpOutMatched,tplOutMatched);
00063
00064 delete subcontext;
00065
00066
00067 context->assignVariableNames(phpOutMatched,tplOutMatched);
00068
00069 phpOut->append(phpOutMatched);
00070 if (!tplOutIsEmpty)
00071 tplOut->append(tplOutMatched);
00072 return true;
00073 }
00074 return false;
00075 };
00076