Base template for loops on a single multimap.
[shibboleth/cpp-xmltooling.git] / xmltooling / util / TemplateEngine.h
1 /*
2  *  Copyright 2001-2009 Internet2
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * @file xmltooling/util/TemplateEngine.h
19  *
20  * Simple template replacement engine.
21  */
22
23 #ifndef __xmltooling_template_h__
24 #define __xmltooling_template_h__
25
26 #include <xmltooling/io/GenericRequest.h>
27
28 #include <map>
29 #include <string>
30 #include <iostream>
31 #include <vector>
32
33 #if defined (_MSC_VER)
34     #pragma warning( push )
35     #pragma warning( disable : 4251 )
36 #endif
37
38 namespace xmltooling {
39
40     /**
41      * Simple template replacement engine. Supports the following:
42      * <ul>
43      *  <li> &lt;mlp key/&gt; </li>
44      *  <li> &lt;mlpif key&gt; stuff &lt;/mlpif&gt;</li>
45      *  <li> &lt;mlpifnot key&gt; stuff &lt;/mlpifnot&gt;</li>
46      *  <li> &lt;mlpfor key&gt; stuff &lt;/mlpfor&gt;</li>
47      *  <li> &lt;mlp $name/&gt; (in for loop only) </li>
48      *  <li> &lt;mlp $value/&gt; (in for loop only) </li>
49      * </ul>
50      *
51      * The default tag prefix is "mlp". This can be overridden for
52      * compatibility.
53      */
54     class XMLTOOL_API TemplateEngine
55     {
56         MAKE_NONCOPYABLE(TemplateEngine);
57     public:
58         TemplateEngine() {
59             setTagPrefix("mlp");
60         }
61
62         virtual ~TemplateEngine() {}
63
64         /**
65          * Sets the tag name to use when locating template replacement tags.
66          *
67          * @param tagPrefix base prefix for tags
68          */
69         void setTagPrefix(const char* tagPrefix);
70
71         /**
72          * Interface to parameters to plug into templates.
73          * Allows callers to supply a more dynamic lookup mechanism to supplement a basic map.
74          */
75         class XMLTOOL_API TemplateParameters {
76             // MAKE_NONCOPYABLE(TemplateParameters);
77         public:
78             TemplateParameters() : m_request(NULL) {}
79             virtual ~TemplateParameters() {}
80
81             /** Map of known parameters to supply to template. */
82             std::map<std::string,std::string> m_map;
83
84             /** Map of sub-collections used in for loops. */
85             std::map< std::string,std::multimap<std::string,std::string> > m_collectionMap;
86
87             /** Request from client that resulted in template being processed. */
88             const GenericRequest* m_request;
89
90             /**
91              * Returns the value of a parameter to plug into the template.
92              *
93              * @param name  name of parameter
94              * @return value of parameter, or NULL
95              */
96             virtual const char* getParameter(const char* name) const {
97                 std::map<std::string,std::string>::const_iterator i=m_map.find(name);
98                 return (i!=m_map.end() ? i->second.c_str() : (m_request ? m_request->getParameter(name) : NULL));
99             }
100
101             /**
102              * Returns a named collection of sub-parameters to pass into a loop.
103              *
104              * @param name  name of sub-collection
105              * @return pointer to a multimap of sub-parameters, or NULL
106              */
107             virtual const std::multimap<std::string,std::string>* getLoopCollection(const char* name) const {
108                 std::map< std::string,std::multimap<std::string,std::string> >::const_iterator i=m_collectionMap.find(name);
109                 return (i!=m_collectionMap.end() ? &(i->second) : NULL);
110             }
111         };
112
113         /**
114          * Processes template from an input stream and executes replacements and
115          * conditional logic based on parameters.
116          *
117          * @param is            input stream providing template
118          * @param os            output stream to send results of executing template
119          * @param parameters    parameters to plug into template
120          * @param e             optional exception to extract parameters from
121          */
122         virtual void run(
123             std::istream& is,
124             std::ostream& os,
125             const TemplateParameters& parameters,
126             const XMLToolingException* e=NULL
127             ) const;
128
129         /**
130          * List of non-built-in characters considered "unsafe" and requiring HTML encoding.
131          * The default set is #%&():[]\\`{}
132          */
133         static std::string unsafe_chars;
134
135     private:
136         void trimspace(std::string& s) const;
137         void html_encode(std::ostream& os, const char* start) const;
138         void process(
139             bool visible,
140             const std::string& buf,
141             const char*& lastpos,
142             std::ostream& os,
143             const TemplateParameters& parameters,
144             const std::pair<std::string,std::string>& loopentry,
145             const XMLToolingException* e
146             ) const;
147
148         std::string keytag,iftag,ifendtag,ifnottag,ifnotendtag,fortag,forendtag;
149     };
150 };
151
152 #if defined (_MSC_VER)
153     #pragma warning( pop )
154 #endif
155
156 #endif /* __xmltooling_template_h__ */