1 /*
2 * Copyright 2012-2025 CodeLibs Project and the Others.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13 * either express or implied. See the License for the specific language
14 * governing permissions and limitations under the License.
15 */
16 package org.codelibs.fess.crawler.util;
17
18 import java.util.Map;
19 import java.util.regex.Pattern;
20
21 import org.codelibs.core.lang.StringUtil;
22 import org.codelibs.core.stream.StreamUtil;
23 import org.codelibs.fess.Constants;
24 import org.dbflute.optional.OptionalThing;
25
26 /**
27 * Utility class for managing field configurations with parameter mappings.
28 * This class provides functionality to retrieve and manage field-specific configurations
29 * from a parameter map.
30 */
31 public class FieldConfigs {
32
33 /**
34 * Map containing field names as keys and their corresponding configuration values as values.
35 */
36 private final Map<String, String> params;
37
38 /**
39 * Constructs a new FieldConfigs instance with the specified parameter map.
40 *
41 * @param params the map containing field names as keys and configuration values as values
42 */
43 public FieldConfigs(final Map<String, String> params) {
44 this.params = params;
45 }
46
47 /**
48 * Retrieves the configuration for the specified field name.
49 *
50 * @param fieldName the name of the field to get configuration for
51 * @return an OptionalThing containing the Config if the field exists and has a non-blank value,
52 * otherwise an empty OptionalThing
53 */
54 public OptionalThing<Config> getConfig(final String fieldName) {
55 final String value = params.get(fieldName);
56 if (StringUtil.isNotBlank(value)) {
57 return OptionalThing.of(new Config(value));
58 }
59 return OptionalThing.empty();
60 }
61
62 /**
63 * Configuration class that holds parsed configuration values for a field.
64 * This class parses pipe-separated configuration values and provides methods
65 * to check for specific configuration options.
66 */
67 public static class Config {
68
69 /**
70 * Array of parsed configuration values split by pipe character and trimmed.
71 */
72 private final String[] values;
73
74 /**
75 * Constructs a new Config instance by parsing the provided configuration value.
76 * The value is split by pipe character (|) and each part is trimmed.
77 *
78 * @param value the configuration value string to parse
79 */
80 public Config(final String value) {
81 values = StreamUtil.split(value, Pattern.quote("|")).get(stream -> stream.map(String::trim).toArray(n -> new String[n]));
82 }
83
84 /**
85 * Checks if the cache option is enabled in the configuration.
86 * Returns true if "cache" is present in the values or if there's a single "true" value
87 * for backward compatibility.
88 *
89 * @return true if caching is enabled, false otherwise
90 */
91 public boolean isCache() {
92 for (final String value : values) {
93 if ("cache".equalsIgnoreCase(value)) {
94 return true;
95 }
96 }
97 // backward compatibility
98 if (values.length == 1 && Constants.TRUE.equalsIgnoreCase(values[0])) {
99 return true;
100 }
101 return false;
102 }
103
104 /**
105 * Checks if the overwrite option is enabled in the configuration.
106 * Returns true if "overwrite" is present in the values.
107 *
108 * @return true if overwriting is enabled, false otherwise
109 */
110 public boolean isOverwrite() {
111 for (final String value : values) {
112 if ("overwrite".equalsIgnoreCase(value)) {
113 return true;
114 }
115 }
116 return false;
117 }
118
119 /**
120 * Returns the array of parsed configuration values.
121 *
122 * @return the array of configuration values
123 */
124 public String[] getValues() {
125 return values;
126 }
127 }
128 }