diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-05-02 21:50:05 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-05-02 21:50:05 +0200 |
commit | 66835b12c35dad7a8ddbe816556635635b836e95 (patch) | |
tree | f870d5a7d98a42ddb0707edf8d55c2b6307293ad /src/main/java/de/pixart/messenger/utils | |
parent | 536106b0793495a3635d641c78cedf65ec1723f7 (diff) |
search term parsing + highlighting
Diffstat (limited to 'src/main/java/de/pixart/messenger/utils')
-rw-r--r-- | src/main/java/de/pixart/messenger/utils/FtsUtils.java | 94 | ||||
-rw-r--r-- | src/main/java/de/pixart/messenger/utils/StylingHelper.java | 10 |
2 files changed, 103 insertions, 1 deletions
diff --git a/src/main/java/de/pixart/messenger/utils/FtsUtils.java b/src/main/java/de/pixart/messenger/utils/FtsUtils.java new file mode 100644 index 000000000..86eeeeeaf --- /dev/null +++ b/src/main/java/de/pixart/messenger/utils/FtsUtils.java @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2018, Daniel Gultsch All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package de.pixart.messenger.utils; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Locale; + +public class FtsUtils { + + private static List<String> KEYWORDS = Arrays.asList("OR", "AND"); + + public static List<String> parse(String input) { + List<String> term = new ArrayList<>(); + for (String part : input.split("\\s+")) { + if (part.isEmpty()) { + continue; + } + final String cleaned = part.substring(getStartIndex(part), getEndIndex(part) + 1); + if (isKeyword(cleaned)) { + term.add(part); + } else { + term.add(cleaned); + } + } + return term; + } + + public static String toMatchString(List<String> terms) { + StringBuilder builder = new StringBuilder(); + for (String term : terms) { + if (builder.length() != 0) { + builder.append(' '); + } + if (isKeyword(term)) { + builder.append(term.toUpperCase(Locale.ENGLISH)); + } else if (term.contains("*") || term.startsWith("-")) { + builder.append(term); + } else { + builder.append('*').append(term).append('*'); + } + } + return builder.toString(); + } + + public static boolean isKeyword(String term) { + return KEYWORDS.contains(term.toUpperCase(Locale.ENGLISH)); + } + + private static int getStartIndex(String term) { + int index = 0; + while (term.charAt(index) == '*') { + ++index; + } + return index; + } + + private static int getEndIndex(String term) { + int index = term.length() - 1; + while (term.charAt(index) == '*') { + --index; + } + return index; + } + +}
\ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/utils/StylingHelper.java b/src/main/java/de/pixart/messenger/utils/StylingHelper.java index 21e074603..f429a2573 100644 --- a/src/main/java/de/pixart/messenger/utils/StylingHelper.java +++ b/src/main/java/de/pixart/messenger/utils/StylingHelper.java @@ -91,7 +91,15 @@ public class StylingHelper { format(editable, end, editable.length() - 1, textColor); } - public static void highlight(final Context context, final Editable editable, String needle, boolean dark) { + public static void highlight(final Context context, final Editable editable, List<String> needles, boolean dark) { + for (String needle : needles) { + if (!FtsUtils.isKeyword(needle)) { + highlight(context, editable, needle, dark); + } + } + } + + private static void highlight(final Context context, final Editable editable, String needle, boolean dark) { final int length = needle.length(); String string = editable.toString(); int start = indexOfIgnoreCase(string, needle, 0); |