ffead.server.doc
AOPEngine.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  * AOPEngine.cpp
18  *
19  * Created on: Oct 17, 2009
20  * Author: sumeet
21  */
22 
23 #include "AOPEngine.h"
24 
25 AOPEngine::AOPEngine() {
26  // TODO Auto-generated constructor stub
27 
28 }
29 
30 AOPEngine::~AOPEngine() {
31  // TODO Auto-generated destructor stub
32 }
33 
34 void AOPEngine::execute(string fileName)
35 {
36  string data;
37  string include;
38  ifstream infile;
39  ofstream ofile;
40  infile.open(fileName.c_str());
41  Aspect aspect;
42  if(infile)
43  {
44  while(getline(infile, data))
45  {
46  if(data!="")
47  {
48  if(data.find("@INCLUDE")!=string::npos)
49  {
50  StringUtil::eraseAll(data,"@INCLUDE(\"");
51  StringUtil::eraseAll(data,"\")");
52  args tr;
53  StringUtil::split(tr, data, (";"));
54  for(unsigned int i=0;i<tr.size();i++)
55  {
56  include += ("#include \"" + tr.at(i) + "\"\n");
57  }
58  }
59  else if(data.find("@ASPECTBEGIN")!=string::npos)
60  {
61  aspect.clear();
62  }
63  else if(data.find("@EXECUTION")!=string::npos)
64  {
65  StringUtil::eraseAll(data,"@EXECUTION(\"");
66  StringUtil::eraseAll(data,"\")");
67  args tr;
68  StringUtil::split(tr, data, (" "));
69  for(unsigned int i=0;i<tr.size();i++)
70  {
71  if(i==0)
72  aspect.setClassName(tr.at(i));
73  else
74  aspect.setMethodName(tr.at(i));
75  }
76  }
77  else if(data.find("@STRICT")!=string::npos)
78  {
79  StringUtil::eraseAll(data,"@STRICT(\"");
80  StringUtil::eraseAll(data,"\")");
81  aspect.setStrict(data);
82  }
83  else if(data.find("@ARGS")!=string::npos)
84  {
85  StringUtil::eraseAll(data,"@ARGS(\"");
86  StringUtil::eraseAll(data,"\")");
87  aspect.setArguTypes(data);
88  args tr;
89  StringUtil::split(tr, data, (";"));
90  aspect.setArgumentTypes(tr);
91  }
92  else if(data.find("@RETURN")!=string::npos)
93  {
94  StringUtil::eraseAll(data,"@RETURN(\"");
95  StringUtil::eraseAll(data,"\")");
96  aspect.setReturnType(data);
97  }
98  else if(data.find("@WHEN")!=string::npos)
99  {
100  StringUtil::eraseAll(data,"@WHEN(\"");
101  StringUtil::eraseAll(data,"\")");
102  aspect.setWhen(data);
103  }
104  else if(data.find("@CODE")!=string::npos)
105  {
106  StringUtil::eraseAll(data,"@CODE(");
107  StringUtil::eraseAll(data,")");
108  aspect.setCode(data);
109  }
110  else if(data.find("@ASPECTEND")!=string::npos)
111  {
112  this->aspects.push_back(aspect);
113  }
114  }
115  }
116  infile.close();
117  }
118  if(this->aspects.size()>0)
119  {
120  for(unsigned int i=0;i<this->aspects.size();i++)
121  {
122  aspect.clear();
123  aspect = this->aspects.at(i);
124  string classN = "/home/sumeet/workspace/default/"+aspect.getClassName()+".cpp";
125  infile.open(classN.c_str());
126  string content;
127  if(infile)
128  {
129  while(getline(infile, data))
130  {
131  content += (data + "\n");
132  }
133  string signature;
134  if(aspect.getReturnType()=="*")
135  {
136  if(aspect.getStrict()=="true")
137  signature += (" "+aspect.getClassName()+"::"+aspect.getMethodName()+"(");
138  else
139  signature += (aspect.getMethodName()+"(");
140  }
141  else
142  {
143  if(aspect.getStrict()=="true")
144  signature += (aspect.getReturnType()+" "+aspect.getClassName()+"::"+aspect.getMethodName()+"(");
145  else
146  signature += (aspect.getReturnType()+" "+aspect.getMethodName()+"(");
147  }
148  string prior,later,temp,argus,arginit,argsig;
149  unsigned int f = content.find(signature);
150  if(f!=string::npos)
151  {
152  prior = content.substr(0,f);
153  later = content.substr(f);
154  unsigned s = later.find_first_of("(")+1;
155  unsigned e = later.find_first_of(")");
156  argus = later.substr(s,e-s);
157  args tr,tc;
158  StringUtil::split(tr, argus, (","));
159  for(unsigned int i=0;i<tr.size();i++)
160  {
161  StringUtil::split(tc, tr.at(i), (" "));
162  stringstream ss;
163  ss << (i+1);
164  string te;
165  ss >> te;
166  arginit += (tc.at(0) + " _" + te + " = " + tc.at(1)) + ";\n";
167  argsig += (tc.at(0));
168  if(i!=tr.size()-1)
169  argsig += ",";
170  }
171  if(aspect.getArguTypes()==argsig)
172  {
173  unsigned g = later.find_first_of("{");
174  prior = content.substr(0,f+g+1);
175  later = content.substr(f+g+1);
176  unsigned h = later.find_first_of("}");
177  temp = later.substr(0,h);
178  later = later.substr(h);
179  if(aspect.getWhen()=="BEFORE")
180  {
181  temp = ("\n" + arginit + aspect.getCode() + "\n" + temp);
182  }
183  else
184  {
185  temp = (temp + "\n" + arginit + aspect.getCode() + "\n");
186  }
187  content = (include + prior + temp + later);
188  string aop_name = "/home/sumeet/Desktop/_AOP_"+aspect.getClassName()+".cpp";
189  ofile.open(aop_name.c_str());
190  ofile.write(content.c_str(),content.length());
191  ofile.close();
192  }
193  }
194  infile.close();
195  }
196  }
197  }
198 }