From 7e147c7d80c3b80ddad9244e7006bb7004e8c60e Mon Sep 17 00:00:00 2001 From: Hosang Kim Date: Tue, 21 Feb 2023 13:41:45 +0900 Subject: [PATCH] AurumXML: refactoring AurumXML for improving performance Change-Id: I0bc48392281b479457956e6a73ed64ebe9115e23 --- libaurum/inc/AurumXML.h | 13 +++--- libaurum/src/AurumXML.cc | 114 +++++++++++++++++++++++------------------------ 2 files changed, 64 insertions(+), 63 deletions(-) diff --git a/libaurum/inc/AurumXML.h b/libaurum/inc/AurumXML.h index 6574b49..b18ace1 100644 --- a/libaurum/inc/AurumXML.h +++ b/libaurum/inc/AurumXML.h @@ -20,6 +20,7 @@ #include #include +#include #include "Accessible.h" #include "config.h" @@ -45,7 +46,7 @@ public: * * @since_tizen 7.0 */ - AurumXML(std::shared_ptr root, std::mutex& XMLMutex); + AurumXML(const std::shared_ptr root, std::mutex& XMLMutex); /** * @brief Destroy the AurumXML object @@ -72,7 +73,7 @@ public: * * @since_tizen 7.0 */ - std::string getXPath(std::shared_ptr node); + std::string getXPath(const std::shared_ptr& node); /** * @brief Finds that objects that satisfied with the xpath in the object tree. @@ -97,7 +98,7 @@ private: * * @since_tizen 7.0 */ - void traverse(xml_node element, std::shared_ptr node); + void traverse(xml_node& element, const std::shared_ptr& node); /** * @internal @@ -125,7 +126,7 @@ private: * * @since_tizen 7.0 */ - xml_node checkNode(std::shared_ptr node); + xml_node checkNode(const std::shared_ptr& node); /** * @internal @@ -138,11 +139,11 @@ private: * * @since_tizen 7.5 */ - std::shared_ptr checkParentNode(std::shared_ptr node); + std::shared_ptr checkParentNode(const std::shared_ptr& node); private: xml_document *mDoc; - std::shared_ptr mRoot; + const std::shared_ptr mRoot; std::unordered_map> mXNodeMap; std::mutex& XMLMutex; }; diff --git a/libaurum/src/AurumXML.cc b/libaurum/src/AurumXML.cc index 7ca1303..2925fde 100644 --- a/libaurum/src/AurumXML.cc +++ b/libaurum/src/AurumXML.cc @@ -19,7 +19,7 @@ using namespace Aurum; -AurumXML::AurumXML(std::shared_ptr root, std::mutex& XMLMutex) : mRoot(root), XMLMutex(XMLMutex) +AurumXML::AurumXML(const std::shared_ptr root, std::mutex& XMLMutex) : mRoot(root), XMLMutex(XMLMutex) { XMLMutex.lock(); @@ -34,7 +34,7 @@ AurumXML::~AurumXML() delete mDoc; } -void AurumXML::traverse(xml_node element, std::shared_ptr node) +void AurumXML::traverse(xml_node& element, const std::shared_ptr& node) { if (!node) return; @@ -65,25 +65,30 @@ void AurumXML::traverse(xml_node element, std::shared_ptr node) element.append_attribute("width") = size.width(); element.append_attribute("height") = size.height(); - element.append_attribute("checked") = node->isChecked(); - element.append_attribute("checkable") = node->isCheckable(); - element.append_attribute("clickable") = node->isClickable(); - element.append_attribute("enabled") = node->isEnabled(); - element.append_attribute("focused") = node->isFocused(); - element.append_attribute("focusable") = node->isFocusable(); - element.append_attribute("scrollable") = node->isScrollable(); - element.append_attribute("selected") = node->isSelected(); - element.append_attribute("showing") = node->isShowing(); - element.append_attribute("active") = node->isActive(); - element.append_attribute("visible") = node->isVisible(); - element.append_attribute("selectable") = node->isSelectable(); - element.append_attribute("highlightable") = node->isHighlightable(); + const std::vector> attributes = { + {"checked", node->isChecked()}, + {"checkable", node->isCheckable()}, + {"clickable", node->isClickable()}, + {"enabled", node->isEnabled()}, + {"focused", node->isFocused()}, + {"focusable", node->isFocusable()}, + {"scrollable", node->isScrollable()}, + {"selected", node->isSelected()}, + {"showing", node->isShowing()}, + {"active", node->isActive()}, + {"visible", node->isVisible()}, + {"selectable", node->isSelectable()}, + {"highlightable", node->isHighlightable()} + }; + for (const auto& attribute : attributes) { + element.append_attribute(attribute.first.c_str()) = attribute.second; + } mXNodeMap[node->getId()] = node; int childCnt = node->getChildCount(); for (int i = 0; i < childCnt; i++) { - std::shared_ptr childNode = node->getChildAt(i); + const std::shared_ptr& childNode = node->getChildAt(i); if (childNode->getRawHandler() == nullptr) continue; xml_node childElement = element.append_child(""); @@ -111,20 +116,19 @@ std::string AurumXML::getOptimalXPath(xml_document *doc, xml_node node) int count = 0; int index = 0; - const char *automationId = node.attribute("automationid").value(); - if (strlen(automationId) > 0) { - xpath += "//"; - xpath += node.name(); - xpath += "[@automationid=\""; - xpath += automationId; - xpath += "\"]"; + const auto automationId = node.attribute("automationid").value(); + const auto len = strlen(automationId); + if (len > 0) { + xpath.append("//") + .append(node.name()) + .append("[@automationid=\"") + .append(automationId) + .append("\"]"); try { - auto nodes = doc->select_nodes(xpath.c_str()); + const auto nodes = doc->select_nodes(xpath.c_str()); if (nodes.size() > 1) { - for (xpath_node_set::const_iterator it = nodes.begin(); - it != nodes.end(); ++it) { - xpath_node no = *it; + for (const auto& no : nodes) { count++; if (no.node() == node) { index = count; @@ -132,8 +136,8 @@ std::string AurumXML::getOptimalXPath(xml_document *doc, xml_node node) } } xpath.append(1, '[') - .append(std::to_string(index)) - .append(1, ']'); + .append(std::to_string(index)) + .append(1, ']'); } } catch (const xpath_exception &e) { LOGI("getOptimalXPath Error: %s", e.what()); @@ -142,10 +146,10 @@ std::string AurumXML::getOptimalXPath(xml_document *doc, xml_node node) return xpath; } - xml_node parent = node.parent(); + auto parent = node.parent(); if (parent) { - xml_node child = parent.first_child(); - for (xml_node el = child; el; el = el.next_sibling()) { + const xml_node& child = parent.first_child(); + for (auto el = child; el; el = el.next_sibling()) { if (el.name() && node.name() && !strcmp(el.name(), node.name())) { count++; if (el == node) index = count; @@ -153,39 +157,37 @@ std::string AurumXML::getOptimalXPath(xml_document *doc, xml_node node) } } - xpath += "/"; - xpath += node.name(); + xpath.append("/") + .append(node.name()); if (count > 1) - xpath.append(1, '[').append(std::to_string(index)).append(1, ']'); + xpath.append(1, '[') + .append(std::to_string(index)) + .append(1, ']'); if (node == doc->first_child()) return xpath; return getOptimalXPath(doc, parent).append(xpath); } -std::string makeQuery(std::string id) -{ - std::string query; - query += "//*[@id=\""; - query += id; - query += "\"]"; - - return query; +std::string makeQuery(const std::string& id) { + std::stringstream query; + query << "//*[@id=\"" << id << "\"]"; + return query.str(); } -std::shared_ptr AurumXML::checkParentNode(std::shared_ptr node) +std::shared_ptr AurumXML::checkParentNode(const std::shared_ptr& node) { - auto parent = node->getParent(); + const auto parent = node->getParent(); if (!parent) return nullptr; - std::string query = makeQuery(node->getId()); + const std::string query = makeQuery(node->getId()); xml_node xmlNode = mDoc->select_node(query.c_str()).node(); if (xmlNode) { - int childCnt = parent->getChildCount(); + int childCnt = parent->getChildCount(); for (int i = 0; i < childCnt; i++) { - std::shared_ptr childNode = parent->getChildAt(i); + const std::shared_ptr& childNode = parent->getChildAt(i); if (childNode->getRawHandler() == nullptr) continue; xml_node childElement = xmlNode.append_child(""); @@ -197,12 +199,12 @@ std::shared_ptr AurumXML::checkParentNode(std::shared_ptr node) +xml_node AurumXML::checkNode(const std::shared_ptr& node) { xml_node xmlNode; try { - std::string query = makeQuery(node->getId()); + const std::string query = makeQuery(node->getId()); xmlNode = mDoc->select_node(query.c_str()).node(); if (!xmlNode) { @@ -220,7 +222,7 @@ xml_node AurumXML::checkNode(std::shared_ptr node) return xmlNode; } -std::string AurumXML::getXPath(std::shared_ptr node) +std::string AurumXML::getXPath(const std::shared_ptr& node) { xml_node xmlNode = checkNode(node); @@ -243,16 +245,14 @@ std::vector> AurumXML::findObjects( try { if (earlyReturn) { xml_node node = mDoc->select_node(xpath.c_str()).node(); - std::string id(node.attribute("id").value()); + const std::string id(node.attribute("id").value()); if (mXNodeMap.count(id) > 0) ret.push_back(mXNodeMap[id]); } else { - xpath_node_set results = mDoc->select_nodes(xpath.c_str()); + const auto nodes = mDoc->select_nodes(xpath.c_str()); - for (xpath_node_set::const_iterator it = results.begin(); - it != results.end(); ++it) { - auto node = (*it).node(); - std::string id(node.attribute("id").value()); + for (const auto& no : nodes) { + const std::string id(no.node().attribute("id").value()); if (mXNodeMap.count(id) > 0) ret.push_back(mXNodeMap[id]); } -- 2.7.4