Expose client request parameters to template engine.
[shibboleth/xmltooling.git] / xmltooling / util / TemplateEngine.h
1 /*
2  *  Copyright 2001-2007 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
32 #if defined (_MSC_VER)
33     #pragma warning( push )
34     #pragma warning( disable : 4251 )
35 #endif
36
37 namespace xmltooling {
38
39     /**
40      * Simple template replacement engine. Supports the following:
41      * <ul>
42      *  <li> &lt;mlp key/&gt; </li>
43      *  <li> &lt;mlpif key&gt; stuff &lt;/mlpif&gt;</li>
44      *  <li> &lt;mlpifnot key&gt; stuff &lt;/mlpifnot&gt;</li>
45      * </ul>
46      * 
47      * The default tag prefix is "mlp". This can be overridden for
48      * compatibility.
49      */
50     class XMLTOOL_API TemplateEngine
51     {
52         MAKE_NONCOPYABLE(TemplateEngine);
53     public:
54         
55         TemplateEngine() {
56             setTagPrefix("mlp"); 
57         }
58
59         virtual ~TemplateEngine() {}
60         
61         /**
62          * Sets the tag name to use when locating template replacement tags.
63          * 
64          * @param tagPrefix base prefix for tags
65          */
66         void setTagPrefix(const char* tagPrefix);
67         
68         /**
69          * Interface to parameters to plug into templates.
70          * Allows callers to supply a more dynamic lookup mechanism to supplement a basic map.
71          */
72         class XMLTOOL_API TemplateParameters {
73             MAKE_NONCOPYABLE(TemplateParameters);
74         public:
75             TemplateParameters() : m_request(NULL) {}
76             virtual ~TemplateParameters() {}
77             
78             /** Map of known parameters to supply to template. */
79             std::map<std::string,std::string> m_map;
80             
81             /** Request from client that resulted in template being processed. */
82             const GenericRequest* m_request;
83             
84             /**
85              * Returns the value of a parameter to plug into the template.
86              * 
87              * @param name  name of parameter
88              * @return value of parameter, or NULL
89              */
90             virtual const char* getParameter(const char* name) const {
91                 std::map<std::string,std::string>::const_iterator i=m_map.find(name);
92                 return (i!=m_map.end() ? i->second.c_str() : (m_request ? m_request->getParameter(name) : NULL));
93             }
94         };
95         
96         /**
97          * Processes template from an input stream and executes replacements and
98          * conditional logic based on parameters. 
99          * 
100          * @param is            input stream providing template
101          * @param os            output stream to send results of executing template
102          * @param parameters    parameters to plug into template
103          * @param e             optional exception to extract parameters from
104          */
105         virtual void run(
106             std::istream& is,
107             std::ostream& os,
108             const TemplateParameters& parameters,
109             const XMLToolingException* e=NULL
110             ) const;
111
112     private:
113         void trimspace(std::string& s) const;
114         void html_encode(std::ostream& os, const char* start) const;
115         void process(
116             bool visible,
117             const std::string& buf,
118             const char*& lastpos,
119             std::ostream& os,
120             const TemplateParameters& parameters,
121             const XMLToolingException* e
122             ) const;
123             
124         std::string keytag,iftag,ifendtag,ifnottag,ifnotendtag;
125     };
126 };
127
128 #if defined (_MSC_VER)
129     #pragma warning( pop )
130 #endif
131
132 #endif /* __xmltooling_template_h__ */