cmCPackIFWRepository.cxx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCPackIFWRepository.h"
  11. #include "CPack/cmCPackGenerator.h"
  12. #include "cmCPackIFWGenerator.h"
  13. #include "cmGeneratedFileStream.h"
  14. #include "cmSystemTools.h"
  15. #include "cmXMLParser.h"
  16. #include "cmXMLWriter.h"
  17. #include <cmConfigure.h>
  18. #include <stddef.h>
  19. #ifdef cmCPackLogger
  20. #undef cmCPackLogger
  21. #endif
  22. #define cmCPackLogger(logType, msg) \
  23. do { \
  24. std::ostringstream cmCPackLog_msg; \
  25. cmCPackLog_msg << msg; \
  26. if (Generator) { \
  27. Generator->Logger->Log(logType, __FILE__, __LINE__, \
  28. cmCPackLog_msg.str().c_str()); \
  29. } \
  30. } while (0)
  31. cmCPackIFWRepository::cmCPackIFWRepository()
  32. : Update(None)
  33. , Generator(CM_NULLPTR)
  34. {
  35. }
  36. bool cmCPackIFWRepository::IsValid() const
  37. {
  38. bool valid = true;
  39. switch (Update) {
  40. case None:
  41. valid = !Url.empty();
  42. break;
  43. case Add:
  44. valid = !Url.empty();
  45. break;
  46. case Remove:
  47. valid = !Url.empty();
  48. break;
  49. case Replace:
  50. valid = !OldUrl.empty() && !NewUrl.empty();
  51. break;
  52. }
  53. return valid;
  54. }
  55. const char* cmCPackIFWRepository::GetOption(const std::string& op) const
  56. {
  57. return Generator ? Generator->GetOption(op) : CM_NULLPTR;
  58. }
  59. bool cmCPackIFWRepository::IsOn(const std::string& op) const
  60. {
  61. return Generator ? Generator->IsOn(op) : false;
  62. }
  63. bool cmCPackIFWRepository::IsVersionLess(const char* version)
  64. {
  65. return Generator ? Generator->IsVersionLess(version) : false;
  66. }
  67. bool cmCPackIFWRepository::IsVersionGreater(const char* version)
  68. {
  69. return Generator ? Generator->IsVersionGreater(version) : false;
  70. }
  71. bool cmCPackIFWRepository::IsVersionEqual(const char* version)
  72. {
  73. return Generator ? Generator->IsVersionEqual(version) : false;
  74. }
  75. bool cmCPackIFWRepository::ConfigureFromOptions()
  76. {
  77. // Name;
  78. if (Name.empty()) {
  79. return false;
  80. }
  81. std::string prefix =
  82. "CPACK_IFW_REPOSITORY_" + cmsys::SystemTools::UpperCase(Name) + "_";
  83. // Update
  84. if (IsOn(prefix + "ADD")) {
  85. Update = Add;
  86. } else if (IsOn(prefix + "REMOVE")) {
  87. Update = Remove;
  88. } else if (IsOn(prefix + "REPLACE")) {
  89. Update = Replace;
  90. } else {
  91. Update = None;
  92. }
  93. // Url
  94. if (const char* url = GetOption(prefix + "URL")) {
  95. Url = url;
  96. } else {
  97. Url = "";
  98. }
  99. // Old url
  100. if (const char* oldUrl = GetOption(prefix + "OLD_URL")) {
  101. OldUrl = oldUrl;
  102. } else {
  103. OldUrl = "";
  104. }
  105. // New url
  106. if (const char* newUrl = GetOption(prefix + "NEW_URL")) {
  107. NewUrl = newUrl;
  108. } else {
  109. NewUrl = "";
  110. }
  111. // Enabled
  112. if (IsOn(prefix + "DISABLED")) {
  113. Enabled = "0";
  114. } else {
  115. Enabled = "";
  116. }
  117. // Username
  118. if (const char* username = GetOption(prefix + "USERNAME")) {
  119. Username = username;
  120. } else {
  121. Username = "";
  122. }
  123. // Password
  124. if (const char* password = GetOption(prefix + "PASSWORD")) {
  125. Password = password;
  126. } else {
  127. Password = "";
  128. }
  129. // DisplayName
  130. if (const char* displayName = GetOption(prefix + "DISPLAY_NAME")) {
  131. DisplayName = displayName;
  132. } else {
  133. DisplayName = "";
  134. }
  135. return IsValid();
  136. }
  137. /** \class cmCPackeIFWUpdatesPatcher
  138. * \brief Helper class that parses and patch Updates.xml file (QtIFW)
  139. */
  140. class cmCPackeIFWUpdatesPatcher : public cmXMLParser
  141. {
  142. public:
  143. cmCPackeIFWUpdatesPatcher(cmCPackIFWRepository* r, cmXMLWriter& x)
  144. : repository(r)
  145. , xout(x)
  146. , patched(false)
  147. {
  148. }
  149. cmCPackIFWRepository* repository;
  150. cmXMLWriter& xout;
  151. bool patched;
  152. protected:
  153. void StartElement(const std::string& name, const char** atts) CM_OVERRIDE
  154. {
  155. xout.StartElement(name);
  156. StartFragment(atts);
  157. }
  158. void StartFragment(const char** atts)
  159. {
  160. for (size_t i = 0; atts[i]; i += 2) {
  161. const char* key = atts[i];
  162. const char* value = atts[i + 1];
  163. xout.Attribute(key, value);
  164. }
  165. }
  166. void EndElement(const std::string& name) CM_OVERRIDE
  167. {
  168. if (name == "Updates" && !patched) {
  169. repository->WriteRepositoryUpdates(xout);
  170. patched = true;
  171. }
  172. xout.EndElement();
  173. if (patched) {
  174. return;
  175. }
  176. if (name == "Checksum") {
  177. repository->WriteRepositoryUpdates(xout);
  178. patched = true;
  179. }
  180. }
  181. void CharacterDataHandler(const char* data, int length) CM_OVERRIDE
  182. {
  183. std::string content(data, data + length);
  184. if (content == "" || content == " " || content == " " ||
  185. content == "\n") {
  186. return;
  187. }
  188. xout.Content(content);
  189. }
  190. };
  191. bool cmCPackIFWRepository::PatchUpdatesXml()
  192. {
  193. // Lazy directory initialization
  194. if (Directory.empty() && Generator) {
  195. Directory = Generator->toplevel;
  196. }
  197. // Filenames
  198. std::string updatesXml = Directory + "/repository/Updates.xml";
  199. std::string updatesPatchXml = Directory + "/repository/UpdatesPatch.xml";
  200. // Output stream
  201. cmGeneratedFileStream fout(updatesPatchXml.data());
  202. cmXMLWriter xout(fout);
  203. xout.StartDocument();
  204. WriteGeneratedByToStrim(xout);
  205. // Patch
  206. {
  207. cmCPackeIFWUpdatesPatcher patcher(this, xout);
  208. patcher.ParseFile(updatesXml.data());
  209. }
  210. xout.EndDocument();
  211. fout.Close();
  212. return cmSystemTools::RenameFile(updatesPatchXml.data(), updatesXml.data());
  213. }
  214. void cmCPackIFWRepository::WriteRepositoryConfig(cmXMLWriter& xout)
  215. {
  216. xout.StartElement("Repository");
  217. // Url
  218. xout.Element("Url", Url);
  219. // Enabled
  220. if (!Enabled.empty()) {
  221. xout.Element("Enabled", Enabled);
  222. }
  223. // Username
  224. if (!Username.empty()) {
  225. xout.Element("Username", Username);
  226. }
  227. // Password
  228. if (!Password.empty()) {
  229. xout.Element("Password", Password);
  230. }
  231. // DisplayName
  232. if (!DisplayName.empty()) {
  233. xout.Element("DisplayName", DisplayName);
  234. }
  235. xout.EndElement();
  236. }
  237. void cmCPackIFWRepository::WriteRepositoryUpdate(cmXMLWriter& xout)
  238. {
  239. xout.StartElement("Repository");
  240. switch (Update) {
  241. case None:
  242. break;
  243. case Add:
  244. xout.Attribute("action", "add");
  245. break;
  246. case Remove:
  247. xout.Attribute("action", "remove");
  248. break;
  249. case Replace:
  250. xout.Attribute("action", "replace");
  251. break;
  252. }
  253. // Url
  254. if (Update == Add || Update == Remove) {
  255. xout.Attribute("url", Url);
  256. } else if (Update == Replace) {
  257. xout.Attribute("oldUrl", OldUrl);
  258. xout.Attribute("newUrl", NewUrl);
  259. }
  260. // Enabled
  261. if (!Enabled.empty()) {
  262. xout.Attribute("enabled", Enabled);
  263. }
  264. // Username
  265. if (!Username.empty()) {
  266. xout.Attribute("username", Username);
  267. }
  268. // Password
  269. if (!Password.empty()) {
  270. xout.Attribute("password", Password);
  271. }
  272. // DisplayName
  273. if (!DisplayName.empty()) {
  274. xout.Attribute("displayname", DisplayName);
  275. }
  276. xout.EndElement();
  277. }
  278. void cmCPackIFWRepository::WriteRepositoryUpdates(cmXMLWriter& xout)
  279. {
  280. if (!RepositoryUpdate.empty()) {
  281. xout.StartElement("RepositoryUpdate");
  282. for (RepositoriesVector::iterator rit = RepositoryUpdate.begin();
  283. rit != RepositoryUpdate.end(); ++rit) {
  284. (*rit)->WriteRepositoryUpdate(xout);
  285. }
  286. xout.EndElement();
  287. }
  288. }
  289. void cmCPackIFWRepository::WriteGeneratedByToStrim(cmXMLWriter& xout)
  290. {
  291. if (Generator) {
  292. Generator->WriteGeneratedByToStrim(xout);
  293. }
  294. }