001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 *     http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.gbif.common.parsers.utils;
015
016import java.io.BufferedReader;
017import java.io.IOException;
018import java.io.InputStreamReader;
019import java.io.Reader;
020import java.nio.charset.StandardCharsets;
021import java.util.HashSet;
022import java.util.Set;
023
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027/**
028 * Set of bad names in UPPERCASE.
029 */
030public final class BlacklistedNames {
031
032  private static final Logger LOGGER = LoggerFactory.getLogger(BlacklistedNames.class);
033
034  private static final Set<String> NAMES = new HashSet<String>();
035
036  private static final String BLACKLIST_FILE = "utils/blacklistedNames.tsv";
037
038  static {
039    init(BLACKLIST_FILE);
040  }
041
042  private BlacklistedNames() {
043    throw new UnsupportedOperationException("Can't initialize class");
044  }
045
046  public static boolean contains(String s) {
047    return NAMES.contains(s.toUpperCase());
048  }
049
050  public static synchronized void init(Reader reader) {
051    NAMES.clear();
052    BufferedReader br = new BufferedReader(reader);
053    try {
054      String line = br.readLine();
055      while (line != null) {
056        LOGGER.debug("Blacklisting: {}", line);
057        NAMES.add(line.toUpperCase());
058        line = br.readLine();
059      }
060    } catch (IOException e) {
061      LOGGER.debug("Exception thrown", e);
062    } finally {
063      try {
064        br.close();
065      } catch (IOException ignored) {
066      }
067    }
068  }
069
070  public static synchronized void init(String filepath) {
071    init(new InputStreamReader(BlacklistedNames.class.getClassLoader().getResourceAsStream(filepath), StandardCharsets.UTF_8));
072  }
073}