Base: Added helper macro for caching transcode-ed string literal

This commit is contained in:
Kohei Takahashi
2024-12-25 10:42:33 +09:00
parent 6e5b6e3ead
commit f032b44f44
5 changed files with 146 additions and 118 deletions

View File

@@ -158,7 +158,7 @@ void Metadata::loadFromInputSource(const InputSource& source)
throw Base::XMLBaseException(
"Malformed package.xml document: Root <package> group not found");
}
auto formatVersion = XMLString::parseInt(_dom->getAttribute(XUTF8Str("format").unicodeForm()));
auto formatVersion = XMLString::parseInt(_dom->getAttribute(XUTF8StrLiteral("format").unicodeForm()));
switch (formatVersion) {
case 1:
parseVersion1(_dom);
@@ -560,10 +560,10 @@ void addAttribute(DOMElement* node, const std::string& key, const std::string& v
void addAttribute(DOMElement* node, const std::string& key, bool value)
{
if (value) {
node->setAttribute(XUTF8Str(key.c_str()).unicodeForm(), XUTF8Str("True").unicodeForm());
node->setAttribute(XUTF8Str(key.c_str()).unicodeForm(), XUTF8StrLiteral("True").unicodeForm());
}
else {
node->setAttribute(XUTF8Str(key.c_str()).unicodeForm(), XUTF8Str("False").unicodeForm());
node->setAttribute(XUTF8Str(key.c_str()).unicodeForm(), XUTF8StrLiteral("False").unicodeForm());
}
}
@@ -608,13 +608,13 @@ void addDependencyNode(DOMElement* root, const std::string& name, const Meta::De
void Metadata::write(const fs::path& file) const
{
DOMImplementation* impl =
DOMImplementationRegistry::getDOMImplementation(XUTF8Str("Core LS").unicodeForm());
DOMImplementationRegistry::getDOMImplementation(XUTF8StrLiteral("Core LS").unicodeForm());
DOMDocument* doc = impl->createDocument(nullptr, XUTF8Str("package").unicodeForm(), nullptr);
DOMDocument* doc = impl->createDocument(nullptr, XUTF8StrLiteral("package").unicodeForm(), nullptr);
DOMElement* root = doc->getDocumentElement();
root->setAttribute(XUTF8Str("format").unicodeForm(), XUTF8Str("1").unicodeForm());
root->setAttribute(XUTF8Str("xmlns").unicodeForm(),
XUTF8Str("https://wiki.freecad.org/Package_Metadata").unicodeForm());
root->setAttribute(XUTF8StrLiteral("format").unicodeForm(), XUTF8StrLiteral("1").unicodeForm());
root->setAttribute(XUTF8StrLiteral("xmlns").unicodeForm(),
XUTF8StrLiteral("https://wiki.freecad.org/Package_Metadata").unicodeForm());
appendToElement(root);
@@ -852,7 +852,7 @@ void Metadata::appendToElement(DOMElement* root) const
if (!_content.empty()) {
auto doc = root->getOwnerDocument();
DOMElement* contentRootElement = doc->createElement(XUTF8Str("content").unicodeForm());
DOMElement* contentRootElement = doc->createElement(XUTF8StrLiteral("content").unicodeForm());
root->appendChild(contentRootElement);
for (const auto& content : _content) {
DOMElement* contentElement =
@@ -982,7 +982,7 @@ Meta::Contact::Contact(const XERCES_CPP_NAMESPACE::DOMElement* elem)
if (!elem) {
return;
}
auto emailAttribute = elem->getAttribute(XUTF8Str("email").unicodeForm());
auto emailAttribute = elem->getAttribute(XUTF8StrLiteral("email").unicodeForm());
name = StrXUTF8(elem->getTextContent()).str;
email = StrXUTF8(emailAttribute).str;
}
@@ -1004,7 +1004,7 @@ Meta::License::License(const XERCES_CPP_NAMESPACE::DOMElement* elem)
if (!elem) {
return;
}
auto fileAttribute = elem->getAttribute(XUTF8Str("file").unicodeForm());
auto fileAttribute = elem->getAttribute(XUTF8StrLiteral("file").unicodeForm());
if (XMLString::stringLen(fileAttribute) > 0) {
file = fs::path(StrXUTF8(fileAttribute).str);
}
@@ -1033,7 +1033,7 @@ Meta::Url::Url(const XERCES_CPP_NAMESPACE::DOMElement* elem)
if (!elem) {
return;
}
auto typeAttribute = StrXUTF8(elem->getAttribute(XUTF8Str("type").unicodeForm())).str;
auto typeAttribute = StrXUTF8(elem->getAttribute(XUTF8StrLiteral("type").unicodeForm())).str;
if (typeAttribute.empty() || typeAttribute == "website") {
type = UrlType::website;
}
@@ -1057,7 +1057,7 @@ Meta::Url::Url(const XERCES_CPP_NAMESPACE::DOMElement* elem)
}
if (type == UrlType::repository) {
branch = StrXUTF8(elem->getAttribute(XUTF8Str("branch").unicodeForm())).str;
branch = StrXUTF8(elem->getAttribute(XUTF8StrLiteral("branch").unicodeForm())).str;
}
location = StrXUTF8(elem->getTextContent()).str;
}
@@ -1083,13 +1083,13 @@ App::Meta::Dependency::Dependency(std::string pkg)
Meta::Dependency::Dependency(const XERCES_CPP_NAMESPACE::DOMElement* elem)
{
version_lt = StrXUTF8(elem->getAttribute(XUTF8Str("version_lt").unicodeForm())).str;
version_lte = StrXUTF8(elem->getAttribute(XUTF8Str("version_lte").unicodeForm())).str;
version_eq = StrXUTF8(elem->getAttribute(XUTF8Str("version_eq").unicodeForm())).str;
version_gte = StrXUTF8(elem->getAttribute(XUTF8Str("version_gte").unicodeForm())).str;
version_gt = StrXUTF8(elem->getAttribute(XUTF8Str("version_gt").unicodeForm())).str;
condition = StrXUTF8(elem->getAttribute(XUTF8Str("condition").unicodeForm())).str;
std::string opt_string = StrXUTF8(elem->getAttribute(XUTF8Str("optional").unicodeForm())).str;
version_lt = StrXUTF8(elem->getAttribute(XUTF8StrLiteral("version_lt").unicodeForm())).str;
version_lte = StrXUTF8(elem->getAttribute(XUTF8StrLiteral("version_lte").unicodeForm())).str;
version_eq = StrXUTF8(elem->getAttribute(XUTF8StrLiteral("version_eq").unicodeForm())).str;
version_gte = StrXUTF8(elem->getAttribute(XUTF8StrLiteral("version_gte").unicodeForm())).str;
version_gt = StrXUTF8(elem->getAttribute(XUTF8StrLiteral("version_gt").unicodeForm())).str;
condition = StrXUTF8(elem->getAttribute(XUTF8StrLiteral("condition").unicodeForm())).str;
std::string opt_string = StrXUTF8(elem->getAttribute(XUTF8StrLiteral("optional").unicodeForm())).str;
if (opt_string == "true"
|| opt_string == "True") { // Support Python capitalization in this one case...
optional = true;
@@ -1097,7 +1097,7 @@ Meta::Dependency::Dependency(const XERCES_CPP_NAMESPACE::DOMElement* elem)
else {
optional = false;
}
std::string type_string = StrXUTF8(elem->getAttribute(XUTF8Str("type").unicodeForm())).str;
std::string type_string = StrXUTF8(elem->getAttribute(XUTF8StrLiteral("type").unicodeForm())).str;
if (type_string == "automatic" || type_string.empty()) {
dependencyType = Meta::DependencyType::automatic;
}

View File

@@ -84,12 +84,12 @@ public:
std::map<std::string, std::string> propMap = initMap();
DOMNodeList* nodes = xmlDocument->getElementsByTagName(XStr("Properties").unicodeForm());
DOMNodeList* nodes = xmlDocument->getElementsByTagName(XStrLiteral("Properties").unicodeForm());
for (XMLSize_t i = 0; i < nodes->getLength(); i++) {
DOMNode* node = nodes->item(i);
if (node->getNodeType() == DOMNode::ELEMENT_NODE) {
auto elem = static_cast<DOMElement*>(node); // NOLINT
DOMNodeList* propList = elem->getElementsByTagName(XStr("Property").unicodeForm());
DOMNodeList* propList = elem->getElementsByTagName(XStrLiteral("Property").unicodeForm());
for (XMLSize_t j = 0; j < propList->getLength(); j++) {
DOMNode* propNode = propList->item(j);
readProperty(propNode, propMap);
@@ -105,12 +105,12 @@ private:
void readProgramVersion()
{
if (DOMNodeList* nodes =
xmlDocument->getElementsByTagName(XStr("Document").unicodeForm())) {
xmlDocument->getElementsByTagName(XStrLiteral("Document").unicodeForm())) {
for (XMLSize_t i = 0; i < nodes->getLength(); i++) {
DOMNode* node = nodes->item(i);
if (node->getNodeType() == DOMNode::ELEMENT_NODE) {
DOMNode* nameAttr =
node->getAttributes()->getNamedItem(XStr("ProgramVersion").unicodeForm());
node->getAttributes()->getNamedItem(XStrLiteral("ProgramVersion").unicodeForm());
if (nameAttr) {
std::string value = StrX(nameAttr->getNodeValue()).c_str();
metadata.programVersion = value;
@@ -153,7 +153,7 @@ private:
static void readProperty(DOMNode* propNode, std::map<std::string, std::string>& propMap)
{
DOMNode* nameAttr = propNode->getAttributes()->getNamedItem(XStr("name").unicodeForm());
DOMNode* nameAttr = propNode->getAttributes()->getNamedItem(XStrLiteral("name").unicodeForm());
if (nameAttr) {
std::string name = StrX(nameAttr->getNodeValue()).c_str();
auto it = propMap.find(name);
@@ -169,7 +169,7 @@ private:
if (DOMElement* child =
static_cast<DOMElement*>(node)->getFirstElementChild()) { // NOLINT
if (DOMNode* nameAttr =
child->getAttributes()->getNamedItem(XStr("value").unicodeForm())) {
child->getAttributes()->getNamedItem(XStrLiteral("value").unicodeForm())) {
std::string value = StrX(nameAttr->getNodeValue()).c_str();
return value;
}
@@ -260,18 +260,18 @@ std::list<ProjectFile::Object> ProjectFile::getObjects() const
return names;
}
DOMNodeList* nodes = xmlDocument->getElementsByTagName(XStr("Objects").unicodeForm());
DOMNodeList* nodes = xmlDocument->getElementsByTagName(XStrLiteral("Objects").unicodeForm());
for (XMLSize_t i = 0; i < nodes->getLength(); i++) {
DOMNode* node = nodes->item(i);
if (node->getNodeType() == DOMNode::ELEMENT_NODE) {
DOMNodeList* objectList = static_cast<DOMElement*>(node)->getElementsByTagName(
XStr("Object").unicodeForm()); // NOLINT
XStrLiteral("Object").unicodeForm()); // NOLINT
for (XMLSize_t j = 0; j < objectList->getLength(); j++) {
DOMNode* objectNode = objectList->item(j);
DOMNode* typeAttr =
objectNode->getAttributes()->getNamedItem(XStr("type").unicodeForm());
objectNode->getAttributes()->getNamedItem(XStrLiteral("type").unicodeForm());
DOMNode* nameAttr =
objectNode->getAttributes()->getNamedItem(XStr("name").unicodeForm());
objectNode->getAttributes()->getNamedItem(XStrLiteral("name").unicodeForm());
if (typeAttr && nameAttr) {
Object obj;
obj.name = StrX(nameAttr->getNodeValue()).c_str();
@@ -292,18 +292,18 @@ std::list<std::string> ProjectFile::getObjectsOfType(const Base::Type& typeId) c
return names;
}
DOMNodeList* nodes = xmlDocument->getElementsByTagName(XStr("Objects").unicodeForm());
DOMNodeList* nodes = xmlDocument->getElementsByTagName(XStrLiteral("Objects").unicodeForm());
for (XMLSize_t i = 0; i < nodes->getLength(); i++) {
DOMNode* node = nodes->item(i);
if (node->getNodeType() == DOMNode::ELEMENT_NODE) {
DOMNodeList* objectList = static_cast<DOMElement*>(node)->getElementsByTagName(
XStr("Object").unicodeForm()); // NOLINT
XStrLiteral("Object").unicodeForm()); // NOLINT
for (XMLSize_t j = 0; j < objectList->getLength(); j++) {
DOMNode* objectNode = objectList->item(j);
DOMNode* typeAttr =
objectNode->getAttributes()->getNamedItem(XStr("type").unicodeForm());
objectNode->getAttributes()->getNamedItem(XStrLiteral("type").unicodeForm());
DOMNode* nameAttr =
objectNode->getAttributes()->getNamedItem(XStr("name").unicodeForm());
objectNode->getAttributes()->getNamedItem(XStrLiteral("name").unicodeForm());
if (typeAttr && nameAttr) {
if (Base::Type::fromName(StrX(typeAttr->getNodeValue()).c_str()) == typeId) {
names.emplace_back(StrX(nameAttr->getNodeValue()).c_str());
@@ -366,18 +366,18 @@ Base::Type ProjectFile::getTypeId(const std::string& name) const
return Base::Type::badType();
}
DOMNodeList* nodes = xmlDocument->getElementsByTagName(XStr("Objects").unicodeForm());
DOMNodeList* nodes = xmlDocument->getElementsByTagName(XStrLiteral("Objects").unicodeForm());
for (XMLSize_t i = 0; i < nodes->getLength(); i++) {
DOMNode* node = nodes->item(i);
if (node->getNodeType() == DOMNode::ELEMENT_NODE) {
DOMNodeList* objectList = static_cast<DOMElement*>(node)->getElementsByTagName(
XStr("Object").unicodeForm()); // NOLINT
XStrLiteral("Object").unicodeForm()); // NOLINT
for (XMLSize_t j = 0; j < objectList->getLength(); j++) {
DOMNode* objectNode = objectList->item(j);
DOMNode* typeAttr =
objectNode->getAttributes()->getNamedItem(XStr("type").unicodeForm());
objectNode->getAttributes()->getNamedItem(XStrLiteral("type").unicodeForm());
DOMNode* nameAttr =
objectNode->getAttributes()->getNamedItem(XStr("name").unicodeForm());
objectNode->getAttributes()->getNamedItem(XStrLiteral("name").unicodeForm());
if (typeAttr && nameAttr) {
if (strcmp(name.c_str(), StrX(nameAttr->getNodeValue()).c_str()) == 0) {
std::string typeId = StrX(typeAttr->getNodeValue()).c_str();
@@ -407,16 +407,16 @@ std::list<ProjectFile::PropertyFile> ProjectFile::getPropertyFiles(const std::st
}
std::list<PropertyFile> files;
DOMNodeList* nodes = xmlDocument->getElementsByTagName(XStr("ObjectData").unicodeForm());
DOMNodeList* nodes = xmlDocument->getElementsByTagName(XStrLiteral("ObjectData").unicodeForm());
for (XMLSize_t i = 0; i < nodes->getLength(); i++) {
DOMNode* node = nodes->item(i);
if (node->getNodeType() == DOMNode::ELEMENT_NODE) {
DOMNodeList* objectList = static_cast<DOMElement*>(node)->getElementsByTagName(
XStr("Object").unicodeForm()); // NOLINT
XStrLiteral("Object").unicodeForm()); // NOLINT
for (XMLSize_t j = 0; j < objectList->getLength(); j++) {
DOMNode* objectNode = objectList->item(j);
DOMNode* nameAttr =
objectNode->getAttributes()->getNamedItem(XStr("name").unicodeForm());
objectNode->getAttributes()->getNamedItem(XStrLiteral("name").unicodeForm());
if (nameAttr && strcmp(name.c_str(), StrX(nameAttr->getNodeValue()).c_str()) == 0) {
// now go recursively through the sub-tree (i.e. the properties) and collect
// every file attribute
@@ -434,18 +434,18 @@ void ProjectFile::findFiles(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* node,
{
if (node->hasAttributes()) {
ProjectFile::PropertyFile prop;
DOMNode* fileAttr = node->getAttributes()->getNamedItem(XStr("file").unicodeForm());
DOMNode* fileAttr = node->getAttributes()->getNamedItem(XStrLiteral("file").unicodeForm());
if (fileAttr) {
DOMNode* parentNode = node->getParentNode();
if (parentNode) {
DOMNode* nameAttr =
parentNode->getAttributes()->getNamedItem(XStr("name").unicodeForm());
parentNode->getAttributes()->getNamedItem(XStrLiteral("name").unicodeForm());
if (nameAttr) {
prop.name = StrX(nameAttr->getNodeValue()).c_str();
}
DOMNode* typeAttr =
parentNode->getAttributes()->getNamedItem(XStr("type").unicodeForm());
parentNode->getAttributes()->getNamedItem(XStrLiteral("type").unicodeForm());
if (typeAttr) {
prop.type = Base::Type::fromName(StrX(typeAttr->getNodeValue()).c_str());
}
@@ -486,16 +486,16 @@ std::list<std::string> ProjectFile::getInputFiles(const std::string& name) const
}
std::list<std::string> files;
DOMNodeList* nodes = xmlDocument->getElementsByTagName(XStr("ObjectData").unicodeForm());
DOMNodeList* nodes = xmlDocument->getElementsByTagName(XStrLiteral("ObjectData").unicodeForm());
for (XMLSize_t i = 0; i < nodes->getLength(); i++) {
DOMNode* node = nodes->item(i);
if (node->getNodeType() == DOMNode::ELEMENT_NODE) {
DOMNodeList* objectList = static_cast<DOMElement*>(node)->getElementsByTagName(
XStr("Object").unicodeForm()); // NOLINT
XStrLiteral("Object").unicodeForm()); // NOLINT
for (XMLSize_t j = 0; j < objectList->getLength(); j++) {
DOMNode* objectNode = objectList->item(j);
DOMNode* nameAttr =
objectNode->getAttributes()->getNamedItem(XStr("name").unicodeForm());
objectNode->getAttributes()->getNamedItem(XStrLiteral("name").unicodeForm());
if (nameAttr && strcmp(name.c_str(), StrX(nameAttr->getNodeValue()).c_str()) == 0) {
// now go recursively through the sub-tree (i.e. the properties) and collect
// every file attribute
@@ -512,7 +512,7 @@ void ProjectFile::findFiles(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* node,
std::list<std::string>& files) const
{
if (node->hasAttributes()) {
DOMNode* fileAttr = node->getAttributes()->getNamedItem(XStr("file").unicodeForm());
DOMNode* fileAttr = node->getAttributes()->getNamedItem(XStrLiteral("file").unicodeForm());
if (fileAttr) {
files.emplace_back(StrX(fileAttr->getNodeValue()).c_str());
}