Template replacement engine ported from Shib, added conditional nesting.
[shibboleth/cpp-xmltooling.git] / xmltooling / util / TemplateEngine.h
1 /*
2  *  Copyright 2001-2006 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
32 namespace xmltooling {
33
34     /**
35      * Simple template replacement engine. Supports the following:
36      * <ul>
37      *  <li> &lt;mlp key/&gt; </li>
38      *  <li> &lt;mlpif key&gt; stuff &lt;/mlpif&gt;</li>
39      *  <li> &lt;mlpifnot key&gt; stuff &lt;/mlpifnot&gt;</li>
40      * </ul>
41      * 
42      * The default tag prefix is "mlp". This can be overridden for
43      * compatibility.
44      */
45     class XMLTOOL_API TemplateEngine
46     {
47         MAKE_NONCOPYABLE(TemplateEngine);
48     public:
49         
50         TemplateEngine() {
51             setTagPrefix("mlp"); 
52         }
53
54         virtual ~TemplateEngine() {}
55         
56         /**
57          * Sets the tag name to use when locating template replacement tags.
58          * 
59          * @param tagPrefix base prefix for tags
60          */
61         void setTagPrefix(const char* tagPrefix);
62         
63         /**
64          * Processes template from an input stream and executes replacements and
65          * conditional logic based on parameters. 
66          * 
67          * @param is            input stream providing template
68          * @param os            output stream to send results of executing template
69          * @param parameters    name/value parameters to plug into template
70          * @param e             optional exception to extract parameters from
71          */
72         virtual void run(
73             std::istream& is,
74             std::ostream& os,
75             const std::map<std::string,std::string>& parameters,
76             const XMLToolingException* e=NULL
77             ) const;
78
79     private:
80         void trimspace(std::string& s) const;
81         void html_encode(std::ostream& os, const char* start) const;
82         void process(
83             bool visible,
84             const std::string& buf,
85             const char*& lastpos,
86             std::ostream& os,
87             const std::map<std::string,std::string>& parameters,
88             const XMLToolingException* e
89             ) const;
90             
91         std::string keytag,iftag,ifendtag,ifnottag,ifnotendtag;
92     };
93 };
94
95 #endif /* __xmltooling_template_h__ */