2012-10-03 18:08:49 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2012 Kitware, Inc.
|
|
|
|
|
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
|
|
|
|
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
|
|
|
|
|
|
|
#include "cmWIXSourceWriter.h"
|
|
|
|
|
|
|
|
#include <CPack/cmCPackGenerator.h>
|
|
|
|
|
2016-09-06 13:11:00 +03:00
|
|
|
#include <cmUuid.h>
|
|
|
|
|
2012-10-03 18:08:49 +04:00
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
cmWIXSourceWriter::cmWIXSourceWriter(cmCPackLog* logger,
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string const& filename,
|
2016-09-06 13:11:00 +03:00
|
|
|
GuidType componentGuidType,
|
|
|
|
RootElementType rootElementType)
|
2016-05-16 17:34:04 +03:00
|
|
|
: Logger(logger)
|
|
|
|
, File(filename.c_str())
|
|
|
|
, State(DEFAULT)
|
|
|
|
, SourceFilename(filename)
|
2016-09-06 13:11:00 +03:00
|
|
|
, ComponentGuidType(componentGuidType)
|
2012-10-03 18:08:49 +04:00
|
|
|
{
|
|
|
|
WriteXMLDeclaration();
|
|
|
|
|
2016-09-06 13:11:00 +03:00
|
|
|
if (rootElementType == INCLUDE_ELEMENT_ROOT) {
|
2012-10-03 18:08:49 +04:00
|
|
|
BeginElement("Include");
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2012-10-03 18:08:49 +04:00
|
|
|
BeginElement("Wix");
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-10-03 18:08:49 +04:00
|
|
|
|
|
|
|
AddAttribute("xmlns", "http://schemas.microsoft.com/wix/2006/wi");
|
|
|
|
}
|
|
|
|
|
|
|
|
cmWIXSourceWriter::~cmWIXSourceWriter()
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (Elements.size() > 1) {
|
|
|
|
cmCPackLogger(cmCPackLog::LOG_ERROR, Elements.size() - 1
|
|
|
|
<< " WiX elements were still open when closing '"
|
|
|
|
<< SourceFilename << "'" << std::endl);
|
2013-10-23 14:14:39 +04:00
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-10-23 14:14:39 +04:00
|
|
|
|
2013-12-20 21:12:01 +04:00
|
|
|
EndElement(Elements.back());
|
2012-10-03 18:08:49 +04:00
|
|
|
}
|
|
|
|
|
2014-02-25 02:21:12 +04:00
|
|
|
void cmWIXSourceWriter::BeginElement(std::string const& name)
|
2012-10-03 18:08:49 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (State == BEGIN) {
|
2013-12-20 21:12:01 +04:00
|
|
|
File << ">";
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-10-03 18:08:49 +04:00
|
|
|
|
2013-12-20 21:12:01 +04:00
|
|
|
File << "\n";
|
|
|
|
Indent(Elements.size());
|
|
|
|
File << "<" << name;
|
2012-10-03 18:08:49 +04:00
|
|
|
|
2013-12-20 21:12:01 +04:00
|
|
|
Elements.push_back(name);
|
|
|
|
State = BEGIN;
|
2012-10-03 18:08:49 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 14:14:39 +04:00
|
|
|
void cmWIXSourceWriter::EndElement(std::string const& name)
|
2012-10-03 18:08:49 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (Elements.empty()) {
|
2012-10-03 18:08:49 +04:00
|
|
|
cmCPackLogger(cmCPackLog::LOG_ERROR,
|
2016-05-16 17:34:04 +03:00
|
|
|
"can not end WiX element with no open elements in '"
|
|
|
|
<< SourceFilename << "'" << std::endl);
|
2013-10-23 14:14:39 +04:00
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-10-23 14:14:39 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (Elements.back() != name) {
|
|
|
|
cmCPackLogger(cmCPackLog::LOG_ERROR, "WiX element <"
|
|
|
|
<< Elements.back() << "> can not be closed by </" << name
|
|
|
|
<< "> in '" << SourceFilename << "'" << std::endl);
|
2012-10-03 18:08:49 +04:00
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-10-03 18:08:49 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (State == DEFAULT) {
|
2013-12-20 21:12:01 +04:00
|
|
|
File << "\n";
|
2016-05-16 17:34:04 +03:00
|
|
|
Indent(Elements.size() - 1);
|
2013-12-20 21:12:01 +04:00
|
|
|
File << "</" << Elements.back() << ">";
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2013-12-20 21:12:01 +04:00
|
|
|
File << "/>";
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-10-03 18:08:49 +04:00
|
|
|
|
2013-12-20 21:12:01 +04:00
|
|
|
Elements.pop_back();
|
|
|
|
State = DEFAULT;
|
2012-10-03 18:08:49 +04:00
|
|
|
}
|
|
|
|
|
2015-10-12 22:53:08 +03:00
|
|
|
void cmWIXSourceWriter::AddTextNode(std::string const& text)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (State == BEGIN) {
|
2015-10-12 22:53:08 +03:00
|
|
|
File << ">";
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-10-12 22:53:08 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (Elements.empty()) {
|
2015-10-12 22:53:08 +03:00
|
|
|
cmCPackLogger(cmCPackLog::LOG_ERROR,
|
2016-05-16 17:34:04 +03:00
|
|
|
"can not add text without open WiX element in '"
|
|
|
|
<< SourceFilename << "'" << std::endl);
|
2015-10-12 22:53:08 +03:00
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-10-12 22:53:08 +03:00
|
|
|
|
|
|
|
File << this->EscapeAttributeValue(text);
|
|
|
|
State = DEFAULT;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
void cmWIXSourceWriter::AddProcessingInstruction(std::string const& target,
|
|
|
|
std::string const& content)
|
2012-10-03 18:08:49 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (State == BEGIN) {
|
2013-12-20 21:12:01 +04:00
|
|
|
File << ">";
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-10-03 18:08:49 +04:00
|
|
|
|
2013-12-20 21:12:01 +04:00
|
|
|
File << "\n";
|
|
|
|
Indent(Elements.size());
|
|
|
|
File << "<?" << target << " " << content << "?>";
|
2012-10-03 18:08:49 +04:00
|
|
|
|
2013-12-20 21:12:01 +04:00
|
|
|
State = DEFAULT;
|
2012-10-03 18:08:49 +04:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
void cmWIXSourceWriter::AddAttribute(std::string const& key,
|
|
|
|
std::string const& value)
|
2012-10-03 18:08:49 +04:00
|
|
|
{
|
2015-03-27 14:22:15 +03:00
|
|
|
std::string utf8 = CMakeEncodingToUtf8(value);
|
2012-10-03 18:08:49 +04:00
|
|
|
|
2013-12-20 21:12:01 +04:00
|
|
|
File << " " << key << "=\"" << EscapeAttributeValue(utf8) << '"';
|
2012-10-03 18:08:49 +04:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
void cmWIXSourceWriter::AddAttributeUnlessEmpty(std::string const& key,
|
|
|
|
std::string const& value)
|
2013-11-20 00:38:09 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!value.empty()) {
|
2013-11-20 00:38:09 +04:00
|
|
|
AddAttribute(key, value);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-11-20 00:38:09 +04:00
|
|
|
}
|
|
|
|
|
2015-03-27 14:22:15 +03:00
|
|
|
std::string cmWIXSourceWriter::CMakeEncodingToUtf8(std::string const& value)
|
2012-10-03 18:08:49 +04:00
|
|
|
{
|
2015-03-27 14:22:15 +03:00
|
|
|
#ifdef CMAKE_ENCODING_UTF8
|
|
|
|
return value;
|
|
|
|
#else
|
2016-05-16 17:34:04 +03:00
|
|
|
if (value.empty()) {
|
2012-10-03 18:08:49 +04:00
|
|
|
return std::string();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-10-03 18:08:49 +04:00
|
|
|
|
|
|
|
int characterCount = MultiByteToWideChar(
|
2012-12-03 22:35:29 +04:00
|
|
|
CP_ACP, 0, value.c_str(), static_cast<int>(value.size()), 0, 0);
|
2012-10-03 18:08:49 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (characterCount == 0) {
|
2012-10-03 18:08:49 +04:00
|
|
|
return std::string();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-10-03 18:08:49 +04:00
|
|
|
|
|
|
|
std::vector<wchar_t> utf16(characterCount);
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
MultiByteToWideChar(CP_ACP, 0, value.c_str(), static_cast<int>(value.size()),
|
|
|
|
&utf16[0], static_cast<int>(utf16.size()));
|
2012-10-03 18:08:49 +04:00
|
|
|
|
2012-12-03 22:35:29 +04:00
|
|
|
int utf8ByteCount = WideCharToMultiByte(
|
|
|
|
CP_UTF8, 0, &utf16[0], static_cast<int>(utf16.size()), 0, 0, 0, 0);
|
2012-10-03 18:08:49 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (utf8ByteCount == 0) {
|
2012-10-03 18:08:49 +04:00
|
|
|
return std::string();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-10-03 18:08:49 +04:00
|
|
|
|
|
|
|
std::vector<char> utf8(utf8ByteCount);
|
|
|
|
|
2012-12-03 22:35:29 +04:00
|
|
|
WideCharToMultiByte(CP_UTF8, 0, &utf16[0], static_cast<int>(utf16.size()),
|
2016-05-16 17:34:04 +03:00
|
|
|
&utf8[0], static_cast<int>(utf8.size()), 0, 0);
|
2012-10-03 18:08:49 +04:00
|
|
|
|
|
|
|
return std::string(&utf8[0], utf8.size());
|
2015-03-27 14:22:15 +03:00
|
|
|
#endif
|
2012-10-03 18:08:49 +04:00
|
|
|
}
|
|
|
|
|
2016-09-06 13:11:00 +03:00
|
|
|
std::string cmWIXSourceWriter::CreateGuidFromComponentId(
|
|
|
|
std::string const& componentId)
|
|
|
|
{
|
|
|
|
std::string guid = "*";
|
|
|
|
if (this->ComponentGuidType == CMAKE_GENERATED_GUID) {
|
|
|
|
std::string md5 = cmSystemTools::ComputeStringMD5(componentId);
|
|
|
|
cmUuid uuid;
|
|
|
|
std::vector<unsigned char> ns;
|
|
|
|
guid = uuid.FromMd5(ns, md5);
|
|
|
|
}
|
|
|
|
return guid;
|
|
|
|
}
|
|
|
|
|
2012-10-03 18:08:49 +04:00
|
|
|
void cmWIXSourceWriter::WriteXMLDeclaration()
|
|
|
|
{
|
2013-12-20 21:12:01 +04:00
|
|
|
File << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
|
2012-10-03 18:08:49 +04:00
|
|
|
}
|
|
|
|
|
2012-12-04 23:37:41 +04:00
|
|
|
void cmWIXSourceWriter::Indent(size_t count)
|
2012-10-03 18:08:49 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
for (size_t i = 0; i < count; ++i) {
|
2013-12-20 21:12:01 +04:00
|
|
|
File << " ";
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-10-03 18:08:49 +04:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string cmWIXSourceWriter::EscapeAttributeValue(std::string const& value)
|
2012-10-03 18:08:49 +04:00
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
result.reserve(value.size());
|
|
|
|
|
|
|
|
char c = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (size_t i = 0; i < value.size(); ++i) {
|
2012-10-03 18:08:49 +04:00
|
|
|
c = value[i];
|
2016-05-16 17:34:04 +03:00
|
|
|
switch (c) {
|
|
|
|
case '<':
|
|
|
|
result += "<";
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
result += ">";
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
result += "&";
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
result += """;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result += c;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-10-03 18:08:49 +04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|