View Javadoc
1   /*
2    * Copyright 2012-2017 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.util;
17  
18  import java.util.HashMap;
19  import java.util.LinkedHashMap;
20  import java.util.Map;
21  
22  import org.codelibs.core.lang.StringUtil;
23  import org.codelibs.fess.es.config.exentity.CrawlingConfig.ConfigName;
24  import org.codelibs.fess.exception.FessSystemException;
25  
26  public class ParameterUtil {
27      protected static final String XPATH_PREFIX = "field.xpath.";
28  
29      protected static final String META_PREFIX = "field.meta.";
30  
31      protected static final String VALUE_PREFIX = "field.value.";
32  
33      protected static final String SCRIPT_PREFIX = "field.script.";
34  
35      protected static final String CLIENT_PREFIX = "client.";
36  
37      protected static final String CONFIG_PREFIX = "config.";
38  
39      protected static final String FIELD_PREFIX = "field.config.";
40  
41      protected ParameterUtil() {
42          // nothing
43      }
44  
45      public static Map<String, String> parse(final String value) {
46          final Map<String, String> paramMap = new LinkedHashMap<>();
47          if (value != null) {
48              final String[] lines = value.split("[\r\n]");
49              for (final String line : lines) {
50                  if (StringUtil.isNotBlank(line)) {
51                      final int pos = line.indexOf('=');
52                      if (pos == 0) {
53                          throw new FessSystemException("Invalid parameter. The key is null.");
54                      } else if (pos > 0) {
55                          if (pos < line.length()) {
56                              paramMap.put(line.substring(0, pos).trim(), line.substring(pos + 1).trim());
57                          } else {
58                              paramMap.put(line.substring(0, pos).trim(), StringUtil.EMPTY);
59                          }
60                      } else {
61                          paramMap.put(line.trim(), StringUtil.EMPTY);
62                      }
63                  }
64              }
65          }
66          return paramMap;
67      }
68  
69      public static void loadConfigParams(final Map<String, Object> paramMap, final String configParam) {
70          final Map<String, String> map = ParameterUtil.parse(configParam);
71          if (!map.isEmpty()) {
72              paramMap.putAll(map);
73          }
74      }
75  
76      public static Map<ConfigName, Map<String, String>> createConfigParameterMap(final String configParameters) {
77          final Map<ConfigName, Map<String, String>> map = new HashMap<>();
78          final Map<String, String> configConfigMap = new LinkedHashMap<>();
79          final Map<String, String> clientConfigMap = new LinkedHashMap<>();
80          final Map<String, String> xpathConfigMap = new LinkedHashMap<>();
81          final Map<String, String> metaConfigMap = new LinkedHashMap<>();
82          final Map<String, String> valueConfigMap = new LinkedHashMap<>();
83          final Map<String, String> scriptConfigMap = new LinkedHashMap<>();
84          final Map<String, String> fieldConfigMap = new LinkedHashMap<>();
85          map.put(ConfigName.CONFIG, configConfigMap);
86          map.put(ConfigName.CLIENT, clientConfigMap);
87          map.put(ConfigName.XPATH, xpathConfigMap);
88          map.put(ConfigName.META, metaConfigMap);
89          map.put(ConfigName.VALUE, valueConfigMap);
90          map.put(ConfigName.SCRIPT, scriptConfigMap);
91          map.put(ConfigName.FIELD, fieldConfigMap);
92          for (final Map.Entry<String, String> entry : ParameterUtil.parse(configParameters).entrySet()) {
93              final String key = entry.getKey();
94              if (key.startsWith(CONFIG_PREFIX)) {
95                  configConfigMap.put(key.substring(CONFIG_PREFIX.length()), entry.getValue());
96              } else if (key.startsWith(CLIENT_PREFIX)) {
97                  clientConfigMap.put(key.substring(CLIENT_PREFIX.length()), entry.getValue());
98              } else if (key.startsWith(XPATH_PREFIX)) {
99                  xpathConfigMap.put(key.substring(XPATH_PREFIX.length()), entry.getValue());
100             } else if (key.startsWith(META_PREFIX)) {
101                 metaConfigMap.put(key.substring(META_PREFIX.length()), entry.getValue());
102             } else if (key.startsWith(VALUE_PREFIX)) {
103                 valueConfigMap.put(key.substring(VALUE_PREFIX.length()), entry.getValue());
104             } else if (key.startsWith(SCRIPT_PREFIX)) {
105                 scriptConfigMap.put(key.substring(SCRIPT_PREFIX.length()), entry.getValue());
106             } else if (key.startsWith(FIELD_PREFIX)) {
107                 fieldConfigMap.put(key.substring(FIELD_PREFIX.length()), entry.getValue());
108             }
109         }
110 
111         return map;
112     }
113 }