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