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