/**
   * Finds illegal entries in black list.
   *
   * @param blacklistToUse The blacklist to be checked.
   * @param blEngine The blacklist engine which is used to check
   * @param allowRegex Set to true to allow regular expressions in host part of blacklist entry.
   * @return A map which contains all entries whoch have been identified as being illegal by the
   *     blacklistEngine with the entry as key and an error code as value.
   */
  private static Map<String, BlacklistError> getIllegalEntries(
      final String blacklistToUse, final Blacklist blEngine, final boolean allowRegex) {
    final Map<String, BlacklistError> illegalEntries = new HashMap<String, BlacklistError>();
    final Set<String> legalEntries = new HashSet<String>();

    final List<String> list =
        FileUtils.getListArray(new File(ListManager.listsPath, blacklistToUse));
    final Map<String, String> properties = new HashMap<String, String>();
    properties.put("allowRegex", String.valueOf(allowRegex));

    BlacklistError err = BlacklistError.NO_ERROR;

    for (String element : list) {
      element = element.trim();

      // check for double-occurance
      if (legalEntries.contains(element)) {
        illegalEntries.put(element, BlacklistError.DOUBLE_OCCURANCE);
        continue;
      }
      legalEntries.add(element);

      err = blEngine.checkError(element, properties);

      if (err.getInt() > 0) {
        illegalEntries.put(element, err);
      }
    }

    return illegalEntries;
  }
  /**
   * Removes existing entries from a blacklist.
   *
   * @param blacklistToUse The blacklist which contains the
   * @param supportedBlacklistTypes Types of blacklists which the entry is to changed in.
   * @param entries Array of entries to be deleted.
   * @return Length of the list of entries to be removed.
   */
  private static int removeEntries(
      final String blacklistToUse, final String[] supportedBlacklistTypes, final String[] entries) {
    // load blacklist data from file
    final List<String> list =
        FileUtils.getListArray(new File(ListManager.listsPath, blacklistToUse));

    boolean listChanged = false;

    // delete the old entry from file
    for (final String entry : entries) {
      String s = entry;

      if (list != null) {

        // get rid of escape characters which make it impossible to
        // properly use contains()
        if (s.contains("\\\\")) {
          s = s.replaceAll(Pattern.quote("\\\\"), Matcher.quoteReplacement("\\"));
        }

        if (list.contains(s)) {
          listChanged = list.remove(s);
        }
      }

      // remove the entry from the running blacklist engine
      for (final String supportedBlacklistType : supportedBlacklistTypes) {
        if (ListManager.listSetContains(supportedBlacklistType + ".BlackLists", blacklistToUse)) {
          final String host = (s.indexOf('/') == -1) ? s : s.substring(0, s.indexOf('/'));
          final String path = (s.indexOf('/') == -1) ? ".*" : s.substring(s.indexOf('/') + 1);
          try {
            Switchboard.urlBlacklist.remove(supportedBlacklistType, host, path);
          } catch (final RuntimeException e) {
            Log.logSevere("BLACKLIST-CLEANER", e.getMessage() + ": " + host + "/" + path);
          }
        }
      }
      SearchEventCache.cleanupEvents(true);
    }
    if (listChanged) {
      FileUtils.writeList(
          new File(ListManager.listsPath, blacklistToUse), list.toArray(new String[list.size()]));
    }
    return entries.length;
  }