Documentation for OpenFLUID 2.2.0
TemplateProcessor.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 
33 /**
34  @file TemplateProcessor.hpp
35 
36  @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
37  @author Dorian GERARDIN <dorian.gerardin@inrae.fr>
38 */
39 
40 
41 #ifndef __OPENFLUID_TOOLS_TEMPLATEPROCESSOR_HPP__
42 #define __OPENFLUID_TOOLS_TEMPLATEPROCESSOR_HPP__
43 
44 
45 #include <string>
46 #include <map>
47 #include <set>
48 #include <regex>
49 
50 #include <openfluid/dllexport.hpp>
51 
52 
53 namespace openfluid { namespace tools {
54 
55 
56 /**
57  Class for rendering templates from string, files or directories.
58 
59 _Example of use_
60 ~~~~~~~~~~~~~~~{.cpp}
61  const openfluid::tools::TemplateProcessor::Data Data = {
62  {"key1","value #1"},
63  {"key2","value #2"}
64  };
65  openfluid::tools::TemplateProcessor::Errors Errors;
66 
67  // construction of the processor using the ${datakey} pattern
68  // where datakey can be keys in the Data structure
69  openfluid::tools::TemplateProcessor TplProc("${","}");
70 
71  // file rendering
72  TplProc.renderFile("/path/to/template/file.tpl","/path/to/rendered/file.txt",Data,Errors);
73 
74  // recursive directory rendering
75  TplProc.renderDirectory("/path/to/template/directory","/path/to/rendered/directory",Data,Errors);
76 ~~~~~~~~~~~~~~~
77 */
79 {
80  private:
81 
82  std::regex m_RegEx;
83 
84  bool m_AutoComment = false;
85 
86  std::string m_CommentStr;
87 
88  bool m_IgnoreUnknown = false;
89 
90 
91  public:
92 
93  /**
94  Type for the input data used to resolve the template, as a key-value structure
95  */
96  using Data = std::map<std::string,std::string>;
97 
98  /**
99  Type for the resolution errors returned at render time, containing unresolved patterns found in the template
100  */
101  using Errors = std::set<std::string>;
102 
103 
104  TemplateProcessor() = delete;
105 
106  /**
107  Constructs a template processor, giving the beginning end ending delimiters used ath rendering time
108  @param[in] Begin The beginning delimiter
109  @param[in] End The ending delimiter
110  @throw openfluid::base::FrameworkException if the beginning or ending delimiter is empty
111  */
112  TemplateProcessor(const std::string& Begin, const std::string& End);
113 
114  virtual ~TemplateProcessor() = default;
115 
116  /**
117  Enables automatic comment mode. When enabled, the '\#KEYNAME' pattern is replaced by a comment string
118  during the rendering process if 'KEYNAME' is empty or does not exist.
119  @param[in] CommentStr The comment string to use
120  */
121  void enableAutoComment(const std::string& CommentStr);
122 
123  /**
124  Enables or disables the ignore unknown mode. When enabled, unknown keys are ignored and removed
125  during the rendering process.
126  */
127  void ignoreUnknown(bool Enabled);
128 
129  /**
130  Renders a template string into a string
131  @param[in] Str The template content
132  @param[in] D The data to resolve the template
133  @param[inout] E The unresolved patterns if any
134  @return The resolved template as a string
135  */
136  std::string renderString(const std::string& Str,const Data& D,Errors& E);
137 
138  /**
139  Renders a template file into a string
140  @param[in] InFile The template file path
141  @param[in] D The data to resolve the template
142  @param[inout] E The unresolved patterns if any
143  @return The resolved template as a string
144  @throw openfluid::base::FrameworkException if the template file cannot be open
145  */
146  std::string renderFile(const std::string& InFile,const Data& D,Errors& E);
147 
148  /**
149  Renders a template file into another file
150  @param[in] InFile The template file path
151  @param[in] OutFile The rendered file path
152  @param[in] D The data to resolve the template
153  @param[inout] E The unresolved patterns if any
154  @throw openfluid::base::FrameworkException if the output file is an existing directory or if rendering fails
155  */
156  void renderFile(const std::string& InFile, const std::string& OutFile,const Data& D,Errors& E);
157 
158  /**
159  Renders a template directory into another directory recursively.
160  @param[in] InDir The template directory path
161  @param[in] OutDir The rendered directory path
162  @param[in] D The data to resolve the template
163  @param[inout] E The unresolved patterns if any
164  @throw openfluid::base::FrameworkException if the output directory is an existing file or if rendering fails
165  */
166  void renderDirectory(const std::string& InDir,const std::string& OutDir,const Data& D,Errors& E);
167 
168 };
169 
170 
171 } } // namespaces
172 
173 
174 #endif /* __OPENFLUID_TOOLS_TEMPLATEPROCESSOR_HPP__ */
Definition: TemplateProcessor.hpp:79
std::set< std::string > Errors
Definition: TemplateProcessor.hpp:101
void renderFile(const std::string &InFile, const std::string &OutFile, const Data &D, Errors &E)
std::string renderFile(const std::string &InFile, const Data &D, Errors &E)
TemplateProcessor(const std::string &Begin, const std::string &End)
void enableAutoComment(const std::string &CommentStr)
void renderDirectory(const std::string &InDir, const std::string &OutDir, const Data &D, Errors &E)
std::string renderString(const std::string &Str, const Data &D, Errors &E)
std::map< std::string, std::string > Data
Definition: TemplateProcessor.hpp:96
#define OPENFLUID_API
Definition: dllexport.hpp:86
Definition: ApplicationException.hpp:47