2 * Copyright 2001-2007 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;
29 void TemplateEngine::setTagPrefix(const char* tagPrefix)
31 keytag = string("<") + tagPrefix + " ";
32 iftag = string("<") + tagPrefix + "if ";
33 ifnottag = string("<") + tagPrefix + "ifnot ";
34 ifendtag = string("</") + tagPrefix + "if>";
35 ifnotendtag = string("</") + tagPrefix + "ifnot>";
38 void TemplateEngine::html_encode(ostream& os, const char* start) const
40 while (start && *start) {
42 case '<': os << "<"; break;
43 case '>': os << ">"; break;
44 case '"': os << """; break;
45 case '#': os << "#"; break;
46 case '%': os << "%"; break;
47 case '&': os << "&"; break;
48 case '\'': os << "'"; break;
49 case '(': os << "("; break;
50 case ')': os << ")"; break;
51 case ':': os << ":"; break;
52 case '[': os << "["; break;
53 case '\\': os << "\"; break;
54 case ']': os << "]"; break;
55 case '`': os << "`"; break;
56 case '{': os << "{"; break;
57 case '}': os << "}"; break;
58 default: os << *start;
64 void TemplateEngine::trimspace(string& s) const
66 string::size_type end = s.size() - 1, start = 0;
68 // Trim stuff on right.
69 while (end > 0 && !isgraph(s[end])) end--;
71 // Trim stuff on left.
72 while (start < end && !isgraph(s[start])) start++;
75 s = s.substr(start, end - start + 1);
78 void TemplateEngine::process(
83 const TemplateParameters& parameters,
84 const XMLToolingException* e
87 const char* line = buf.c_str();
90 while ((thispos = strchr(lastpos, '<')) != NULL) {
91 // Output the string up to this token.
93 os << buf.substr(lastpos-line, thispos-lastpos);
95 // Make sure this token matches our tokens.
96 #ifdef HAVE_STRCASECMP
97 if (visible && !strncasecmp(thispos, keytag.c_str(), keytag.length()))
99 if (visible && !_strnicmp(thispos, keytag.c_str(), keytag.length()))
102 // Save this position off.
103 lastpos = thispos + keytag.length();
105 // search for the end-tag
106 if ((thispos = strstr(lastpos, "/>")) != NULL) {
107 string key = buf.substr(lastpos-line, thispos-lastpos);
110 const char* p = parameters.getParameter(key.c_str());
112 p = e->getProperty(key.c_str());
115 lastpos = thispos + 2; // strlen("/>")
118 #ifdef HAVE_STRCASECMP
119 else if (!strncasecmp(thispos, iftag.c_str(), iftag.length()))
121 else if (!_strnicmp(thispos, iftag.c_str(), iftag.length()))
124 // Save this position off.
125 lastpos = thispos + iftag.length();
127 // search for the end of this tag
128 if ((thispos = strchr(lastpos, '>')) != NULL) {
129 string key = buf.substr(lastpos-line, thispos-lastpos);
133 cond = parameters.getParameter(key.c_str()) || (e && e->getProperty(key.c_str()));
134 lastpos = thispos + 1; // strlen(">")
135 process(cond, buf, lastpos, os, parameters, e);
138 #ifdef HAVE_STRCASECMP
139 else if (!strncasecmp(thispos, ifendtag.c_str(), ifendtag.length()))
141 else if (!_strnicmp(thispos, ifendtag.c_str(), ifendtag.length()))
144 // Save this position off and pop the stack.
145 lastpos = thispos + ifendtag.length();
148 #ifdef HAVE_STRCASECMP
149 else if (!strncasecmp(thispos, ifnottag.c_str(), ifnottag.length()))
151 else if (!_strnicmp(thispos, ifnottag.c_str(), ifnottag.length()))
154 // Save this position off.
155 lastpos = thispos + ifnottag.length();
157 // search for the end of this tag
158 if ((thispos = strchr(lastpos, '>')) != NULL) {
159 string key = buf.substr(lastpos-line, thispos-lastpos);
163 cond = !(parameters.getParameter(key.c_str()) || (e && e->getProperty(key.c_str())));
164 lastpos = thispos + 1; // strlen(">")
165 process(cond, buf, lastpos, os, parameters, e);
168 #ifdef HAVE_STRCASECMP
169 else if (!strncasecmp(thispos, ifnotendtag.c_str(), ifnotendtag.length()))
171 else if (!_strnicmp(thispos, ifnotendtag.c_str(), ifnotendtag.length()))
174 // Save this position off and pop the stack.
175 lastpos = thispos + ifnotendtag.length();
182 lastpos = thispos + 1;
186 os << buf.substr(lastpos-line);
189 void TemplateEngine::run(istream& is, ostream& os, const TemplateParameters& parameters, const XMLToolingException* e) const
192 while (getline(is, line))
195 const char* pos=buf.c_str();
196 process(true, buf, pos, os, parameters, e);