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