-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #2826 Added support for defining field configurations via field.c…
…onfig.fieldname in crawl settings.
- Loading branch information
Showing
7 changed files
with
214 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/main/java/org/codelibs/fess/crawler/util/FieldConfigs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2012-2024 CodeLibs Project and the Others. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
package org.codelibs.fess.crawler.util; | ||
|
||
import java.util.Map; | ||
import java.util.regex.Pattern; | ||
|
||
import org.codelibs.core.lang.StringUtil; | ||
import org.codelibs.core.stream.StreamUtil; | ||
import org.codelibs.fess.Constants; | ||
import org.dbflute.optional.OptionalThing; | ||
|
||
public class FieldConfigs { | ||
|
||
private final Map<String, String> params; | ||
|
||
public FieldConfigs(Map<String, String> params) { | ||
this.params = params; | ||
} | ||
|
||
public OptionalThing<Config> getConfig(String fieldName) { | ||
String value = params.get(fieldName); | ||
if (StringUtil.isNotBlank(value)) { | ||
return OptionalThing.of(new Config(value)); | ||
} | ||
return OptionalThing.empty(); | ||
} | ||
|
||
public static class Config { | ||
|
||
private final String[] values; | ||
|
||
public Config(String value) { | ||
values = StreamUtil.split(value, Pattern.quote("|")).get(stream -> stream.map(s -> s.trim()).toArray(n -> new String[n])); | ||
} | ||
|
||
public boolean isCache() { | ||
for (final String value : values) { | ||
if ("cache".equalsIgnoreCase(value)) { | ||
return true; | ||
} | ||
} | ||
// backward compatibility | ||
if (values.length == 1 && Constants.TRUE.equalsIgnoreCase(values[0])) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public boolean isOverwrite() { | ||
for (final String value : values) { | ||
if ("overwrite".equalsIgnoreCase(value)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public String[] getValues() { | ||
return values; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/test/java/org/codelibs/fess/crawler/util/FieldConfigsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2012-2024 CodeLibs Project and the Others. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
package org.codelibs.fess.crawler.util; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
import org.apache.groovy.util.Maps; | ||
import org.codelibs.fess.unit.UnitFessTestCase; | ||
|
||
public class FieldConfigsTest extends UnitFessTestCase { | ||
public void test_empty() { | ||
final FieldConfigs fieldConfigs = new FieldConfigs(Collections.emptyMap()); | ||
assertTrue(fieldConfigs.getConfig("test").isEmpty()); | ||
} | ||
|
||
public void test_values() { | ||
final Map<String, String> params = Maps.of("foo", "bar"); | ||
FieldConfigs fieldConfigs = new FieldConfigs(params); | ||
assertTrue(fieldConfigs.getConfig("test").isEmpty()); | ||
assertFalse(fieldConfigs.getConfig("foo").isEmpty()); | ||
assertFalse(fieldConfigs.getConfig("foo").map(FieldConfigs.Config::isCache).orElse(false)); | ||
assertFalse(fieldConfigs.getConfig("foo").map(FieldConfigs.Config::isOverwrite).orElse(false)); | ||
assertEquals("bar", fieldConfigs.getConfig("foo").map(FieldConfigs.Config::getValues).orElse(new String[0])[0]); | ||
} | ||
|
||
public void test_cache_true() { | ||
final Map<String, String> params = Maps.of("foo", "true"); | ||
FieldConfigs fieldConfigs = new FieldConfigs(params); | ||
assertTrue(fieldConfigs.getConfig("test").isEmpty()); | ||
assertTrue(fieldConfigs.getConfig("foo").map(FieldConfigs.Config::isCache).orElse(false)); | ||
assertFalse(fieldConfigs.getConfig("foo").map(FieldConfigs.Config::isOverwrite).orElse(false)); | ||
} | ||
|
||
public void test_cache() { | ||
final Map<String, String> params = Maps.of("foo", "cache"); | ||
FieldConfigs fieldConfigs = new FieldConfigs(params); | ||
assertTrue(fieldConfigs.getConfig("test").isEmpty()); | ||
assertTrue(fieldConfigs.getConfig("foo").map(FieldConfigs.Config::isCache).orElse(false)); | ||
assertFalse(fieldConfigs.getConfig("foo").map(FieldConfigs.Config::isOverwrite).orElse(false)); | ||
} | ||
|
||
public void test_overwrite() { | ||
final Map<String, String> params = Maps.of("foo", "overwrite"); | ||
FieldConfigs fieldConfigs = new FieldConfigs(params); | ||
assertTrue(fieldConfigs.getConfig("test").isEmpty()); | ||
assertFalse(fieldConfigs.getConfig("foo").map(FieldConfigs.Config::isCache).orElse(false)); | ||
assertTrue(fieldConfigs.getConfig("foo").map(FieldConfigs.Config::isOverwrite).orElse(false)); | ||
} | ||
|
||
public void test_cache_overwrite() { | ||
final Map<String, String> params = Maps.of("foo", "cache|overwrite"); | ||
FieldConfigs fieldConfigs = new FieldConfigs(params); | ||
assertTrue(fieldConfigs.getConfig("test").isEmpty()); | ||
assertTrue(fieldConfigs.getConfig("foo").map(FieldConfigs.Config::isCache).orElse(false)); | ||
assertTrue(fieldConfigs.getConfig("foo").map(FieldConfigs.Config::isOverwrite).orElse(false)); | ||
} | ||
} |