OSDN Git Service

Reworked search logic to better handle multiple positive/negative matching of the...
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / sql / REnSearch.java
index c6802e6..cec989a 100644 (file)
@@ -129,19 +129,14 @@ public class REnSearch {
                        \r
                        if (tagNames.size() == 0 && !negative)\r
                                return false;\r
-                       if (tagNames.size() == 0 && negative)\r
-                               return true;\r
                        \r
-                       boolean good = false;\r
-                       for (int i=0; i<tagNames.size() && !good; i++) {                \r
+                       for (int i=0; i<tagNames.size(); i++) { \r
                                boolean matches = Pattern.matches(filterName.toLowerCase(),tagNames.get(i).toLowerCase());\r
-                               if (matches && !negative)\r
-                                       good = true;\r
-                               if (!matches && negative)\r
-                                       good = true;\r
+                               if (!matches && !negative)\r
+                                       return false;\r
+                               if (matches && negative)\r
+                                       return false;\r
                        }\r
-                       if (!good)\r
-                               return false;\r
                }\r
                return true;\r
        }\r
@@ -152,8 +147,7 @@ public class REnSearch {
                        return true;\r
                \r
                boolean negative = false;               \r
-               boolean found = false;\r
-               \r
+\r
                for (int j=0; j<list.size(); j++) {\r
                        negative = false;\r
                        if (list.get(j).startsWith("-"))\r
@@ -162,19 +156,16 @@ public class REnSearch {
                        String filterName = cleanupWord(list.get(j).substring(pos+1));\r
                        filterName = filterName.replace("*", ".*");   // setup for regular expression pattern match\r
                        \r
-                       if (tagNames.size() == 0)\r
-                               found = false;\r
+                       if (tagNames.size() == 0 && !negative)\r
+                               return false;\r
 \r
                        for (int i=0; i<tagNames.size(); i++) {         \r
                                boolean matches = Pattern.matches(filterName.toLowerCase(),tagNames.get(i).toLowerCase());\r
-                               if (matches)\r
-                                       found = true;\r
+                               if (!matches && !negative)\r
+                                       return false;\r
                        }\r
                }\r
-               if (negative)\r
-                       return !found;\r
-               else\r
-                       return found;\r
+               return true;\r
        }\r
        \r
        \r
@@ -507,18 +498,6 @@ public class REnSearch {
                if (todo.size() == 0 && resource.size() == 0 && searchPhrases.size() == 0)\r
                        return true;\r
                \r
-               boolean returnTodo = false;\r
-               boolean returnResource = false;\r
-               boolean returnPhrase = false;\r
-               \r
-               if (todo.size() == 0)\r
-                       returnTodo = true;\r
-               if (resource.size() == 0)\r
-                       returnResource = true;\r
-               if (searchPhrases.size() == 0)\r
-                       returnPhrase = true;\r
-               \r
-               \r
                n = conn.getNoteTable().getNote(n.getGuid(), true, true, false, false, false);\r
                \r
                // Check for search phrases\r
@@ -533,12 +512,11 @@ public class REnSearch {
                                negative = false;\r
                        phrase = phrase.substring(1);\r
                        phrase = phrase.substring(0,phrase.length()-1);\r
-                       if (text.indexOf(phrase)>=0) {\r
-                               if (!negative)\r
-                                       returnPhrase = true;\r
-                       }\r
-                       if (text.indexOf(phrase)<0 && negative)\r
-                               returnPhrase = true;\r
+                       if (text.indexOf(phrase)>=0 && negative) {\r
+                               return false;\r
+                       } \r
+                       if (text.indexOf(phrase) < 0 && !negative)\r
+                               return false;\r
                }\r
 \r
                \r
@@ -555,14 +533,13 @@ public class REnSearch {
                        if (value.startsWith("-"))\r
                                desiredState = !desiredState;\r
                        int pos = n.getContent().indexOf("<en-todo");\r
-                       if (pos == -1 && value.startsWith("-") && (value.endsWith("*") || value.endsWith(":")))\r
-                               return true;\r
+                       if (pos == -1 && !value.startsWith("-"))\r
+                               return false;\r
                        if (pos > -1 && value.startsWith("-") && (value.endsWith("*") || value.endsWith(":")))\r
                                return false;\r
-                       if (pos == -1) \r
+                       if (pos == -1 && !value.startsWith("-")\r
                                return false;\r
-                       if (value.endsWith("*"))\r
-                               returnTodo = true;\r
+                       boolean returnTodo = false;\r
                        while (pos > -1) {\r
                                int endPos = n.getContent().indexOf("/>", pos);\r
                                String segment = n.getContent().substring(pos, endPos);\r
@@ -571,11 +548,15 @@ public class REnSearch {
                                        currentState = false;\r
                                else\r
                                        currentState = true;\r
-                               if (desiredState == currentState)\r
+                               if (desiredState == currentState) \r
+                                       returnTodo = true;\r
+                               if (value.endsWith("*") || value.endsWith(":"))\r
                                        returnTodo = true;\r
                                \r
                                pos = n.getContent().indexOf("<en-todo", pos+1);\r
                        }\r
+                       if (!returnTodo)\r
+                               return false;\r
                }\r
                \r
                // Check resources\r
@@ -590,13 +571,14 @@ public class REnSearch {
                                return false;\r
                        for (int j=0; j<n.getResourcesSize(); j++) {\r
                                boolean match = stringMatch(n.getResources().get(j).getMime(), resourceString, negative);\r
-                               if (!match)\r
+                               if (!match && !negative)\r
+                                       return false;\r
+                               if (match && negative) \r
                                        return false;\r
-                               returnResource = true;\r
                        }\r
                }\r
                \r
-               return returnResource && returnTodo && returnPhrase;\r
+               return true;\r
        }\r
        \r
        private boolean stringMatch(String content, String text, boolean negative) {\r