ffead.server.doc
TemplateEngine.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  * TemplateEngine.cpp
18  *
19  * Created on: Sep 5, 2009
20  * Author: sumeet
21  */
22 
23 #include "TemplateEngine.h"
24 
25 TemplateEngine::TemplateEngine() {
26  // TODO Auto-generated constructor stub
27 
28 }
29 
30 TemplateEngine::~TemplateEngine() {
31  // TODO Auto-generated destructor stub
32 }
33 
34 /*string TemplateEngine::evaluate(string str,Context cntxt)
35 {
36  string ret(str);
37  Context::iterator itr;
38  for(itr = cntxt.begin();itr!=cntxt.end();itr++)
39  {
40  string rep;
41  rep = "{$"+itr->first+"}";
42  StringUtil::replaceAll(ret,rep,itr->second);
43  }
44  return ret;
45 }*/
46 
47 string TemplateEngine::evaluate(string fileName,StringContext cntxt)
48 {
49  string ret,data;
50  ifstream infile;
51  infile.open(fileName.c_str());
52  if(infile.is_open())
53  {
54  string file;
55  int s,en;
56  s = fileName.find_last_of("/")+1;
57  en = fileName.find_last_of(".");
58  file = fileName.substr(s,en-s);
59  while(getline(infile, data))
60  {
61  if(data.find("<import>")!=string::npos && data.find("</import>")!=string::npos)
62  {
63  int s = data.find("<import>")+8;
64  int e = data.find("</import>");
65  data=data.substr(s,e-s);
66  ifstream inf(data.c_str());
67  if(inf)
68  {
69  while(getline(inf, data))
70  {
71  ret.append(data+"\n");
72  }
73  }
74  }
75  ret.append(data+"\n");
76  }
77  }
78  else
79  ret = fileName;
80  StringContext::iterator itr;
81  for(itr = cntxt.begin();itr!=cntxt.end();itr++)
82  {
83  string rep;
84  rep = "${"+itr->first+"}";
85  StringUtil::replaceAll(ret,rep,itr->second);
86  }
87  return ret;
88 }