Browse Source

fix #1702 ignore case in title

Shinsuke Sugaya 7 years ago
parent
commit
cd16bf831c

+ 22 - 10
src/main/java/org/codelibs/fess/helper/ViewHelper.java

@@ -60,6 +60,7 @@ import org.codelibs.fess.mylasta.direction.FessConfig;
 import org.codelibs.fess.util.ComponentUtil;
 import org.codelibs.fess.util.ComponentUtil;
 import org.codelibs.fess.util.DocumentUtil;
 import org.codelibs.fess.util.DocumentUtil;
 import org.codelibs.fess.util.ResourceUtil;
 import org.codelibs.fess.util.ResourceUtil;
+import org.dbflute.optional.OptionalThing;
 import org.lastaflute.taglib.function.LaFunctions;
 import org.lastaflute.taglib.function.LaFunctions;
 import org.lastaflute.web.response.ActionResponse;
 import org.lastaflute.web.response.ActionResponse;
 import org.lastaflute.web.response.StreamResponse;
 import org.lastaflute.web.response.StreamResponse;
@@ -150,19 +151,30 @@ public class ViewHelper {
             title = StringUtils.abbreviate(title, size);
             title = StringUtils.abbreviate(title, size);
         }
         }
         final String value = LaFunctions.h(title);
         final String value = LaFunctions.h(title);
+        return getQuerySet().map(
+                querySet -> {
+                    String t = value;
+                    for (final String query : querySet) {
+                        final String target = LaFunctions.h(query);
+                        final Matcher matcher =
+                                Pattern.compile(target, Pattern.LITERAL | Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE).matcher(t);
+                        final StringBuffer buf = new StringBuffer(t.length() + 100);
+                        while (matcher.find()) {
+                            matcher.appendReplacement(buf, highlightTagPre + matcher.group(0) + highlightTagPost);
+                        }
+                        matcher.appendTail(buf);
+                        t = buf.toString();
+                    }
+                    return t;
+                }).orElse(value);
+    }
+
+    protected OptionalThing<Set<String>> getQuerySet() {
         return LaRequestUtil.getOptionalRequest().map(req -> {
         return LaRequestUtil.getOptionalRequest().map(req -> {
             @SuppressWarnings("unchecked")
             @SuppressWarnings("unchecked")
             final Set<String> querySet = (Set<String>) req.getAttribute(Constants.HIGHLIGHT_QUERIES);
             final Set<String> querySet = (Set<String>) req.getAttribute(Constants.HIGHLIGHT_QUERIES);
-            if (querySet != null) {
-                String t = value;
-                for (final String query : querySet) {
-                    final String target = LaFunctions.h(query);
-                    t = t.replace(target, highlightTagPre + target + highlightTagPost);
-                }
-                return t;
-            }
-            return value;
-        }).orElse(value);
+            return querySet;
+        }).filter(s -> s != null);
     }
     }
 
 
     public String getContentDescription(final Map<String, Object> document) {
     public String getContentDescription(final Map<String, Object> document) {

+ 41 - 1
src/test/java/org/codelibs/fess/helper/ViewHelperTest.java

@@ -18,7 +18,9 @@ package org.codelibs.fess.helper;
 import java.io.File;
 import java.io.File;
 import java.io.IOException;
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
 import java.util.Map;
+import java.util.Set;
 
 
 import org.codelibs.core.io.FileUtil;
 import org.codelibs.core.io.FileUtil;
 import org.codelibs.core.misc.DynamicProperties;
 import org.codelibs.core.misc.DynamicProperties;
@@ -26,6 +28,7 @@ import org.codelibs.fess.es.config.exentity.PathMapping;
 import org.codelibs.fess.mylasta.direction.FessConfig;
 import org.codelibs.fess.mylasta.direction.FessConfig;
 import org.codelibs.fess.unit.UnitFessTestCase;
 import org.codelibs.fess.unit.UnitFessTestCase;
 import org.codelibs.fess.util.ComponentUtil;
 import org.codelibs.fess.util.ComponentUtil;
+import org.dbflute.optional.OptionalThing;
 
 
 public class ViewHelperTest extends UnitFessTestCase {
 public class ViewHelperTest extends UnitFessTestCase {
     public ViewHelper viewHelper;
     public ViewHelper viewHelper;
@@ -209,7 +212,7 @@ public class ViewHelperTest extends UnitFessTestCase {
     }
     }
 
 
     public void test_escapeHighlight() {
     public void test_escapeHighlight() {
-        viewHelper = new ViewHelper();
+        ViewHelper viewHelper = new ViewHelper();
         viewHelper.init();
         viewHelper.init();
 
 
         String text = "";
         String text = "";
@@ -285,4 +288,41 @@ public class ViewHelperTest extends UnitFessTestCase {
         docMap.put(fieldName, urlLink);
         docMap.put(fieldName, urlLink);
         assertEquals(sitePath, viewHelper.getSitePath(docMap));
         assertEquals(sitePath, viewHelper.getSitePath(docMap));
     }
     }
+
+    public void test_getContentTitle() {
+        final Set<String> querySet = new HashSet<>();
+        ViewHelper viewHelper = new ViewHelper() {
+            @Override
+            protected OptionalThing<Set<String>> getQuerySet() {
+                return OptionalThing.of(querySet);
+            }
+        };
+        viewHelper.init();
+
+        querySet.add("aaa");
+
+        final Map<String, Object> document = new HashMap<>();
+        document.put("title", "");
+        assertEquals("", viewHelper.getContentTitle(document));
+
+        document.put("title", "111");
+        assertEquals("111", viewHelper.getContentTitle(document));
+
+        document.put("title", "aaa");
+        assertEquals("<strong>aaa</strong>", viewHelper.getContentTitle(document));
+
+        document.put("title", "AAA");
+        assertEquals("<strong>AAA</strong>", viewHelper.getContentTitle(document));
+
+        document.put("title", "111AaA222bbb");
+        assertEquals("111<strong>AaA</strong>222bbb", viewHelper.getContentTitle(document));
+
+        document.put("title", "aaaAAA");
+        assertEquals("<strong>aaa</strong><strong>AAA</strong>", viewHelper.getContentTitle(document));
+
+        querySet.add("BBB");
+
+        document.put("title", "111AaA222bbb");
+        assertEquals("111<strong>AaA</strong>222<strong>bbb</strong>", viewHelper.getContentTitle(document));
+    }
 }
 }