00001 #include "multicontext.h"
00002
00003 MultiContext::MultiContext(RuleContext* isuperContext, const int icontrolVariableId,
00004 const string icontrolVariableName)
00005 : RuleContext(isuperContext->rules, isuperContext->nameSpace),
00006 superContext(isuperContext), usedNames(0),
00007 namePrefix(isuperContext->generateVariableName()),
00008 controlVariableName(icontrolVariableName.substr(1)),
00009 controlVariableId(icontrolVariableId){
00010 };
00011
00012 MultiContext::MultiContext(RuleContext* isuperContext, const int icontrolVariableId)
00013 : RuleContext(isuperContext->rules, isuperContext->nameSpace),
00014 superContext(isuperContext), usedNames(0),
00015 namePrefix(isuperContext->generateVariableName()),
00016 controlVariableName(generateVariableName()),
00017 controlVariableId(icontrolVariableId){
00018
00019 };
00020
00021 const string MultiContext::getVariableNameSufix(const bool stripControlVariableDollar){
00022 stringstream str;
00023 str << superContext->getVariableNameSufix(stripControlVariableDollar) << "[";
00024 if (!stripControlVariableDollar) str << "$";
00025 str << controlVariableName << "]";
00026 return str.str();
00027 };
00028
00029 const string MultiContext::getVariableNamePrefix(){
00030 stringstream str;
00031 str << namePrefix << "_context";
00032 return str.str();
00033 };
00034
00035
00036 void MultiContext::generateVariableNames(const Node::VariableList &vlist, Node::Assignment &phpAssign, Node::Assignment &tplAssign){
00037
00038 Node::VariableList::const_iterator it = vlist.begin();
00039 Node::VariableList::const_iterator eit = vlist.end();
00040 while (it!=eit){
00041 if (*it == 0){
00042 phpAssign.insert(pair<int,const Node*>(0,new Constant(namePrefix + superContext->getVariableNameSufix(false))));
00043 tplAssign.insert(pair<int,const Node*>(0,new Constant(namePrefix + superContext->getVariableNameSufix(false))));
00044 } else if (*it == controlVariableId) {
00045 phpAssign.insert(pair<int,const Node*>(*it, new Constant("$" + controlVariableName)));
00046 tplAssign.insert(pair<int,const Node*>(*it, new Constant(controlVariableName)));
00047 } else {
00048 string newVariableName = generateVariableName();
00049 phpAssign.insert(pair<int,const Node*>(*it,new Constant(newVariableName + getVariableNameSufix(false))));
00050 tplAssign.insert(pair<int,const Node*>(*it,new Constant(newVariableName + getVariableNameSufix(false))));
00051 }
00052 it++;
00053 };
00054 };
00055
00056 void MultiContext::enterContext(List *phpOut, List *tplOut) {
00057 stringstream str;
00058
00059
00060 registerSmartyVariable(namePrefix);
00061 };
00062
00063
00064 const string MultiContext::generateVariableName(){
00065 usedNames++;
00066 stringstream str;
00067 str << getVariableNamePrefix() << usedNames;
00068 registerSmartyVariable(str.str());
00069 return str.str();
00070 };
00071
00072 void MultiContext::registerSmartyVariable(const string &variable){
00073 superContext->registerSmartyVariable(variable);
00074 };
00075
00076 void MultiContext::processSmartyAssign(SmartyAssign *smartyAssign){
00077 stringstream str;
00078 str << "$" << smartyAssign->getVariable() << "="
00079 << smartyAssign->getAssignment() << ";" << endl;
00080 smartyAssign->setPHPOut(str.str());
00081 };
00082
00083 void MultiContext::processTemplate(Node* tpl) const {
00084 Node::Assignment newAssign;
00085 newAssign[controlVariableId] = new Constant(controlVariableName);
00086 substituteNode(tpl,newAssign);
00087
00088 };