You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
219 lines
7.7 KiB
219 lines
7.7 KiB
19 years ago
|
// listManager.java
|
||
20 years ago
|
// -------------------------------------
|
||
|
// part of YACY
|
||
13 years ago
|
//
|
||
19 years ago
|
// (C) 2005, 2006 by Alexander Schier
|
||
17 years ago
|
// (C) 2007 by Bjoern 'Fuchs' Krombholz; fox.box@gmail.com
|
||
13 years ago
|
//
|
||
19 years ago
|
// last change: $LastChangedDate$ by $LastChangedBy$
|
||
19 years ago
|
// $LastChangedRevision$
|
||
20 years ago
|
//
|
||
|
// This program is free software; you can redistribute it and/or modify
|
||
|
// it under the terms of the GNU General Public License as published by
|
||
|
// the Free Software Foundation; either version 2 of the License, or
|
||
|
// (at your option) any later version.
|
||
|
//
|
||
|
// This program is distributed in the hope that it will be useful,
|
||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
// GNU General Public License for more details.
|
||
|
//
|
||
|
// You should have received a copy of the GNU General Public License
|
||
|
// along with this program; if not, write to the Free Software
|
||
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||
|
|
||
12 years ago
|
package net.yacy.data;
|
||
20 years ago
|
|
||
20 years ago
|
import java.io.File;
|
||
19 years ago
|
import java.util.ArrayList;
|
||
17 years ago
|
import java.util.Arrays;
|
||
|
import java.util.Collection;
|
||
19 years ago
|
import java.util.HashSet;
|
||
20 years ago
|
import java.util.Iterator;
|
||
13 years ago
|
import java.util.List;
|
||
17 years ago
|
import java.util.Set;
|
||
19 years ago
|
import java.util.Vector;
|
||
13 years ago
|
import java.util.regex.Pattern;
|
||
19 years ago
|
|
||
15 years ago
|
import net.yacy.kelondro.util.FileUtils;
|
||
13 years ago
|
import net.yacy.repository.Blacklist.BlacklistType;
|
||
15 years ago
|
import net.yacy.repository.BlacklistFile;
|
||
13 years ago
|
import net.yacy.search.Switchboard;
|
||
|
import net.yacy.search.query.SearchEventCache;
|
||
15 years ago
|
|
||
19 years ago
|
// The Naming of the functions is a bit strange...
|
||
20 years ago
|
|
||
14 years ago
|
public class ListManager {
|
||
13 years ago
|
|
||
14 years ago
|
private final static Pattern commaPattern = Pattern.compile(",");
|
||
13 years ago
|
|
||
16 years ago
|
public static Switchboard switchboard = null;
|
||
17 years ago
|
public static File listsPath = null;
|
||
20 years ago
|
|
||
17 years ago
|
/**
|
||
|
* Get ListSet from configuration file and return it as a unified Set.
|
||
13 years ago
|
*
|
||
17 years ago
|
* <b>Meaning of ListSet</b>: There are various "lists" in YaCy which are
|
||
|
* actually disjunct (pairwise unequal) sets which themselves can be seperated
|
||
|
* into different subsets. E.g., there can be more than one blacklist of a type.
|
||
13 years ago
|
* A ListSet is the set of all those "lists" (subsets) of an equal type.
|
||
|
*
|
||
17 years ago
|
* @param setName name of the ListSet
|
||
|
* @return a ListSet from configuration file
|
||
|
*/
|
||
17 years ago
|
public static Set<String> getListSet(final String setName) {
|
||
17 years ago
|
return string2set(switchboard.getConfig(setName, ""));
|
||
|
}
|
||
19 years ago
|
|
||
17 years ago
|
/**
|
||
|
* Removes an element from a ListSet and updates the configuration file
|
||
|
* accordingly. If the element doesn't exist, then nothing will be changed.
|
||
13 years ago
|
*
|
||
17 years ago
|
* @param setName name of the ListSet.
|
||
|
* @param listName name of the element to remove from the ListSet.
|
||
|
*/
|
||
17 years ago
|
public static void removeFromListSet(final String setName, final String listName) {
|
||
|
final Set<String> listSet = getListSet(setName);
|
||
13 years ago
|
|
||
15 years ago
|
if (!listSet.isEmpty()) {
|
||
17 years ago
|
listSet.remove(listName);
|
||
|
switchboard.setConfig(setName, collection2string(listSet));
|
||
20 years ago
|
}
|
||
17 years ago
|
}
|
||
20 years ago
|
|
||
17 years ago
|
/**
|
||
|
* Adds an element to an existing ListSet. If the ListSet doesn't exist yet,
|
||
|
* a new one will be added. If the ListSet already contains an identical element,
|
||
|
* then nothing happens.
|
||
13 years ago
|
*
|
||
17 years ago
|
* The new list will be written to the configuartion file.
|
||
13 years ago
|
*
|
||
17 years ago
|
* @param setName
|
||
|
* @param newListName
|
||
|
*/
|
||
17 years ago
|
public static void updateListSet(final String setName, final String newListName) {
|
||
|
final Set<String> listSet = getListSet(setName);
|
||
17 years ago
|
listSet.add(newListName);
|
||
20 years ago
|
|
||
17 years ago
|
switchboard.setConfig(setName, collection2string(listSet));
|
||
|
}
|
||
20 years ago
|
|
||
17 years ago
|
/**
|
||
|
* @param setName ListSet in which to search for an element.
|
||
13 years ago
|
* @param listName the element to search for.
|
||
17 years ago
|
* @return <code>true</code> if the ListSet "setName" contains an element
|
||
|
* "listName", <code>false</code> otherwise.
|
||
|
*/
|
||
17 years ago
|
public static boolean listSetContains(final String setName, final String listName) {
|
||
15 years ago
|
return getListSet(setName).contains(listName);
|
||
17 years ago
|
}
|
||
20 years ago
|
|
||
|
|
||
17 years ago
|
//================general Lists==================
|
||
20 years ago
|
|
||
13 years ago
|
public static String getListString(final String filename, final boolean withcomments) {
|
||
15 years ago
|
return FileUtils.getListString(new File(listsPath ,filename), withcomments);
|
||
19 years ago
|
}
|
||
13 years ago
|
|
||
17 years ago
|
//================Helper functions for collection conversion==================
|
||
13 years ago
|
|
||
17 years ago
|
/**
|
||
|
* Simple conversion of a Collection of Strings to a comma separated String.
|
||
|
* If the implementing Collection subclass guaranties an order of its elements,
|
||
|
* the substrings of the result will have the same order.
|
||
13 years ago
|
*
|
||
17 years ago
|
* @param col a Collection of Strings.
|
||
|
* @return String with elements from set separated by comma.
|
||
|
*/
|
||
17 years ago
|
public static String collection2string(final Collection<String> col){
|
||
14 years ago
|
final StringBuilder str = new StringBuilder(col.size() * 40);
|
||
13 years ago
|
|
||
15 years ago
|
if (col != null && !col.isEmpty()) {
|
||
17 years ago
|
final Iterator<String> it = col.iterator();
|
||
17 years ago
|
str.append(it.next());
|
||
17 years ago
|
while(it.hasNext()) {
|
||
13 years ago
|
if (str.length() > 0) str.append(',');
|
||
|
str.append(it.next());
|
||
19 years ago
|
}
|
||
|
}
|
||
13 years ago
|
|
||
17 years ago
|
return str.toString();
|
||
19 years ago
|
}
|
||
17 years ago
|
|
||
|
/**
|
||
|
* @see listManager#string2vector(String)
|
||
|
*/
|
||
17 years ago
|
public static ArrayList<String> string2arraylist(final String string){
|
||
15 years ago
|
ArrayList<String> list;
|
||
17 years ago
|
|
||
17 years ago
|
if (string != null && string.length() > 0) {
|
||
14 years ago
|
list = new ArrayList<String>(Arrays.asList(commaPattern.split(string, 0)));
|
||
17 years ago
|
} else {
|
||
15 years ago
|
list = new ArrayList<String>();
|
||
19 years ago
|
}
|
||
17 years ago
|
|
||
15 years ago
|
return list;
|
||
19 years ago
|
}
|
||
17 years ago
|
|
||
|
/**
|
||
|
* Simple conversion of a comma separated list to a unified Set.
|
||
13 years ago
|
*
|
||
17 years ago
|
* @param string list of comma separated Strings
|
||
|
* @return resulting Set or empty Set if string is <code>null</code>
|
||
|
*/
|
||
17 years ago
|
public static Set<String> string2set(final String string){
|
||
17 years ago
|
HashSet<String> set;
|
||
13 years ago
|
|
||
17 years ago
|
if (string != null) {
|
||
14 years ago
|
set = new HashSet<String>(Arrays.asList(commaPattern.split(string, 0)));
|
||
17 years ago
|
} else {
|
||
17 years ago
|
set = new HashSet<String>();
|
||
19 years ago
|
}
|
||
17 years ago
|
|
||
|
return set;
|
||
19 years ago
|
}
|
||
|
|
||
17 years ago
|
/**
|
||
|
* Simple conversion of a comma separated list to a Vector containing
|
||
|
* the order of the substrings.
|
||
13 years ago
|
*
|
||
17 years ago
|
* @param string list of comma separated Strings
|
||
|
* @return resulting Vector or empty Vector if string is <code>null</code>
|
||
|
*/
|
||
17 years ago
|
public static Vector<String> string2vector(final String string){
|
||
17 years ago
|
Vector<String> v;
|
||
17 years ago
|
|
||
|
if (string != null) {
|
||
14 years ago
|
v = new Vector<String>(Arrays.asList(commaPattern.split(string, 0)));
|
||
17 years ago
|
} else {
|
||
17 years ago
|
v = new Vector<String>();
|
||
19 years ago
|
}
|
||
19 years ago
|
|
||
17 years ago
|
return v;
|
||
|
}
|
||
20 years ago
|
|
||
19 years ago
|
//=============Blacklist specific================
|
||
|
|
||
17 years ago
|
/**
|
||
|
* Load or reload all active Blacklists
|
||
|
*/
|
||
13 years ago
|
public static void reloadBlacklists(){
|
||
|
final List<BlacklistFile> blacklistFiles = new ArrayList<BlacklistFile>(BlacklistType.values().length);
|
||
|
for (final BlacklistType supportedBlacklistType : BlacklistType.values()) {
|
||
15 years ago
|
final BlacklistFile blFile = new BlacklistFile(
|
||
18 years ago
|
switchboard.getConfig(
|
||
13 years ago
|
supportedBlacklistType.toString() + ".BlackLists", switchboard.getConfig("BlackLists.DefaultList", "url.default.black")),
|
||
15 years ago
|
supportedBlacklistType);
|
||
18 years ago
|
blacklistFiles.add(blFile);
|
||
19 years ago
|
}
|
||
13 years ago
|
|
||
16 years ago
|
Switchboard.urlBlacklist.loadList(
|
||
15 years ago
|
blacklistFiles.toArray(new BlacklistFile[blacklistFiles.size()]),
|
||
18 years ago
|
"/");
|
||
15 years ago
|
SearchEventCache.cleanupEvents(true);
|
||
19 years ago
|
|
||
19 years ago
|
// switchboard.urlBlacklist.clear();
|
||
|
// if (f != "") switchboard.urlBlacklist.loadLists("black", f, "/");
|
||
|
}
|
||
19 years ago
|
}
|