1
/**************************************************************************
3
* Copyright (c) 2021-2023 FreeCAD Project Association *
5
* This file is part of FreeCAD. *
7
* FreeCAD is free software: you can redistribute it and/or modify it *
8
* under the terms of the GNU Lesser General Public License as *
9
* published by the Free Software Foundation, either version 2.1 of the *
10
* License, or (at your option) any later version. *
12
* FreeCAD is distributed in the hope that it will be useful, but *
13
* WITHOUT ANY WARRANTY; without even the implied warranty of *
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15
* Lesser General Public License for more details. *
17
* You should have received a copy of the GNU Lesser General Public *
18
* License along with FreeCAD. If not, see *
19
* <https://www.gnu.org/licenses/>. *
21
***************************************************************************/
23
#include "PreCompiled.h"
26
# include <boost/core/ignore_unused.hpp>
31
#include <xercesc/framework/LocalFileFormatTarget.hpp>
32
#include <xercesc/framework/LocalFileInputSource.hpp>
33
#include <xercesc/framework/MemBufInputSource.hpp>
34
#include <xercesc/sax/HandlerBase.hpp>
36
#include "App/Application.h"
37
#include "App/Expression.h"
38
#include "Base/XMLTools.h"
45
In the GNU C Library, "major" and "minor" are defined
46
by <sys/sysmacros.h>. For historical compatibility, it is
47
currently defined by <sys/types.h> as well, but we plan to
48
remove this soon. To use "major", include <sys/sysmacros.h>
49
directly. If you did not intend to use a system-defined macro
50
"major", you should undefine it after including <sys/types.h>.
60
namespace fs = boost::filesystem;
61
#ifndef XERCES_CPP_NAMESPACE_BEGIN
62
#define XERCES_CPP_NAMESPACE_QUALIFIER
63
using namespace XERCES_CPP_NAMESPACE;
65
XERCES_CPP_NAMESPACE_USE
68
namespace MetadataInternal
70
class XMLErrorHandler: public HandlerBase
72
void warning(const SAXParseException& toCatch) override
74
// Don't deal with warnings at all
75
boost::ignore_unused(toCatch);
78
void error(const SAXParseException& toCatch) override
80
std::stringstream message;
81
message << "Error at file \"" << StrX(toCatch.getSystemId()) << "\", line "
82
<< toCatch.getLineNumber() << ", column " << toCatch.getColumnNumber()
83
<< "\n Message: " << StrX(toCatch.getMessage()) << std::endl;
84
throw Base::XMLBaseException(message.str());
87
void fatalError(const SAXParseException& toCatch) override
89
std::stringstream message;
90
message << "Fatal error at file \"" << StrX(toCatch.getSystemId()) << "\", line "
91
<< toCatch.getLineNumber() << ", column " << toCatch.getColumnNumber()
92
<< "\n Message: " << StrX(toCatch.getMessage()) << std::endl;
93
throw Base::XMLBaseException(message.str());
96
}// namespace MetadataInternal
98
Metadata::Metadata(const fs::path& metadataFile)
101
#if defined(FC_OS_WIN32)
103
LocalFileInputSource(reinterpret_cast<const XMLCh*>(metadataFile.wstring().c_str()));
105
auto source = LocalFileInputSource(XUTF8Str(metadataFile.string().c_str()).unicodeForm());
107
loadFromInputSource(source);
114
Metadata::Metadata(const DOMNode* domNode, int format)
117
auto element = dynamic_cast<const DOMElement*>(domNode);
121
parseVersion1(element);
124
throw Base::XMLBaseException(
125
"package.xml format version is not supported by this version of FreeCAD");
130
App::Metadata::Metadata(const std::string& rawData)
133
MemBufInputSource buffer(
134
reinterpret_cast<const XMLByte*>(rawData.c_str()),
136
"raw data (in memory)");
137
loadFromInputSource(buffer);
140
void Metadata::loadFromInputSource(const InputSource& source)
142
// Any exception thrown by the XML code propagates out and prevents object creation
143
XMLPlatformUtils::Initialize();
145
_parser = std::make_shared<XercesDOMParser>();
146
_parser->setValidationScheme(XercesDOMParser::Val_Never);
147
_parser->setDoNamespaces(true);
149
auto errHandler = std::make_unique<MetadataInternal::XMLErrorHandler>();
150
_parser->setErrorHandler(errHandler.get());
152
_parser->parse(source);
154
auto doc = _parser->getDocument();
155
_dom = doc->getDocumentElement();
157
auto rootTagName = StrXUTF8(_dom->getTagName()).str;
158
if (rootTagName != "package") {
159
throw Base::XMLBaseException(
160
"Malformed package.xml document: Root <package> group not found");
162
auto formatVersion = XMLString::parseInt(_dom->getAttribute(XUTF8Str("format").unicodeForm()));
163
switch (formatVersion) {
168
throw Base::XMLBaseException(
169
"package.xml format version is not supported by this version of FreeCAD");
173
Metadata::~Metadata() = default;
175
std::string Metadata::name() const
180
std::string Metadata::type() const
185
Meta::Version Metadata::version() const
190
std::string App::Metadata::date() const
195
std::string Metadata::description() const
200
std::vector<Meta::Contact> Metadata::maintainer() const
205
std::vector<Meta::License> Metadata::license() const
210
std::vector<Meta::Url> Metadata::url() const
215
std::vector<Meta::Contact> Metadata::author() const
220
std::vector<Meta::Dependency> Metadata::depend() const
225
std::vector<Meta::Dependency> Metadata::conflict() const
230
std::vector<Meta::Dependency> Metadata::replace() const
235
std::vector<std::string> Metadata::tag() const
240
fs::path Metadata::icon() const
245
std::string Metadata::classname() const
250
boost::filesystem::path Metadata::subdirectory() const
252
return _subdirectory;
255
std::vector<fs::path> Metadata::file() const
260
Meta::Version Metadata::freecadmin() const
265
Meta::Version Metadata::freecadmax() const
270
Meta::Version Metadata::pythonmin() const
275
std::multimap<std::string, Metadata> Metadata::content() const
280
std::vector<Meta::GenericMetadata> Metadata::operator[](const std::string& tag) const
282
std::vector<Meta::GenericMetadata> returnValue;
283
auto range = _genericMetadata.equal_range(tag);
284
for (auto item = range.first; item != range.second; ++item) {
285
returnValue.push_back(item->second);
290
XERCES_CPP_NAMESPACE::DOMElement* Metadata::dom() const
295
void Metadata::setName(const std::string& name)
297
std::string invalidCharacters = "/\\?%*:|\"<>";// Should cover all OSes
298
if (_name.find_first_of(invalidCharacters) != std::string::npos) {
299
throw Base::RuntimeError("Name cannot contain any of: " + invalidCharacters);
304
void Metadata::setType(const std::string& type)
309
void Metadata::setVersion(const Meta::Version& version)
314
void App::Metadata::setDate(const std::string& date)
319
void Metadata::setDescription(const std::string& description)
321
_description = description;
324
void Metadata::addMaintainer(const Meta::Contact& maintainer)
326
_maintainer.push_back(maintainer);
329
void Metadata::addLicense(const Meta::License& license)
331
_license.push_back(license);
334
void Metadata::addUrl(const Meta::Url& url)
339
void Metadata::addAuthor(const Meta::Contact& author)
341
_author.push_back(author);
344
void Metadata::addDepend(const Meta::Dependency& dep)
346
_depend.push_back(dep);
349
void Metadata::addConflict(const Meta::Dependency& dep)
351
_conflict.push_back(dep);
354
void Metadata::addReplace(const Meta::Dependency& dep)
356
_replace.push_back(dep);
359
void Metadata::addTag(const std::string& tag)
364
void Metadata::setIcon(const fs::path& path)
369
void Metadata::setClassname(const std::string& name)
374
void Metadata::setSubdirectory(const boost::filesystem::path& path)
376
_subdirectory = path;
379
void Metadata::addFile(const fs::path& path)
381
_file.push_back(path);
384
void Metadata::addContentItem(const std::string& tag, const Metadata& item)
386
_content.insert(std::make_pair(tag, item));
389
void Metadata::setFreeCADMin(const Meta::Version& version)
391
_freecadmin = version;
394
void Metadata::setPythonMin(const Meta::Version& version)
396
_pythonmin = version;
399
void Metadata::setFreeCADMax(const Meta::Version& version)
401
_freecadmax = version;
404
void Metadata::addGenericMetadata(const std::string& tag,
405
const Meta::GenericMetadata& genericMetadata)
407
_genericMetadata.insert(std::make_pair(tag, genericMetadata));
410
void Metadata::removeContentItem(const std::string& tag, const std::string& itemName)
412
auto tagRange = _content.equal_range(tag);
414
std::find_if(tagRange.first, tagRange.second, [&itemName](const auto& check) -> bool {
415
return itemName == check.second.name();
417
if (foundItem != tagRange.second) {
418
_content.erase(foundItem);
422
void Metadata::removeMaintainer(const Meta::Contact& maintainer)
424
auto new_end = std::remove(_maintainer.begin(), _maintainer.end(), maintainer);
425
_maintainer.erase(new_end, _maintainer.end());
428
void Metadata::removeLicense(const Meta::License& license)
430
auto new_end = std::remove(_license.begin(), _license.end(), license);
431
_license.erase(new_end, _license.end());
434
void Metadata::removeUrl(const Meta::Url& url)
436
auto new_end = std::remove(_url.begin(), _url.end(), url);
437
_url.erase(new_end, _url.end());
440
void Metadata::removeAuthor(const Meta::Contact& author)
442
auto new_end = std::remove(_author.begin(), _author.end(), author);
443
_author.erase(new_end, _author.end());
446
void Metadata::removeDepend(const Meta::Dependency& dep)
449
for (const auto& check : _depend) {
455
throw Base::RuntimeError("No match found for dependency to remove");
457
auto new_end = std::remove(_depend.begin(), _depend.end(), dep);
458
_depend.erase(new_end, _depend.end());
461
void Metadata::removeConflict(const Meta::Dependency& dep)
463
auto new_end = std::remove(_conflict.begin(), _conflict.end(), dep);
464
_conflict.erase(new_end, _conflict.end());
467
void Metadata::removeReplace(const Meta::Dependency& dep)
469
auto new_end = std::remove(_replace.begin(), _replace.end(), dep);
470
_replace.erase(new_end, _replace.end());
473
void Metadata::removeTag(const std::string& tag)
475
auto new_end = std::remove(_tag.begin(), _tag.end(), tag);
476
_tag.erase(new_end, _tag.end());
479
void Metadata::removeFile(const boost::filesystem::path& path)
481
auto new_end = std::remove(_file.begin(), _file.end(), path);
482
_file.erase(new_end, _file.end());
486
void Metadata::clearContent()
491
void Metadata::clearMaintainer()
496
void Metadata::clearLicense()
501
void Metadata::clearUrl()
506
void Metadata::clearAuthor()
511
void Metadata::clearDepend()
516
void Metadata::clearConflict()
521
void Metadata::clearReplace()
526
void Metadata::clearTag()
531
void Metadata::clearFile()
537
DOMElement* appendSimpleXMLNode(DOMElement* baseNode, const std::string& nodeName,
538
const std::string& nodeContents)
540
// For convenience (and brevity of final output) don't create nodes that don't have contents
541
if (nodeContents.empty()) {
544
auto doc = baseNode->getOwnerDocument();
545
DOMElement* namedElement = doc->createElement(XUTF8Str(nodeName.c_str()).unicodeForm());
546
baseNode->appendChild(namedElement);
547
DOMText* namedNode = doc->createTextNode(XUTF8Str(nodeContents.c_str()).unicodeForm());
548
namedElement->appendChild(namedNode);
552
void addAttribute(DOMElement* node, const std::string& key, const std::string& value)
557
node->setAttribute(XUTF8Str(key.c_str()).unicodeForm(), XUTF8Str(value.c_str()).unicodeForm());
560
void addAttribute(DOMElement* node, const std::string& key, bool value)
563
node->setAttribute(XUTF8Str(key.c_str()).unicodeForm(), XUTF8Str("True").unicodeForm());
566
node->setAttribute(XUTF8Str(key.c_str()).unicodeForm(), XUTF8Str("False").unicodeForm());
570
void addAttribute(DOMElement* node, const std::string& key, Meta::DependencyType value)
572
// Someday we should be able to change this to use reflection, but it's not yet
573
// available (using C++17)
574
std::string stringified("automatic");
576
case Meta::DependencyType::automatic:
577
stringified = "automatic";
579
case Meta::DependencyType::internal:
580
stringified = "internal";
582
case Meta::DependencyType::addon:
583
stringified = "addon";
585
case Meta::DependencyType::python:
586
stringified = "python";
589
node->setAttribute(XUTF8Str(key.c_str()).unicodeForm(),
590
XUTF8Str(stringified.c_str()).unicodeForm());
593
void addDependencyNode(DOMElement* root, const std::string& name, const Meta::Dependency& depend)
595
auto element = appendSimpleXMLNode(root, name, depend.package);
597
addAttribute(element, "version_lt", depend.version_lt);
598
addAttribute(element, "version_lte", depend.version_lte);
599
addAttribute(element, "version_eq", depend.version_eq);
600
addAttribute(element, "version_gte", depend.version_gte);
601
addAttribute(element, "version_gt", depend.version_gt);
602
addAttribute(element, "condition", depend.condition);
603
addAttribute(element, "optional", depend.optional);
604
addAttribute(element, "type", depend.dependencyType);
608
void Metadata::write(const fs::path& file) const
610
DOMImplementation* impl =
611
DOMImplementationRegistry::getDOMImplementation(XUTF8Str("Core LS").unicodeForm());
613
DOMDocument* doc = impl->createDocument(nullptr, XUTF8Str("package").unicodeForm(), nullptr);
614
DOMElement* root = doc->getDocumentElement();
615
root->setAttribute(XUTF8Str("format").unicodeForm(), XUTF8Str("1").unicodeForm());
616
root->setAttribute(XUTF8Str("xmlns").unicodeForm(),
617
XUTF8Str("https://wiki.freecad.org/Package_Metadata").unicodeForm());
619
appendToElement(root);
621
DOMLSSerializer* theSerializer = ((DOMImplementationLS*)impl)->createLSSerializer();
622
DOMConfiguration* config = theSerializer->getDomConfig();
623
if (config->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true)) {
624
config->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
627
// set feature if the serializer supports the feature/mode
628
if (config->canSetParameter(XMLUni::fgDOMWRTSplitCdataSections, true)) {
629
config->setParameter(XMLUni::fgDOMWRTSplitCdataSections, true);
632
if (config->canSetParameter(XMLUni::fgDOMWRTDiscardDefaultContent, true)) {
633
config->setParameter(XMLUni::fgDOMWRTDiscardDefaultContent, true);
637
XMLFormatTarget* myFormTarget = new LocalFileFormatTarget(file.string().c_str());
638
DOMLSOutput* theOutput = ((DOMImplementationLS*)impl)->createLSOutput();
640
theOutput->setByteStream(myFormTarget);
641
theSerializer->write(doc, theOutput);
643
theOutput->release();
644
theSerializer->release();
647
catch (const XMLException& toCatch) {
648
char* message = XMLString::transcode(toCatch.getMessage());
649
std::string what = message;
650
XMLString::release(&message);
651
throw Base::XMLBaseException(what);
653
catch (const DOMException& toCatch) {
654
char* message = XMLString::transcode(toCatch.getMessage());
655
std::string what = message;
656
XMLString::release(&message);
657
throw Base::XMLBaseException(what);
663
bool Metadata::satisfies(const Meta::Dependency& dep)
665
if (dep.package != _name) {
668
// The "condition" attribute allows an expression to enable or disable this dependency check: it must contain a valid
669
// FreeCAD Expression. If it evaluates to false, this dependency is bypassed (e.g. this function returns false).
670
if (!dep.condition.empty()) {
671
auto injectedString = dep.condition;
672
std::map<std::string, std::string> replacements;
673
std::map<std::string, std::string>& config = App::Application::Config();
674
replacements.insert(std::make_pair("$BuildVersionMajor", config["BuildVersionMajor"]));
675
replacements.insert(std::make_pair("$BuildVersionMinor", config["BuildVersionMinor"]));
676
replacements.insert(std::make_pair("$BuildVersionMinor", config["BuildVersionPoint"]));
677
replacements.insert(std::make_pair("$BuildRevision", config["BuildRevision"]));
678
for (const auto& replacement : replacements) {
679
auto pos = injectedString.find(replacement.first);
680
while (pos != std::string::npos) {
681
injectedString.replace(pos, replacement.first.length(), replacement.second);
682
pos = injectedString.find(replacement.first);
685
auto parsedExpression = App::Expression::parse(nullptr, dep.condition);
686
auto result = parsedExpression->eval();
687
if (!boost::any_cast<bool>(result->getValueAsAny())) {
692
if (!dep.version_eq.empty()) {
693
return _version == Meta::Version(dep.version_eq);
695
// Any of the others might be specified in pairs, so only return the "false" case
697
if (!dep.version_lt.empty()) {
698
if (!(_version < Meta::Version(dep.version_lt))) {
702
if (!dep.version_lte.empty()) {
703
if (!(_version <= Meta::Version(dep.version_lt))) {
707
if (!dep.version_gt.empty()) {
708
if (!(_version > Meta::Version(dep.version_lt))) {
712
if (!dep.version_gte.empty()) {
713
if (!(_version >= Meta::Version(dep.version_lt))) {
720
bool Metadata::supportsCurrentFreeCAD() const
722
static auto fcVersion = Meta::Version();
723
if (fcVersion == Meta::Version()) {
724
std::map<std::string, std::string>& config = App::Application::Config();
725
std::stringstream ss;
726
ss << config["BuildVersionMajor"] << "." << config["BuildVersionMinor"] << "."
727
<< config["BuildVersionPoint"] << "."
728
<< (config["BuildRevision"].empty() ? "0" : config["BuildRevision"]);
729
fcVersion = Meta::Version(ss.str());
732
if (_freecadmin != Meta::Version() && _freecadmin > fcVersion) {
735
if (_freecadmax != Meta::Version() && _freecadmax < fcVersion) {
741
void Metadata::appendToElement(DOMElement* root) const
743
appendSimpleXMLNode(root, "name", _name);
744
appendSimpleXMLNode(root, "type", _type);
745
appendSimpleXMLNode(root, "description", _description);
746
if (_version != Meta::Version()) {
747
// Only append version if it's not 0.0.0
748
appendSimpleXMLNode(root, "version", _version.str());
751
if (!_date.empty()) {
752
appendSimpleXMLNode(root, "date", _date);
755
for (const auto& maintainer : _maintainer) {
756
auto element = appendSimpleXMLNode(root, "maintainer", maintainer.name);
758
addAttribute(element, "email", maintainer.email);
762
for (const auto& license : _license) {
763
auto element = appendSimpleXMLNode(root, "license", license.name);
765
addAttribute(element, "file", license.file.string());
769
if (_freecadmin != Meta::Version()) {
770
appendSimpleXMLNode(root, "freecadmin", _freecadmin.str());
773
if (_freecadmax != Meta::Version()) {
774
appendSimpleXMLNode(root, "freecadmax", _freecadmax.str());
777
if (_pythonmin != Meta::Version()) {
778
appendSimpleXMLNode(root, "pythonmin", _pythonmin.str());
781
for (const auto& url : _url) {
782
auto element = appendSimpleXMLNode(root, "url", url.location);
784
std::string typeAsString("website");
786
case Meta::UrlType::website:
787
typeAsString = "website";
789
case Meta::UrlType::repository:
790
typeAsString = "repository";
792
case Meta::UrlType::bugtracker:
793
typeAsString = "bugtracker";
795
case Meta::UrlType::readme:
796
typeAsString = "readme";
798
case Meta::UrlType::documentation:
799
typeAsString = "documentation";
801
case Meta::UrlType::discussion:
802
typeAsString = "discussion";
805
addAttribute(element, "type", typeAsString);
806
if (url.type == Meta::UrlType::repository) {
807
addAttribute(element, "branch", url.branch);
812
for (const auto& author : _author) {
813
auto element = appendSimpleXMLNode(root, "author", author.name);
815
addAttribute(element, "email", author.email);
819
for (const auto& depend : _depend) {
820
addDependencyNode(root, "depend", depend);
823
for (const auto& conflict : _conflict) {
824
addDependencyNode(root, "conflict", conflict);
827
for (const auto& replace : _replace) {
828
addDependencyNode(root, "replace", replace);
831
for (const auto& tag : _tag) {
832
appendSimpleXMLNode(root, "tag", tag);
835
appendSimpleXMLNode(root, "icon", _icon.string());
837
appendSimpleXMLNode(root, "classname", _classname);
839
appendSimpleXMLNode(root, "subdirectory", _subdirectory.string());
841
for (const auto& file : _file) {
842
appendSimpleXMLNode(root, "file", file.string());
845
for (const auto& md : _genericMetadata) {
846
auto element = appendSimpleXMLNode(root, md.first, md.second.contents);
847
for (const auto& attr : md.second.attributes) {
848
addAttribute(element, attr.first, attr.second);
852
if (!_content.empty()) {
853
auto doc = root->getOwnerDocument();
854
DOMElement* contentRootElement = doc->createElement(XUTF8Str("content").unicodeForm());
855
root->appendChild(contentRootElement);
856
for (const auto& content : _content) {
857
DOMElement* contentElement =
858
doc->createElement(XUTF8Str(content.first.c_str()).unicodeForm());
859
contentRootElement->appendChild(contentElement);
860
content.second.appendToElement(contentElement);
866
void Metadata::parseVersion1(const DOMNode* startNode)
868
auto children = startNode->getChildNodes();
870
for (XMLSize_t i = 0; i < children->getLength(); ++i) {
871
auto child = children->item(i);
872
auto element = dynamic_cast<const DOMElement*>(child);
877
auto tag = element->getNodeName();
878
auto tagString = StrXUTF8(tag).str;
880
if (tagString == "name") {
881
_name = StrXUTF8(element->getTextContent()).str;
883
else if (tagString == "type") {
884
_type = StrXUTF8(element->getTextContent()).str;
886
else if (tagString == "version") {
887
_version = Meta::Version(StrXUTF8(element->getTextContent()).str);
889
else if (tagString == "date") {
890
_date = StrXUTF8(element->getTextContent()).str;
892
else if (tagString == "description") {
893
_description = StrXUTF8(element->getTextContent()).str;
895
else if (tagString == "maintainer") {
896
_maintainer.emplace_back(element);
898
else if (tagString == "license") {
899
_license.emplace_back(element);
901
else if (tagString == "freecadmin") {
902
_freecadmin = Meta::Version(StrXUTF8(element->getTextContent()).str);
904
else if (tagString == "freecadmax") {
905
_freecadmax = Meta::Version(StrXUTF8(element->getTextContent()).str);
907
else if (tagString == "pythonmin") {
908
_pythonmin = Meta::Version(StrXUTF8(element->getTextContent()).str);
910
else if (tagString == "url") {
911
_url.emplace_back(element);
913
else if (tagString == "author") {
914
_author.emplace_back(element);
916
else if (tagString == "depend") {
917
_depend.emplace_back(element);
919
else if (tagString == "conflict") {
920
_conflict.emplace_back(element);
922
else if (tagString == "replace") {
923
_replace.emplace_back(element);
925
else if (tagString == "tag") {
926
_tag.emplace_back(StrXUTF8(element->getTextContent()).str);
928
else if (tagString == "file") {
929
_file.emplace_back(StrXUTF8(element->getTextContent()).str);
931
else if (tagString == "classname") {
932
_classname = StrXUTF8(element->getTextContent()).str;
934
else if (tagString == "subdirectory") {
935
_subdirectory = StrXUTF8(element->getTextContent()).str;
937
else if (tagString == "icon") {
938
_icon = fs::path(StrXUTF8(element->getTextContent()).str);
940
else if (tagString == "content") {
941
parseContentNodeVersion1(element);// Recursive call
944
// If none of this node's nodeChildren have nodeChildren of their own, it is a simple element and we
945
// can handle it as a GenericMetadata object
946
auto nodeChildren = element->getChildNodes();
947
bool hasGrandchildren = false;
948
for (XMLSize_t j = 0; j < nodeChildren->getLength() && !hasGrandchildren; ++j) {
949
if (nodeChildren->item(j)->getChildNodes()->getLength() > 0) {
950
hasGrandchildren = true;
953
if (!hasGrandchildren) {
954
_genericMetadata.insert(std::make_pair(tagString, Meta::GenericMetadata(element)));
960
void Metadata::parseContentNodeVersion1(const DOMElement* contentNode)
962
auto children = contentNode->getChildNodes();
963
for (XMLSize_t i = 0; i < children->getLength(); ++i) {
964
auto child = dynamic_cast<const DOMElement*>(children->item(i));
966
auto tag = StrXUTF8(child->getTagName()).str;
967
_content.insert(std::make_pair(tag, Metadata(child, 1)));
972
Meta::Contact::Contact(std::string name, std::string email)
973
: name(std::move(name)),
974
email(std::move(email))
976
// This has to be provided manually since we have another constructor
979
Meta::Contact::Contact(const XERCES_CPP_NAMESPACE::DOMElement* elem)
984
auto emailAttribute = elem->getAttribute(XUTF8Str("email").unicodeForm());
985
name = StrXUTF8(elem->getTextContent()).str;
986
email = StrXUTF8(emailAttribute).str;
989
bool App::Meta::Contact::operator==(const Contact& rhs) const
991
return name == rhs.name && email == rhs.email;
994
Meta::License::License(std::string name, fs::path file)
995
: name(std::move(name)),
996
file(std::move(file))
998
// This has to be provided manually since we have another constructor
1001
Meta::License::License(const XERCES_CPP_NAMESPACE::DOMElement* elem)
1006
auto fileAttribute = elem->getAttribute(XUTF8Str("file").unicodeForm());
1007
if (XMLString::stringLen(fileAttribute) > 0) {
1008
file = fs::path(StrXUTF8(fileAttribute).str);
1010
name = StrXUTF8(elem->getTextContent()).str;
1013
bool App::Meta::License::operator==(const License& rhs) const
1015
return name == rhs.name && file == rhs.file;
1018
App::Meta::Url::Url()
1020
type(App::Meta::UrlType::website)
1023
Meta::Url::Url(std::string location, UrlType type)
1024
: location(std::move(location)),
1027
// This has to be provided manually since we have another constructor
1030
Meta::Url::Url(const XERCES_CPP_NAMESPACE::DOMElement* elem)
1035
auto typeAttribute = StrXUTF8(elem->getAttribute(XUTF8Str("type").unicodeForm())).str;
1036
if (typeAttribute.empty() || typeAttribute == "website") {
1037
type = UrlType::website;
1039
else if (typeAttribute == "bugtracker") {
1040
type = UrlType::bugtracker;
1042
else if (typeAttribute == "repository") {
1043
type = UrlType::repository;
1045
else if (typeAttribute == "readme") {
1046
type = UrlType::readme;
1048
else if (typeAttribute == "documentation") {
1049
type = UrlType::documentation;
1051
else if (typeAttribute == "discussion") {
1052
type = UrlType::discussion;
1055
type = UrlType::website;
1058
if (type == UrlType::repository) {
1059
branch = StrXUTF8(elem->getAttribute(XUTF8Str("branch").unicodeForm())).str;
1061
location = StrXUTF8(elem->getTextContent()).str;
1064
bool App::Meta::Url::operator==(const Url& rhs) const
1066
if (type == UrlType::repository && branch != rhs.branch) {
1069
return type == rhs.type && location == rhs.location;
1072
App::Meta::Dependency::Dependency()
1074
dependencyType(App::Meta::DependencyType::automatic)
1077
App::Meta::Dependency::Dependency(std::string pkg)
1078
: package(std::move(pkg)),
1080
dependencyType(App::Meta::DependencyType::automatic)
1083
Meta::Dependency::Dependency(const XERCES_CPP_NAMESPACE::DOMElement* elem)
1085
version_lt = StrXUTF8(elem->getAttribute(XUTF8Str("version_lt").unicodeForm())).str;
1086
version_lte = StrXUTF8(elem->getAttribute(XUTF8Str("version_lte").unicodeForm())).str;
1087
version_eq = StrXUTF8(elem->getAttribute(XUTF8Str("version_eq").unicodeForm())).str;
1088
version_gte = StrXUTF8(elem->getAttribute(XUTF8Str("version_gte").unicodeForm())).str;
1089
version_gt = StrXUTF8(elem->getAttribute(XUTF8Str("version_gt").unicodeForm())).str;
1090
condition = StrXUTF8(elem->getAttribute(XUTF8Str("condition").unicodeForm())).str;
1091
std::string opt_string = StrXUTF8(elem->getAttribute(XUTF8Str("optional").unicodeForm())).str;
1092
if (opt_string == "true"
1093
|| opt_string == "True") {// Support Python capitalization in this one case...
1099
std::string type_string = StrXUTF8(elem->getAttribute(XUTF8Str("type").unicodeForm())).str;
1100
if (type_string == "automatic" || type_string.empty()) {
1101
dependencyType = Meta::DependencyType::automatic;
1103
else if (type_string == "addon") {
1104
dependencyType = Meta::DependencyType::addon;
1106
else if (type_string == "internal") {
1107
dependencyType = Meta::DependencyType::internal;
1109
else if (type_string == "python") {
1110
dependencyType = Meta::DependencyType::python;
1113
auto message = std::string("Invalid dependency type \"") + type_string + "\"";
1114
throw Base::XMLBaseException(message);
1117
package = StrXUTF8(elem->getTextContent()).str;
1120
bool App::Meta::Dependency::operator==(const Dependency& rhs) const
1122
return package == rhs.package && version_lt == rhs.version_lt && version_lte == rhs.version_lte
1123
&& version_eq == rhs.version_eq && version_gte == rhs.version_gte
1124
&& version_gt == rhs.version_gt && condition == rhs.condition && optional == rhs.optional
1125
&& dependencyType == rhs.dependencyType;
1128
Meta::Version::Version() = default;
1130
Meta::Version::Version(int major, int minor, int patch, std::string suffix)
1134
suffix(std::move(suffix))
1137
Meta::Version::Version(const std::string& versionString)
1139
std::istringstream stream(versionString);
1140
char separator {'.'};
1143
stream >> separator;
1149
stream >> separator;
1159
std::string Meta::Version::str() const
1161
if (*this == Meta::Version()) {
1164
std::ostringstream stream;
1165
stream << major << "." << minor << "." << patch << suffix;
1166
return stream.str();
1169
bool Meta::Version::operator<(const Version& rhs) const
1171
return std::tie(major, minor, patch, suffix)
1172
< std::tie(rhs.major, rhs.minor, rhs.patch, rhs.suffix);
1175
bool Meta::Version::operator>(const Version& rhs) const
1177
return std::tie(major, minor, patch, suffix)
1178
> std::tie(rhs.major, rhs.minor, rhs.patch, rhs.suffix);
1181
bool Meta::Version::operator<=(const Version& rhs) const
1183
return std::tie(major, minor, patch, suffix)
1184
<= std::tie(rhs.major, rhs.minor, rhs.patch, rhs.suffix);
1187
bool Meta::Version::operator>=(const Version& rhs) const
1189
return std::tie(major, minor, patch, suffix)
1190
>= std::tie(rhs.major, rhs.minor, rhs.patch, rhs.suffix);
1193
bool Meta::Version::operator==(const Version& rhs) const
1195
return std::tie(major, minor, patch, suffix)
1196
== std::tie(rhs.major, rhs.minor, rhs.patch, rhs.suffix);
1199
bool Meta::Version::operator!=(const Version& rhs) const
1201
return std::tie(major, minor, patch, suffix)
1202
!= std::tie(rhs.major, rhs.minor, rhs.patch, rhs.suffix);
1205
Meta::GenericMetadata::GenericMetadata(const XERCES_CPP_NAMESPACE::DOMElement* elem)
1207
contents = StrXUTF8(elem->getTextContent()).str;
1208
for (XMLSize_t i = 0; i < elem->getAttributes()->getLength(); ++i) {
1209
auto attr = elem->getAttributes()->item(i);
1211
std::make_pair(StrXUTF8(attr->getNodeName()).str, StrXUTF8(attr->getTextContent()).str));
1215
App::Meta::GenericMetadata::GenericMetadata(std::string contents)
1216
: contents(std::move(contents))