cmWIXSourceWriter.cxx 4.2 KB

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