PluginSourceWriter.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/qt/widgets/plugin/builder/PluginSourceWriter.cpp
22 
23  \brief This class is used to create the source files for the new plugin builded.
24 */
25 
26 #include "PluginSourceWriter.h"
27 #include "../../../../common/Version.h"
28 #include "../../../../common/StringUtils.h"
29 
30 // STL Includes
31 #include <cstdlib>
32 #include <iostream>
33 #include <ctime>
34 
35 #define PLUGIN_H_FILE_NAME "Plugin.h"
36 #define PLUGIN_CPP_FILE_NAME "Plugin.cpp"
37 #define PLUGIN_CONFIG_FILE_NAME "Config.h"
38 
40 
42 
43 void te::qt::widgets::PluginSourceWriter::createHeaderFile(const std::string& sourcePath, const std::string& nameSpace)
44 {
45  std::ofstream file;
46 
47  std::string fileName = sourcePath;
48  fileName += "/";
49  fileName += PLUGIN_H_FILE_NAME;
50 
51  file.open(fileName.c_str());
52 
53  //insert default header
55 
56  //insert terralib header
58 
59  //insert file info
61 
62  //insert ifdef
63  insertIfDefInfo(file, nameSpace, PLUGIN_H_FILE_NAME);
64 
65  //add includes
66  file << "// TerraLib Includes\n";
67  file << "#include \"Config.h\"\n";
68  file << "#include <terralib/plugin/Plugin.h>\n\n";
69 
70  //define class
71  file << "namespace te\n";
72  file << "{\n";
73  file << " namespace qt\n";
74  file << " {\n";
75  file << " namespace plugins\n";
76  file << " {\n";
77  file << " namespace " + nameSpace +"\n";
78  file << " {\n";
79  file << " class Plugin : public te::plugin::Plugin\n";
80  file << " {\n";
81  file << " public:\n";
82  file << "\n";
83  file << " Plugin(const te::plugin::PluginInfo& pluginInfo);\n";
84  file << "\n";
85  file << " ~Plugin();\n";
86  file << "\n";
87  file << " void startup();\n";
88  file << "\n";
89  file << " void shutdown();\n";
90  file << " };\n";
91  file << "\n";
92  file << " } // end namespace " + nameSpace + "\n";
93  file << " } // end namespace plugins\n";
94  file << " } // end namespace qt\n";
95  file << "} // end namespace te\n";
96 
97  file << "\nPLUGIN_CALL_BACK_DECLARATION(" + getExportMacro(nameSpace) + ")\n";
98 
99  //insert endif
100  insertEndIfInfo(file, nameSpace, PLUGIN_H_FILE_NAME);
101 
102  file.close();
103 }
104 
105 void te::qt::widgets::PluginSourceWriter::createCppFile(const std::string& sourcePath, const std::string& nameSpace, const std::string projName)
106 {
107  std::ofstream file;
108 
109  std::string fileName = sourcePath;
110  fileName += "/";
111  fileName += PLUGIN_CPP_FILE_NAME;
112 
113  file.open(fileName.c_str());
114 
115  //insert default header
117 
118  //insert terralib header
119  insertTerraLibHeader(file);
120 
121  //insert file info
123 
124  //add includes
125  file << "// TerraLib Includes\n";
126  file << "#include \"Plugin.h\"\n\n";
127  file << "#include <terralib/common/Config.h>\n";
128  file << "#include <terralib/core/logger/Logger.h>\n";
129  file << "#include <terralib/core/translator/Translator.h>\n\n";
130 
131  //constructor
132  file << "te::qt::plugins::" + nameSpace + "::Plugin::Plugin(const te::plugin::PluginInfo& pluginInfo)\n";
133  file << " : te::plugin::Plugin(pluginInfo)\n";
134  file << "{\n";
135  file << "}\n\n";
136 
137  //destructor
138  file << "te::qt::plugins::" + nameSpace + "::Plugin::~Plugin()\n";
139  file << "{\n";
140  file << "}\n\n";
141 
142  std::string nsUpperCase = te::common::Convert2UCase(nameSpace);
143 
144  //startup
145  file << "void te::qt::plugins::" + nameSpace + "::Plugin::startup()\n";
146  file << "{\n";
147  file << " if(m_initialized)\n";
148  file << " return;\n";
149  file << "\n";
150  file << "// it initializes the Translator support\n";
151  file << " TE_ADD_TEXT_DOMAIN(TE_QT_PLUGIN_BUILDER_" + nsUpperCase + "_TEXT_DOMAIN, TE_QT_PLUGIN_BUILDER_" + nsUpperCase + "_TEXT_DOMAIN_DIR, \"UTF-8\");\n";
152  file << "\n";
153  file << " TE_LOG_TRACE(TE_QT_PLUGIN_BUILDER_" + nsUpperCase + "(\"Plugin " + projName + " startup!\"));\n";
154  file << "\n";
155  file << " m_initialized = true;\n";
156  file << "}\n\n";
157 
158  //shutdown
159  file << "void te::qt::plugins::" + nameSpace + "::Plugin::shutdown()\n";
160  file << "{\n";
161  file << " if(!m_initialized)\n";
162  file << " return;\n";
163  file << "\n";
164  file << " TE_LOG_TRACE(TE_QT_PLUGIN_BUILDER_" + nsUpperCase + "(\"Plugin " + projName + " shutdown!\"));\n";
165  file << "\n";
166  file << " m_initialized = false;\n";
167  file << "}\n\n";
168 
169 
170  file << "PLUGIN_CALL_BACK_IMPL(te::qt::plugins::" + nameSpace + "::Plugin)\n";
171 
172  file.close();
173 }
174 
175 void te::qt::widgets::PluginSourceWriter::createConfigFile(const std::string& sourcePath, const std::string& nameSpace, const std::string exportMacro, const std::string projName)
176 {
177  std::ofstream file;
178 
179  std::string fileName = sourcePath;
180  fileName += "/";
181  fileName += PLUGIN_CONFIG_FILE_NAME;
182 
183  file.open(fileName.c_str());
184 
185  //insert default header
187 
188  //insert terralib header
189  insertTerraLibHeader(file);
190 
191  //insert file info
193 
194  //insert ifdef
195  insertIfDefInfo(file, nameSpace, PLUGIN_CONFIG_FILE_NAME);
196 
197  std::string nsUpperCase = te::common::Convert2UCase(nameSpace);
198 
199  //plugin name
200  file << "/*!\n";
201  file << " \\def TE_QT_PLUGIN_BUILDER_" + nsUpperCase + "_PLUGIN_NAME\n";
202  file << "\n";
203  file << " \\brief It contains the plugin name.\n";
204  file << "*/\n";
205  file << "#define TE_QT_PLUGIN_BUILDER_" + nsUpperCase + "_PLUGIN_NAME \"" + projName + "\"\n\n";
206 
207  //text domain
208  file << "/*!\n";
209  file << " \\def TE_QT_PLUGIN_BUILDER_" + nsUpperCase + "_TEXT_DOMAIN\n";
210  file << "\n";
211  file << " \\brief It contains the name of the text domain used in the translation of messages in this plugin.\n";
212  file << "*/\n";
213  file << "#define TE_QT_PLUGIN_BUILDER_" + nsUpperCase + "_TEXT_DOMAIN \"" + projName + "\"\n\n";
214 
215  //translation catalog dir
216  file << "/*!\n";
217  file << " \\def TE_QT_PLUGIN_BUILDER_" + nsUpperCase + "_TEXT_DOMAIN_DIR\n";
218  file << "\n";
219  file << " \\brief It contains the translation catalog directory.\n";
220  file << "*/\n";
221  file << "#define TE_QT_PLUGIN_BUILDER_" + nsUpperCase + "_TEXT_DOMAIN_DIR \"locale\"\n\n";
222 
223  //translation macro
224  file << "/*!\n";
225  file << " \\def TE_QT_PLUGIN_BUILDER_" + nsUpperCase + "\n";
226  file << "\n";
227  file << " \\brief It marks a string in order to get translated.\n";
228  file << "*/\n";
229  file << "#define TE_QT_PLUGIN_BUILDER_" + nsUpperCase + "(message) TR(message, TE_QT_PLUGIN_BUILDER_" + nsUpperCase + "_TEXT_DOMAIN)\n\n";
230 
231  //export information
232  file << "/*!\n";
233  file << " \\def " + getExportMacro(nameSpace) + "\n";
234  file << "\n";
235  file << " \\brief You can use this macro in order to export/import classes and functions from all plug-ins files.\n";
236  file << "\n";
237  file << " \\note To compile plug-ins in Windows, remember to insert " + exportMacro + " into the project's list of defines.\n";
238  file << "*/\n";
239  file << "#ifdef WIN32\n";
240  file << " #ifdef " + exportMacro + "\n";
241  file << " #define " + getExportMacro(nameSpace) + " __declspec(dllexport) // export DLL information\n";
242  file << " #else\n";
243  file << " #define " + getExportMacro(nameSpace) + " __declspec(dllimport) // import DLL information\n";
244  file << " #endif \n";
245  file << "#else\n";
246  file << " #define " + getExportMacro(nameSpace) + "\n";
247  file << "#endif\n\n";
248 
249  //insert endif
250  insertEndIfInfo(file, nameSpace, PLUGIN_CONFIG_FILE_NAME);
251 
252  file.close();
253 }
254 
255 void te::qt::widgets::PluginSourceWriter::insertDefaultHeader(std::ofstream& stream, const std::string& fileName)
256 {
257  time_t now = time(nullptr);
258  char* dt = ctime(&now);
259  std::string currentTime = dt;
260  std::string teVersion = te::common::Version::asString();
261 
262  std::string defaultHeader;
263 
264  defaultHeader += "/**********************************************************************\n";
265  defaultHeader += "** This file was automatically generated by TerraView - Plugin Builder\n";
266  defaultHeader += "** TerraLib Version: " + teVersion + " \n";
267  defaultHeader += "** File: " + fileName + "\n";
268  defaultHeader += "** Date: " + currentTime;
269  defaultHeader += "**********************************************************************/\n";
270 
271  stream << defaultHeader.c_str();
272  stream << "\n";
273 }
274 
276 {
277  std::string defaultHeader;
278 
279  defaultHeader += "/* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.\n";
280  defaultHeader += "\n";
281  defaultHeader += " This file is part of the TerraLib - a Framework for building GIS enabled applications.\n";
282  defaultHeader += "\n";
283  defaultHeader += " TerraLib is free software: you can redistribute it and/or modify\n";
284  defaultHeader += " it under the terms of the GNU Lesser General Public License as published by\n";
285  defaultHeader += " the Free Software Foundation, either version 3 of the License,\n";
286  defaultHeader += " or (at your option) any later version.\n";
287  defaultHeader += "\n";
288  defaultHeader += " TerraLib is distributed in the hope that it will be useful,\n";
289  defaultHeader += " but WITHOUT ANY WARRANTY; without even the implied warranty of\n";
290  defaultHeader += " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n";
291  defaultHeader += " GNU Lesser General Public License for more details.\n";
292  defaultHeader += "\n";
293  defaultHeader += " You should have received a copy of the GNU Lesser General Public License\n";
294  defaultHeader += " along with TerraLib. See COPYING. If not, write to\n";
295  defaultHeader += " TerraLib Team at <terralib-team@terralib.org>.\n";
296  defaultHeader += "*/\n";
297 
298  stream << defaultHeader.c_str();
299  stream << "\n";
300 }
301 
302 void te::qt::widgets::PluginSourceWriter::insertFileInfo(std::ofstream& stream, const std::string& fileName)
303 {
304  std::string info;
305 
306  info += "/*!\n";
307  info += " \\file " + fileName +"\n";
308  info += "\n";
309  info += " \\brief ...\n";
310  info += "*/\n";
311 
312  stream << info.c_str();
313  stream << "\n";
314 }
315 
316 void te::qt::widgets::PluginSourceWriter::insertIfDefInfo(std::ofstream& stream, const std::string& nameSpace, const std::string& fileName)
317 {
318  std::string str;
319 
320  std::string nsUpperCase = te::common::Convert2UCase(nameSpace);
321 
322  int pos = static_cast<int>(fileName.find("."));
323  std::string body = fileName.substr(0, pos);
324  std::string flUPpperCase = te::common::Convert2UCase(body);
325 
326  str += "#ifndef __TE_QT_PLUGIN_BUILDER_" + nsUpperCase + "_INTERNAL_" + flUPpperCase + "_H\n";
327  str += "#define __TE_QT_PLUGIN_BUILDER_" + nsUpperCase + "_INTERNAL_" + flUPpperCase + "_H\n";
328 
329  stream << str.c_str();
330  stream << "\n";
331 }
332 
333 void te::qt::widgets::PluginSourceWriter::insertEndIfInfo(std::ofstream& stream, const std::string& nameSpace, const std::string& fileName)
334 {
335  std::string str;
336 
337  std::string nsUpperCase = te::common::Convert2UCase(nameSpace);
338 
339  int pos = static_cast<int>(fileName.find("."));
340  std::string body = fileName.substr(0, pos);
341  std::string flUPpperCase = te::common::Convert2UCase(body);
342 
343  str += "#endif //__TE_QT_PLUGIN_BUILDER_" + nsUpperCase + "_INTERNAL_" + flUPpperCase + "_H\n";
344 
345  stream << "\n";
346  stream << str.c_str();
347  stream << "\n";
348 }
349 
350 std::string te::qt::widgets::PluginSourceWriter::getExportMacro(const std::string& nameSpace)
351 {
352  std::string nsUpperCase = te::common::Convert2UCase(nameSpace);
353 
354  std::string tag = "TEQTPLUGIN" + nsUpperCase + "EXPORT";
355 
356  return tag;
357 }
#define PLUGIN_CPP_FILE_NAME
std::string getExportMacro(const std::string &nameSpace)
static std::string asString()
Definition: Version.cpp:60
std::string Convert2UCase(const std::string &value)
It converts a string to upper case.
Definition: StringUtils.h:168
void insertFileInfo(std::ofstream &stream, const std::string &fileName)
void createCppFile(const std::string &sourcePath, const std::string &nameSpace, const std::string projName)
static te::dt::TimeDuration dt(20, 30, 50, 11)
void insertTerraLibHeader(std::ofstream &stream)
#define PLUGIN_H_FILE_NAME
void insertDefaultHeader(std::ofstream &stream, const std::string &fileName)
void insertEndIfInfo(std::ofstream &stream, const std::string &nameSpace, const std::string &fileName)
This class is used to create the source files for the new plugin builded.
void createConfigFile(const std::string &sourcePath, const std::string &nameSpace, const std::string exportMacro, const std::string projName)
void insertIfDefInfo(std::ofstream &stream, const std::string &nameSpace, const std::string &fileName)
void createHeaderFile(const std::string &sourcePath, const std::string &nameSpace)
file(WRITE ${CMAKE_BINARY_DIR}/config_qhelp.cmake"configure_file (${TERRALIB_ABSOLUTE_ROOT_DIR}/doc/qhelp/help.qhcp.in ${CMAKE_BINARY_DIR}/share/terraview/help/help.qhcp @ONLY)") add_custom_command(OUTPUT del_dir COMMAND $
#define PLUGIN_CONFIG_FILE_NAME