From ca45b38468e5be66ede4d3f7df025a098f674454 Mon Sep 17 00:00:00 2001 From: Hosang Kim Date: Thu, 14 Apr 2022 20:28:41 +0900 Subject: [PATCH] AurumXML: apply early return and update XMLTree when findObjects function is called Change-Id: Id1843e0490f2b13faf8e09b38222e7783f647203 --- libaurum/inc/AurumXML.h | 16 ++++++++-- libaurum/src/AurumXML.cc | 69 ++++++++++++++++++++++++++++------------ libaurum/src/Comparer.cc | 2 +- 3 files changed, 63 insertions(+), 24 deletions(-) diff --git a/libaurum/inc/AurumXML.h b/libaurum/inc/AurumXML.h index e146ac0..989320d 100644 --- a/libaurum/inc/AurumXML.h +++ b/libaurum/inc/AurumXML.h @@ -82,7 +82,7 @@ public: * * @since_tizen 7.0 */ - std::vector> findObjects(std::string xpath); + std::vector> findObjects(std::string xpath, bool earlyReturn = false); private: /** @@ -113,10 +113,22 @@ private: */ std::string getOptimalXPath(xml_document *doc, xml_node node); + /** + * @internal + * + * @brief Check xml_node exists or not + * + * @param string id + * + * @return xml_node + * + * @since_tizen 7.0 + */ + xml_node checkNode(std::string id); + private: xml_document *mDoc; std::shared_ptr mRoot; - std::unordered_map mXPathMap; std::unordered_map> mXNodeMap; }; } // namespace Aurum diff --git a/libaurum/src/AurumXML.cc b/libaurum/src/AurumXML.cc index c3da79b..e90790e 100644 --- a/libaurum/src/AurumXML.cc +++ b/libaurum/src/AurumXML.cc @@ -42,8 +42,7 @@ void AurumXML::traverse(xml_node element, std::shared_ptr node) else name = node->getType(); - if (!name.compare("application")) - name = node->getPkg(); + if (!name.compare("application")) name = node->getPkg(); // Remove white spaces. name.erase(remove(name.begin(), name.end(), ' '), name.end()); @@ -160,45 +159,73 @@ std::string AurumXML::getOptimalXPath(xml_document *doc, xml_node node) return getOptimalXPath(doc, parent).append(xpath); } -std::string AurumXML::getXPath(std::string id) +std::string makeQuery(std::string id) { - if (mXPathMap.count(id) > 0) return mXPathMap[id]; - std::string query; query += "//*[@id=\""; query += id; query += "\"]"; - try { - auto node = mDoc->select_node(query.c_str()).node(); + return query; +} - if (!node) createXMLtree(); +xml_node AurumXML::checkNode(std::string id) +{ + xml_node node; + try { + std::string query = makeQuery(id); + node = mDoc->select_node(query.c_str()).node(); - if (node) { - std::string xpath = getOptimalXPath(mDoc, node); - mXPathMap[id] = xpath; - return xpath; + if (!node) { + createXMLtree(); + node = mDoc->select_node(query.c_str()).node(); } - } catch (const xpath_exception &e) { LOGI("getXPath Error: %s", e.what()); } + return node; +} + +std::string AurumXML::getXPath(std::string id) +{ + xml_node node = checkNode(id); + + if (node) { + std::string xpath = getOptimalXPath(mDoc, node); + return xpath; + } return "NotSupported"; } -std::vector> AurumXML::findObjects(std::string xpath) +std::vector> AurumXML::findObjects( + std::string xpath, bool earlyReturn) { std::vector> ret; - xpath_node_set results = 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()); + createXMLtree(); + + LOGI("xpath %s earlyReturn %d", xpath.c_str(), earlyReturn); + try { + if (earlyReturn) { + xml_node node = mDoc->select_node(xpath.c_str()).node(); + 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()); - if (mXNodeMap.count(id) > 0) ret.push_back(mXNodeMap[id]); + for (xpath_node_set::const_iterator it = results.begin(); + it != results.end(); ++it) { + auto node = (*it).node(); + std::string id(node.attribute("id").value()); + + if (mXNodeMap.count(id) > 0) ret.push_back(mXNodeMap[id]); + } + } + } catch (const xpath_exception &e) { + LOGI("findObjects Error: %s", e.what()); } return ret; -} \ No newline at end of file +} diff --git a/libaurum/src/Comparer.cc b/libaurum/src/Comparer.cc index e559730..c805a97 100644 --- a/libaurum/src/Comparer.cc +++ b/libaurum/src/Comparer.cc @@ -67,7 +67,7 @@ std::vector> Comparer::findObjects(const std::sh auto XMLDoc = XMLDocMap[pkg]; - auto tmp = XMLDoc->findObjects(selector->mXPath); + auto tmp = XMLDoc->findObjects(selector->mXPath, earlyReturn); std::move(std::begin(tmp), std::end(tmp), std::back_inserter(merged)); return merged; -- 2.34.1