| 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 |
#ifndef BASE_METADATAREADER_H |
| 24 |
#define BASE_METADATAREADER_H |
| 25 |
|
| 26 |
#include "FCConfig.h" |
| 27 |
|
| 28 |
#include <filesystem> |
| 29 |
|
| 30 |
#include <map> |
| 31 |
#include <memory> |
| 32 |
#include <string> |
| 33 |
#include <vector> |
| 34 |
|
| 35 |
#include <xercesc/dom/DOM.hpp> |
| 36 |
#include <xercesc/parsers/XercesDOMParser.hpp> |
| 37 |
|
| 38 |
|
| 39 |
namespace App |
| 40 |
{ |
| 41 |
|
| 42 |
/** |
| 43 |
* @brief A namespace for metadata for Python external addons. |
| 44 |
* |
| 45 |
* This namespace contains functionality to define the contents of a |
| 46 |
* package.xml file, such as Contact, License, Url, and Dependency. |
| 47 |
*/ |
| 48 |
namespace Meta |
| 49 |
{ |
| 50 |
|
| 51 |
/** |
| 52 |
* \struct Contact |
| 53 |
* \brief A person or company representing a point of contact for the package (either author or |
| 54 |
* maintainer). |
| 55 |
*/ |
| 56 |
struct AppExport Contact |
| 57 |
{ |
| 58 |
Contact() = default; |
| 59 |
Contact(std::string name, std::string email); |
| 60 |
explicit Contact(const XERCES_CPP_NAMESPACE::DOMElement* elem); |
| 61 |
std::string name; //< Contact name - required |
| 62 |
std::string email; //< Contact email - may be optional |
| 63 |
bool operator==(const Contact& rhs) const; |
| 64 |
}; |
| 65 |
|
| 66 |
/** |
| 67 |
* \struct License |
| 68 |
* \brief A license that covers some or all of this package. |
| 69 |
* |
| 70 |
* Many licenses also require the inclusion of the complete license text, specified in this struct |
| 71 |
* using the "file" member. |
| 72 |
*/ |
| 73 |
struct AppExport License |
| 74 |
{ |
| 75 |
License() = default; |
| 76 |
License(std::string name, std::filesystem::path file); |
| 77 |
explicit License(const XERCES_CPP_NAMESPACE::DOMElement* elem); |
| 78 |
std::string |
| 79 |
name; //< Short name of license, e.g. "LGPL2", "MIT", "Mozilla Public License", etc. |
| 80 |
std::filesystem::path |
| 81 |
file; //< Optional path to the license file, relative to the XML file's location |
| 82 |
bool operator==(const License& rhs) const; |
| 83 |
}; |
| 84 |
|
| 85 |
enum class UrlType |
| 86 |
{ |
| 87 |
website, |
| 88 |
repository, |
| 89 |
bugtracker, |
| 90 |
readme, |
| 91 |
documentation, |
| 92 |
discussion |
| 93 |
}; |
| 94 |
|
| 95 |
/** |
| 96 |
* \struct Url |
| 97 |
* \brief A URL, including type information (e.g. website, repository, or bugtracker, in |
| 98 |
* package.xml) |
| 99 |
*/ |
| 100 |
struct AppExport Url |
| 101 |
{ |
| 102 |
Url(); |
| 103 |
Url(std::string location, UrlType type); |
| 104 |
explicit Url(const XERCES_CPP_NAMESPACE::DOMElement* elem); |
| 105 |
std::string location; //< The actual URL, including protocol |
| 106 |
UrlType type; //< What kind of URL this is |
| 107 |
std::string branch; //< If it's a repository, which branch to use |
| 108 |
bool operator==(const Url& rhs) const; |
| 109 |
}; |
| 110 |
|
| 111 |
/** |
| 112 |
* \struct Version |
| 113 |
* A semantic version structure providing comparison operators and conversion to and from |
| 114 |
* std::string |
| 115 |
*/ |
| 116 |
struct AppExport Version |
| 117 |
{ |
| 118 |
Version(); |
| 119 |
explicit Version(int major, int minor = 0, int patch = 0, std::string suffix = std::string()); |
| 120 |
explicit Version(const std::string& semanticString); |
| 121 |
|
| 122 |
int major {}; |
| 123 |
int minor {}; |
| 124 |
int patch {}; |
| 125 |
std::string suffix; |
| 126 |
|
| 127 |
std::string str() const; |
| 128 |
|
| 129 |
bool operator<(const Version&) const; |
| 130 |
bool operator>(const Version&) const; |
| 131 |
bool operator<=(const Version&) const; |
| 132 |
bool operator>=(const Version&) const; |
| 133 |
bool operator==(const Version&) const; |
| 134 |
bool operator!=(const Version&) const; |
| 135 |
}; |
| 136 |
|
| 137 |
/** |
| 138 |
* \enum DependencyType |
| 139 |
* The type of dependency. |
| 140 |
*/ |
| 141 |
enum class DependencyType |
| 142 |
{ |
| 143 |
automatic, |
| 144 |
internal, |
| 145 |
addon, |
| 146 |
python |
| 147 |
}; |
| 148 |
|
| 149 |
/** |
| 150 |
* \struct Dependency |
| 151 |
* \brief Another package that this package depends on, conflicts with, or replaces |
| 152 |
*/ |
| 153 |
struct AppExport Dependency |
| 154 |
{ |
| 155 |
Dependency(); |
| 156 |
explicit Dependency(std::string pkg); |
| 157 |
explicit Dependency(const XERCES_CPP_NAMESPACE::DOMElement* elem); |
| 158 |
std::string package; //< Required: must exactly match the contents of the "name" element in the |
| 159 |
// referenced package's package.xml file. |
| 160 |
std::string version_lt; //< Optional: The dependency to the package is restricted to versions |
| 161 |
// less than the stated version number. |
| 162 |
std::string version_lte; //< Optional: The dependency to the package is restricted to versions |
| 163 |
// less or equal than the stated version number. |
| 164 |
std::string version_eq; //< Optional: The dependency to the package is restricted to a version |
| 165 |
// equal than the stated version number. |
| 166 |
std::string version_gte; //< Optional: The dependency to the package is restricted to versions |
| 167 |
// greater or equal than the stated version number. |
| 168 |
std::string version_gt; //< Optional: The dependency to the package is restricted to versions |
| 169 |
// greater than the stated version number. |
| 170 |
std::string condition; //< Optional: Conditional expression as documented in REP149. |
| 171 |
bool optional; //< Optional: Whether this dependency is considered "optional" |
| 172 |
DependencyType dependencyType; //< Optional: defaults to "automatic" |
| 173 |
bool operator==(const Dependency& rhs) const; |
| 174 |
}; |
| 175 |
|
| 176 |
/** |
| 177 |
* \struct GenericMetadata |
| 178 |
* A structure to hold unrecognized single-level metadata. |
| 179 |
* |
| 180 |
* Most unrecognized metadata is simple: when parsing the XML, if the parser finds a tag it |
| 181 |
* does not recognize, and that tag has no children, it is parsed into this data structure |
| 182 |
* for convenient access by client code. |
| 183 |
*/ |
| 184 |
struct AppExport GenericMetadata |
| 185 |
{ |
| 186 |
GenericMetadata() = default; |
| 187 |
explicit GenericMetadata(const XERCES_CPP_NAMESPACE::DOMElement* elem); |
| 188 |
explicit GenericMetadata(std::string contents); |
| 189 |
std::string contents; //< The contents of the tag |
| 190 |
std::map<std::string, std::string> attributes; //< The XML attributes of the tag |
| 191 |
}; |
| 192 |
|
| 193 |
} // namespace Meta |
| 194 |
|
| 195 |
/** |
| 196 |
* \class Metadata |
| 197 |
* \brief Reads data from a metadata file. |
| 198 |
* |
| 199 |
* The metadata format is based on https://ros.org/reps/rep-0149.html, modified for FreeCAD |
| 200 |
* use. Full format documentation is available at the FreeCAD Wiki: |
| 201 |
* https://wiki.freecad.org/Package_Metadata |
| 202 |
*/ |
| 203 |
class AppExport Metadata |
| 204 |
{ |
| 205 |
public: |
| 206 |
Metadata(); |
| 207 |
|
| 208 |
/** |
| 209 |
* Read the data from a file on disk |
| 210 |
* |
| 211 |
* This constructor takes a path to an XML file and loads the XML from that file as |
| 212 |
* metadata. |
| 213 |
*/ |
| 214 |
explicit Metadata(const std::filesystem::path& metadataFile); |
| 215 |
|
| 216 |
/** |
| 217 |
* Construct a Metadata object from a DOM node. |
| 218 |
* |
| 219 |
* This node may have any tag name: it is only accessed via its children, which are |
| 220 |
* expected to follow the standard Metadata format for the contents of the <package> element. |
| 221 |
*/ |
| 222 |
Metadata(const XERCES_CPP_NAMESPACE::DOMNode* domNode, int format); |
| 223 |
|
| 224 |
/** |
| 225 |
* Treat the incoming rawData as metadata to be parsed. |
| 226 |
*/ |
| 227 |
explicit Metadata(const std::string& rawData); |
| 228 |
|
| 229 |
~Metadata(); |
| 230 |
|
| 231 |
|
| 232 |
////////////////////////////////////////////////////////////// |
| 233 |
// Recognized Metadata |
| 234 |
////////////////////////////////////////////////////////////// |
| 235 |
|
| 236 |
std::string name() const; //< A short name for this package, often used as a menu entry. |
| 237 |
std::string type() const; //< The type for this package. |
| 238 |
Meta::Version version() const; //< Version string in semantic triplet format, e.g. "1.2.3". |
| 239 |
std::string date() |
| 240 |
const; //< Date string -- currently arbitrary (when C++20 is well-supported we can revisit) |
| 241 |
std::string description() const; //< Text-only description of the package. No markup. |
| 242 |
std::vector<Meta::Contact> |
| 243 |
maintainer() const; //< Must be at least one, and must specify an email address. |
| 244 |
std::vector<Meta::License> |
| 245 |
license() const; //< Must be at least one, and most licenses require including a license file. |
| 246 |
std::vector<Meta::Url> url() const; //< Any number of URLs may be specified, but at least one |
| 247 |
// repository URL must be included at the package level. |
| 248 |
std::vector<Meta::Contact> |
| 249 |
author() const; //< Any number of authors may be specified, and email addresses are optional. |
| 250 |
std::vector<Meta::Dependency> |
| 251 |
depend() const; //< Zero or more packages this package requires prior to use. |
| 252 |
std::vector<Meta::Dependency> |
| 253 |
conflict() const; //< Zero of more packages this package conflicts with. |
| 254 |
std::vector<Meta::Dependency> |
| 255 |
replace() const; //< Zero or more packages this package is intended to replace. |
| 256 |
std::vector<std::string> tag() const; //< Zero or more text tags related to this package. |
| 257 |
std::filesystem::path icon() const; //< Path to an icon file. |
| 258 |
std::string |
| 259 |
classname() const; //< Recognized for convenience -- generally only used by Workbenches. |
| 260 |
std::filesystem::path |
| 261 |
subdirectory() const; //< Optional, override the default subdirectory name for this item. |
| 262 |
std::vector<std::filesystem::path> |
| 263 |
file() const; //< Arbitrary files associated with this package or content item. |
| 264 |
Meta::Version freecadmin() const; //< The minimum FreeCAD version. |
| 265 |
Meta::Version freecadmax() const; //< The maximum FreeCAD version. |
| 266 |
Meta::Version pythonmin() const; //< The minimum Python version. |
| 267 |
|
| 268 |
/** |
| 269 |
* Access the metadata for the content elements of this package |
| 270 |
* |
| 271 |
* In addition to the overall package metadata, this class reads in metadata contained in a |
| 272 |
* <content> element. Each entry in the content element is an element representing some |
| 273 |
* type of package content (e.g. add-on, macro, theme, etc.). This class places no restriction |
| 274 |
* on the types, it is up to client code to place requirements on the metadata included |
| 275 |
* here. |
| 276 |
* |
| 277 |
* For example, themes might be specified: |
| 278 |
* <content> |
| 279 |
* <theme> |
| 280 |
* <name>High Contrast</name> |
| 281 |
* </theme> |
| 282 |
* </content> |
| 283 |
*/ |
| 284 |
std::multimap<std::string, Metadata> content() const; |
| 285 |
|
| 286 |
/** |
| 287 |
* Convenience accessor for unrecognized simple metadata. |
| 288 |
* |
| 289 |
* If the XML parser encounters tags that it does not recognize, and those tags have |
| 290 |
* no children, a GenericMetadata object is created. Those objects can be accessed using |
| 291 |
* operator[], which returns a (potentially empty) vector containing all instances of the |
| 292 |
* given tag. It cannot be used to *create* a new tag, however. See addGenericMetadata(). |
| 293 |
*/ |
| 294 |
std::vector<Meta::GenericMetadata> operator[](const std::string& tag) const; |
| 295 |
|
| 296 |
/** |
| 297 |
* Directly access the DOM tree to support unrecognized multi-level metadata |
| 298 |
*/ |
| 299 |
XERCES_CPP_NAMESPACE::DOMElement* dom() const; |
| 300 |
|
| 301 |
|
| 302 |
// Setters |
| 303 |
void setName(const std::string& name); |
| 304 |
void setType(const std::string& type); |
| 305 |
void setVersion(const Meta::Version& version); |
| 306 |
void setDate(const std::string& date); |
| 307 |
void setDescription(const std::string& description); |
| 308 |
void addMaintainer(const Meta::Contact& maintainer); |
| 309 |
void addLicense(const Meta::License& license); |
| 310 |
void addUrl(const Meta::Url& url); |
| 311 |
void addAuthor(const Meta::Contact& author); |
| 312 |
void addDepend(const Meta::Dependency& dep); |
| 313 |
void addConflict(const Meta::Dependency& dep); |
| 314 |
void addReplace(const Meta::Dependency& dep); |
| 315 |
void addTag(const std::string& tag); |
| 316 |
void setIcon(const std::filesystem::path& path); |
| 317 |
void setClassname(const std::string& name); |
| 318 |
void setSubdirectory(const std::filesystem::path& path); |
| 319 |
void addFile(const std::filesystem::path& path); |
| 320 |
void addContentItem(const std::string& tag, const Metadata& item); |
| 321 |
void setFreeCADMin(const Meta::Version& version); |
| 322 |
void setFreeCADMax(const Meta::Version& version); |
| 323 |
void setPythonMin(const Meta::Version& version); |
| 324 |
void addGenericMetadata(const std::string& tag, const Meta::GenericMetadata& genericMetadata); |
| 325 |
|
| 326 |
// Deleters |
| 327 |
void removeContentItem(const std::string& tag, const std::string& itemName); |
| 328 |
void removeMaintainer(const Meta::Contact& maintainer); |
| 329 |
void removeLicense(const Meta::License& license); |
| 330 |
void removeUrl(const Meta::Url& url); |
| 331 |
void removeAuthor(const Meta::Contact& author); |
| 332 |
void removeDepend(const Meta::Dependency& dep); |
| 333 |
void removeConflict(const Meta::Dependency& dep); |
| 334 |
void removeReplace(const Meta::Dependency& dep); |
| 335 |
void removeTag(const std::string& tag); |
| 336 |
void removeFile(const std::filesystem::path& path); |
| 337 |
|
| 338 |
// Utility functions to clear lists |
| 339 |
void clearContent(); |
| 340 |
void clearMaintainer(); |
| 341 |
void clearLicense(); |
| 342 |
void clearUrl(); |
| 343 |
void clearAuthor(); |
| 344 |
void clearDepend(); |
| 345 |
void clearConflict(); |
| 346 |
void clearReplace(); |
| 347 |
void clearTag(); |
| 348 |
void clearFile(); |
| 349 |
|
| 350 |
/** |
| 351 |
* Write the metadata to an XML file |
| 352 |
*/ |
| 353 |
void write(const std::filesystem::path& file) const; |
| 354 |
|
| 355 |
/** |
| 356 |
* Determine whether this package satisfies the given dependency |
| 357 |
*/ |
| 358 |
bool satisfies(const Meta::Dependency&); |
| 359 |
|
| 360 |
/** |
| 361 |
* Determine whether the current metadata specifies support for the currently-running version of |
| 362 |
* FreeCAD. Does not interrogate content items, which must be queried individually. |
| 363 |
*/ |
| 364 |
bool supportsCurrentFreeCAD() const; |
| 365 |
|
| 366 |
private: |
| 367 |
std::string _name; |
| 368 |
std::string _type; |
| 369 |
Meta::Version _version; |
| 370 |
std::string _date; |
| 371 |
std::string _description; |
| 372 |
std::vector<Meta::Contact> _maintainer; |
| 373 |
std::vector<Meta::License> _license; |
| 374 |
std::vector<Meta::Url> _url; |
| 375 |
std::vector<Meta::Contact> _author; |
| 376 |
std::vector<Meta::Dependency> _depend; |
| 377 |
std::vector<Meta::Dependency> _conflict; |
| 378 |
std::vector<Meta::Dependency> _replace; |
| 379 |
std::vector<std::string> _tag; |
| 380 |
std::filesystem::path _icon; |
| 381 |
std::string _classname; |
| 382 |
std::filesystem::path _subdirectory; |
| 383 |
std::vector<std::filesystem::path> _file; |
| 384 |
Meta::Version _freecadmin; |
| 385 |
Meta::Version _freecadmax; |
| 386 |
Meta::Version _pythonmin; |
| 387 |
|
| 388 |
std::multimap<std::string, Metadata> _content; |
| 389 |
|
| 390 |
std::multimap<std::string, Meta::GenericMetadata> _genericMetadata; |
| 391 |
|
| 392 |
XERCES_CPP_NAMESPACE::DOMElement* _dom; |
| 393 |
std::shared_ptr<XERCES_CPP_NAMESPACE::XercesDOMParser> _parser; |
| 394 |
|
| 395 |
void loadFromInputSource(const XERCES_CPP_NAMESPACE::InputSource& source); |
| 396 |
void parseVersion1(const XERCES_CPP_NAMESPACE::DOMNode* startNode); |
| 397 |
void parseContentNodeVersion1(const XERCES_CPP_NAMESPACE::DOMElement* contentNode); |
| 398 |
|
| 399 |
void appendToElement(XERCES_CPP_NAMESPACE::DOMElement* root) const; |
| 400 |
}; |
| 401 |
|
| 402 |
} // namespace App |
| 403 |
|
| 404 |
#endif |