View Javadoc
1   /*
2    * Copyright 2012-2021 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.dict;
17  
18  import java.util.Base64;
19  import java.util.Date;
20  import java.util.regex.Pattern;
21  
22  import javax.annotation.Resource;
23  
24  import org.codelibs.fess.Constants;
25  
26  public abstract class DictionaryCreator {
27  
28      protected Pattern pattern;
29  
30      @Resource
31      protected DictionaryManager dictionaryManager;
32  
33      public DictionaryCreator(final String pattern) {
34          this.pattern = Pattern.compile(pattern);
35      }
36  
37      public DictionaryFile<? extends DictionaryItem> create(final String path, final Date timestamp) {
38          if (!isTarget(path)) {
39              return null;
40          }
41  
42          return newDictionaryFile(encodePath(path), path, timestamp);
43      }
44  
45      protected String encodePath(final String path) {
46          return Base64.getUrlEncoder().encodeToString(path.getBytes(Constants.CHARSET_UTF_8));
47      }
48  
49      protected boolean isTarget(final String path) {
50          return pattern.matcher(path).find();
51      }
52  
53      protected abstract DictionaryFile<? extends DictionaryItem> newDictionaryFile(String id, String path, Date timestamp);
54  
55      public void setDictionaryManager(final DictionaryManager dictionaryManager) {
56          this.dictionaryManager = dictionaryManager;
57      }
58  }