2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
24 * Simple template replacement engine.
28 #include "io/GenericRequest.h"
29 #include "util/TemplateEngine.h"
31 #include <boost/algorithm/string/trim.hpp>
33 using namespace xmltooling;
38 static const pair<const string,string> emptyPair;
41 TemplateEngine::TemplateEngine()
46 TemplateEngine::~TemplateEngine()
50 TemplateEngine::TemplateParameters::TemplateParameters() : m_request(nullptr)
54 TemplateEngine::TemplateParameters::~TemplateParameters()
58 const char* TemplateEngine::TemplateParameters::getParameter(const char* name) const
60 map<string,string>::const_iterator i=m_map.find(name);
61 return (i!=m_map.end() ? i->second.c_str() : (m_request ? m_request->getParameter(name) : nullptr));
64 const multimap<string,string>* TemplateEngine::TemplateParameters::getLoopCollection(const char* name) const
66 map< string,multimap<string,string> >::const_iterator i=m_collectionMap.find(name);
67 return (i!=m_collectionMap.end() ? &(i->second) : nullptr);
70 void TemplateEngine::setTagPrefix(const char* tagPrefix)
72 keytag = string("<") + tagPrefix + " ";
73 iftag = string("<") + tagPrefix + "if ";
74 ifnottag = string("<") + tagPrefix + "ifnot ";
75 ifendtag = string("</") + tagPrefix + "if>";
76 ifnotendtag = string("</") + tagPrefix + "ifnot>";
77 fortag = string("<") + tagPrefix + "for ";
78 forendtag = string("</") + tagPrefix + "for>";
81 string TemplateEngine::unsafe_chars = "#%&():[]\\`{}";
83 void TemplateEngine::html_encode(ostream& os, const char* start) const
85 while (start && *start) {
87 case '<': os << "<"; break;
88 case '>': os << ">"; break;
89 case '"': os << """; break;
90 case '&': os << "&"; break;
91 case '\'': os << "'"; break;
94 if (unsafe_chars.find_first_of(*start) != string::npos)
95 os << "&#" << static_cast<short>(*start) << ';';
100 case '#': os << "#"; break;
101 case '%': os << "%"; break;
102 case '(': os << "("; break;
103 case ')': os << ")"; break;
104 case ':': os << ":"; break;
105 case '[': os << "["; break;
106 case '\\': os << "\"; break;
107 case ']': os << "]"; break;
108 case '`': os << "`"; break;
109 case '{': os << "{"; break;
110 case '}': os << "}"; break;
111 default: os << *start;
118 void TemplateEngine::process(
121 const char*& lastpos,
123 const TemplateParameters& parameters,
124 const pair<const string,string>& loopentry,
125 const XMLToolingException* e
128 const char* line = buf.c_str();
131 while ((thispos = strchr(lastpos, '<')) != nullptr) {
132 // Output the string up to this token.
134 os << buf.substr(lastpos-line, thispos-lastpos);
136 // Make sure this token matches our tokens.
137 #ifdef HAVE_STRCASECMP
138 if (visible && !strncasecmp(thispos, keytag.c_str(), keytag.length()))
140 if (visible && !_strnicmp(thispos, keytag.c_str(), keytag.length()))
143 // Save this position off.
144 lastpos = thispos + keytag.length();
146 // search for the end-tag
147 if ((thispos = strstr(lastpos, "/>")) != nullptr) {
148 string key = buf.substr(lastpos-line, thispos-lastpos);
151 if (key == "$name" && !loopentry.first.empty())
152 html_encode(os,loopentry.first.c_str());
153 else if (key == "$value" && !loopentry.second.empty())
154 html_encode(os,loopentry.second.c_str());
156 const char* p = parameters.getParameter(key.c_str());
158 p = e->getProperty(key.c_str());
162 lastpos = thispos + 2; // strlen("/>")
165 #ifdef HAVE_STRCASECMP
166 else if (!strncasecmp(thispos, iftag.c_str(), iftag.length()))
168 else if (!_strnicmp(thispos, iftag.c_str(), iftag.length()))
171 // Save this position off.
172 lastpos = thispos + iftag.length();
174 // search for the end of this tag
175 if ((thispos = strchr(lastpos, '>')) != nullptr) {
176 string key = buf.substr(lastpos-line, thispos-lastpos);
180 cond = parameters.getParameter(key.c_str()) || (e && e->getProperty(key.c_str()));
181 lastpos = thispos + 1; // strlen(">")
182 process(cond, buf, lastpos, os, parameters, loopentry, e);
185 #ifdef HAVE_STRCASECMP
186 else if (!strncasecmp(thispos, ifendtag.c_str(), ifendtag.length()))
188 else if (!_strnicmp(thispos, ifendtag.c_str(), ifendtag.length()))
191 // Save this position off and pop the stack.
192 lastpos = thispos + ifendtag.length();
195 #ifdef HAVE_STRCASECMP
196 else if (!strncasecmp(thispos, ifnottag.c_str(), ifnottag.length()))
198 else if (!_strnicmp(thispos, ifnottag.c_str(), ifnottag.length()))
201 // Save this position off.
202 lastpos = thispos + ifnottag.length();
204 // search for the end of this tag
205 if ((thispos = strchr(lastpos, '>')) != nullptr) {
206 string key = buf.substr(lastpos-line, thispos-lastpos);
210 cond = !(parameters.getParameter(key.c_str()) || (e && e->getProperty(key.c_str())));
211 lastpos = thispos + 1; // strlen(">")
212 process(cond, buf, lastpos, os, parameters, loopentry, e);
215 #ifdef HAVE_STRCASECMP
216 else if (!strncasecmp(thispos, ifnotendtag.c_str(), ifnotendtag.length()))
218 else if (!_strnicmp(thispos, ifnotendtag.c_str(), ifnotendtag.length()))
221 // Save this position off and pop the stack.
222 lastpos = thispos + ifnotendtag.length();
226 #ifdef HAVE_STRCASECMP
227 else if (!strncasecmp(thispos, fortag.c_str(), fortag.length()))
229 else if (!_strnicmp(thispos, fortag.c_str(), fortag.length()))
232 // Save this position off.
233 lastpos = thispos + iftag.length();
237 // search for the end of this tag
238 if ((thispos = strchr(lastpos, '>')) != nullptr) {
239 key = buf.substr(lastpos-line, thispos-lastpos);
241 lastpos = thispos + 1; // strlen(">")
244 const multimap<string,string>* forParams = parameters.getLoopCollection(key.c_str());
245 if (!forParams || forParams->size() == 0) {
246 process(false, buf, lastpos, os, parameters, emptyPair, e);
249 const char* savlastpos = lastpos;
250 for (multimap<string,string>::const_iterator i=forParams->begin(); i!=forParams->end(); ++i) {
251 lastpos = savlastpos;
252 process(cond, buf, lastpos, os, parameters, *i, e);
257 #ifdef HAVE_STRCASECMP
258 else if (!strncasecmp(thispos, forendtag.c_str(), forendtag.length()))
260 else if (!_strnicmp(thispos, forendtag.c_str(), forendtag.length()))
263 // Save this position off and pop the stack.
264 lastpos = thispos + forendtag.length();
272 lastpos = thispos + 1;
276 os << buf.substr(lastpos-line);
279 void TemplateEngine::run(istream& is, ostream& os, const TemplateParameters& parameters, const XMLToolingException* e) const
282 while (getline(is, line))
285 const char* pos=buf.c_str();
286 process(true, buf, pos, os, parameters, emptyPair, e);