2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * @file xmltooling/util/TemplateEngine.h
24 * Simple template replacement engine.
27 #ifndef __xmltooling_template_h__
28 #define __xmltooling_template_h__
30 #include <xmltooling/base.h>
37 #if defined (_MSC_VER)
38 #pragma warning( push )
39 #pragma warning( disable : 4251 )
42 namespace xmltooling {
44 class XMLTOOL_API GenericRequest;
47 * Simple template replacement engine. Supports the following:
49 * <li> <mlp key/> </li>
50 * <li> <mlpif key> stuff </mlpif></li>
51 * <li> <mlpifnot key> stuff </mlpifnot></li>
52 * <li> <mlpfor key> stuff </mlpfor></li>
53 * <li> <mlp $name/> (in for loop only) </li>
54 * <li> <mlp $value/> (in for loop only) </li>
57 * The default tag prefix is "mlp". This can be overridden for
60 class XMLTOOL_API TemplateEngine
62 MAKE_NONCOPYABLE(TemplateEngine);
64 /** Default constructor. */
67 virtual ~TemplateEngine();
70 * Sets the tag name to use when locating template replacement tags.
72 * @param tagPrefix base prefix for tags
74 void setTagPrefix(const char* tagPrefix);
77 * Interface to parameters to plug into templates.
78 * Allows callers to supply a more dynamic lookup mechanism to supplement a basic map.
80 class XMLTOOL_API TemplateParameters {
83 virtual ~TemplateParameters();
85 /** Map of known parameters to supply to template. */
86 std::map<std::string,std::string> m_map;
88 /** Map of sub-collections used in for loops. */
89 std::map< std::string,std::multimap<std::string,std::string> > m_collectionMap;
91 /** Request from client that resulted in template being processed. */
92 const GenericRequest* m_request;
95 * Returns the value of a parameter to plug into the template.
97 * @param name name of parameter
98 * @return value of parameter, or nullptr
100 virtual const char* getParameter(const char* name) const;
103 * Returns a named collection of sub-parameters to pass into a loop.
105 * @param name name of sub-collection
106 * @return pointer to a multimap of sub-parameters, or nullptr
108 virtual const std::multimap<std::string,std::string>* getLoopCollection(const char* name) const;
112 * Processes template from an input stream and executes replacements and
113 * conditional logic based on parameters.
115 * @param is input stream providing template
116 * @param os output stream to send results of executing template
117 * @param parameters parameters to plug into template
118 * @param e optional exception to extract parameters from
123 const TemplateParameters& parameters,
124 const XMLToolingException* e=nullptr
128 * List of non-built-in characters considered "unsafe" and requiring HTML encoding.
129 * The default set is #%&():[]\\`{}
131 static std::string unsafe_chars;
134 void html_encode(std::ostream& os, const char* start) const;
137 const std::string& buf,
138 const char*& lastpos,
140 const TemplateParameters& parameters,
141 const std::pair<const std::string,std::string>& loopentry,
142 const XMLToolingException* e
145 std::string keytag,iftag,ifendtag,ifnottag,ifnotendtag,fortag,forendtag;
149 #if defined (_MSC_VER)
150 #pragma warning( pop )
153 #endif /* __xmltooling_template_h__ */