2 * Copyright 2001-2010 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/base.h>
33 #if defined (_MSC_VER)
34 #pragma warning( push )
35 #pragma warning( disable : 4251 )
38 namespace xmltooling {
40 class XMLTOOL_API GenericRequest;
43 * Simple template replacement engine. Supports the following:
45 * <li> <mlp key/> </li>
46 * <li> <mlpif key> stuff </mlpif></li>
47 * <li> <mlpifnot key> stuff </mlpifnot></li>
48 * <li> <mlpfor key> stuff </mlpfor></li>
49 * <li> <mlp $name/> (in for loop only) </li>
50 * <li> <mlp $value/> (in for loop only) </li>
53 * The default tag prefix is "mlp". This can be overridden for
56 class XMLTOOL_API TemplateEngine
58 MAKE_NONCOPYABLE(TemplateEngine);
60 /** Default constructor. */
63 virtual ~TemplateEngine();
66 * Sets the tag name to use when locating template replacement tags.
68 * @param tagPrefix base prefix for tags
70 void setTagPrefix(const char* tagPrefix);
73 * Interface to parameters to plug into templates.
74 * Allows callers to supply a more dynamic lookup mechanism to supplement a basic map.
76 class XMLTOOL_API TemplateParameters {
79 virtual ~TemplateParameters();
81 /** Map of known parameters to supply to template. */
82 std::map<std::string,std::string> m_map;
84 /** Map of sub-collections used in for loops. */
85 std::map< std::string,std::multimap<std::string,std::string> > m_collectionMap;
87 /** Request from client that resulted in template being processed. */
88 const GenericRequest* m_request;
91 * Returns the value of a parameter to plug into the template.
93 * @param name name of parameter
94 * @return value of parameter, or nullptr
96 virtual const char* getParameter(const char* name) const;
99 * Returns a named collection of sub-parameters to pass into a loop.
101 * @param name name of sub-collection
102 * @return pointer to a multimap of sub-parameters, or nullptr
104 virtual const std::multimap<std::string,std::string>* getLoopCollection(const char* name) const;
108 * Processes template from an input stream and executes replacements and
109 * conditional logic based on parameters.
111 * @param is input stream providing template
112 * @param os output stream to send results of executing template
113 * @param parameters parameters to plug into template
114 * @param e optional exception to extract parameters from
119 const TemplateParameters& parameters,
120 const XMLToolingException* e=nullptr
124 * List of non-built-in characters considered "unsafe" and requiring HTML encoding.
125 * The default set is #%&():[]\\`{}
127 static std::string unsafe_chars;
130 void trimspace(std::string& s) const;
131 void html_encode(std::ostream& os, const char* start) const;
134 const std::string& buf,
135 const char*& lastpos,
137 const TemplateParameters& parameters,
138 const std::pair<const std::string,std::string>& loopentry,
139 const XMLToolingException* e
142 std::string keytag,iftag,ifendtag,ifnottag,ifnotendtag,fortag,forendtag;
146 #if defined (_MSC_VER)
147 #pragma warning( pop )
150 #endif /* __xmltooling_template_h__ */