cmWIXSourceWriter.cxx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmWIXSourceWriter.h"
  4. #include <windows.h>
  5. #include "cmCPackGenerator.h"
  6. #include "cmCryptoHash.h"
  7. #include "cmUuid.h"
  8. cmWIXSourceWriter::cmWIXSourceWriter(cmCPackLog* logger,
  9. std::string const& filename,
  10. GuidType componentGuidType,
  11. RootElementType rootElementType)
  12. : Logger(logger)
  13. , File(filename.c_str())
  14. , State(DEFAULT)
  15. , SourceFilename(filename)
  16. , ComponentGuidType(componentGuidType)
  17. {
  18. WriteXMLDeclaration();
  19. if (rootElementType == INCLUDE_ELEMENT_ROOT) {
  20. BeginElement("Include");
  21. } else {
  22. BeginElement("Wix");
  23. }
  24. AddAttribute("xmlns", "http://schemas.microsoft.com/wix/2006/wi");
  25. }
  26. cmWIXSourceWriter::~cmWIXSourceWriter()
  27. {
  28. if (Elements.size() > 1) {
  29. cmCPackLogger(cmCPackLog::LOG_ERROR,
  30. Elements.size() - 1
  31. << " WiX elements were still open when closing '"
  32. << SourceFilename << '\'' << std::endl);
  33. return;
  34. }
  35. EndElement(Elements.back());
  36. }
  37. void cmWIXSourceWriter::BeginElement(std::string const& name)
  38. {
  39. if (State == BEGIN) {
  40. File << '>';
  41. }
  42. File << '\n';
  43. Indent(Elements.size());
  44. File << '<' << name;
  45. Elements.push_back(name);
  46. State = BEGIN;
  47. }
  48. void cmWIXSourceWriter::EndElement(std::string const& name)
  49. {
  50. if (Elements.empty()) {
  51. cmCPackLogger(cmCPackLog::LOG_ERROR,
  52. "can not end WiX element with no open elements in '"
  53. << SourceFilename << '\'' << std::endl);
  54. return;
  55. }
  56. if (Elements.back() != name) {
  57. cmCPackLogger(cmCPackLog::LOG_ERROR,
  58. "WiX element <"
  59. << Elements.back() << "> can not be closed by </" << name
  60. << "> in '" << SourceFilename << '\'' << std::endl);
  61. return;
  62. }
  63. if (State == DEFAULT) {
  64. File << '\n';
  65. Indent(Elements.size() - 1);
  66. File << "</" << Elements.back() << '>';
  67. } else {
  68. File << "/>";
  69. }
  70. Elements.pop_back();
  71. State = DEFAULT;
  72. }
  73. void cmWIXSourceWriter::AddTextNode(std::string const& text)
  74. {
  75. if (State == BEGIN) {
  76. File << '>';
  77. }
  78. if (Elements.empty()) {
  79. cmCPackLogger(cmCPackLog::LOG_ERROR,
  80. "can not add text without open WiX element in '"
  81. << SourceFilename << '\'' << std::endl);
  82. return;
  83. }
  84. File << cmWIXSourceWriter::EscapeAttributeValue(text);
  85. State = DEFAULT;
  86. }
  87. void cmWIXSourceWriter::AddProcessingInstruction(std::string const& target,
  88. std::string const& content)
  89. {
  90. if (State == BEGIN) {
  91. File << '>';
  92. }
  93. File << '\n';
  94. Indent(Elements.size());
  95. File << "<?" << target << ' ' << content << "?>";
  96. State = DEFAULT;
  97. }
  98. void cmWIXSourceWriter::AddAttribute(std::string const& key,
  99. std::string const& value)
  100. {
  101. File << ' ' << key << "=\"" << EscapeAttributeValue(value) << '"';
  102. }
  103. void cmWIXSourceWriter::AddAttributeUnlessEmpty(std::string const& key,
  104. std::string const& value)
  105. {
  106. if (!value.empty()) {
  107. AddAttribute(key, value);
  108. }
  109. }
  110. std::string cmWIXSourceWriter::CreateGuidFromComponentId(
  111. std::string const& componentId)
  112. {
  113. std::string guid = "*";
  114. if (this->ComponentGuidType == CMAKE_GENERATED_GUID) {
  115. cmCryptoHash hasher(cmCryptoHash::AlgoMD5);
  116. std::string md5 = hasher.HashString(componentId);
  117. cmUuid uuid;
  118. std::vector<unsigned char> ns;
  119. guid = uuid.FromMd5(ns, md5);
  120. }
  121. return guid;
  122. }
  123. void cmWIXSourceWriter::WriteXMLDeclaration()
  124. {
  125. File << R"(<?xml version="1.0" encoding="UTF-8"?>)" << std::endl;
  126. }
  127. void cmWIXSourceWriter::Indent(size_t count)
  128. {
  129. for (size_t i = 0; i < count; ++i) {
  130. File << " ";
  131. }
  132. }
  133. std::string cmWIXSourceWriter::EscapeAttributeValue(std::string const& value)
  134. {
  135. std::string result;
  136. result.reserve(value.size());
  137. for (char c : value) {
  138. switch (c) {
  139. case '<':
  140. result += "&lt;";
  141. break;
  142. case '>':
  143. result += "&gt;";
  144. break;
  145. case '&':
  146. result += "&amp;";
  147. break;
  148. case '"':
  149. result += "&quot;";
  150. break;
  151. default:
  152. result += c;
  153. break;
  154. }
  155. }
  156. return result;
  157. }