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.
18 * @file xmltooling/util/TemplateEngine.h
20 * Simple template replacement engine.
23 #ifndef __xmltooling_template_h__
24 #define __xmltooling_template_h__
26 #include <xmltooling/io/GenericRequest.h>
32 #if defined (_MSC_VER)
33 #pragma warning( push )
34 #pragma warning( disable : 4251 )
37 namespace xmltooling {
40 * Simple template replacement engine. Supports the following:
42 * <li> <mlp key/> </li>
43 * <li> <mlpif key> stuff </mlpif></li>
44 * <li> <mlpifnot key> stuff </mlpifnot></li>
47 * The default tag prefix is "mlp". This can be overridden for
50 class XMLTOOL_API TemplateEngine
52 MAKE_NONCOPYABLE(TemplateEngine);
58 virtual ~TemplateEngine() {}
61 * Sets the tag name to use when locating template replacement tags.
63 * @param tagPrefix base prefix for tags
65 void setTagPrefix(const char* tagPrefix);
68 * Interface to parameters to plug into templates.
69 * Allows callers to supply a more dynamic lookup mechanism to supplement a basic map.
71 class XMLTOOL_API TemplateParameters {
72 MAKE_NONCOPYABLE(TemplateParameters);
74 TemplateParameters() : m_request(NULL) {}
75 virtual ~TemplateParameters() {}
77 /** Map of known parameters to supply to template. */
78 std::map<std::string,std::string> m_map;
80 /** Request from client that resulted in template being processed. */
81 const GenericRequest* m_request;
84 * Returns the value of a parameter to plug into the template.
86 * @param name name of parameter
87 * @return value of parameter, or NULL
89 virtual const char* getParameter(const char* name) const {
90 std::map<std::string,std::string>::const_iterator i=m_map.find(name);
91 return (i!=m_map.end() ? i->second.c_str() : (m_request ? m_request->getParameter(name) : NULL));
96 * Processes template from an input stream and executes replacements and
97 * conditional logic based on parameters.
99 * @param is input stream providing template
100 * @param os output stream to send results of executing template
101 * @param parameters parameters to plug into template
102 * @param e optional exception to extract parameters from
107 const TemplateParameters& parameters,
108 const XMLToolingException* e=NULL
112 * List of non-built-in characters considered "unsafe" and requiring HTML encoding.
113 * The default set is #%&():[]\\`{}
115 static std::string unsafe_chars;
118 void trimspace(std::string& s) const;
119 void html_encode(std::ostream& os, const char* start) const;
122 const std::string& buf,
123 const char*& lastpos,
125 const TemplateParameters& parameters,
126 const XMLToolingException* e
129 std::string keytag,iftag,ifendtag,ifnottag,ifnotendtag;
133 #if defined (_MSC_VER)
134 #pragma warning( pop )
137 #endif /* __xmltooling_template_h__ */