Debian Codegraph @

src/App/Metadata.cpp

raw
1 /**************************************************************************
2 * *
3 * Copyright (c) 2021-2023 FreeCAD Project Association *
4 * *
5 * This file is part of FreeCAD. *
6 * *
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. *
11 * *
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. *
16 * *
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/>. *
20 * *
21 ***************************************************************************/
22
23 #include <boost/core/ignore_unused.hpp>
24 #include <memory>
25 #include <sstream>
26
27 #include <xercesc/framework/LocalFileFormatTarget.hpp>
28 #include <xercesc/framework/LocalFileInputSource.hpp>
29 #include <xercesc/framework/MemBufInputSource.hpp>
30 #include <xercesc/sax/HandlerBase.hpp>
31
32 #include <FCConfig.h>
33
34 #include "App/Application.h"
35 #include "App/Expression.h"
36 #include "Base/XMLTools.h"
37
38 #include "Metadata.h"
39
40
41 /*
42 *** From GCC: ***
43 In the GNU C Library, "major" and "minor" are defined
44 by <sys/sysmacros.h>. For historical compatibility, it is
45 currently defined by <sys/types.h> as well, but we plan to
46 remove this soon. To use "major", include <sys/sysmacros.h>
47 directly. If you did not intend to use a system-defined macro
48 "major", you should undefine it after including <sys/types.h>.
49 */
50 #ifdef major
51 #undef major
52 #endif
53 #ifdef minor
54 #undef minor
55 #endif
56
57 using namespace App;
58 namespace fs = std::filesystem;
59 #ifndef XERCES_CPP_NAMESPACE_BEGIN
60 #define XERCES_CPP_NAMESPACE_QUALIFIER
61 using namespace XERCES_CPP_NAMESPACE;
62 #else
63 XERCES_CPP_NAMESPACE_USE
64 #endif
65
66 namespace MetadataInternal
67 {
68 class XMLErrorHandler: public HandlerBase
69 {
70 void warning(const SAXParseException& toCatch) override
71 {
72 // Don't deal with warnings at all
73 boost::ignore_unused(toCatch);
74 }
75
76 void error(const SAXParseException& toCatch) override
77 {
78 std::stringstream message;
79 message << "Error at file \"" << StrX(toCatch.getSystemId()) << "\", line "
80 << toCatch.getLineNumber() << ", column " << toCatch.getColumnNumber()
81 << "\n Message: " << StrX(toCatch.getMessage()) << std::endl;
82 throw Base::XMLBaseException(message.str());
83 }
84
85 void fatalError(const SAXParseException& toCatch) override
86 {
87 std::stringstream message;
88 message << "Fatal error at file \"" << StrX(toCatch.getSystemId()) << "\", line "
89 << toCatch.getLineNumber() << ", column " << toCatch.getColumnNumber()
90 << "\n Message: " << StrX(toCatch.getMessage()) << std::endl;
91 throw Base::XMLBaseException(message.str());
92 }
93 };
94 } // namespace MetadataInternal
95
96 Metadata::Metadata(const fs::path& metadataFile)
97 : _dom(nullptr)
98 {
99 #if defined(FC_OS_WIN32)
100 auto source =
101 LocalFileInputSource(reinterpret_cast<const XMLCh*>(metadataFile.wstring().c_str()));
102 #else
103 auto source = LocalFileInputSource(XUTF8Str(metadataFile.string().c_str()).unicodeForm());
104 #endif
105 loadFromInputSource(source);
106 }
107
108 Metadata::Metadata()
109 : _dom(nullptr)
110 {}
111
112 Metadata::Metadata(const DOMNode* domNode, int format)
113 : _dom(nullptr)
114 {
115 auto element = dynamic_cast<const DOMElement*>(domNode);
116 if (element) {
117 switch (format) {
118 case 1:
119 parseVersion1(element);
120 break;
121 default:
122 throw Base::XMLBaseException(
123 "package.xml format version is not supported by this version of FreeCAD");
124 }
125 }
126 }
127
128 App::Metadata::Metadata(const std::string& rawData)
129 : _dom(nullptr)
130 {
131 MemBufInputSource buffer(reinterpret_cast<const XMLByte*>(rawData.c_str()),
132 rawData.size(),
133 "raw data (in memory)");
134 loadFromInputSource(buffer);
135 }
136
137 void Metadata::loadFromInputSource(const InputSource& source)
138 {
139 // Any exception thrown by the XML code propagates out and prevents object creation
140 XMLPlatformUtils::Initialize();
141
142 _parser = std::make_shared<XercesDOMParser>();
143 _parser->setValidationScheme(XercesDOMParser::Val_Never);
144 _parser->setDoNamespaces(true);
145
146 auto errHandler = std::make_unique<MetadataInternal::XMLErrorHandler>();
147 _parser->setErrorHandler(errHandler.get());
148
149 _parser->parse(source);
150
151 auto doc = _parser->getDocument();
152 _dom = doc->getDocumentElement();
153
154 auto rootTagName = StrXUTF8(_dom->getTagName()).str;
155 if (rootTagName != "package") {
156 throw Base::XMLBaseException(
157 "Malformed package.xml document: Root <package> group not found");
158 }
159 auto formatVersion = XMLString::parseInt(_dom->getAttribute(XUTF8StrLiteral("format").unicodeForm()));
160 switch (formatVersion) {
161 case 1:
162 parseVersion1(_dom);
163 break;
164 default:
165 throw Base::XMLBaseException(
166 "package.xml format version is not supported by this version of FreeCAD");
167 }
168 }
169
170 Metadata::~Metadata() = default;
171
172 std::string Metadata::name() const
173 {
174 return _name;
175 }
176
177 std::string Metadata::type() const
178 {
179 return _type;
180 }
181
182 Meta::Version Metadata::version() const
183 {
184 return _version;
185 }
186
187 std::string App::Metadata::date() const
188 {
189 return _date;
190 }
191
192 std::string Metadata::description() const
193 {
194 return _description;
195 }
196
197 std::vector<Meta::Contact> Metadata::maintainer() const
198 {
199 return _maintainer;
200 }
201
202 std::vector<Meta::License> Metadata::license() const
203 {
204 return _license;
205 }
206
207 std::vector<Meta::Url> Metadata::url() const
208 {
209 return _url;
210 }
211
212 std::vector<Meta::Contact> Metadata::author() const
213 {
214 return _author;
215 }
216
217 std::vector<Meta::Dependency> Metadata::depend() const
218 {
219 return _depend;
220 }
221
222 std::vector<Meta::Dependency> Metadata::conflict() const
223 {
224 return _conflict;
225 }
226
227 std::vector<Meta::Dependency> Metadata::replace() const
228 {
229 return _replace;
230 }
231
232 std::vector<std::string> Metadata::tag() const
233 {
234 return _tag;
235 }
236
237 fs::path Metadata::icon() const
238 {
239 return _icon;
240 }
241
242 std::string Metadata::classname() const
243 {
244 return _classname;
245 }
246
247 std::filesystem::path Metadata::subdirectory() const
248 {
249 return _subdirectory;
250 }
251
252 std::vector<fs::path> Metadata::file() const
253 {
254 return _file;
255 }
256
257 Meta::Version Metadata::freecadmin() const
258 {
259 return _freecadmin;
260 }
261
262 Meta::Version Metadata::freecadmax() const
263 {
264 return _freecadmax;
265 }
266
267 Meta::Version Metadata::pythonmin() const
268 {
269 return _pythonmin;
270 }
271
272 std::multimap<std::string, Metadata> Metadata::content() const
273 {
274 return _content;
275 }
276
277 std::vector<Meta::GenericMetadata> Metadata::operator[](const std::string& tag) const
278 {
279 std::vector<Meta::GenericMetadata> returnValue;
280 auto range = _genericMetadata.equal_range(tag);
281 for (auto item = range.first; item != range.second; ++item) {
282 returnValue.push_back(item->second);
283 }
284 return returnValue;
285 }
286
287 XERCES_CPP_NAMESPACE::DOMElement* Metadata::dom() const
288 {
289 return _dom;
290 }
291
292 void Metadata::setName(const std::string& name)
293 {
294 std::string invalidCharacters = "/\\?%*:|\"<>"; // Should cover all OSes
295 if (_name.find_first_of(invalidCharacters) != std::string::npos) {
296 throw Base::RuntimeError("Name cannot contain any of: " + invalidCharacters);
297 }
298 _name = name;
299 }
300
301 void Metadata::setType(const std::string& type)
302 {
303 _type = type;
304 }
305
306 void Metadata::setVersion(const Meta::Version& version)
307 {
308 _version = version;
309 }
310
311 void App::Metadata::setDate(const std::string& date)
312 {
313 _date = date;
314 }
315
316 void Metadata::setDescription(const std::string& description)
317 {
318 _description = description;
319 }
320
321 void Metadata::addMaintainer(const Meta::Contact& maintainer)
322 {
323 _maintainer.push_back(maintainer);
324 }
325
326 void Metadata::addLicense(const Meta::License& license)
327 {
328 _license.push_back(license);
329 }
330
331 void Metadata::addUrl(const Meta::Url& url)
332 {
333 _url.push_back(url);
334 }
335
336 void Metadata::addAuthor(const Meta::Contact& author)
337 {
338 _author.push_back(author);
339 }
340
341 void Metadata::addDepend(const Meta::Dependency& dep)
342 {
343 _depend.push_back(dep);
344 }
345
346 void Metadata::addConflict(const Meta::Dependency& dep)
347 {
348 _conflict.push_back(dep);
349 }
350
351 void Metadata::addReplace(const Meta::Dependency& dep)
352 {
353 _replace.push_back(dep);
354 }
355
356 void Metadata::addTag(const std::string& tag)
357 {
358 _tag.push_back(tag);
359 }
360
361 void Metadata::setIcon(const fs::path& path)
362 {
363 _icon = path;
364 }
365
366 void Metadata::setClassname(const std::string& name)
367 {
368 _classname = name;
369 }
370
371 void Metadata::setSubdirectory(const std::filesystem::path& path)
372 {
373 _subdirectory = path;
374 }
375
376 void Metadata::addFile(const fs::path& path)
377 {
378 _file.push_back(path);
379 }
380
381 void Metadata::addContentItem(const std::string& tag, const Metadata& item)
382 {
383 _content.insert(std::make_pair(tag, item));
384 }
385
386 void Metadata::setFreeCADMin(const Meta::Version& version)
387 {
388 _freecadmin = version;
389 }
390
391 void Metadata::setPythonMin(const Meta::Version& version)
392 {
393 _pythonmin = version;
394 }
395
396 void Metadata::setFreeCADMax(const Meta::Version& version)
397 {
398 _freecadmax = version;
399 }
400
401 void Metadata::addGenericMetadata(const std::string& tag,
402 const Meta::GenericMetadata& genericMetadata)
403 {
404 _genericMetadata.insert(std::make_pair(tag, genericMetadata));
405 }
406
407 void Metadata::removeContentItem(const std::string& tag, const std::string& itemName)
408 {
409 auto tagRange = _content.equal_range(tag);
410 auto foundItem =
411 std::find_if(tagRange.first, tagRange.second, [&itemName](const auto& check) -> bool {
412 return itemName == check.second.name();
413 });
414 if (foundItem != tagRange.second) {
415 _content.erase(foundItem);
416 }
417 }
418
419 void Metadata::removeMaintainer(const Meta::Contact& maintainer)
420 {
421 auto new_end = std::remove(_maintainer.begin(), _maintainer.end(), maintainer);
422 _maintainer.erase(new_end, _maintainer.end());
423 }
424
425 void Metadata::removeLicense(const Meta::License& license)
426 {
427 auto new_end = std::remove(_license.begin(), _license.end(), license);
428 _license.erase(new_end, _license.end());
429 }
430
431 void Metadata::removeUrl(const Meta::Url& url)
432 {
433 auto new_end = std::remove(_url.begin(), _url.end(), url);
434 _url.erase(new_end, _url.end());
435 }
436
437 void Metadata::removeAuthor(const Meta::Contact& author)
438 {
439 auto new_end = std::remove(_author.begin(), _author.end(), author);
440 _author.erase(new_end, _author.end());
441 }
442
443 void Metadata::removeDepend(const Meta::Dependency& dep)
444 {
445 bool found = false;
446 for (const auto& check : _depend) {
447 if (dep == check) {
448 found = true;
449 }
450 }
451 if (!found) {
452 throw Base::RuntimeError("No match found for dependency to remove");
453 }
454 auto new_end = std::remove(_depend.begin(), _depend.end(), dep);
455 _depend.erase(new_end, _depend.end());
456 }
457
458 void Metadata::removeConflict(const Meta::Dependency& dep)
459 {
460 auto new_end = std::remove(_conflict.begin(), _conflict.end(), dep);
461 _conflict.erase(new_end, _conflict.end());
462 }
463
464 void Metadata::removeReplace(const Meta::Dependency& dep)
465 {
466 auto new_end = std::remove(_replace.begin(), _replace.end(), dep);
467 _replace.erase(new_end, _replace.end());
468 }
469
470 void Metadata::removeTag(const std::string& tag)
471 {
472 auto new_end = std::remove(_tag.begin(), _tag.end(), tag);
473 _tag.erase(new_end, _tag.end());
474 }
475
476 void Metadata::removeFile(const std::filesystem::path& path)
477 {
478 auto new_end = std::remove(_file.begin(), _file.end(), path);
479 _file.erase(new_end, _file.end());
480 }
481
482
483 void Metadata::clearContent()
484 {
485 _content.clear();
486 }
487
488 void Metadata::clearMaintainer()
489 {
490 _maintainer.clear();
491 }
492
493 void Metadata::clearLicense()
494 {
495 _license.clear();
496 }
497
498 void Metadata::clearUrl()
499 {
500 _url.clear();
501 }
502
503 void Metadata::clearAuthor()
504 {
505 _author.clear();
506 }
507
508 void Metadata::clearDepend()
509 {
510 _depend.clear();
511 }
512
513 void Metadata::clearConflict()
514 {
515 _conflict.clear();
516 }
517
518 void Metadata::clearReplace()
519 {
520 _replace.clear();
521 }
522
523 void Metadata::clearTag()
524 {
525 _tag.clear();
526 }
527
528 void Metadata::clearFile()
529 {
530 _file.clear();
531 }
532
533
534 DOMElement* appendSimpleXMLNode(DOMElement* baseNode,
535 const std::string& nodeName,
536 const std::string& nodeContents)
537 {
538 // For convenience (and brevity of final output) don't create nodes that don't have contents
539 if (nodeContents.empty()) {
540 return nullptr;
541 }
542 auto doc = baseNode->getOwnerDocument();
543 DOMElement* namedElement = doc->createElement(XUTF8Str(nodeName.c_str()).unicodeForm());
544 baseNode->appendChild(namedElement);
545 DOMText* namedNode = doc->createTextNode(XUTF8Str(nodeContents.c_str()).unicodeForm());
546 namedElement->appendChild(namedNode);
547 return namedElement;
548 }
549
550 void addAttribute(DOMElement* node, const std::string& key, const std::string& value)
551 {
552 if (value.empty()) {
553 return;
554 }
555 node->setAttribute(XUTF8Str(key.c_str()).unicodeForm(), XUTF8Str(value.c_str()).unicodeForm());
556 }
557
558 void addAttribute(DOMElement* node, const std::string& key, bool value)
559 {
560 if (value) {
561 node->setAttribute(XUTF8Str(key.c_str()).unicodeForm(), XUTF8StrLiteral("True").unicodeForm());
562 }
563 else {
564 node->setAttribute(XUTF8Str(key.c_str()).unicodeForm(), XUTF8StrLiteral("False").unicodeForm());
565 }
566 }
567
568 void addAttribute(DOMElement* node, const std::string& key, Meta::DependencyType value)
569 {
570 // Someday we should be able to change this to use reflection, but it's not yet
571 // available (using C++17)
572 std::string stringified("automatic");
573 switch (value) {
574 case Meta::DependencyType::automatic:
575 stringified = "automatic";
576 break;
577 case Meta::DependencyType::internal:
578 stringified = "internal";
579 break;
580 case Meta::DependencyType::addon:
581 stringified = "addon";
582 break;
583 case Meta::DependencyType::python:
584 stringified = "python";
585 break;
586 }
587 node->setAttribute(XUTF8Str(key.c_str()).unicodeForm(),
588 XUTF8Str(stringified.c_str()).unicodeForm());
589 }
590
591 void addDependencyNode(DOMElement* root, const std::string& name, const Meta::Dependency& depend)
592 {
593 auto element = appendSimpleXMLNode(root, name, depend.package);
594 if (element) {
595 addAttribute(element, "version_lt", depend.version_lt);
596 addAttribute(element, "version_lte", depend.version_lte);
597 addAttribute(element, "version_eq", depend.version_eq);
598 addAttribute(element, "version_gte", depend.version_gte);
599 addAttribute(element, "version_gt", depend.version_gt);
600 addAttribute(element, "condition", depend.condition);
601 addAttribute(element, "optional", depend.optional);
602 addAttribute(element, "type", depend.dependencyType);
603 }
604 }
605
606 void Metadata::write(const fs::path& file) const
607 {
608 DOMImplementation* impl =
609 DOMImplementationRegistry::getDOMImplementation(XUTF8StrLiteral("Core LS").unicodeForm());
610
611 DOMDocument* doc = impl->createDocument(nullptr, XUTF8StrLiteral("package").unicodeForm(), nullptr);
612 DOMElement* root = doc->getDocumentElement();
613 root->setAttribute(XUTF8StrLiteral("format").unicodeForm(), XUTF8StrLiteral("1").unicodeForm());
614 root->setAttribute(XUTF8StrLiteral("xmlns").unicodeForm(),
615 XUTF8StrLiteral("https://wiki.freecad.org/Package_Metadata").unicodeForm());
616
617 appendToElement(root);
618
619 DOMLSSerializer* theSerializer = ((DOMImplementationLS*)impl)->createLSSerializer();
620 DOMConfiguration* config = theSerializer->getDomConfig();
621 if (config->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true)) {
622 config->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
623 }
624
625 // set feature if the serializer supports the feature/mode
626 if (config->canSetParameter(XMLUni::fgDOMWRTSplitCdataSections, true)) {
627 config->setParameter(XMLUni::fgDOMWRTSplitCdataSections, true);
628 }
629
630 if (config->canSetParameter(XMLUni::fgDOMWRTDiscardDefaultContent, true)) {
631 config->setParameter(XMLUni::fgDOMWRTDiscardDefaultContent, true);
632 }
633
634 try {
635 XMLFormatTarget* myFormTarget = new LocalFileFormatTarget(file.string().c_str());
636 DOMLSOutput* theOutput = ((DOMImplementationLS*)impl)->createLSOutput();
637
638 theOutput->setByteStream(myFormTarget);
639 theSerializer->write(doc, theOutput);
640
641 theOutput->release();
642 theSerializer->release();
643 delete myFormTarget;
644 }
645 catch (const XMLException& toCatch) {
646 char* message = XMLString::transcode(toCatch.getMessage());
647 std::string what = message;
648 XMLString::release(&message);
649 throw Base::XMLBaseException(what);
650 }
651 catch (const DOMException& toCatch) {
652 char* message = XMLString::transcode(toCatch.getMessage());
653 std::string what = message;
654 XMLString::release(&message);
655 throw Base::XMLBaseException(what);
656 }
657
658 doc->release();
659 }
660
661 bool Metadata::satisfies(const Meta::Dependency& dep)
662 {
663 if (dep.package != _name) {
664 return false;
665 }
666 // The "condition" attribute allows an expression to enable or disable this dependency check: it
667 // must contain a valid FreeCAD Expression. If it evaluates to false, this dependency is
668 // bypassed (e.g. this function returns false).
669 if (!dep.condition.empty()) {
670 auto injectedString = dep.condition;
671 std::map<std::string, std::string> replacements;
672 std::map<std::string, std::string>& config = App::Application::Config();
673 replacements.insert(std::make_pair("$BuildVersionMajor", config["BuildVersionMajor"]));
674 replacements.insert(std::make_pair("$BuildVersionMinor", config["BuildVersionMinor"]));
675 replacements.insert(std::make_pair("$BuildVersionMinor", config["BuildVersionPoint"]));
676 replacements.insert(std::make_pair("$BuildRevision", config["BuildRevision"]));
677 for (const auto& replacement : replacements) {
678 auto pos = injectedString.find(replacement.first);
679 while (pos != std::string::npos) {
680 injectedString.replace(pos, replacement.first.length(), replacement.second);
681 pos = injectedString.find(replacement.first);
682 }
683 }
684 auto parsedExpression = App::Expression::parse(nullptr, dep.condition);
685 auto result = parsedExpression->eval();
686 if (!boost::any_cast<bool>(result->getValueAsAny())) {
687 return false;
688 }
689 }
690
691 if (!dep.version_eq.empty()) {
692 return _version == Meta::Version(dep.version_eq);
693 }
694 // Any of the others might be specified in pairs, so only return the "false" case
695
696 if (!dep.version_lt.empty()) {
697 if (!(_version < Meta::Version(dep.version_lt))) {
698 return false;
699 }
700 }
701 if (!dep.version_lte.empty()) {
702 if (!(_version <= Meta::Version(dep.version_lt))) {
703 return false;
704 }
705 }
706 if (!dep.version_gt.empty()) {
707 if (!(_version > Meta::Version(dep.version_lt))) {
708 return false;
709 }
710 }
711 if (!dep.version_gte.empty()) {
712 if (!(_version >= Meta::Version(dep.version_lt))) {
713 return false;
714 }
715 }
716 return true;
717 }
718
719 bool Metadata::supportsCurrentFreeCAD() const
720 {
721 static auto fcVersion = Meta::Version();
722 if (fcVersion == Meta::Version()) {
723 std::map<std::string, std::string>& config = App::Application::Config();
724 std::stringstream ss;
725 ss << config["BuildVersionMajor"] << "." << config["BuildVersionMinor"] << "."
726 << config["BuildVersionPoint"] << "."
727 << (config["BuildRevision"].empty() ? "0" : config["BuildRevision"]);
728 fcVersion = Meta::Version(ss.str());
729 }
730
731 if (_freecadmin != Meta::Version() && _freecadmin > fcVersion) {
732 return false;
733 }
734 if (_freecadmax != Meta::Version() && _freecadmax < fcVersion) {
735 return false;
736 }
737 return true;
738 }
739
740 void Metadata::appendToElement(DOMElement* root) const
741 {
742 appendSimpleXMLNode(root, "name", _name);
743 appendSimpleXMLNode(root, "type", _type);
744 appendSimpleXMLNode(root, "description", _description);
745 if (_version != Meta::Version()) {
746 // Only append version if it's not 0.0.0
747 appendSimpleXMLNode(root, "version", _version.str());
748 }
749
750 if (!_date.empty()) {
751 appendSimpleXMLNode(root, "date", _date);
752 }
753
754 for (const auto& maintainer : _maintainer) {
755 auto element = appendSimpleXMLNode(root, "maintainer", maintainer.name);
756 if (element) {
757 addAttribute(element, "email", maintainer.email);
758 }
759 }
760
761 for (const auto& license : _license) {
762 auto element = appendSimpleXMLNode(root, "license", license.name);
763 if (element) {
764 addAttribute(element, "file", license.file.string());
765 }
766 }
767
768 if (_freecadmin != Meta::Version()) {
769 appendSimpleXMLNode(root, "freecadmin", _freecadmin.str());
770 }
771
772 if (_freecadmax != Meta::Version()) {
773 appendSimpleXMLNode(root, "freecadmax", _freecadmax.str());
774 }
775
776 if (_pythonmin != Meta::Version()) {
777 appendSimpleXMLNode(root, "pythonmin", _pythonmin.str());
778 }
779
780 for (const auto& url : _url) {
781 auto element = appendSimpleXMLNode(root, "url", url.location);
782 if (element) {
783 std::string typeAsString("website");
784 switch (url.type) {
785 case Meta::UrlType::website:
786 typeAsString = "website";
787 break;
788 case Meta::UrlType::repository:
789 typeAsString = "repository";
790 break;
791 case Meta::UrlType::bugtracker:
792 typeAsString = "bugtracker";
793 break;
794 case Meta::UrlType::readme:
795 typeAsString = "readme";
796 break;
797 case Meta::UrlType::documentation:
798 typeAsString = "documentation";
799 break;
800 case Meta::UrlType::discussion:
801 typeAsString = "discussion";
802 break;
803 }
804 addAttribute(element, "type", typeAsString);
805 if (url.type == Meta::UrlType::repository) {
806 addAttribute(element, "branch", url.branch);
807 }
808 }
809 }
810
811 for (const auto& author : _author) {
812 auto element = appendSimpleXMLNode(root, "author", author.name);
813 if (element) {
814 addAttribute(element, "email", author.email);
815 }
816 }
817
818 for (const auto& depend : _depend) {
819 addDependencyNode(root, "depend", depend);
820 }
821
822 for (const auto& conflict : _conflict) {
823 addDependencyNode(root, "conflict", conflict);
824 }
825
826 for (const auto& replace : _replace) {
827 addDependencyNode(root, "replace", replace);
828 }
829
830 for (const auto& tag : _tag) {
831 appendSimpleXMLNode(root, "tag", tag);
832 }
833
834 appendSimpleXMLNode(root, "icon", _icon.string());
835
836 appendSimpleXMLNode(root, "classname", _classname);
837
838 appendSimpleXMLNode(root, "subdirectory", _subdirectory.string());
839
840 for (const auto& file : _file) {
841 appendSimpleXMLNode(root, "file", file.string());
842 }
843
844 for (const auto& md : _genericMetadata) {
845 auto element = appendSimpleXMLNode(root, md.first, md.second.contents);
846 for (const auto& attr : md.second.attributes) {
847 addAttribute(element, attr.first, attr.second);
848 }
849 }
850
851 if (!_content.empty()) {
852 auto doc = root->getOwnerDocument();
853 DOMElement* contentRootElement = doc->createElement(XUTF8StrLiteral("content").unicodeForm());
854 root->appendChild(contentRootElement);
855 for (const auto& content : _content) {
856 DOMElement* contentElement =
857 doc->createElement(XUTF8Str(content.first.c_str()).unicodeForm());
858 contentRootElement->appendChild(contentElement);
859 content.second.appendToElement(contentElement);
860 }
861 }
862 }
863
864
865 void Metadata::parseVersion1(const DOMNode* startNode)
866 {
867 auto children = startNode->getChildNodes();
868
869 for (XMLSize_t i = 0; i < children->getLength(); ++i) {
870 auto child = children->item(i);
871 auto element = dynamic_cast<const DOMElement*>(child);
872 if (!element) {
873 continue;
874 }
875
876 auto tag = element->getNodeName();
877 auto tagString = StrXUTF8(tag).str;
878
879 if (tagString == "name") {
880 _name = StrXUTF8(element->getTextContent()).str;
881 }
882 else if (tagString == "type") {
883 _type = StrXUTF8(element->getTextContent()).str;
884 }
885 else if (tagString == "version") {
886 _version = Meta::Version(StrXUTF8(element->getTextContent()).str);
887 }
888 else if (tagString == "date") {
889 _date = StrXUTF8(element->getTextContent()).str;
890 }
891 else if (tagString == "description") {
892 _description = StrXUTF8(element->getTextContent()).str;
893 }
894 else if (tagString == "maintainer") {
895 _maintainer.emplace_back(element);
896 }
897 else if (tagString == "license") {
898 _license.emplace_back(element);
899 }
900 else if (tagString == "freecadmin") {
901 _freecadmin = Meta::Version(StrXUTF8(element->getTextContent()).str);
902 }
903 else if (tagString == "freecadmax") {
904 _freecadmax = Meta::Version(StrXUTF8(element->getTextContent()).str);
905 }
906 else if (tagString == "pythonmin") {
907 _pythonmin = Meta::Version(StrXUTF8(element->getTextContent()).str);
908 }
909 else if (tagString == "url") {
910 _url.emplace_back(element);
911 }
912 else if (tagString == "author") {
913 _author.emplace_back(element);
914 }
915 else if (tagString == "depend") {
916 _depend.emplace_back(element);
917 }
918 else if (tagString == "conflict") {
919 _conflict.emplace_back(element);
920 }
921 else if (tagString == "replace") {
922 _replace.emplace_back(element);
923 }
924 else if (tagString == "tag") {
925 _tag.emplace_back(StrXUTF8(element->getTextContent()).str);
926 }
927 else if (tagString == "file") {
928 _file.emplace_back(StrXUTF8(element->getTextContent()).str);
929 }
930 else if (tagString == "classname") {
931 _classname = StrXUTF8(element->getTextContent()).str;
932 }
933 else if (tagString == "subdirectory") {
934 _subdirectory = StrXUTF8(element->getTextContent()).str;
935 }
936 else if (tagString == "icon") {
937 _icon = fs::path(StrXUTF8(element->getTextContent()).str);
938 }
939 else if (tagString == "content") {
940 parseContentNodeVersion1(element); // Recursive call
941 }
942 else {
943 // If none of this node's nodeChildren have nodeChildren of their own, it is a simple
944 // element and we can handle it as a GenericMetadata object
945 auto nodeChildren = element->getChildNodes();
946 bool hasGrandchildren = false;
947 for (XMLSize_t j = 0; j < nodeChildren->getLength() && !hasGrandchildren; ++j) {
948 if (nodeChildren->item(j)->getChildNodes()->getLength() > 0) {
949 hasGrandchildren = true;
950 }
951 }
952 if (!hasGrandchildren) {
953 _genericMetadata.insert(std::make_pair(tagString, Meta::GenericMetadata(element)));
954 }
955 }
956 }
957 }
958
959 void Metadata::parseContentNodeVersion1(const DOMElement* contentNode)
960 {
961 auto children = contentNode->getChildNodes();
962 for (XMLSize_t i = 0; i < children->getLength(); ++i) {
963 auto child = dynamic_cast<const DOMElement*>(children->item(i));
964 if (child) {
965 auto tag = StrXUTF8(child->getTagName()).str;
966 _content.insert(std::make_pair(tag, Metadata(child, 1)));
967 }
968 }
969 }
970
971 Meta::Contact::Contact(std::string name, std::string email)
972 : name(std::move(name))
973 , email(std::move(email))
974 {
975 // This has to be provided manually since we have another constructor
976 }
977
978 Meta::Contact::Contact(const XERCES_CPP_NAMESPACE::DOMElement* elem)
979 {
980 if (!elem) {
981 return;
982 }
983 auto emailAttribute = elem->getAttribute(XUTF8StrLiteral("email").unicodeForm());
984 name = StrXUTF8(elem->getTextContent()).str;
985 email = StrXUTF8(emailAttribute).str;
986 }
987
988 bool App::Meta::Contact::operator==(const Contact& rhs) const
989 {
990 return name == rhs.name && email == rhs.email;
991 }
992
993 Meta::License::License(std::string name, fs::path file)
994 : name(std::move(name))
995 , file(std::move(file))
996 {
997 // This has to be provided manually since we have another constructor
998 }
999
1000 Meta::License::License(const XERCES_CPP_NAMESPACE::DOMElement* elem)
1001 {
1002 if (!elem) {
1003 return;
1004 }
1005 auto fileAttribute = elem->getAttribute(XUTF8StrLiteral("file").unicodeForm());
1006 if (XMLString::stringLen(fileAttribute) > 0) {
1007 file = fs::path(StrXUTF8(fileAttribute).str);
1008 }
1009 name = StrXUTF8(elem->getTextContent()).str;
1010 }
1011
1012 bool App::Meta::License::operator==(const License& rhs) const
1013 {
1014 return name == rhs.name && file == rhs.file;
1015 }
1016
1017 App::Meta::Url::Url()
1018 : location("")
1019 , type(App::Meta::UrlType::website)
1020 {}
1021
1022 Meta::Url::Url(std::string location, UrlType type)
1023 : location(std::move(location))
1024 , type(type)
1025 {
1026 // This has to be provided manually since we have another constructor
1027 }
1028
1029 Meta::Url::Url(const XERCES_CPP_NAMESPACE::DOMElement* elem)
1030 {
1031 if (!elem) {
1032 return;
1033 }
1034 auto typeAttribute = StrXUTF8(elem->getAttribute(XUTF8StrLiteral("type").unicodeForm())).str;
1035 if (typeAttribute.empty() || typeAttribute == "website") {
1036 type = UrlType::website;
1037 }
1038 else if (typeAttribute == "bugtracker") {
1039 type = UrlType::bugtracker;
1040 }
1041 else if (typeAttribute == "repository") {
1042 type = UrlType::repository;
1043 }
1044 else if (typeAttribute == "readme") {
1045 type = UrlType::readme;
1046 }
1047 else if (typeAttribute == "documentation") {
1048 type = UrlType::documentation;
1049 }
1050 else if (typeAttribute == "discussion") {
1051 type = UrlType::discussion;
1052 }
1053 else {
1054 type = UrlType::website;
1055 }
1056
1057 if (type == UrlType::repository) {
1058 branch = StrXUTF8(elem->getAttribute(XUTF8StrLiteral("branch").unicodeForm())).str;
1059 }
1060 location = StrXUTF8(elem->getTextContent()).str;
1061 }
1062
1063 bool App::Meta::Url::operator==(const Url& rhs) const
1064 {
1065 if (type == UrlType::repository && branch != rhs.branch) {
1066 return false;
1067 }
1068 return type == rhs.type && location == rhs.location;
1069 }
1070
1071 App::Meta::Dependency::Dependency()
1072 : optional(false)
1073 , dependencyType(App::Meta::DependencyType::automatic)
1074 {}
1075
1076 App::Meta::Dependency::Dependency(std::string pkg)
1077 : package(std::move(pkg))
1078 , optional(false)
1079 , dependencyType(App::Meta::DependencyType::automatic)
1080 {}
1081
1082 Meta::Dependency::Dependency(const XERCES_CPP_NAMESPACE::DOMElement* elem)
1083 {
1084 version_lt = StrXUTF8(elem->getAttribute(XUTF8StrLiteral("version_lt").unicodeForm())).str;
1085 version_lte = StrXUTF8(elem->getAttribute(XUTF8StrLiteral("version_lte").unicodeForm())).str;
1086 version_eq = StrXUTF8(elem->getAttribute(XUTF8StrLiteral("version_eq").unicodeForm())).str;
1087 version_gte = StrXUTF8(elem->getAttribute(XUTF8StrLiteral("version_gte").unicodeForm())).str;
1088 version_gt = StrXUTF8(elem->getAttribute(XUTF8StrLiteral("version_gt").unicodeForm())).str;
1089 condition = StrXUTF8(elem->getAttribute(XUTF8StrLiteral("condition").unicodeForm())).str;
1090 std::string opt_string = StrXUTF8(elem->getAttribute(XUTF8StrLiteral("optional").unicodeForm())).str;
1091 if (opt_string == "true"
1092 || opt_string == "True") { // Support Python capitalization in this one case...
1093 optional = true;
1094 }
1095 else {
1096 optional = false;
1097 }
1098 std::string type_string = StrXUTF8(elem->getAttribute(XUTF8StrLiteral("type").unicodeForm())).str;
1099 if (type_string == "automatic" || type_string.empty()) {
1100 dependencyType = Meta::DependencyType::automatic;
1101 }
1102 else if (type_string == "addon") {
1103 dependencyType = Meta::DependencyType::addon;
1104 }
1105 else if (type_string == "internal") {
1106 dependencyType = Meta::DependencyType::internal;
1107 }
1108 else if (type_string == "python") {
1109 dependencyType = Meta::DependencyType::python;
1110 }
1111 else {
1112 auto message = std::string("Invalid dependency type \"") + type_string + "\"";
1113 throw Base::XMLBaseException(message);
1114 }
1115
1116 package = StrXUTF8(elem->getTextContent()).str;
1117 }
1118
1119 bool App::Meta::Dependency::operator==(const Dependency& rhs) const
1120 {
1121 return package == rhs.package && version_lt == rhs.version_lt && version_lte == rhs.version_lte
1122 && version_eq == rhs.version_eq && version_gte == rhs.version_gte
1123 && version_gt == rhs.version_gt && condition == rhs.condition && optional == rhs.optional
1124 && dependencyType == rhs.dependencyType;
1125 }
1126
1127 Meta::Version::Version() = default;
1128
1129 Meta::Version::Version(int major, int minor, int patch, std::string suffix)
1130 : major(major)
1131 , minor(minor)
1132 , patch(patch)
1133 , suffix(std::move(suffix))
1134 {}
1135
1136 Meta::Version::Version(const std::string& versionString)
1137 {
1138 std::istringstream stream(versionString);
1139 char separator {'.'};
1140 stream >> major;
1141 if (stream) {
1142 stream >> separator;
1143 }
1144 if (stream) {
1145 stream >> minor;
1146 }
1147 if (stream) {
1148 stream >> separator;
1149 }
1150 if (stream) {
1151 stream >> patch;
1152 }
1153 if (stream) {
1154 stream >> suffix;
1155 }
1156 }
1157
1158 std::string Meta::Version::str() const
1159 {
1160 if (*this == Meta::Version()) {
1161 return "";
1162 }
1163 std::ostringstream stream;
1164 stream << major << "." << minor << "." << patch << suffix;
1165 return stream.str();
1166 }
1167
1168 bool Meta::Version::operator<(const Version& rhs) const
1169 {
1170 return std::tie(major, minor, patch, suffix)
1171 < std::tie(rhs.major, rhs.minor, rhs.patch, rhs.suffix);
1172 }
1173
1174 bool Meta::Version::operator>(const Version& rhs) const
1175 {
1176 return std::tie(major, minor, patch, suffix)
1177 > std::tie(rhs.major, rhs.minor, rhs.patch, rhs.suffix);
1178 }
1179
1180 bool Meta::Version::operator<=(const Version& rhs) const
1181 {
1182 return std::tie(major, minor, patch, suffix)
1183 <= std::tie(rhs.major, rhs.minor, rhs.patch, rhs.suffix);
1184 }
1185
1186 bool Meta::Version::operator>=(const Version& rhs) const
1187 {
1188 return std::tie(major, minor, patch, suffix)
1189 >= std::tie(rhs.major, rhs.minor, rhs.patch, rhs.suffix);
1190 }
1191
1192 bool Meta::Version::operator==(const Version& rhs) const
1193 {
1194 return std::tie(major, minor, patch, suffix)
1195 == std::tie(rhs.major, rhs.minor, rhs.patch, rhs.suffix);
1196 }
1197
1198 bool Meta::Version::operator!=(const Version& rhs) const
1199 {
1200 return std::tie(major, minor, patch, suffix)
1201 != std::tie(rhs.major, rhs.minor, rhs.patch, rhs.suffix);
1202 }
1203
1204 Meta::GenericMetadata::GenericMetadata(const XERCES_CPP_NAMESPACE::DOMElement* elem)
1205 {
1206 contents = StrXUTF8(elem->getTextContent()).str;
1207 for (XMLSize_t i = 0; i < elem->getAttributes()->getLength(); ++i) {
1208 auto attr = elem->getAttributes()->item(i);
1209 attributes.insert(std::make_pair(StrXUTF8(attr->getNodeName()).str,
1210 StrXUTF8(attr->getTextContent()).str));
1211 }
1212 }
1213
1214 App::Meta::GenericMetadata::GenericMetadata(std::string contents)
1215 : contents(std::move(contents))
1216 {}