AurumXML: fix find elements for XPath. 74/291074/1 accepted/tizen/unified/20230413.123737
authorHosang Kim <hosang12.kim@samsung.com>
Fri, 7 Apr 2023 10:17:03 +0000 (19:17 +0900)
committerHosang Kim <hosang12.kim@samsung.com>
Fri, 7 Apr 2023 10:17:03 +0000 (19:17 +0900)
The previous approach involved creating a new tree and then finding the item, but it has been modified to first find the item and if unsuccessful, then create a new tree.

Change-Id: I409a4af4d7bb6a4bcfb13bfdab002fd78a2b1e20

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

index 9bc84a4..f4cf4c8 100644 (file)
@@ -142,6 +142,21 @@ private:
      */
     std::shared_ptr<AccessibleNode> checkParentNode(const std::shared_ptr<AccessibleNode>& node);
 
+    /**
+     * @internal
+     *
+     * @brief Finds XNode that satisfied with the xpath in the object tree.
+     *
+     * @param ret vector contains objects
+     *
+     * @param xpath
+     *
+     * @param earlyReturn
+     *
+     * @since_tizen 7.5
+     */
+    void findXNodes(std::vector<std::shared_ptr<AccessibleNode>> &ret, std::string xpath, bool earlyReturn);
+
 private:
     xml_document                                *mDoc;
     const std::shared_ptr<AccessibleNode>        mRoot;
index 8f3ba78..36dfa1b 100644 (file)
@@ -46,7 +46,6 @@ void AurumXML::traverse(xml_node& element, const std::shared_ptr<AccessibleNode>
     node->updateRoleName();
     node->updateAttributes();
     node->updateToolkitName();
-    node->updateApplication();
 
     std::string name;
     if (node->getType().empty())
@@ -54,7 +53,10 @@ void AurumXML::traverse(xml_node& element, const std::shared_ptr<AccessibleNode>
     else
         name = node->getType();
 
-    if (!name.compare("application")) name = node->getPkg();
+    if (!name.compare("application")) {
+        node->updateApplication();
+        name = node->getPkg();
+    }
 
     // Remove white spaces.
     name.erase(remove(name.begin(), name.end(), ' '), name.end());
@@ -68,7 +70,7 @@ void AurumXML::traverse(xml_node& element, const std::shared_ptr<AccessibleNode>
     mXNodeMap[node->getId()] = node;
 
     auto children = node->getChildren();
-    for (auto &child : children)
+    for (const auto &child : children)
     {
         if (child->getRawHandler() == nullptr) continue;
 
@@ -84,7 +86,7 @@ bool AurumXML::createXMLtree()
     mDoc->remove_children();
 
     xml_node element = mDoc->append_child("");
-
+    
     traverse(element, mRoot);
 
     return true;
@@ -167,7 +169,7 @@ std::shared_ptr<AccessibleNode> AurumXML::checkParentNode(const std::shared_ptr<
 
     if (xmlNode) {
         auto children = node->getChildren();
-        for (auto &child : children) {
+        for (const auto &child : children) {
             if (child->getRawHandler() == nullptr) continue;
 
             xml_node childElement = xmlNode.append_child("");
@@ -214,26 +216,33 @@ std::string AurumXML::getXPath(const std::shared_ptr<AccessibleNode>& node)
     return "NotSupported";
 }
 
-void AurumXML::findObjects(std::vector<std::shared_ptr<AccessibleNode>> &ret,
+void AurumXML::findXNodes(std::vector<std::shared_ptr<AccessibleNode>> &ret,
     std::string xpath, bool earlyReturn)
 {
-    createXMLtree();
-
-    LOGI("xpath %s earlyReturn %d", xpath.c_str(), earlyReturn);
-    try {
-        if (earlyReturn) {
-            xml_node    node = mDoc->select_node(xpath.c_str()).node();
-            const std::string id(node.attribute("id").value());
-            if (mXNodeMap.count(id) > 0) ret.push_back(mXNodeMap[id]);
+    if (earlyReturn) {
+        xml_node    node = mDoc->select_node(xpath.c_str()).node();
+        const std::string id(node.attribute("id").value());
+        if (mXNodeMap.count(id) > 0) ret.push_back(mXNodeMap[id]);
+    } else {
+        const auto nodes = mDoc->select_nodes(xpath.c_str());
 
-        } else {
-            const auto nodes = mDoc->select_nodes(xpath.c_str());
+        for (const auto& no : nodes) {
+            const std::string id(no.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]);
+        }
+    }    
+}
 
-                if (mXNodeMap.count(id) > 0) ret.push_back(mXNodeMap[id]);
-            }
+void AurumXML::findObjects(std::vector<std::shared_ptr<AccessibleNode>> &ret,
+    std::string xpath, bool earlyReturn)
+{
+    LOGI("xpath %s earlyReturn %d", xpath.c_str(), earlyReturn);
+    try {
+        findXNodes(ret, xpath, earlyReturn);
+        if (ret.size() == 0) {
+            createXMLtree();
+            findXNodes(ret, xpath, earlyReturn);
         }
     } catch (const xpath_exception &e) {
         LOGI("findObjects Error: %s", e.what());