const string& buf,
const char*& lastpos,
ostream& os,
- const map<string,string>& parameters,
+ const TemplateParameters& parameters,
const XMLToolingException* e
) const
{
string key = buf.substr(lastpos-line, thispos-lastpos);
trimspace(key);
- map<string,string>::const_iterator i=parameters.find(key);
- if (i != parameters.end()) {
- html_encode(os,i->second.c_str());
- }
- else if (e) {
- const char* ep = e->getProperty(key.c_str());
- if (ep)
- html_encode(os,ep);
- }
+ const char* p = parameters.getParameter(key.c_str());
+ if (!p && e)
+ p = e->getProperty(key.c_str());
+ if (p)
+ html_encode(os,p);
lastpos = thispos + 2; // strlen("/>")
}
}
if ((thispos = strchr(lastpos, '>')) != NULL) {
string key = buf.substr(lastpos-line, thispos-lastpos);
trimspace(key);
- map<string,string>::const_iterator i=parameters.find(key);
bool cond=false;
- if (visible) {
- if (i != parameters.end())
- cond=true;
- else if (e && e->getProperty(key.c_str()))
- cond=true;
- }
+ if (visible)
+ cond = parameters.getParameter(key.c_str()) || (e && e->getProperty(key.c_str()));
lastpos = thispos + 1; // strlen(">")
process(cond, buf, lastpos, os, parameters, e);
}
if ((thispos = strchr(lastpos, '>')) != NULL) {
string key = buf.substr(lastpos-line, thispos-lastpos);
trimspace(key);
- map<string,string>::const_iterator i=parameters.find(key);
bool cond=visible;
- if (visible) {
- if (i != parameters.end())
- cond=false;
- else if (e && e->getProperty(key.c_str()))
- cond=false;
- }
+ if (visible)
+ cond = !(parameters.getParameter(key.c_str()) || (e && e->getProperty(key.c_str())));
lastpos = thispos + 1; // strlen(">")
process(cond, buf, lastpos, os, parameters, e);
}
os << buf.substr(lastpos-line);
}
-void TemplateEngine::run(istream& is, ostream& os, const map<string,string>& parameters, const XMLToolingException* e) const
+void TemplateEngine::run(istream& is, ostream& os, const TemplateParameters& parameters, const XMLToolingException* e) const
{
string buf,line;
while (getline(is, line))
void setTagPrefix(const char* tagPrefix);
/**
+ * Interface to parameters to plug into templates.
+ * Allows callers to supply a more dynamic lookup mechanism to supplement a basic map.
+ */
+ class XMLTOOL_API TemplateParameters {
+ MAKE_NONCOPYABLE(TemplateParameters);
+ public:
+ TemplateParameters() {}
+ virtual ~TemplateParameters() {}
+
+ /** Map of known parameters to supply to template. */
+ std::map<std::string,std::string> m_map;
+
+ /**
+ * Returns the value of a parameter to plug into the template.
+ *
+ * @param name name of parameter
+ * @return value of parameter, or NULL
+ */
+ virtual const char* getParameter(const char* name) const {
+ std::map<std::string,std::string>::const_iterator i=m_map.find(name);
+ return (i!=m_map.end() ? i->second.c_str() : NULL);
+ }
+ };
+
+ /**
* Processes template from an input stream and executes replacements and
* conditional logic based on parameters.
*
* @param is input stream providing template
* @param os output stream to send results of executing template
- * @param parameters name/value parameters to plug into template
+ * @param parameters parameters to plug into template
* @param e optional exception to extract parameters from
*/
virtual void run(
std::istream& is,
std::ostream& os,
- const std::map<std::string,std::string>& parameters,
+ const TemplateParameters& parameters,
const XMLToolingException* e=NULL
) const;
const std::string& buf,
const char*& lastpos,
std::ostream& os,
- const std::map<std::string,std::string>& parameters,
+ const TemplateParameters& parameters,
const XMLToolingException* e
) const;