AurumXML: apply early return and update XMLTree when findObjects function is called 54/273854/2
authorHosang Kim <hosang12.kim@samsung.com>
Thu, 14 Apr 2022 11:28:41 +0000 (20:28 +0900)
committerkim hosang <hosang12.kim@samsung.com>
Fri, 22 Apr 2022 04:43:28 +0000 (04:43 +0000)
Change-Id: Id1843e0490f2b13faf8e09b38222e7783f647203

libaurum/inc/AurumXML.h
libaurum/src/AurumXML.cc
libaurum/src/Comparer.cc

index e146ac0f3bfe4a1e6328c07fcd93ddc75531115b..989320da49548524589d93d8e9d0536121e678f1 100644 (file)
@@ -82,7 +82,7 @@ public:
      *
      * @since_tizen 7.0
      */
-    std::vector<std::shared_ptr<AccessibleNode>> findObjects(std::string xpath);
+    std::vector<std::shared_ptr<AccessibleNode>> 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<AccessibleNode>              mRoot;
-    std::unordered_map<std::string, std::string> mXPathMap;
     std::unordered_map<std::string, std::shared_ptr<AccessibleNode>> mXNodeMap;
 };
 }  // namespace Aurum
index c3da79b13de66be45835c0243c2eec8ca84333b9..e90790e66dd4f8fc572af937bc239e001fc30058 100644 (file)
@@ -42,8 +42,7 @@ void AurumXML::traverse(xml_node element, std::shared_ptr<AccessibleNode> 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<std::shared_ptr<AccessibleNode>> AurumXML::findObjects(std::string xpath)
+std::vector<std::shared_ptr<AccessibleNode>> AurumXML::findObjects(
+    std::string xpath, bool earlyReturn)
 {
     std::vector<std::shared_ptr<AccessibleNode>> 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
+}
index e5597308648f0f60484c939b8bb5bc76f13154a2..c805a97971d2d831f66c20a671fe739b59ca0e23 100644 (file)
@@ -67,7 +67,7 @@ std::vector<std::shared_ptr<AccessibleNode>> 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;