2 * Copyright 2001-2009 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * Simple template replacement engine.
24 #include "util/TemplateEngine.h"
26 using namespace xmltooling;
30 static const pair<string,string> emptyPair;
33 void TemplateEngine::setTagPrefix(const char* tagPrefix)
35 keytag = string("<") + tagPrefix + " ";
36 iftag = string("<") + tagPrefix + "if ";
37 ifnottag = string("<") + tagPrefix + "ifnot ";
38 ifendtag = string("</") + tagPrefix + "if>";
39 ifnotendtag = string("</") + tagPrefix + "ifnot>";
40 fortag = string("<") + tagPrefix + "for ";
41 forendtag = string("</") + tagPrefix + "for>";
44 string TemplateEngine::unsafe_chars = "#%&():[]\\`{}";
46 void TemplateEngine::html_encode(ostream& os, const char* start) const
48 while (start && *start) {
50 case '<': os << "<"; break;
51 case '>': os << ">"; break;
52 case '"': os << """; break;
53 case '&': os << "&"; break;
54 case '\'': os << "'"; break;
57 if (unsafe_chars.find_first_of(*start) != string::npos)
58 os << "&#" << static_cast<short>(*start) << ';';
63 case '#': os << "#"; break;
64 case '%': os << "%"; break;
65 case '(': os << "("; break;
66 case ')': os << ")"; break;
67 case ':': os << ":"; break;
68 case '[': os << "["; break;
69 case '\\': os << "\"; break;
70 case ']': os << "]"; break;
71 case '`': os << "`"; break;
72 case '{': os << "{"; break;
73 case '}': os << "}"; break;
74 default: os << *start;
81 void TemplateEngine::trimspace(string& s) const
83 string::size_type end = s.size() - 1, start = 0;
85 // Trim stuff on right.
86 while (end > 0 && !isgraph(s[end])) end--;
88 // Trim stuff on left.
89 while (start < end && !isgraph(s[start])) start++;
92 s = s.substr(start, end - start + 1);
95 void TemplateEngine::process(
100 const TemplateParameters& parameters,
101 const std::pair<std::string,std::string>& loopentry,
102 const XMLToolingException* e
105 const char* line = buf.c_str();
108 while ((thispos = strchr(lastpos, '<')) != NULL) {
109 // Output the string up to this token.
111 os << buf.substr(lastpos-line, thispos-lastpos);
113 // Make sure this token matches our tokens.
114 #ifdef HAVE_STRCASECMP
115 if (visible && !strncasecmp(thispos, keytag.c_str(), keytag.length()))
117 if (visible && !_strnicmp(thispos, keytag.c_str(), keytag.length()))
120 // Save this position off.
121 lastpos = thispos + keytag.length();
123 // search for the end-tag
124 if ((thispos = strstr(lastpos, "/>")) != NULL) {
125 string key = buf.substr(lastpos-line, thispos-lastpos);
128 if (key == "$name" && !loopentry.first.empty())
129 html_encode(os,loopentry.first.c_str());
130 else if (key == "$value" && !loopentry.second.empty())
131 html_encode(os,loopentry.second.c_str());
133 const char* p = parameters.getParameter(key.c_str());
135 p = e->getProperty(key.c_str());
139 lastpos = thispos + 2; // strlen("/>")
142 #ifdef HAVE_STRCASECMP
143 else if (!strncasecmp(thispos, iftag.c_str(), iftag.length()))
145 else if (!_strnicmp(thispos, iftag.c_str(), iftag.length()))
148 // Save this position off.
149 lastpos = thispos + iftag.length();
151 // search for the end of this tag
152 if ((thispos = strchr(lastpos, '>')) != NULL) {
153 string key = buf.substr(lastpos-line, thispos-lastpos);
157 cond = parameters.getParameter(key.c_str()) || (e && e->getProperty(key.c_str()));
158 lastpos = thispos + 1; // strlen(">")
159 process(cond, buf, lastpos, os, parameters, loopentry, e);
162 #ifdef HAVE_STRCASECMP
163 else if (!strncasecmp(thispos, ifendtag.c_str(), ifendtag.length()))
165 else if (!_strnicmp(thispos, ifendtag.c_str(), ifendtag.length()))
168 // Save this position off and pop the stack.
169 lastpos = thispos + ifendtag.length();
172 #ifdef HAVE_STRCASECMP
173 else if (!strncasecmp(thispos, ifnottag.c_str(), ifnottag.length()))
175 else if (!_strnicmp(thispos, ifnottag.c_str(), ifnottag.length()))
178 // Save this position off.
179 lastpos = thispos + ifnottag.length();
181 // search for the end of this tag
182 if ((thispos = strchr(lastpos, '>')) != NULL) {
183 string key = buf.substr(lastpos-line, thispos-lastpos);
187 cond = !(parameters.getParameter(key.c_str()) || (e && e->getProperty(key.c_str())));
188 lastpos = thispos + 1; // strlen(">")
189 process(cond, buf, lastpos, os, parameters, loopentry, e);
192 #ifdef HAVE_STRCASECMP
193 else if (!strncasecmp(thispos, ifnotendtag.c_str(), ifnotendtag.length()))
195 else if (!_strnicmp(thispos, ifnotendtag.c_str(), ifnotendtag.length()))
198 // Save this position off and pop the stack.
199 lastpos = thispos + ifnotendtag.length();
203 #ifdef HAVE_STRCASECMP
204 else if (!strncasecmp(thispos, fortag.c_str(), fortag.length()))
206 else if (!_strnicmp(thispos, fortag.c_str(), fortag.length()))
209 // Save this position off.
210 lastpos = thispos + iftag.length();
214 // search for the end of this tag
215 if ((thispos = strchr(lastpos, '>')) != NULL) {
216 key = buf.substr(lastpos-line, thispos-lastpos);
218 lastpos = thispos + 1; // strlen(">")
221 const multimap<string,string>* forParams = parameters.getLoopCollection(key.c_str());
222 if (!forParams || forParams->size() == 0) {
223 process(false, buf, lastpos, os, parameters, emptyPair, e);
226 const char* savlastpos = lastpos;
227 for (multimap<string,string>::const_iterator i=forParams->begin(); i!=forParams->end(); ++i) {
228 lastpos = savlastpos;
229 process(cond, buf, lastpos, os, parameters, *i, e);
234 #ifdef HAVE_STRCASECMP
235 else if (!strncasecmp(thispos, forendtag.c_str(), forendtag.length()))
237 else if (!_strnicmp(thispos, forendtag.c_str(), forendtag.length()))
240 // Save this position off and pop the stack.
241 lastpos = thispos + forendtag.length();
249 lastpos = thispos + 1;
253 os << buf.substr(lastpos-line);
256 void TemplateEngine::run(istream& is, ostream& os, const TemplateParameters& parameters, const XMLToolingException* e) const
259 while (getline(is, line))
262 const char* pos=buf.c_str();
263 process(true, buf, pos, os, parameters, emptyPair, e);