ffead.server.doc
ApplicationUtil.cpp
1 /*
2  Copyright 2009-2012, Sumeet Chhetri
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 /*
17  * ApplicationUtil.cpp
18  *
19  * Created on: Dec 30, 2009
20  * Author: sumeet
21  */
22 
23 #include "ApplicationUtil.h"
24 
25 ApplicationUtil::ApplicationUtil()
26 {
27 }
28 
29 ApplicationUtil::~ApplicationUtil() {
30  // TODO Auto-generated destructor stub
31 }
32 
33 string ApplicationUtil::buildAllApplications(vector<string> files,vector<string> apps,map<string,string> &appMap)
34 {
35  string headers = "#include \"fstream\"\n#include \"string\"\n#include \"HttpSession.h\"\n#include \"CastUtil.h\"\nusing namespace std;\n";
36  string code;
37  string meth,methdef,vars;
38  for (unsigned int var = 0; var < files.size(); ++var)
39  {
40  string path = files.at(var).substr(0,files.at(var).find_last_of("/")+1);
41  string appName = apps.at(var).substr(0,apps.at(var).find_last_of("/"));
42 
43  XmlParser parser("Parser");
44  Element root = parser.getDocument(files.at(var)).getRootElement();
45  ElementList eles = root.getChildElements();
46  if(eles.size()==0)
47  appMap[appName] = "false";
48  else
49  {
50  meth += "string "+appName+"checkRules(string to,HttpSession session)\n{\nstring curr=session.getAttribute(\"CURR\");\n";
51  for (unsigned int var = 0; var < eles.size(); var++)
52  {
53  if(eles.at(var).getTagName()=="include")
54  {
55  string inc = eles.at(var).getText();
56  strVec incs;
57  StringUtil::split(incs, inc, (" "));
58  for (unsigned int i = 0; i < incs.size(); i++)
59  {
60  headers += ("#include \"" + incs.at(i) + "\"\n");
61  }
62  }
63  else if(eles.at(var).getTagName()=="page" || eles.at(var).getTagName()=="welcome")
64  {
65  meth += ("string " + eles.at(var).getAttribute("id") + " = \""+path+eles.at(var).getAttribute("path")+"\";\n");
66  if(eles.at(var).getAttribute("who")!="" && eles.at(var).getAttribute("where")!="")
67  {
68  if(eles.at(var).getAttribute("where").find("FILE:")!=string::npos)
69  {
70  string fileName = eles.at(var).getAttribute("where");
71  StringUtil::replaceFirst(fileName,"FILE:","");
72  fileName = (path + fileName);
73  meth += "string path;\nif(to=="+eles.at(var).getAttribute("id")+")\n{";
74  meth += "string user = session.getAttribute(\"USER\");\n";
75  meth += "string pass = session.getAttribute(\"PASS\");\n";
76  meth += "ifstream f(\""+fileName+"\");\n";
77  meth += "if(f.is_open())\n{\nstring temp;\nwhile(getline(f,temp,'\\n'))\n{";
78  meth += "if(temp==(user+\" \"+pass)){f.close();path = "+eles.at(var).getAttribute("id")+";break;}\n}}\nif(path==\"\")\npath=\"FAILED\";\n}";
79  }
80  }
81  else if(eles.at(var).getAttribute("onsuccess")!="" && eles.at(var).getAttribute("onfail")!="")
82  {
83 
84  }
85  }
86  else if(eles.at(var).getTagName()=="rule")
87  {
88  meth += "\nif(to=="+eles.at(var).getAttribute("topage")+" && curr=="+eles.at(var).getAttribute("currpage")+")\n{";
89  ElementList elesc = eles.at(var).getChildElements();
90  for (unsigned int var1 = 0; var1 < elesc.size(); var1++)
91  {
92  if(elesc.at(var1).getTagName()=="execute")
93  {
94  string call = elesc.at(var1).getAttribute("call");
95  string clas = call.substr(0,call.find("."));
96  string method = call.substr(call.find(".")+1);
97  headers += ("#include \""+clas+".h\"\n");
98  meth += clas + " _object;\n";
99  string args;
100  ElementList elesce = elesc.at(var1).getChildElements();
101  for (unsigned int var2 = 0; var2 < elesce.size(); var2++)
102  {
103  meth += (elesce.at(var2).getTagName() + " _" + CastUtil::lexical_cast<string>(var2+1) + " = CastUtil::lexical_cast<"+elesce.at(var2).getTagName()+">(\""+elesce.at(var2).getText()+"\");\n");
104  args += ("_"+CastUtil::lexical_cast<string>(var2+1));
105  if(var2!=elesce.size()-1)
106  args += ",";
107  }
108  if(elesc.at(var1).getAttribute("rettype")!="" || elesc.at(var1).getAttribute("rettype")!="void")
109  {
110  meth += (elesc.at(var1).getAttribute("rettype") + " " + elesc.at(var1).getAttribute("retname") + " = ");
111  meth += "_object."+method+"("+args+");\n";
112  }
113  }
114  }
115  meth += "path=to;\n}\n";
116  }
117  }
118  meth += "return path;}\n";
119  code += methdef;
120  }
121  }
122  code += (headers + "extern \"C\"\n{\n"+vars+meth+"}\n");
123  return code;
124 
125 }