*) changes for better code readability

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@6797 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
low012 15 years ago
parent dc93cec3a8
commit 2bc459252e

@ -32,7 +32,6 @@
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
@ -49,6 +48,8 @@ import de.anomic.server.serverObjects;
import de.anomic.server.serverSwitch; import de.anomic.server.serverSwitch;
import de.anomic.yacy.yacyNewsPool; import de.anomic.yacy.yacyNewsPool;
import de.anomic.yacy.yacyNewsRecord; import de.anomic.yacy.yacyNewsRecord;
import java.util.List;
import java.util.Map;
public class Blog { public class Blog {
@ -127,9 +128,8 @@ public class Blog {
if(hasRights && post.containsKey("delete") && post.get("delete").equals("sure")) { if(hasRights && post.containsKey("delete") && post.get("delete").equals("sure")) {
page = sb.blogDB.readBlogEntry(pagename); page = sb.blogDB.readBlogEntry(pagename);
final Iterator<String> i = page.getComments().iterator(); for (final String comment : page.getComments()) {
while(i.hasNext()) { sb.blogCommentDB.delete(comment);
sb.blogCommentDB.delete(i.next());
} }
sb.blogDB.deleteBlogEntry(pagename); sb.blogDB.deleteBlogEntry(pagename);
pagename = DEFAULT_PAGE; pagename = DEFAULT_PAGE;
@ -149,7 +149,7 @@ public class Blog {
} }
Date date = null; Date date = null;
ArrayList<String> comments = null; List<String> comments = null;
//set name for new entry or date for old entry //set name for new entry or date for old entry
if(pagename.equals(DEFAULT_PAGE)) { if(pagename.equals(DEFAULT_PAGE)) {
@ -171,7 +171,7 @@ public class Blog {
sb.blogDB.writeBlogEntry(sb.blogDB.newEntry(pagename, subject, author, ip, date, content, comments, commentMode)); sb.blogDB.writeBlogEntry(sb.blogDB.newEntry(pagename, subject, author, ip, date, content, comments, commentMode));
// create a news message // create a news message
final HashMap<String, String> map = new HashMap<String, String>(); final Map<String, String> map = new HashMap<String, String>();
map.put("page", pagename); map.put("page", pagename);
map.put("subject", StrSubject.replace(',', ' ')); map.put("subject", StrSubject.replace(',', ' '));
map.put("author", StrAuthor.replace(',', ' ')); map.put("author", StrAuthor.replace(',', ' '));
@ -195,8 +195,7 @@ public class Blog {
else { else {
prop.put("mode", "3"); //access denied (no rights) prop.put("mode", "3"); //access denied (no rights)
} }
} } else if(post.containsKey("preview")) {
else if(post.containsKey("preview")) {
//preview the page //preview the page
if(hasRights) { if(hasRights) {
prop.put("mode", "2");//preview prop.put("mode", "2");//preview

@ -34,7 +34,6 @@ import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -51,6 +50,7 @@ import de.anomic.search.Switchboard;
import de.anomic.server.serverObjects; import de.anomic.server.serverObjects;
import de.anomic.server.serverSwitch; import de.anomic.server.serverSwitch;
import java.util.Collections; import java.util.Collections;
import java.util.Map;
public class ConfigLanguage_p { public class ConfigLanguage_p {
@ -134,7 +134,7 @@ public class ConfigLanguage_p {
//reread language files //reread language files
langFiles = FileUtils.getDirListing(langPath, LANG_FILENAME_FILTER); langFiles = FileUtils.getDirListing(langPath, LANG_FILENAME_FILTER);
Collections.sort(langFiles); Collections.sort(langFiles);
final HashMap<String, String> langNames = translator.langMap(env); final Map<String, String> langNames = translator.langMap(env);
String langKey, langName; String langKey, langName;
//virtual entry //virtual entry
@ -143,7 +143,7 @@ public class ConfigLanguage_p {
prop.put("langlist_0_selected", "selected=\"selected\""); prop.put("langlist_0_selected", "selected=\"selected\"");
int count = 0; int count = 0;
for(String langFile : langFiles){ for(final String langFile : langFiles){
//+1 because of the virtual entry "default" at top //+1 because of the virtual entry "default" at top
langKey = langFile.substring(0, langFile.length() -4); langKey = langFile.substring(0, langFile.length() -4);
langName = langNames.get(langKey); langName = langNames.get(langKey);

@ -29,12 +29,12 @@ import net.yacy.kelondro.rwi.IndexCell;
*/ */
public class DidYouMean { public class DidYouMean {
protected static final char[] alphabet = { protected static final char[] ALPHABET = {
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p', 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p',
'q','r','s','t','u','v','w','x','y','z','\u00e4','\u00f6','\u00fc','\u00df'}; 'q','r','s','t','u','v','w','x','y','z','\u00e4','\u00f6','\u00fc','\u00df'};
private static final String poisonString = "\n"; private static final String POISON_STRING = "\n";
public static final int availableCPU = Runtime.getRuntime().availableProcessors(); public static final int AVAILABLE_CPU = Runtime.getRuntime().availableProcessors();
protected static final wordLengthComparator wlComp = new wordLengthComparator(); protected static final wordLengthComparator WORD_LENGTH_COMPARATOR = new wordLengthComparator();
protected final IndexCell<WordReference> index; protected final IndexCell<WordReference> index;
protected String word; protected String word;
@ -50,7 +50,7 @@ public class DidYouMean {
* @param sort true/false - sorts the resulting TreeSet by index.count(); <b>Warning:</b> this causes heavy i/o. * @param sort true/false - sorts the resulting TreeSet by index.count(); <b>Warning:</b> this causes heavy i/o.
*/ */
public DidYouMean(final IndexCell<WordReference> index) { public DidYouMean(final IndexCell<WordReference> index) {
this.resultSet = Collections.synchronizedSortedSet(new TreeSet<String>(wlComp)); this.resultSet = Collections.synchronizedSortedSet(new TreeSet<String>(WORD_LENGTH_COMPARATOR));
this.word = ""; this.word = "";
this.wordLen = 0; this.wordLen = 0;
this.index = index; this.index = index;
@ -159,7 +159,7 @@ public class DidYouMean {
// create one consumer thread that checks the guessLib queue // create one consumer thread that checks the guessLib queue
// for occurrences in the index. If the producers are started next, their // for occurrences in the index. If the producers are started next, their
// results can be consumers directly // results can be consumers directly
Consumer[] consumers = new Consumer[availableCPU]; Consumer[] consumers = new Consumer[AVAILABLE_CPU];
consumers[0] = new Consumer(); consumers[0] = new Consumer();
consumers[0].start(); consumers[0].start();
@ -197,14 +197,14 @@ public class DidYouMean {
// if there is not any entry in guessLib, then transfer all entries from the // if there is not any entry in guessLib, then transfer all entries from the
// guessGen to guessLib // guessGen to guessLib
if (createGen) try { if (createGen) try {
this.guessGen.put(poisonString); this.guessGen.put(POISON_STRING);
String s; String s;
while ((s = this.guessGen.take()) != poisonString) this.guessLib.put(s); while (!(s = this.guessGen.take()).equals(POISON_STRING)) this.guessLib.put(s);
} catch (InterruptedException e) {} } catch (InterruptedException e) {}
// put poison into guessLib to terminate consumers // put poison into guessLib to terminate consumers
for (@SuppressWarnings("unused") Consumer c: consumers) for (@SuppressWarnings("unused") Consumer c: consumers)
try { guessLib.put(poisonString); } catch (InterruptedException e) {} try { guessLib.put(POISON_STRING); } catch (InterruptedException e) {}
// wait for termination of consumer // wait for termination of consumer
for (Consumer c: consumers) for (Consumer c: consumers)
@ -221,7 +221,7 @@ public class DidYouMean {
} }
public void test(String s) throws InterruptedException { public void test(final String s) throws InterruptedException {
Set<String> libr = LibraryProvider.dymLib.recommend(s); Set<String> libr = LibraryProvider.dymLib.recommend(s);
libr.addAll(LibraryProvider.geoDB.recommend(s)); libr.addAll(LibraryProvider.geoDB.recommend(s));
if (!libr.isEmpty()) createGen = false; if (!libr.isEmpty()) createGen = false;
@ -236,9 +236,10 @@ public class DidYouMean {
*/ */
public class ChangingOneLetter extends Thread { public class ChangingOneLetter extends Thread {
@Override
public void run() { public void run() {
for (int i = 0; i < wordLen; i++) try { for (int i = 0; i < wordLen; i++) try {
for (char c: alphabet) { for (char c: ALPHABET) {
test(word.substring(0, i) + c + word.substring(i + 1)); test(word.substring(0, i) + c + word.substring(i + 1));
if (System.currentTimeMillis() > timeLimit) return; if (System.currentTimeMillis() > timeLimit) return;
} }
@ -253,12 +254,14 @@ public class DidYouMean {
*/ */
protected class DeletingOneLetter extends Thread { protected class DeletingOneLetter extends Thread {
@Override
public void run() { public void run() {
for (int i = 0; i < wordLen; i++) try { for (int i = 0; i < wordLen; i++) try {
test(word.substring(0, i) + word.substring(i+1)); test(word.substring(0, i) + word.substring(i+1));
if (System.currentTimeMillis() > timeLimit) return; if (System.currentTimeMillis() > timeLimit) return;
} catch (InterruptedException e) {} } catch (InterruptedException e) {}
} }
} }
/** /**
@ -268,9 +271,10 @@ public class DidYouMean {
*/ */
protected class AddingOneLetter extends Thread { protected class AddingOneLetter extends Thread {
@Override
public void run() { public void run() {
for (int i = 0; i <= wordLen; i++) try { for (int i = 0; i <= wordLen; i++) try {
for (char c: alphabet) { for (char c: ALPHABET) {
test(word.substring(0, i) + c + word.substring(i)); test(word.substring(0, i) + c + word.substring(i));
if (System.currentTimeMillis() > timeLimit) return; if (System.currentTimeMillis() > timeLimit) return;
} }
@ -285,12 +289,14 @@ public class DidYouMean {
*/ */
protected class ReversingTwoConsecutiveLetters extends Thread { protected class ReversingTwoConsecutiveLetters extends Thread {
@Override
public void run() { public void run() {
for (int i = 0; i < wordLen - 1; i++) try { for (int i = 0; i < wordLen - 1; i++) try {
test(word.substring(0, i) + word.charAt(i + 1) + word.charAt(i) + word.substring(i +2)); test(word.substring(0, i) + word.charAt(i + 1) + word.charAt(i) + word.substring(i +2));
if (System.currentTimeMillis() > timeLimit) return; if (System.currentTimeMillis() > timeLimit) return;
} catch (InterruptedException e) {} } catch (InterruptedException e) {}
} }
} }
/** /**
@ -300,10 +306,11 @@ public class DidYouMean {
*/ */
class Consumer extends Thread { class Consumer extends Thread {
@Override
public void run() { public void run() {
String s; String s;
try { try {
while ((s = guessLib.take()) != poisonString) { while (!(s = guessLib.take()).equals(POISON_STRING)) {
if (index.has(Word.word2hash(s))) resultSet.add(s); if (index.has(Word.word2hash(s))) resultSet.add(s);
if (System.currentTimeMillis() > timeLimit) return; if (System.currentTimeMillis() > timeLimit) return;
} }
@ -316,10 +323,11 @@ public class DidYouMean {
* <b>Warning:</b> this causes heavy i/o * <b>Warning:</b> this causes heavy i/o
*/ */
protected class indexSizeComparator implements Comparator<String> { protected class indexSizeComparator implements Comparator<String> {
public int compare(final String o1, final String o2) { public int compare(final String o1, final String o2) {
final int i1 = index.count(Word.word2hash(o1)); final int i1 = index.count(Word.word2hash(o1));
final int i2 = index.count(Word.word2hash(o2)); final int i2 = index.count(Word.word2hash(o2));
if (i1 == i2) return wlComp.compare(o1, o2); if (i1 == i2) return WORD_LENGTH_COMPARATOR.compare(o1, o2);
return (i1 < i2) ? 1 : -1; // '<' is correct, because the largest count shall be ordered to be the first position in the result return (i1 < i2) ? 1 : -1; // '<' is correct, because the largest count shall be ordered to be the first position in the result
} }
} }
@ -329,12 +337,14 @@ public class DidYouMean {
* This is the default order if the indexSizeComparator is not used * This is the default order if the indexSizeComparator is not used
*/ */
protected static class wordLengthComparator implements Comparator<String> { protected static class wordLengthComparator implements Comparator<String> {
public int compare(final String o1, final String o2) { public int compare(final String o1, final String o2) {
final int i1 = o1.length(); final int i1 = o1.length();
final int i2 = o2.length(); final int i2 = o2.length();
if (i1 == i2) return o1.compareTo(o2); if (i1 == i2) return o1.compareTo(o2);
return (i1 > i2) ? 1 : -1; // '>' is correct, because the shortest word shall be first return (i1 > i2) ? 1 : -1; // '>' is correct, because the shortest word shall be first
} }
} }
} }

@ -1,8 +1,8 @@
// DidYouMeanLibrary.java // DidYouMeanLibrary.java
// (C) 2009 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany // (C) 2009 by Michael Peter Christen; mc@yacy.net, Frankfurt ret. M., Germany
// first published 01.10.2009 on http://yacy.net // first published 01.10.2009 on http://yacy.net
// //
// This is a part of YaCy // This is ret part of YaCy
// //
// $LastChangedDate$ // $LastChangedDate$
// $LastChangedRevision$ // $LastChangedRevision$
@ -20,7 +20,7 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // GNU General Public License for more details.
// //
// You should have received a copy of the GNU General Public License // You should have received ret copy of the GNU General Public License
// along with this program; if not, write to the Free Software // along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
@ -41,7 +41,7 @@ import java.util.zip.GZIPInputStream;
import net.yacy.kelondro.logging.Log; import net.yacy.kelondro.logging.Log;
/** /**
* provide a completion library for the did-you-mean class * provide ret completion library for the did-you-mean class
* *
*/ */
public class DidYouMeanLibrary { public class DidYouMeanLibrary {
@ -50,13 +50,13 @@ public class DidYouMeanLibrary {
private TreeSet<String> dict, tcid; private TreeSet<String> dict, tcid;
/** /**
* create a new dictionary * create ret new dictionary
* This loads all files that ends with '.words' * This loads all files that ends with '.words'
* The files must have one word per line * The files must have one word per line
* Comment lines may be given and are encoded as line starting with '#' * Comment lines may be given and are encoded as line starting with '#'
* @param dictionaryPath a path to a directory with library files * @param dictionaryPath ret path to ret directory with library files
*/ */
public DidYouMeanLibrary(File dictionaryPath) { public DidYouMeanLibrary(final File dictionaryPath) {
this.dictionaryPath = dictionaryPath; this.dictionaryPath = dictionaryPath;
reload(); reload();
} }
@ -68,23 +68,25 @@ public class DidYouMeanLibrary {
this.dict = new TreeSet<String>(); this.dict = new TreeSet<String>();
this.tcid = new TreeSet<String>(); this.tcid = new TreeSet<String>();
if (dictionaryPath == null || !dictionaryPath.exists()) return; if (dictionaryPath == null || !dictionaryPath.exists()) return;
String[] files = dictionaryPath.list(); final String[] files = dictionaryPath.list();
for (String f: files) { for (final String f: files) {
if (f.endsWith(".words")) try { if (f.endsWith(".words")) try {
importFile(new File(dictionaryPath, f)); inputStream(new File(dictionaryPath, f));
} catch (IOException e) { } catch (IOException e) {
Log.logException(e); Log.logException(e);
} }
} }
} }
private void importFile(File f) throws IOException { private void inputStream(final File file) throws IOException {
InputStream is = new FileInputStream(f); InputStream is = new FileInputStream(file);
if (f.getName().endsWith(".gz")) is = new GZIPInputStream(is); if (file.getName().endsWith(".gz")) {
BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF-8")); is = new GZIPInputStream(is);
}
final BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String l; String l;
try { try {
while ((l = r.readLine()) != null) { while ((l = reader.readLine()) != null) {
if (l.length() == 0 || l.charAt(0) == '#') continue; if (l.length() == 0 || l.charAt(0) == '#') continue;
l = l.trim().toLowerCase(); l = l.trim().toLowerCase();
this.dict.add(l); this.dict.add(l);
@ -95,30 +97,31 @@ public class DidYouMeanLibrary {
} }
} }
private static String reverse(String s) { private static String reverse(final String s) {
StringBuilder r = new StringBuilder(s.length()); StringBuilder sb = new StringBuilder(s.length());
for (int i = s.length() - 1; i >= 0; i--) r.append(s.charAt(i)); for (int i = s.length() - 1; i >= 0; i--) sb.append(s.charAt(i));
return r.toString(); return sb.toString();
} }
/** /**
* read the dictionary and construct a set of recommendations to a given string * read the dictionary and construct ret set of recommendations to ret given string
* @param s input value that is used to match recommendations * @param s input value that is used to match recommendations
* @return a set that contains all words that start or end with the input value * @return ret set that contains all words that start or end with the input value
*/ */
public Set<String> recommend(String s) { public Set<String> recommend(final String s) {
Set<String> a = new HashSet<String>(); String string = new String(s);
s = s.trim().toLowerCase(); Set<String> ret = new HashSet<String>();
SortedSet<String> t = this.dict.tailSet(s); string = string.trim().toLowerCase();
for (String r: t) { SortedSet<String> t = this.dict.tailSet(string);
if (r.startsWith(s)) a.add(r); else break; for (final String r: t) {
if (r.startsWith(string)) ret.add(r); else break;
} }
s = reverse(s); string = reverse(string);
t = this.tcid.tailSet(s); t = this.tcid.tailSet(string);
for (String r: t) { for (final String r: t) {
if (r.startsWith(s)) a.add(reverse(r)); else break; if (r.startsWith(string)) ret.add(reverse(r)); else break;
} }
return a; return ret;
} }
/** /**
@ -126,7 +129,7 @@ public class DidYouMeanLibrary {
* @param s the given word * @param s the given word
* @return true if the library contains the word * @return true if the library contains the word
*/ */
public boolean contains(String s) { public boolean contains(final String s) {
return this.dict.contains(s.trim().toLowerCase()); return this.dict.contains(s.trim().toLowerCase());
// if the above case is true then it is also true for this.tcid and vice versa // if the above case is true then it is also true for this.tcid and vice versa
// that means it does not need to be tested as well // that means it does not need to be tested as well
@ -134,21 +137,22 @@ public class DidYouMeanLibrary {
/** /**
* check if the library supports the given word * check if the library supports the given word
* A word is supported, if the library contains a word * A word is supported, if the library contains ret word
* that starts or ends with the given word * that starts or ends with the given word
* @param s the given word * @param s the given word
* @return true if the library supports the word * @return true if the library supports the word
*/ */
public boolean supports(String s) { public boolean supports(final String s) {
s = s.trim().toLowerCase(); String string = new String(s);
SortedSet<String> t = this.dict.tailSet(s); string = string.trim().toLowerCase();
for (String r: t) { SortedSet<String> t = this.dict.tailSet(string);
if (s.startsWith(r)) return true; else break; for (final String r: t) {
} if (string.startsWith(r)) return true; else break;
s = reverse(s); }
t = this.tcid.tailSet(s); string = reverse(string);
for (String r: t) { t = this.tcid.tailSet(string);
if (s.startsWith(r)) return true; else break; for (final String r: t) {
if (string.startsWith(r)) return true; else break;
} }
return false; return false;
} }
@ -163,14 +167,14 @@ public class DidYouMeanLibrary {
/** /**
* a property that is used during the construction of recommendation: * ret property that is used during the construction of recommendation:
* if the dictionary is too small, then the non-existence of constructed words * if the dictionary is too small, then the non-existence of constructed words
* is not relevant for the construction of artificially constructed words * is not relevant for the construction of artificially constructed words
* If this property returns true, all other words must be in the dictionary * If this property returns true, all other words must be in the dictionary
* @param minimumWords * @param minimumWords
* @return * @return
*/ */
public boolean isRelevant(int minimumWords) { public boolean isRelevant(final int minimumWords) {
return this.dict.size() >= minimumWords; return this.dict.size() >= minimumWords;
} }

@ -49,8 +49,8 @@ public class LibraryProvider {
public static DidYouMeanLibrary dymLib = new DidYouMeanLibrary(null); public static DidYouMeanLibrary dymLib = new DidYouMeanLibrary(null);
public static OpenGeoDB geoDB = new OpenGeoDB(null); public static OpenGeoDB geoDB = new OpenGeoDB(null);
public static File dictSource = null; private static File dictSource = null;
public static File dictRoot = null; private static File dictRoot = null;
/** /**
* initialize the LibraryProvider as static class. * initialize the LibraryProvider as static class.
@ -61,7 +61,7 @@ public class LibraryProvider {
* @param pathToSource * @param pathToSource
* @param pathToDICTIONARIES * @param pathToDICTIONARIES
*/ */
public static void initialize(File rootPath) { public static void initialize(final File rootPath) {
dictSource = new File(rootPath, path_to_source_dictionaries); dictSource = new File(rootPath, path_to_source_dictionaries);
if (!dictSource.exists()) dictSource.mkdirs(); if (!dictSource.exists()) dictSource.mkdirs();
dictRoot = rootPath; dictRoot = rootPath;
@ -91,21 +91,21 @@ public class LibraryProvider {
} }
public static void initDidYouMean() { public static void initDidYouMean() {
File dymDict = new File(dictRoot, path_to_did_you_mean_dictionaries); final File dymDict = new File(dictRoot, path_to_did_you_mean_dictionaries);
if (!dymDict.exists()) dymDict.mkdirs(); if (!dymDict.exists()) dymDict.mkdirs();
dymLib = new DidYouMeanLibrary(dymDict); dymLib = new DidYouMeanLibrary(dymDict);
} }
public static void integrateDeReWo() { public static void integrateDeReWo() {
// translate input files (once..) // translate input files (once..)
File dymDict = new File(dictRoot, path_to_did_you_mean_dictionaries); final File dymDict = new File(dictRoot, path_to_did_you_mean_dictionaries);
if (!dymDict.exists()) dymDict.mkdirs(); if (!dymDict.exists()) dymDict.mkdirs();
File pathToSource = new File(dictRoot, path_to_source_dictionaries); final File pathToSource = new File(dictRoot, path_to_source_dictionaries);
File derewoInput = new File(pathToSource, "derewo-v-30000g-2007-12-31-0.1.txt"); final File derewoInput = new File(pathToSource, "derewo-v-30000g-2007-12-31-0.1.txt");
File derewoOutput = new File(dymDict, "derewo-v-30000g-2007-12-31-0.1.words"); final File derewoOutput = new File(dymDict, "derewo-v-30000g-2007-12-31-0.1.words");
if (!derewoOutput.exists() && derewoInput.exists()) { if (!derewoOutput.exists() && derewoInput.exists()) {
// create the translation of the derewo file (which is easy in this case) // create the translation of the derewo file (which is easy in this case)
ArrayList<String> derewo = loadDeReWo(derewoInput, true); final ArrayList<String> derewo = loadDeReWo(derewoInput, true);
try { try {
writeWords(derewoOutput, derewo); writeWords(derewoOutput, derewo);
} catch (IOException e) { } catch (IOException e) {
@ -138,20 +138,20 @@ public class LibraryProvider {
} }
*/ */
private static Set<String> sortUnique(List<String> list) { private static Set<String> sortUnique(final List<String> list) {
TreeSet<String> s = new TreeSet<String>(); final Set<String> s = new TreeSet<String>();
for (String t: list) s.add(t); for (final String t: list) s.add(t);
return s; return s;
} }
private static void writeWords(File f, ArrayList<String> list) throws IOException { private static void writeWords(final File f, final ArrayList<String> list) throws IOException {
Set<String> s = sortUnique(list); final Set<String> s = sortUnique(list);
PrintWriter w = new PrintWriter(new BufferedWriter(new FileWriter(f))); final PrintWriter w = new PrintWriter(new BufferedWriter(new FileWriter(f)));
for (String t: s) w.println(t); for (final String t: s) w.println(t);
w.close(); w.close();
} }
private static ArrayList<String> loadDeReWo(final File file, boolean toLowerCase) { private static ArrayList<String> loadDeReWo(final File file, final boolean toLowerCase) {
final ArrayList<String> list = new ArrayList<String>(); final ArrayList<String> list = new ArrayList<String>();
BufferedReader reader = null; BufferedReader reader = null;
try { try {

@ -11,7 +11,7 @@ public class MimeTable {
private static final Properties mimeTable = new Properties(); private static final Properties mimeTable = new Properties();
public static void init(File mimeFile) { public static void init(final File mimeFile) {
if (mimeTable.isEmpty()) { if (mimeTable.isEmpty()) {
// load the mime table // load the mime table
BufferedInputStream mimeTableInputStream = null; BufferedInputStream mimeTableInputStream = null;
@ -34,19 +34,19 @@ public class MimeTable {
return mimeTable.isEmpty(); return mimeTable.isEmpty();
} }
public static String ext2mime(String ext) { public static String ext2mime(final String ext) {
return mimeTable.getProperty(ext, "application/" + ext); return mimeTable.getProperty(ext, "application/" + ext);
} }
public static String ext2mime(String ext, String dfltMime) { public static String ext2mime(final String ext, final String dfltMime) {
return mimeTable.getProperty(ext, dfltMime); return mimeTable.getProperty(ext, dfltMime);
} }
public static String url2mime(DigestURI url, String dfltMime) { public static String url2mime(final DigestURI url, final String dfltMime) {
return ext2mime(url.getFileExtension(), dfltMime); return ext2mime(url.getFileExtension(), dfltMime);
} }
public static String url2mime(DigestURI url) { public static String url2mime(final DigestURI url) {
return ext2mime(url.getFileExtension()); return ext2mime(url.getFileExtension());
} }
} }

@ -224,6 +224,7 @@ public class SitemapParser extends DefaultHandler {
* @param qName qualified name * @param qName qualified name
* @see DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes) * @see DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
*/ */
@Override
public void startElement(final String namespaceURI, final String localName, final String qName, final Attributes attrs) throws SAXException { public void startElement(final String namespaceURI, final String localName, final String qName, final Attributes attrs) throws SAXException {
this.currentElement = qName; this.currentElement = qName;
@ -242,6 +243,7 @@ public class SitemapParser extends DefaultHandler {
* @throws SAXException * @throws SAXException
* @see DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String) * @see DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
*/ */
@Override
public void endElement(final String namespaceURI, final String localName, final String qName) throws SAXException { public void endElement(final String namespaceURI, final String localName, final String qName) throws SAXException {
this.currentElement = ""; this.currentElement = "";
@ -291,6 +293,7 @@ public class SitemapParser extends DefaultHandler {
} }
} }
@Override
public void characters(final char[] buf, final int offset, final int len) throws SAXException { public void characters(final char[] buf, final int offset, final int len) throws SAXException {
if (this.currentElement.equalsIgnoreCase(SITEMAP_URL_LOC)) { if (this.currentElement.equalsIgnoreCase(SITEMAP_URL_LOC)) {
// TODO: we need to decode the URL here // TODO: we need to decode the URL here

@ -71,7 +71,7 @@ public class URLAnalysis {
* processes to analyse URL lists * processes to analyse URL lists
*/ */
public static DigestURI poison = null; private static DigestURI poison = null;
static { static {
try { try {
poison = new DigestURI("http://poison.org/poison", null); poison = new DigestURI("http://poison.org/poison", null);
@ -82,18 +82,19 @@ public class URLAnalysis {
public static class splitter extends Thread { public static class splitter extends Thread {
ArrayBlockingQueue<DigestURI> in; private ArrayBlockingQueue<DigestURI> in;
ConcurrentHashMap<String, Integer> out; private ConcurrentHashMap<String, Integer> out;
public splitter(ArrayBlockingQueue<DigestURI> in, ConcurrentHashMap<String, Integer> out) { public splitter(final ArrayBlockingQueue<DigestURI> in, final ConcurrentHashMap<String, Integer> out) {
this.in = in; this.in = in;
this.out = out; this.out = out;
} }
@Override
public void run() { public void run() {
try { try {
DigestURI url; DigestURI url;
Pattern p = Pattern.compile("~|\\(|\\)|\\+|-|@|:|%|\\.|;|_"); final Pattern p = Pattern.compile("~|\\(|\\)|\\+|-|@|:|%|\\.|;|_");
while (true) { while (true) {
try { try {
url = in.take(); url = in.take();
@ -109,9 +110,9 @@ public class URLAnalysis {
} }
} }
private void update(String[] s) { private void update(final String[] s) {
Integer c; Integer c;
for (String t: s) { for (final String t: s) {
if (t.length() == 0) continue; if (t.length() == 0) continue;
c = out.get(t); c = out.get(t);
out.put(t, (c == null) ? 1 : c.intValue() + 1); out.put(t, (c == null) ? 1 : c.intValue() + 1);
@ -119,7 +120,7 @@ public class URLAnalysis {
} }
} }
public static void cleanup(ConcurrentHashMap<String, Integer> stat) { public static void cleanup(final ConcurrentHashMap<String, Integer> stat) {
Map.Entry<String, Integer> entry; Map.Entry<String, Integer> entry;
int c, low = Integer.MAX_VALUE; int c, low = Integer.MAX_VALUE;
Iterator<Map.Entry<String, Integer>> i = stat.entrySet().iterator(); Iterator<Map.Entry<String, Integer>> i = stat.entrySet().iterator();
@ -143,22 +144,22 @@ public class URLAnalysis {
Runtime.getRuntime().gc(); Runtime.getRuntime().gc();
} }
public static void genstat(String urlfile) { public static void genstat(final String urlfile) {
boolean gz = urlfile.endsWith(".gz"); final boolean gz = urlfile.endsWith(".gz");
String analysis = (gz) ? urlfile.substring(0, urlfile.length() - 3) + ".stats.gz" : urlfile + ".stats"; final String analysis = (gz) ? urlfile.substring(0, urlfile.length() - 3) + ".stats.gz" : urlfile + ".stats";
long cleanuplimit = Math.max(50 * 1024 * 1024, MemoryControl.available() / 8); final long cleanuplimit = Math.max(50 * 1024 * 1024, MemoryControl.available() / 8);
// start threads // start threads
ArrayBlockingQueue<DigestURI> in = new ArrayBlockingQueue<DigestURI>(1000); final ArrayBlockingQueue<DigestURI> in = new ArrayBlockingQueue<DigestURI>(1000);
ConcurrentHashMap<String, Integer> out = new ConcurrentHashMap<String, Integer>(); final ConcurrentHashMap<String, Integer> out = new ConcurrentHashMap<String, Integer>();
for (int i = 0; i < Runtime.getRuntime().availableProcessors(); i++) new splitter(in, out).start(); for (int i = 0, available = Runtime.getRuntime().availableProcessors(); i < available; i++) new splitter(in, out).start();
splitter spl = new splitter(in, out); final splitter spl = new splitter(in, out);
spl.start(); spl.start();
// put urls in queue // put urls in queue
File infile = new File(urlfile); final File infile = new File(urlfile);
File outfile = new File(analysis); final File outfile = new File(analysis);
BufferedReader reader = null; BufferedReader reader = null;
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
long start = time; long start = time;
@ -202,7 +203,7 @@ public class URLAnalysis {
// stop threads // stop threads
System.out.println("stopping threads"); System.out.println("stopping threads");
for (int i = 0; i < Runtime.getRuntime().availableProcessors() + 1; i++) try { for (int i = 0, available = Runtime.getRuntime().availableProcessors() + 1; i < available; i++) try {
in.put(poison); in.put(poison);
} catch (InterruptedException e) { } catch (InterruptedException e) {
Log.logException(e); Log.logException(e);
@ -215,10 +216,10 @@ public class URLAnalysis {
// generate statistics // generate statistics
System.out.println("start processing results"); System.out.println("start processing results");
TreeMap<String, Integer> results = new TreeMap<String, Integer>(); final TreeMap<String, Integer> results = new TreeMap<String, Integer>();
count = 0; count = 0;
Map.Entry<String, Integer> entry; Map.Entry<String, Integer> entry;
Iterator<Map.Entry<String, Integer>> i = out.entrySet().iterator(); final Iterator<Map.Entry<String, Integer>> i = out.entrySet().iterator();
while (i.hasNext()) { while (i.hasNext()) {
entry = i.next(); entry = i.next();
results.put(num(entry.getValue().intValue() * (entry.getKey().length() - 1)) + " - " + entry.getKey(), entry.getValue()); results.put(num(entry.getValue().intValue() * (entry.getKey().length() - 1)) + " - " + entry.getKey(), entry.getValue());
@ -236,7 +237,7 @@ public class URLAnalysis {
OutputStream os = new BufferedOutputStream(new FileOutputStream(outfile)); OutputStream os = new BufferedOutputStream(new FileOutputStream(outfile));
if (gz) os = new GZIPOutputStream(os); if (gz) os = new GZIPOutputStream(os);
count = 0; count = 0;
for (Map.Entry<String, Integer> e: results.entrySet()) { for (final Map.Entry<String, Integer> e: results.entrySet()) {
os.write(e.getKey().getBytes()); os.write(e.getKey().getBytes());
os.write(new byte[]{'\t'}); os.write(new byte[]{'\t'});
os.write(Integer.toString(e.getValue()).getBytes()); os.write(Integer.toString(e.getValue()).getBytes());
@ -255,15 +256,15 @@ public class URLAnalysis {
System.out.println("finished"); System.out.println("finished");
} }
public static void genhost(String urlfile) { public static void genhost(final String urlfile) {
boolean gz = urlfile.endsWith(".gz"); final boolean gz = urlfile.endsWith(".gz");
String trunk = (gz) ? urlfile.substring(0, urlfile.length() - 3) + ".host" : urlfile + ".host"; final String trunk = (gz) ? urlfile.substring(0, urlfile.length() - 3) + ".host" : urlfile + ".host";
HashSet<String> hosts = new HashSet<String>(); final HashSet<String> hosts = new HashSet<String>();
File infile = new File(urlfile); final File infile = new File(urlfile);
BufferedReader reader = null; BufferedReader reader = null;
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
long start = time; final long start = time;
int count = 0; int count = 0;
System.out.println("start processing"); System.out.println("start processing");
@ -297,9 +298,9 @@ public class URLAnalysis {
// copy everything into a TreeSet to order it // copy everything into a TreeSet to order it
System.out.println("start processing results"); System.out.println("start processing results");
TreeSet<String> results = new TreeSet<String>(); final TreeSet<String> results = new TreeSet<String>();
count = 0; count = 0;
Iterator<String> i = hosts.iterator(); final Iterator<String> i = hosts.iterator();
while (i.hasNext()) { while (i.hasNext()) {
results.add(i.next()); results.add(i.next());
count++; count++;
@ -316,7 +317,7 @@ public class URLAnalysis {
System.out.println("finished"); System.out.println("finished");
} }
private static void writeSet(String trunk, boolean gz, Set<String> set) { private static void writeSet(final String trunk, final boolean gz, final Set<String> set) {
// write hosts // write hosts
System.out.println("start writing results"); System.out.println("start writing results");
@ -326,7 +327,7 @@ public class URLAnalysis {
OutputStream os = new BufferedOutputStream(new FileOutputStream(outfile)); OutputStream os = new BufferedOutputStream(new FileOutputStream(outfile));
if (gz) os = new GZIPOutputStream(os); if (gz) os = new GZIPOutputStream(os);
int count = 0; int count = 0;
for (String h: set) { for (final String h: set) {
os.write(h.getBytes()); os.write(h.getBytes());
os.write(new byte[]{'\n'}); os.write(new byte[]{'\n'});
count++; count++;
@ -343,18 +344,18 @@ public class URLAnalysis {
System.out.println("finished writing results"); System.out.println("finished writing results");
} }
public static void sortsplit(String urlfile) { public static void sortsplit(final String urlfile) {
boolean gz = urlfile.endsWith(".gz"); final boolean gz = urlfile.endsWith(".gz");
String trunk = ((gz) ? urlfile.substring(0, urlfile.length() - 3) : urlfile) + ".sort"; final String trunk = ((gz) ? urlfile.substring(0, urlfile.length() - 3) : urlfile) + ".sort";
File infile = new File(urlfile); final File infile = new File(urlfile);
TreeSet<String> urls = new TreeSet<String>(); final TreeSet<String> urls = new TreeSet<String>();
BufferedReader reader = null; BufferedReader reader = null;
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
long start = time; final long start = time;
int count = 0; int count = 0;
int filecount = 0; int filecount = 0;
long cleanuplimit = Math.max(50 * 1024 * 1024, MemoryControl.available() / 8); final long cleanuplimit = Math.max(50 * 1024 * 1024, MemoryControl.available() / 8);
System.out.println("start processing"); System.out.println("start processing");
try { try {
@ -397,9 +398,9 @@ public class URLAnalysis {
System.out.println("finished"); System.out.println("finished");
} }
public static void incell(File cellPath, String statisticPath) { public static void incell(final File cellPath, final String statisticPath) {
try { try {
HandleMap idx = ReferenceContainerArray.referenceHashes( final HandleMap idx = ReferenceContainerArray.referenceHashes(
cellPath, cellPath,
Segment.wordReferenceFactory, Segment.wordReferenceFactory,
Base64Order.enhancedCoder, Base64Order.enhancedCoder,
@ -412,40 +413,40 @@ public class URLAnalysis {
} }
} }
public static int diffurlcol(String metadataPath, String statisticFile, String diffFile) throws IOException, RowSpaceExceededException { public static int diffurlcol(final String metadataPath, final String statisticFile, final String diffFile) throws IOException, RowSpaceExceededException {
System.out.println("INDEX DIFF URL-COL startup"); System.out.println("INDEX DIFF URL-COL startup");
HandleMap idx = new HandleMap(URIMetadataRow.rowdef.primaryKeyLength, URIMetadataRow.rowdef.objectOrder, 4, new File(statisticFile), 0); final HandleMap idx = new HandleMap(URIMetadataRow.rowdef.primaryKeyLength, URIMetadataRow.rowdef.objectOrder, 4, new File(statisticFile), 0);
MetadataRepository mr = new MetadataRepository(new File(metadataPath), "text.urlmd", false, false); final MetadataRepository mr = new MetadataRepository(new File(metadataPath), "text.urlmd", false, false);
HandleSet hs = new HandleSet(URIMetadataRow.rowdef.primaryKeyLength, URIMetadataRow.rowdef.objectOrder, 1000000); final HandleSet hs = new HandleSet(URIMetadataRow.rowdef.primaryKeyLength, URIMetadataRow.rowdef.objectOrder, 1000000);
System.out.println("INDEX DIFF URL-COL loaded dump, starting diff"); System.out.println("INDEX DIFF URL-COL loaded dump, starting diff");
long start = System.currentTimeMillis(); final long start = System.currentTimeMillis();
long update = start - 7000; long update = start - 7000;
int c = 0; int count = 0;
for (byte[] refhash: mr) { for (final byte[] refhash: mr) {
if (idx.get(refhash) == -1) { if (idx.get(refhash) == -1) {
// the key exists as urlhash in the URL database, but not in the collection as referenced urlhash // the key exists as urlhash in the URL database, but not in the collection as referenced urlhash
hs.put(refhash); hs.put(refhash);
} }
c++; count++;
if (System.currentTimeMillis() - update > 10000) { if (System.currentTimeMillis() - update > 10000) {
System.out.println("INDEX DIFF URL-COL running, checked " + c + ", found " + hs.size() + " missing references so far, " + (((System.currentTimeMillis() - start) * (mr.size() - c) / c) / 60000) + " minutes remaining"); System.out.println("INDEX DIFF URL-COL running, checked " + count + ", found " + hs.size() + " missing references so far, " + (((System.currentTimeMillis() - start) * (mr.size() - count) / count) / 60000) + " minutes remaining");
update = System.currentTimeMillis(); update = System.currentTimeMillis();
} }
} }
mr.close(); mr.close();
System.out.println("INDEX DIFF URL-COL finished diff, starting dump to " + diffFile); System.out.println("INDEX DIFF URL-COL finished diff, starting dump to " + diffFile);
c = hs.dump(new File(diffFile)); count = hs.dump(new File(diffFile));
System.out.println("INDEX DIFF URL-COL finished dump, wrote " + c + " references that occur in the URL-DB, but not in the collection-dump"); System.out.println("INDEX DIFF URL-COL finished dump, wrote " + count + " references that occur in the URL-DB, but not in the collection-dump");
return c; return count;
} }
public static void export(String metadataPath, int format, String export, String diffFile) throws IOException, RowSpaceExceededException { public static void export(final String metadataPath, final int format, final String export, final String diffFile) throws IOException, RowSpaceExceededException {
// format: 0=text, 1=html, 2=rss/xml // format: 0=text, 1=html, 2=rss/xml
System.out.println("URL EXPORT startup"); System.out.println("URL EXPORT startup");
MetadataRepository mr = new MetadataRepository(new File(metadataPath), "text.urlmd", false, false); final MetadataRepository mr = new MetadataRepository(new File(metadataPath), "text.urlmd", false, false);
HandleSet hs = (diffFile == null) ? null : new HandleSet(URIMetadataRow.rowdef.primaryKeyLength, URIMetadataRow.rowdef.objectOrder, new File(diffFile), 0); final HandleSet hs = (diffFile == null) ? null : new HandleSet(URIMetadataRow.rowdef.primaryKeyLength, URIMetadataRow.rowdef.objectOrder, new File(diffFile), 0);
System.out.println("URL EXPORT loaded dump, starting export"); System.out.println("URL EXPORT loaded dump, starting export");
Export e = mr.export(new File(export), ".*", hs, format, false); final Export e = mr.export(new File(export), ".*", hs, format, false);
try { try {
e.join(); e.join();
} catch (InterruptedException e1) { } catch (InterruptedException e1) {
@ -454,19 +455,19 @@ public class URLAnalysis {
System.out.println("URL EXPORT finished export, wrote " + ((hs == null) ? mr.size() : hs.size()) + " entries"); System.out.println("URL EXPORT finished export, wrote " + ((hs == null) ? mr.size() : hs.size()) + " entries");
} }
public static void delete(String metadataPath, String diffFile) throws IOException, RowSpaceExceededException { public static void delete(final String metadataPath, final String diffFile) throws IOException, RowSpaceExceededException {
System.out.println("URL DELETE startup"); System.out.println("URL DELETE startup");
MetadataRepository mr = new MetadataRepository(new File(metadataPath), "text.urlmd", false, false); final MetadataRepository mr = new MetadataRepository(new File(metadataPath), "text.urlmd", false, false);
int mrSize = mr.size(); final int mrSize = mr.size();
HandleSet hs = new HandleSet(URIMetadataRow.rowdef.primaryKeyLength, URIMetadataRow.rowdef.objectOrder, new File(diffFile), 0); final HandleSet hs = new HandleSet(URIMetadataRow.rowdef.primaryKeyLength, URIMetadataRow.rowdef.objectOrder, new File(diffFile), 0);
System.out.println("URL DELETE loaded dump, starting deletion of " + hs.size() + " entries from " + mrSize); System.out.println("URL DELETE loaded dump, starting deletion of " + hs.size() + " entries from " + mrSize);
for (byte[] refhash: hs) { for (final byte[] refhash: hs) {
mr.remove(refhash); mr.remove(refhash);
} }
System.out.println("URL DELETE finished deletions, " + mr.size() + " entries left in URL database"); System.out.println("URL DELETE finished deletions, " + mr.size() + " entries left in URL database");
} }
public static void main(String[] args) { public static void main(final String[] args) {
if (args[0].equals("-stat") && args.length >= 2) { if (args[0].equals("-stat") && args.length >= 2) {
// generate a statistics about common words in file, store to <file>.stat // generate a statistics about common words in file, store to <file>.stat
// example: // example:
@ -548,9 +549,9 @@ public class URLAnalysis {
System.exit(0); // kill remaining threads System.exit(0); // kill remaining threads
} }
private static final String num(int i) { private static final String num(final int i) {
String s = Integer.toString(i); StringBuffer s = new StringBuffer(Integer.toString(i));
while (s.length() < 9) s = "0" + s; while (s.length() < 9) s.insert(0, "0");
return s; return s.toString();
} }
} }

@ -56,10 +56,10 @@ public class URLLicense {
public String aquireLicense(final DigestURI url) { public String aquireLicense(final DigestURI url) {
// generate license key // generate license key
String license = ""; StringBuilder stringBuilder = new StringBuilder();
if (url == null) return license; if (url == null) return stringBuilder.toString();
while (license.length() < keylen) license += Integer.toHexString(random.nextInt()); while (stringBuilder.length() < keylen) stringBuilder.append(Integer.toHexString(random.nextInt()));
license = license.substring(0, keylen); String license = stringBuilder.substring(0, keylen);
// store reference to url with license key // store reference to url with license key
permissions.put(license, url); permissions.put(license, url);
aging.add(license); aging.add(license);

@ -50,12 +50,12 @@ public class WorkTables extends Tables {
public final static String TABLE_ROBOTS_NAME = "robots"; public final static String TABLE_ROBOTS_NAME = "robots";
public WorkTables(File workPath) { public WorkTables(final File workPath) {
super(workPath, 12); super(workPath, 12);
} }
public void recordAPICall(final serverObjects post, final String servletName, String type, String comment) { public void recordAPICall(final serverObjects post, final String servletName, final String type, final String comment) {
String apiurl = /*"http://localhost:" + getConfig("port", "8080") +*/ "/" + servletName + "?" + post.toString(); final String apiurl = /*"http://localhost:" + getConfig("port", "8080") +*/ "/" + servletName + "?" + post.toString();
try { try {
super.insert( super.insert(
TABLE_API_NAME, TABLE_API_NAME,

@ -43,6 +43,7 @@ import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import net.yacy.kelondro.blob.MapHeap; import net.yacy.kelondro.blob.MapHeap;
import net.yacy.kelondro.index.RowSpaceExceededException;
import net.yacy.kelondro.logging.Log; import net.yacy.kelondro.logging.Log;
import net.yacy.kelondro.order.Base64Order; import net.yacy.kelondro.order.Base64Order;
import net.yacy.kelondro.order.NaturalOrder; import net.yacy.kelondro.order.NaturalOrder;
@ -55,19 +56,19 @@ import org.w3c.dom.NodeList;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import de.anomic.data.wiki.wikiBoard; import de.anomic.data.wiki.wikiBoard;
import java.util.List;
import java.util.Set;
public class blogBoard { public class blogBoard {
public static final int keyLength = 64; private static final int KEY_LENGTH = 64;
MapHeap database = null; private MapHeap database = null;
public blogBoard(final File actpath) throws IOException { public blogBoard(final File actpath) throws IOException {
new File(actpath.getParent()).mkdir(); new File(actpath.getParent()).mkdir();
if (database == null) {
//database = new MapView(BLOBTree.toHeap(actpath, true, true, keyLength, recordSize, '_', NaturalOrder.naturalOrder, newFile), 500, '_'); //database = new MapView(BLOBTree.toHeap(actpath, true, true, keyLength, recordSize, '_', NaturalOrder.naturalOrder, newFile), 500, '_');
database = new MapHeap(actpath, keyLength, NaturalOrder.naturalOrder, 1024 * 64, 500, '_'); database = new MapHeap(actpath, KEY_LENGTH, NaturalOrder.naturalOrder, 1024 * 64, 500, '_');
}
} }
public int size() { public int size() {
@ -92,17 +93,11 @@ public class blogBoard {
} }
private static String normalize(final String key) { private static String normalize(final String key) {
if (key == null) return "null"; return (key == null) ? "null" : key.trim().toLowerCase();
return key.trim().toLowerCase();
} }
public static String webalize(String key) { public static String webalize(final String key) {
if (key == null) return "null"; return (key == null) ? "null": key.trim().toLowerCase().replaceAll(" ", "%20");
key = key.trim().toLowerCase();
int p;
while ((p = key.indexOf(" ")) >= 0)
key = key.substring(0, p) + "%20" + key.substring(p +1);
return key;
} }
public String guessAuthor(final String ip) { public String guessAuthor(final String ip) {
@ -110,7 +105,7 @@ public class blogBoard {
} }
/** /**
* Create a new BlogEntry an return it * Create a new BlogEntry and return it
* @param key * @param key
* @param subject * @param subject
* @param author * @param author
@ -121,7 +116,7 @@ public class blogBoard {
* @param commentMode possible params are: 0 - no comments allowed, 1 - comments allowed, 2 - comments moderated * @param commentMode possible params are: 0 - no comments allowed, 1 - comments allowed, 2 - comments moderated
* @return BlogEntry * @return BlogEntry
*/ */
public BlogEntry newEntry(final String key, final byte[] subject, final byte[] author, final String ip, final Date date, final byte[] page, final ArrayList<String> comments, final String commentMode) { public BlogEntry newEntry(final String key, final byte[] subject, final byte[] author, final String ip, final Date date, final byte[] page, final List<String> comments, final String commentMode) {
return new BlogEntry(normalize(key), subject, author, ip, date, page, comments, commentMode); return new BlogEntry(normalize(key), subject, author, ip, date, page, comments, commentMode);
} }
@ -129,78 +124,85 @@ public class blogBoard {
* writes a new page and return the key * writes a new page and return the key
*/ */
public String writeBlogEntry(final BlogEntry page) { public String writeBlogEntry(final BlogEntry page) {
String ret = null;
try { try {
database.put(page.key, page.record); database.put(page.key, page.record);
return page.key; ret = page.key;
} catch (final Exception e) { } catch (IOException ex) {
Log.logException(e); Log.logException(ex);
return null; } catch (RowSpaceExceededException ex) {
Log.logException(ex);
} }
return ret;
} }
public BlogEntry readBlogEntry(final String key) { public BlogEntry readBlogEntry(final String key) {
return readBlogEntry(key, database); return readBlogEntry(key, database);
} }
private BlogEntry readBlogEntry(String key, final MapHeap base) { private BlogEntry readBlogEntry(final String key, final MapHeap base) {
key = normalize(key); final String normalized = normalize(key);
if (key.length() > keyLength) key = key.substring(0, keyLength);
Map<String, String> record; Map<String, String> record;
try { try {
record = base.get(key); record = base.get(normalized.substring(0, Math.min(normalized.length(), KEY_LENGTH)));
} catch (final IOException e) { } catch (final IOException e) {
record = null; record = null;
} }
if (record == null) return (record == null) ?
return newEntry(key, "".getBytes(), "anonymous".getBytes(), "127.0.0.1", new Date(), "".getBytes(), null, null); newEntry(key, new byte[0], "anonymous".getBytes(), "127.0.0.1", new Date(), new byte[0], null, null) :
return new BlogEntry(key, record); new BlogEntry(key, record);
} }
public boolean importXML(final String input) { public boolean importXML(final String input) {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try { try {
final DocumentBuilder builder = factory.newDocumentBuilder(); final DocumentBuilder builder = factory.newDocumentBuilder();
final Document doc = builder.parse(new ByteArrayInputStream(input.getBytes("UTF-8"))); return parseXMLimport(builder.parse(new ByteArrayInputStream(input.getBytes("UTF-8"))));
return parseXMLimport(doc); } catch (final ParserConfigurationException ex) {
} catch (final ParserConfigurationException e) { Log.logException(ex);
} catch (final SAXException e) { } catch (final SAXException ex) {
} catch (final IOException e) {} Log.logException(ex);
} catch (final IOException ex) {
Log.logException(ex);
}
return false; return false;
} }
private boolean parseXMLimport(final Document doc) { private boolean parseXMLimport(final Document doc) {
if(!doc.getDocumentElement().getTagName().equals("blog")) if(!doc.getDocumentElement().getTagName().equals("blog")) {
return false; return false;
}
final NodeList items = doc.getDocumentElement().getElementsByTagName("item"); final NodeList items = doc.getDocumentElement().getElementsByTagName("item");
if(items.getLength() == 0) if(items.getLength() == 0) {
return false; return false;
}
for(int i=0;i<items.getLength();++i) { for (int i = 0, n = items.getLength(); i < n; ++i) {
String key = null, ip = null, StrSubject = null, StrAuthor = null, StrPage = null, StrDate = null; String key = null, ip = null, StrSubject = null, StrAuthor = null, StrPage = null, StrDate = null;
Date date = null; Date date = null;
if(!items.item(i).getNodeName().equals("item")) if(!items.item(i).getNodeName().equals("item")) continue;
continue;
final NodeList currentNodeChildren = items.item(i).getChildNodes(); final NodeList currentNodeChildren = items.item(i).getChildNodes();
for(int j=0;j<currentNodeChildren.getLength();++j) { for (int j = 0, m = currentNodeChildren.getLength(); j < m; ++j) {
final Node currentNode = currentNodeChildren.item(j); final Node currentNode = currentNodeChildren.item(j);
if(currentNode.getNodeName().equals("id")) if (currentNode.getNodeName().equals("id")) {
key = currentNode.getFirstChild().getNodeValue(); key = currentNode.getFirstChild().getNodeValue();
else if(currentNode.getNodeName().equals("ip")) } else if (currentNode.getNodeName().equals("ip")) {
ip = currentNode.getFirstChild().getNodeValue(); ip = currentNode.getFirstChild().getNodeValue();
else if(currentNode.getNodeName().equals("timestamp")) } else if (currentNode.getNodeName().equals("timestamp")) {
StrDate = currentNode.getFirstChild().getNodeValue(); StrDate = currentNode.getFirstChild().getNodeValue();
else if(currentNode.getNodeName().equals("subject")) } else if (currentNode.getNodeName().equals("subject")) {
StrSubject = currentNode.getFirstChild().getNodeValue(); StrSubject = currentNode.getFirstChild().getNodeValue();
else if(currentNode.getNodeName().equals("author")) } else if (currentNode.getNodeName().equals("author")) {
StrAuthor = currentNode.getFirstChild().getNodeValue(); StrAuthor = currentNode.getFirstChild().getNodeValue();
else if(currentNode.getNodeName().equals("content")) } else if (currentNode.getNodeName().equals("content")) {
StrPage = currentNode.getFirstChild().getNodeValue(); StrPage = currentNode.getFirstChild().getNodeValue();
} }
}
try { try {
date = DateFormatter.parseShortSecond(StrDate); date = DateFormatter.parseShortSecond(StrDate);
@ -208,8 +210,9 @@ public class blogBoard {
date = new Date(); date = new Date();
} }
if(key == null || ip == null || StrSubject == null || StrAuthor == null || StrPage == null || date == null) if(key == null || ip == null || StrSubject == null || StrAuthor == null || StrPage == null || date == null) {
return false; return false;
}
byte[] subject,author,page; byte[] subject,author,page;
try { try {
@ -217,11 +220,13 @@ public class blogBoard {
} catch (final UnsupportedEncodingException e1) { } catch (final UnsupportedEncodingException e1) {
subject = StrSubject.getBytes(); subject = StrSubject.getBytes();
} }
try { try {
author = StrAuthor.getBytes("UTF-8"); author = StrAuthor.getBytes("UTF-8");
} catch (final UnsupportedEncodingException e1) { } catch (final UnsupportedEncodingException e1) {
author = StrAuthor.getBytes(); author = StrAuthor.getBytes();
} }
try { try {
page = StrPage.getBytes("UTF-8"); page = StrPage.getBytes("UTF-8");
} catch (final UnsupportedEncodingException e1) { } catch (final UnsupportedEncodingException e1) {
@ -233,10 +238,9 @@ public class blogBoard {
return true; return true;
} }
public void deleteBlogEntry(String key) { public void deleteBlogEntry(final String key) {
key = normalize(key);
try { try {
database.remove(key); database.remove(normalize(key));
} catch (final IOException e) { } } catch (final IOException e) { }
} }
@ -264,18 +268,20 @@ public class blogBoard {
if (blogEntry1 == null || blogEntry2 == null) if (blogEntry1 == null || blogEntry2 == null)
return 0; return 0;
if (this.newestFirst) { if (this.newestFirst) {
if (Long.parseLong(blogEntry2.getTimestamp()) - Long.parseLong(blogEntry1.getTimestamp()) > 0) if (Long.parseLong(blogEntry2.getTimestamp()) - Long.parseLong(blogEntry1.getTimestamp()) > 0) {
return 1; return 1;
}
return -1; return -1;
} }
if (Long.parseLong(blogEntry1.getTimestamp()) - Long.parseLong(blogEntry2.getTimestamp()) > 0) if (Long.parseLong(blogEntry1.getTimestamp()) - Long.parseLong(blogEntry2.getTimestamp()) > 0) {
return 1; return 1;
}
return -1; return -1;
} }
} }
public Iterator<String> getBlogIterator(final boolean priv){ public Iterator<String> getBlogIterator(final boolean priv){
final TreeSet<String> set = new TreeSet<String>(new BlogComparator(true)); final Set<String> set = new TreeSet<String>(new BlogComparator(true));
final Iterator<BlogEntry> iterator = blogIterator(true); final Iterator<BlogEntry> iterator = blogIterator(true);
BlogEntry blogEntry; BlogEntry blogEntry;
while (iterator.hasNext()) { while (iterator.hasNext()) {
@ -333,6 +339,7 @@ public class blogBoard {
// //resetDatabase(); // //resetDatabase();
// } // }
// } // }
throw new UnsupportedOperationException("Method not implemented yet.");
} }
} }
@ -341,7 +348,7 @@ public class blogBoard {
String key; String key;
Map<String, String> record; Map<String, String> record;
public BlogEntry(final String nkey, final byte[] subject, final byte[] author, final String ip, final Date date, final byte[] page, final ArrayList<String> comments, final String commentMode) { public BlogEntry(final String nkey, final byte[] subject, final byte[] author, final String ip, final Date date, final byte[] page, final List<String> comments, final String commentMode) {
record = new HashMap<String, String>(); record = new HashMap<String, String>();
setKey(nkey); setKey(nkey);
setDate(date); setDate(date);
@ -361,14 +368,16 @@ public class blogBoard {
BlogEntry(final String key, final Map<String, String> record) { BlogEntry(final String key, final Map<String, String> record) {
this.key = key; this.key = key;
this.record = record; this.record = record;
if (this.record.get("comments")==null) this.record.put("comments", listManager.collection2string(new ArrayList<String>())); if (this.record.get("comments") == null) {
if (this.record.get("commentMode")==null || this.record.get("commentMode").equals("")) this.record.put("commentMode", "2"); this.record.put("comments", listManager.collection2string(new ArrayList<String>()));
}
if (this.record.get("commentMode") == null || this.record.get("commentMode").equals("")) {
this.record.put("commentMode", "2");
}
} }
private void setKey(final String key) { private void setKey(final String key) {
if (key.length() > keyLength) this.key = key.substring(0, Math.min(key.length(), KEY_LENGTH));
this.key = key.substring(0, keyLength);
this.key = key;
} }
public String getKey() { public String getKey() {
@ -377,42 +386,50 @@ public class blogBoard {
public byte[] getSubject() { public byte[] getSubject() {
final String m = record.get("subject"); final String m = record.get("subject");
if (m == null) return new byte[0]; if (m == null) {
return new byte[0];
}
final byte[] b = Base64Order.enhancedCoder.decode(m); final byte[] b = Base64Order.enhancedCoder.decode(m);
if (b == null) return "".getBytes(); return (b == null) ? new byte[0] : b;
return b;
} }
private void setSubject(final byte[] subject) { private void setSubject(final byte[] subject) {
if (subject == null) if (subject == null) {
record.put("subject",""); record.put("subject","");
else } else {
record.put("subject", Base64Order.enhancedCoder.encode(subject)); record.put("subject", Base64Order.enhancedCoder.encode(subject));
} }
}
public Date getDate() { public Date getDate() {
try { try {
final String date = record.get("date"); final String date = record.get("date");
if (date == null) { if (date == null) {
if (Log.isFinest("Blog")) Log.logFinest("Blog", "ERROR: date field missing in blogBoard"); if (Log.isFinest("Blog")) {
Log.logFinest("Blog", "ERROR: date field missing in blogBoard");
}
return new Date(); return new Date();
} }
return DateFormatter.parseShortSecond(date); return DateFormatter.parseShortSecond(date);
} catch (final ParseException e) { } catch (final ParseException ex) {
return new Date(); return new Date();
} }
} }
private void setDate(Date date) { private void setDate(final Date date) {
if(date == null) Date ret = date;
date = new Date(); if (ret == null) {
record.put("date", DateFormatter.formatShortSecond(date)); ret = new Date();
}
record.put("date", DateFormatter.formatShortSecond(ret));
} }
public String getTimestamp() { public String getTimestamp() {
final String timestamp = record.get("date"); final String timestamp = record.get("date");
if (timestamp == null) { if (timestamp == null) {
if (Log.isFinest("Blog")) Log.logFinest("Blog", "ERROR: date field missing in blogBoard"); if (Log.isFinest("Blog")) {
Log.logFinest("Blog", "ERROR: date field missing in blogBoard");
}
return DateFormatter.formatShortSecond(); return DateFormatter.formatShortSecond();
} }
return timestamp; return timestamp;
@ -420,10 +437,11 @@ public class blogBoard {
public byte[] getAuthor() { public byte[] getAuthor() {
final String author = record.get("author"); final String author = record.get("author");
if (author == null) return new byte[0]; if (author == null) {
return new byte[0];
}
final byte[] b = Base64Order.enhancedCoder.decode(author); final byte[] b = Base64Order.enhancedCoder.decode(author);
if (b == null) return "".getBytes(); return (b == null) ? new byte[0] : b;
return b;
} }
private void setAuthor(final byte[] author) { private void setAuthor(final byte[] author) {
@ -439,57 +457,59 @@ public class blogBoard {
record.put("comments", record.get("comments").substring(1)); record.put("comments", record.get("comments").substring(1));
writeBlogEntry(this); writeBlogEntry(this);
} }
final ArrayList<String> commentsize = listManager.string2arraylist(record.get("comments")); final List<String> commentsize = listManager.string2arraylist(record.get("comments"));
return commentsize.size(); return commentsize.size();
} }
public ArrayList<String> getComments() { public List<String> getComments() {
final ArrayList<String> comments = listManager.string2arraylist(record.get("comments")); return listManager.string2arraylist(record.get("comments"));
return comments;
} }
private void setComments(final ArrayList<String> comments) { private void setComments(final List<String> comments) {
if (comments == null) if (comments == null) {
record.put("comments", listManager.collection2string(new ArrayList<String>())); record.put("comments", listManager.collection2string(new ArrayList<String>()));
else } else {
record.put("comments", listManager.collection2string(comments)); record.put("comments", listManager.collection2string(comments));
} }
}
public String getIp() { public String getIp() {
final String ip = record.get("ip"); final String ip = record.get("ip");
if (ip == null) return "127.0.0.1"; return (ip == null) ? "127.0.0.1" : ip;
return ip;
} }
private void setIp(String ip) { private void setIp(final String ip) {
if ((ip == null) || (ip.length() == 0)) String ret = ip;
ip = ""; if ((ret == null) || (ret.length() == 0))
record.put("ip", ip); ret = "";
record.put("ip", ret);
} }
public byte[] getPage() { public byte[] getPage() {
final String page = record.get("page"); final String page = record.get("page");
if (page == null) return new byte[0]; if (page == null) {
return new byte[0];
}
final byte[] page_as_byte = Base64Order.enhancedCoder.decode(page); final byte[] page_as_byte = Base64Order.enhancedCoder.decode(page);
if (page_as_byte == null) return "".getBytes(); return (page_as_byte == null) ? new byte[0] : page_as_byte;
return page_as_byte;
} }
private void setPage(final byte[] page) { private void setPage(final byte[] page) {
if (page == null) if (page == null) {
record.put("page", ""); record.put("page", "");
else } else {
record.put("page", Base64Order.enhancedCoder.encode(page)); record.put("page", Base64Order.enhancedCoder.encode(page));
} }
}
public void addComment(final String commentID) { public void addComment(final String commentID) {
final ArrayList<String> comments = listManager.string2arraylist(record.get("comments")); final List<String> comments = listManager.string2arraylist(record.get("comments"));
comments.add(commentID); comments.add(commentID);
record.put("comments", listManager.collection2string(comments)); record.put("comments", listManager.collection2string(comments));
} }
public boolean removeComment(final String commentID) { public boolean removeComment(final String commentID) {
final ArrayList<String> comments = listManager.string2arraylist(record.get("comments")); final List<String> comments = listManager.string2arraylist(record.get("comments"));
final boolean success = comments.remove(commentID); final boolean success = comments.remove(commentID);
record.put("comments", listManager.collection2string(comments)); record.put("comments", listManager.collection2string(comments));
return success; return success;
@ -507,19 +527,16 @@ public class blogBoard {
} }
private void setCommentMode(final String mode) { private void setCommentMode(final String mode) {
if (mode == null) if (mode == null) {
record.put("commentMode", "2"); record.put("commentMode", "2");
else } else {
record.put("commentMode", mode); record.put("commentMode", mode);
} }
}
public boolean isPublic() { public boolean isPublic() {
final String privacy = record.get("privacy"); final String privacy = record.get("privacy");
if (privacy == null) return (privacy == null || privacy.equalsIgnoreCase("public")) ? true : false;
return true;
if(privacy.equalsIgnoreCase("public"))
return true;
return false;
} }
} }

@ -56,22 +56,20 @@ import de.anomic.data.wiki.wikiBoard;
public class blogBoardComments { public class blogBoardComments {
public static final int keyLength = 64; private final static int KEY_LENGTH = 64;
private static final String dateFormat = "yyyyMMddHHmmss"; private final static String DATE_FORMAT = "yyyyMMddHHmmss";
static SimpleDateFormat SimpleFormatter = new SimpleDateFormat(dateFormat, Locale.US); private final static SimpleDateFormat SIMPLE_DATE_FORMATTER = new SimpleDateFormat(DATE_FORMAT, Locale.US);
static { static {
SimpleFormatter.setTimeZone(TimeZone.getTimeZone("GMT")); SIMPLE_DATE_FORMATTER.setTimeZone(TimeZone.getTimeZone("GMT"));
} }
private MapHeap database = null; private MapHeap database = null;
public blogBoardComments(final File actpath) throws IOException { public blogBoardComments(final File actpath) throws IOException {
new File(actpath.getParent()).mkdir(); new File(actpath.getParent()).mkdir();
if (database == null) {
//database = new MapView(BLOBTree.toHeap(actpath, true, true, keyLength, recordSize, '_', NaturalOrder.naturalOrder, newFile), 500, '_'); //database = new MapView(BLOBTree.toHeap(actpath, true, true, keyLength, recordSize, '_', NaturalOrder.naturalOrder, newFile), 500, '_');
database = new MapHeap(actpath, keyLength, NaturalOrder.naturalOrder, 1024 * 64, 500, '_'); database = new MapHeap(actpath, KEY_LENGTH, NaturalOrder.naturalOrder, 1024 * 64, 500, '_');
}
} }
public int size() { public int size() {
@ -83,29 +81,33 @@ public class blogBoardComments {
} }
static String dateString(final Date date) { static String dateString(final Date date) {
synchronized (SimpleFormatter) { synchronized (SIMPLE_DATE_FORMATTER) {
return SimpleFormatter.format(date); return SIMPLE_DATE_FORMATTER.format(date);
} }
} }
private static String normalize(final String key) { private static String normalize(final String key) {
if (key == null) return "null"; return (key == null) ? "null" : key.trim().toLowerCase();
return key.trim().toLowerCase(); }
public static String webalize(final String key) {
if (key == null) {
return "null";
} }
public static String webalize(String key) { String ret = key.trim().toLowerCase();
if (key == null) return "null";
key = key.trim().toLowerCase();
int p; int p;
while ((p = key.indexOf(" ")) >= 0) while ((p = ret.indexOf(" ")) >= 0)
key = key.substring(0, p) + "%20" + key.substring(p +1); ret = ret.substring(0, p) + "%20" + key.substring(p +1);
return key; return ret;
} }
public String guessAuthor(final String ip) { public String guessAuthor(final String ip) {
return wikiBoard.guessAuthor(ip); return wikiBoard.guessAuthor(ip);
} }
public CommentEntry newEntry(final String key, final byte[] subject, final byte[] author, final String ip, final Date date, final byte[] page) { public CommentEntry newEntry(final String key, final byte[] subject, final byte[] author, final String ip, final Date date, final byte[] page) {
return new CommentEntry(normalize(key), subject, author, ip, date, page); return new CommentEntry(normalize(key), subject, author, ip, date, page);
} }
public String write(final CommentEntry page) { public String write(final CommentEntry page) {
// writes a new page and returns key // writes a new page and returns key
try { try {
@ -120,39 +122,47 @@ public class blogBoardComments {
//System.out.println("DEBUG: read from blogBoardComments"); //System.out.println("DEBUG: read from blogBoardComments");
return read(key, database); return read(key, database);
} }
private CommentEntry read(String key, final MapHeap base) {
key = normalize(key); private CommentEntry read(final String key, final MapHeap base) {
if (key.length() > keyLength) key = key.substring(0, keyLength); String copyOfKey = normalize(key);
copyOfKey = copyOfKey.substring(0, Math.min(copyOfKey.length(), KEY_LENGTH));
Map<String, String> record; Map<String, String> record;
try { try {
record = base.get(key); record = base.get(copyOfKey);
} catch (final IOException e) { } catch (final IOException e) {
record = null; record = null;
} }
if (record == null) return newEntry(key, "".getBytes(), "anonymous".getBytes(), "127.0.0.1", new Date(), "".getBytes()); return (record == null) ?
return new CommentEntry(key, record); newEntry(copyOfKey, new byte[0], "anonymous".getBytes(), "127.0.0.1", new Date(), new byte[0]) :
new CommentEntry(copyOfKey, record);
} }
public boolean importXML(final String input) { public boolean importXML(final String input) {
boolean ret = false;
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try { try {
final DocumentBuilder builder = factory.newDocumentBuilder(); final DocumentBuilder builder = factory.newDocumentBuilder();
final Document doc = builder.parse(new ByteArrayInputStream(input.getBytes())); final Document doc = builder.parse(new ByteArrayInputStream(input.getBytes()));
return parseXMLimport(doc); ret = parseXMLimport(doc);
} catch (final ParserConfigurationException e) { }
} catch (final SAXException e) { catch (final ParserConfigurationException e) {}
} catch (final IOException e) {} catch (final SAXException e) {}
catch (final IOException e) {}
return false; return ret;
} }
private boolean parseXMLimport(final Document doc) { private boolean parseXMLimport(final Document doc) {
if(!doc.getDocumentElement().getTagName().equals("blog")) if(!doc.getDocumentElement().getTagName().equals("blog")) {
return false; return false;
}
final NodeList items = doc.getDocumentElement().getElementsByTagName("item"); final NodeList items = doc.getDocumentElement().getElementsByTagName("item");
if(items.getLength() == 0) if(items.getLength() == 0) {
return false; return false;
}
for(int i=0;i<items.getLength();++i) { for(int i = 0, n = items.getLength(); i < n; ++i) {
String key = null, ip = null, StrSubject = null, StrAuthor = null, StrPage = null, StrDate = null; String key = null, ip = null, StrSubject = null, StrAuthor = null, StrPage = null, StrDate = null;
Date date = null; Date date = null;
@ -161,25 +171,27 @@ public class blogBoardComments {
final NodeList currentNodeChildren = items.item(i).getChildNodes(); final NodeList currentNodeChildren = items.item(i).getChildNodes();
for(int j=0;j<currentNodeChildren.getLength();++j) { for(int j=0, m = currentNodeChildren.getLength(); j < m; ++j) {
final Node currentNode = currentNodeChildren.item(j); final Node currentNode = currentNodeChildren.item(j);
if(currentNode.getNodeName().equals("id")) if (currentNode.getNodeName().equals("id")) {
key = currentNode.getFirstChild().getNodeValue(); key = currentNode.getFirstChild().getNodeValue();
else if(currentNode.getNodeName().equals("ip")) } else if(currentNode.getNodeName().equals("ip")) {
ip = currentNode.getFirstChild().getNodeValue(); ip = currentNode.getFirstChild().getNodeValue();
else if(currentNode.getNodeName().equals("timestamp")) } else if(currentNode.getNodeName().equals("timestamp")) {
StrDate = currentNode.getFirstChild().getNodeValue(); StrDate = currentNode.getFirstChild().getNodeValue();
else if(currentNode.getNodeName().equals("subject")) } else if(currentNode.getNodeName().equals("subject")) {
StrSubject = currentNode.getFirstChild().getNodeValue(); StrSubject = currentNode.getFirstChild().getNodeValue();
else if(currentNode.getNodeName().equals("author")) } else if(currentNode.getNodeName().equals("author")) {
StrAuthor = currentNode.getFirstChild().getNodeValue(); StrAuthor = currentNode.getFirstChild().getNodeValue();
else if(currentNode.getNodeName().equals("content")) } else if(currentNode.getNodeName().equals("content")) {
StrPage = currentNode.getFirstChild().getNodeValue(); StrPage = currentNode.getFirstChild().getNodeValue();
} }
}
try { try {
date = SimpleFormatter.parse(StrDate); date = SIMPLE_DATE_FORMATTER.parse(StrDate);
} catch (final ParseException e1) { } catch (final ParseException ex) {
date = new Date(); date = new Date();
} }
@ -189,30 +201,36 @@ public class blogBoardComments {
byte[] subject,author,page; byte[] subject,author,page;
try { try {
subject = StrSubject.getBytes("UTF-8"); subject = StrSubject.getBytes("UTF-8");
} catch (final UnsupportedEncodingException e1) { }
catch (final UnsupportedEncodingException ex) {
subject = StrSubject.getBytes(); subject = StrSubject.getBytes();
} }
try { try {
author = StrAuthor.getBytes("UTF-8"); author = StrAuthor.getBytes("UTF-8");
} catch (final UnsupportedEncodingException e1) { }
catch (final UnsupportedEncodingException ex) {
author = StrAuthor.getBytes(); author = StrAuthor.getBytes();
} }
try { try {
page = StrPage.getBytes("UTF-8"); page = StrPage.getBytes("UTF-8");
} catch (final UnsupportedEncodingException e1) { }
catch (final UnsupportedEncodingException ex) {
page = StrPage.getBytes(); page = StrPage.getBytes();
} }
write (newEntry(key, subject, author, ip, date, page)); write (newEntry(key, subject, author, ip, date, page));
} }
return true; return true;
} }
public void delete(String key) {
key = normalize(key); public void delete(final String key) {
try { try {
database.remove(key); database.remove(normalize(key));
} catch (final IOException e) { } }
catch (final IOException e) { }
} }
public Iterator<byte[]> keys(final boolean up) throws IOException { public Iterator<byte[]> keys(final boolean up) throws IOException {
return database.keys(up, false); return database.keys(up, false);
} }
@ -238,17 +256,19 @@ public class blogBoardComments {
CommentEntry(final String key, final Map<String, String> record) { CommentEntry(final String key, final Map<String, String> record) {
this.key = key; this.key = key;
this.record = record; this.record = record;
if (this.record.get("comments")==null) this.record.put("comments", listManager.collection2string(new ArrayList<String>())); if (this.record.get("comments") == null) {
this.record.put("comments", listManager.collection2string(new ArrayList<String>()));
}
} }
public String getKey() { public String getKey() {
return key; return key;
} }
private void setKey(final String var) { private void setKey(final String var) {
key = var; key = var.substring(0, Math.min(var.length(), KEY_LENGTH));
if (key.length() > keyLength)
key = var.substring(0, keyLength);
} }
private void setSubject(final byte[] subject) { private void setSubject(final byte[] subject) {
if (subject == null) if (subject == null)
record.put("subject",""); record.put("subject","");
@ -259,7 +279,7 @@ public class blogBoardComments {
final String subject = record.get("subject"); final String subject = record.get("subject");
if (subject == null) return new byte[0]; if (subject == null) return new byte[0];
final byte[] subject_bytes = Base64Order.enhancedCoder.decode(subject); final byte[] subject_bytes = Base64Order.enhancedCoder.decode(subject);
if (subject_bytes == null) return "".getBytes(); if (subject_bytes == null) return new byte[0];
return subject_bytes; return subject_bytes;
} }
private void setDate(Date date) { private void setDate(Date date) {
@ -274,8 +294,8 @@ public class blogBoardComments {
if (Log.isFinest("Blog")) Log.logFinest("Blog", "ERROR: date field missing in blogBoard"); if (Log.isFinest("Blog")) Log.logFinest("Blog", "ERROR: date field missing in blogBoard");
return new Date(); return new Date();
} }
synchronized (SimpleFormatter) { synchronized (SIMPLE_DATE_FORMATTER) {
return SimpleFormatter.parse(date); return SIMPLE_DATE_FORMATTER.parse(date);
} }
} catch (final ParseException e) { } catch (final ParseException e) {
return new Date(); return new Date();
@ -302,7 +322,7 @@ public class blogBoardComments {
return new byte[0]; return new byte[0];
final byte[] author_byte = Base64Order.enhancedCoder.decode(author); final byte[] author_byte = Base64Order.enhancedCoder.decode(author);
if (author_byte == null) if (author_byte == null)
return "".getBytes(); return new byte[0];
return author_byte; return author_byte;
} }
private void setIp(String ip) { private void setIp(String ip) {
@ -328,7 +348,7 @@ public class blogBoardComments {
return new byte[0]; return new byte[0];
final byte[] page_byte = Base64Order.enhancedCoder.decode(page); final byte[] page_byte = Base64Order.enhancedCoder.decode(page);
if (page_byte == null) if (page_byte == null)
return "".getBytes(); return new byte[0];
return page_byte; return page_byte;
} }
/** /**

@ -65,21 +65,21 @@ public class bookmarksDB {
// Declaration of Class-Attributes // Declaration of Class-Attributes
// ------------------------------------ // ------------------------------------
final static int SORT_ALPHA = 1; //final static int SORT_ALPHA = 1;
final static int SORT_SIZE = 2; private final static int SORT_SIZE = 2;
final static int SHOW_ALL = -1; private final static int SHOW_ALL = -1;
final static String SLEEP_TIME = "3600000"; // default sleepTime: check for recrawls every hour private final static String SLEEP_TIME = "3600000"; // default sleepTime: check for recrawls every hour
// bookmarks // bookmarks
MapHeap bookmarks; private MapHeap bookmarks;
// tags // tags
ConcurrentHashMap<String, Tag> tags; private ConcurrentHashMap<String, Tag> tags;
// autoReCrawl // autoReCrawl
private final BusyThread autoReCrawl; private final BusyThread autoReCrawl;
BookmarkDate dates; private BookmarkDate dates;
// ------------------------------------ // ------------------------------------
// bookmarksDB's class constructor // bookmarksDB's class constructor
@ -99,15 +99,15 @@ public class bookmarksDB {
final Iterator<Bookmark> it = new bookmarkIterator(true); final Iterator<Bookmark> it = new bookmarkIterator(true);
Bookmark bookmark; Bookmark bookmark;
Tag tag; Tag tag;
String[] tags; String[] tagArray;
while(it.hasNext()){ while(it.hasNext()){
bookmark = it.next(); bookmark = it.next();
tags = BookmarkHelper.cleanTagsString(bookmark.getTagsString() + bookmark.getFoldersString()).split(","); tagArray = BookmarkHelper.cleanTagsString(bookmark.getTagsString() + bookmark.getFoldersString()).split(",");
tag = null; tag = null;
for (int i = 0; i < tags.length; i++) { for (final String element : tagArray) {
tag = getTag(BookmarkHelper.tagHash(tags[i])); tag = getTag(BookmarkHelper.tagHash(element));
if (tag == null) { if (tag == null) {
tag = new Tag(tags[i]); tag = new Tag(element);
} }
tag.addUrl(bookmark.getUrlHash()); tag.addUrl(bookmark.getUrlHash());
putTag(tag); putTag(tag);
@ -122,9 +122,9 @@ public class bookmarksDB {
if (!datesExisted) this.dates.init(new bookmarkIterator(true)); if (!datesExisted) this.dates.init(new bookmarkIterator(true));
// autoReCrawl // autoReCrawl
Switchboard sb = Switchboard.getSwitchboard(); final Switchboard sb = Switchboard.getSwitchboard();
this.autoReCrawl = new InstantBusyThread(this, "autoReCrawl", null, null, Long.MIN_VALUE, Long.MAX_VALUE, Long.MIN_VALUE, Long.MAX_VALUE); this.autoReCrawl = new InstantBusyThread(this, "autoReCrawl", null, null, Long.MIN_VALUE, Long.MAX_VALUE, Long.MIN_VALUE, Long.MAX_VALUE);
long sleepTime = Long.parseLong(sb.getConfig("autoReCrawl_idlesleep" , SLEEP_TIME)); final long sleepTime = Long.parseLong(sb.getConfig("autoReCrawl_idlesleep" , SLEEP_TIME));
sb.deployThread("autoReCrawl", "autoReCrawl Scheduler", "simple scheduler for automatic re-crawls of bookmarked urls", null, autoReCrawl, 120000, sb.deployThread("autoReCrawl", "autoReCrawl Scheduler", "simple scheduler for automatic re-crawls of bookmarked urls", null, autoReCrawl, 120000,
sleepTime, sleepTime, Long.parseLong(sb.getConfig("autoReCrawl_memprereq" , "-1")) sleepTime, sleepTime, Long.parseLong(sb.getConfig("autoReCrawl_memprereq" , "-1"))
); );
@ -148,14 +148,14 @@ public class bookmarksDB {
public boolean autoReCrawl() { public boolean autoReCrawl() {
// read crontab // read crontab
File f = new File (Switchboard.getSwitchboard().getRootPath(),"DATA/SETTINGS/autoReCrawl.conf"); final File file = new File (Switchboard.getSwitchboard().getRootPath(),"DATA/SETTINGS/autoReCrawl.conf");
String s; String s;
try { try {
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(f))); final BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
Log.logInfo("BOOKMARKS", "autoReCrawl - reading schedules from " + f); Log.logInfo("BOOKMARKS", "autoReCrawl - reading schedules from " + file);
while( null != (s = in.readLine()) ) { while( null != (s = in.readLine()) ) {
if (s.length() > 0 && s.charAt(0) != '#') { if (s.length() > 0 && s.charAt(0) != '#') {
String parser[] = s.split("\t"); final String parser[] = s.split("\t");
if (parser.length == 13) { if (parser.length == 13) {
folderReCrawl(Long.parseLong(parser[0]), parser[1], parser[2], Integer.parseInt(parser[3]), Long.parseLong(parser[4]), folderReCrawl(Long.parseLong(parser[0]), parser[1], parser[2], Integer.parseInt(parser[3]), Long.parseLong(parser[4]),
Integer.parseInt(parser[5]), Integer.parseInt(parser[6]), Boolean.parseBoolean(parser[7]), Integer.parseInt(parser[5]), Integer.parseInt(parser[6]), Boolean.parseBoolean(parser[7]),
@ -178,13 +178,14 @@ public class bookmarksDB {
} catch( FileNotFoundException ex ) { } catch( FileNotFoundException ex ) {
try { try {
Log.logInfo("BOOKMARKS", "autoReCrawl - creating new autoReCrawl.conf"); Log.logInfo("BOOKMARKS", "autoReCrawl - creating new autoReCrawl.conf");
File inputFile = new File(Switchboard.getSwitchboard().getRootPath(),"defaults/autoReCrawl.conf"); final File inputFile = new File(Switchboard.getSwitchboard().getRootPath(),"defaults/autoReCrawl.conf");
File outputFile = new File(Switchboard.getSwitchboard().getRootPath(),"DATA/SETTINGS/autoReCrawl.conf"); final File outputFile = new File(Switchboard.getSwitchboard().getRootPath(),"DATA/SETTINGS/autoReCrawl.conf");
FileReader i = new FileReader(inputFile); final FileReader i = new FileReader(inputFile);
FileWriter o = new FileWriter(outputFile); final FileWriter o = new FileWriter(outputFile);
int c; int c;
while ((c = i.read()) != -1) while ((c = i.read()) != -1) {
o.write(c); o.write(c);
}
i.close(); i.close();
o.close(); o.close();
autoReCrawl(); autoReCrawl();
@ -197,7 +198,7 @@ public class bookmarksDB {
return false; return false;
} }
} catch( Exception ex ) { } catch( Exception ex ) {
Log.logSevere("BOOKMARKS", "autoReCrawl - error reading " + f, ex); Log.logSevere("BOOKMARKS", "autoReCrawl - error reading " + file, ex);
return false; return false;
} }
return true; return true;
@ -207,30 +208,30 @@ public class bookmarksDB {
int crawlingDomFilterDepth, int crawlingDomMaxPages, boolean crawlingQ, boolean indexText, boolean indexMedia, int crawlingDomFilterDepth, int crawlingDomMaxPages, boolean crawlingQ, boolean indexText, boolean indexMedia,
boolean crawlOrder, boolean xsstopw, boolean storeHTCache, int cacheStrategy) { boolean crawlOrder, boolean xsstopw, boolean storeHTCache, int cacheStrategy) {
Switchboard sb = Switchboard.getSwitchboard(); final Switchboard sb = Switchboard.getSwitchboard();
Iterator<String> bit=getBookmarksIterator(folder, true); final Iterator<String> bit = getBookmarksIterator(folder, true);
Log.logInfo("BOOKMARKS", "autoReCrawl - processing: "+folder); Log.logInfo("BOOKMARKS", "autoReCrawl - processing: "+folder);
boolean xdstopw = xsstopw; final boolean xdstopw = xsstopw;
boolean xpstopw = xsstopw; final boolean xpstopw = xsstopw;
while(bit.hasNext()) { while(bit.hasNext()) {
Bookmark bm = getBookmark(bit.next()); final Bookmark bm = getBookmark(bit.next());
long sleepTime = Long.parseLong(sb.getConfig("autoReCrawl_idlesleep" , SLEEP_TIME)); final long sleepTime = Long.parseLong(sb.getConfig("autoReCrawl_idlesleep" , SLEEP_TIME));
long interTime = (System.currentTimeMillis()-bm.getTimeStamp())%schedule; final long interTime = (System.currentTimeMillis()-bm.getTimeStamp())%schedule;
Date date=new Date(bm.getTimeStamp()); final Date date = new Date(bm.getTimeStamp());
Log.logInfo("BOOKMARKS", "autoReCrawl - checking schedule for: "+"["+DateFormatter.formatISO8601(date)+"] "+bm.getUrl()); Log.logInfo("BOOKMARKS", "autoReCrawl - checking schedule for: "+"["+DateFormatter.formatISO8601(date)+"] "+bm.getUrl());
if (interTime >= 0 && interTime < sleepTime) { if (interTime >= 0 && interTime < sleepTime) {
try { try {
int pos = 0; int pos = 0;
// set crawlingStart to BookmarkUrl // set crawlingStart to BookmarkUrl
String crawlingStart = bm.getUrl(); final String crawlingStart = bm.getUrl();
String newcrawlingMustMatch = crawlingfilter; String newcrawlingMustMatch = crawlingfilter;
DigestURI crawlingStartURL = new DigestURI(crawlingStart, null); final DigestURI crawlingStartURL = new DigestURI(crawlingStart, null);
// set the crawling filter // set the crawling filter
if (newcrawlingMustMatch.length() < 2) newcrawlingMustMatch = ".*"; // avoid that all urls are filtered out if bad value was submitted if (newcrawlingMustMatch.length() < 2) newcrawlingMustMatch = ".*"; // avoid that all urls are filtered out if bad value was submitted
@ -245,14 +246,15 @@ public class bookmarksDB {
// check if the crawl filter works correctly // check if the crawl filter works correctly
Pattern.compile(newcrawlingMustMatch); Pattern.compile(newcrawlingMustMatch);
byte[] urlhash = crawlingStartURL.hash(); final byte[] urlhash = crawlingStartURL.hash();
sb.indexSegments.urlMetadata(Segments.Process.LOCALCRAWLING).remove(urlhash); sb.indexSegments.urlMetadata(Segments.Process.LOCALCRAWLING).remove(urlhash);
sb.crawlQueues.noticeURL.removeByURLHash(urlhash); sb.crawlQueues.noticeURL.removeByURLHash(urlhash);
sb.crawlQueues.errorURL.remove(urlhash); sb.crawlQueues.errorURL.remove(urlhash);
// stack url // stack url
sb.crawler.profilesPassiveCrawls.removeEntry(crawlingStartURL.hash()); // if there is an old entry, delete it sb.crawler.profilesPassiveCrawls.removeEntry(crawlingStartURL.hash()); // if there is an old entry, delete it
CrawlProfile.entry pe = sb.crawler.profilesActiveCrawls.newEntry( final CrawlProfile.entry pe = sb.crawler.profilesActiveCrawls.newEntry(
folder+"/"+crawlingStartURL, crawlingStartURL, folder+"/"+crawlingStartURL, crawlingStartURL,
newcrawlingMustMatch, newcrawlingMustMatch,
CrawlProfile.MATCH_BAD_URL, CrawlProfile.MATCH_BAD_URL,
@ -295,7 +297,6 @@ public class bookmarksDB {
} catch (MalformedURLException e1) {} } catch (MalformedURLException e1) {}
} // if } // if
} // while(bit.hasNext()) } // while(bit.hasNext())
return;
} // } autoReCrawl() } // } autoReCrawl()
// ----------------------------------------------------------- // -----------------------------------------------------------
@ -323,7 +324,7 @@ public class bookmarksDB {
} }
} }
public String addBookmark(final Bookmark bookmark){ public String addBookmark(final Bookmark bookmark){
saveBookmark(bookmark); this.saveBookmark(bookmark);
return bookmark.getUrlHash(); return bookmark.getUrlHash();
} }
@ -331,8 +332,7 @@ public class bookmarksDB {
public Bookmark getBookmark(final String urlHash){ public Bookmark getBookmark(final String urlHash){
try { try {
final Map<String, String> map = bookmarks.get(urlHash); final Map<String, String> map = bookmarks.get(urlHash);
if (map == null) return null; return (map == null) ? new Bookmark(map) : null;
return new Bookmark(map);
} catch (final IOException e) { } catch (final IOException e) {
return null; return null;
} }
@ -341,9 +341,9 @@ public class bookmarksDB {
public boolean removeBookmark(final String urlHash){ public boolean removeBookmark(final String urlHash){
final Bookmark bookmark = getBookmark(urlHash); final Bookmark bookmark = getBookmark(urlHash);
if (bookmark == null) return false; //does not exist if (bookmark == null) return false; //does not exist
final Set<String> tags = bookmark.getTags(); final Set<String> tagSet = bookmark.getTags();
bookmarksDB.Tag tag=null; bookmarksDB.Tag tag=null;
final Iterator<String> it=tags.iterator(); final Iterator<String> it=tagSet.iterator();
while(it.hasNext()){ while(it.hasNext()){
tag=getTag(BookmarkHelper.tagHash(it.next())); tag=getTag(BookmarkHelper.tagHash(it.next()));
if(tag!=null){ if(tag!=null){
@ -442,7 +442,7 @@ public class bookmarksDB {
} }
private Iterator<Tag> getTagIterator(final boolean priv, final int c) { private Iterator<Tag> getTagIterator(final boolean priv, final int c) {
final TreeSet<Tag> set=new TreeSet<Tag>((c == SORT_SIZE) ? tagSizeComparator : tagComparator); final Set<Tag> set = new TreeSet<Tag>((c == SORT_SIZE) ? tagSizeComparator : tagComparator);
final Iterator<Tag> it = this.tags.values().iterator(); final Iterator<Tag> it = this.tags.values().iterator();
Tag tag; Tag tag;
while (it.hasNext()) { while (it.hasNext()) {
@ -474,11 +474,11 @@ public class bookmarksDB {
final Iterator<String> bit=getBookmarksIterator(tagName, priv); final Iterator<String> bit=getBookmarksIterator(tagName, priv);
Bookmark bm; Bookmark bm;
Tag tag; Tag tag;
Set<String> tags; Set<String> tagSet;
while(bit.hasNext()){ while(bit.hasNext()){
bm=getBookmark(bit.next()); bm=getBookmark(bit.next());
tags = bm.getTags(); tagSet = bm.getTags();
it = tags.iterator(); it = tagSet.iterator();
while (it.hasNext()) { while (it.hasNext()) {
tag=getTag(BookmarkHelper.tagHash(it.next()) ); tag=getTag(BookmarkHelper.tagHash(it.next()) );
if(priv ||tag.hasPublicItems()){ if(priv ||tag.hasPublicItems()){
@ -512,14 +512,14 @@ public class bookmarksDB {
if (oldTag != null) { if (oldTag != null) {
final Set<String> urlHashes = oldTag.getUrlHashes(); // preserve urlHashes of oldTag final Set<String> urlHashes = oldTag.getUrlHashes(); // preserve urlHashes of oldTag
removeTag(BookmarkHelper.tagHash(oldName)); // remove oldHash from TagsDB removeTag(BookmarkHelper.tagHash(oldName)); // remove oldHash from TagsDB
final Iterator<String> it = urlHashes.iterator();
Bookmark bookmark; Bookmark bookmark;
Set<String> tags = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); Set<String> tagSet = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
while (it.hasNext()) { // looping through all bookmarks which were tagged with oldName for (String urlHash : urlHashes) { // looping through all bookmarks which were tagged with oldName
bookmark = getBookmark(it.next()); bookmark = getBookmark(urlHash);
tags = bookmark.getTags(); tagSet = bookmark.getTags();
tags.remove(oldName); tagSet.remove(oldName);
bookmark.setTags(tags, true); // might not be needed, but doesn't hurt bookmark.setTags(tagSet, true); // might not be needed, but doesn't hurt
if(!newName.equals("")) bookmark.addTag(newName); if(!newName.equals("")) bookmark.addTag(newName);
saveBookmark(bookmark); saveBookmark(bookmark);
} }
@ -529,10 +529,10 @@ public class bookmarksDB {
} }
public void addTag(final String selectTag, final String newTag) { public void addTag(final String selectTag, final String newTag) {
final Iterator<String> it = getTag(BookmarkHelper.tagHash(selectTag)).getUrlHashes().iterator(); // get urlHashes for selectTag
Bookmark bookmark; Bookmark bookmark;
while (it.hasNext()) { // looping through all bookmarks which were tagged with selectTag for (final String urlHash : getTag(BookmarkHelper.tagHash(selectTag)).getUrlHashes()) { // looping through all bookmarks which were tagged with selectTag
bookmark = getBookmark(it.next()); bookmark = getBookmark(urlHash);
bookmark.addTag(newTag); bookmark.addTag(newTag);
saveBookmark(bookmark); saveBookmark(bookmark);
} }
@ -555,11 +555,12 @@ public class bookmarksDB {
public Tag(final String hash, final Map<String, String> map){ public Tag(final String hash, final Map<String, String> map){
tagHash = hash; tagHash = hash;
mem = map; mem = map;
if(mem.containsKey(URL_HASHES)) if (mem.containsKey(URL_HASHES)) {
urlHashes = listManager.string2set(mem.get(URL_HASHES)); urlHashes = listManager.string2set(mem.get(URL_HASHES));
else } else {
urlHashes = new HashSet<String>(); urlHashes = new HashSet<String>();
} }
}
public Tag(final String name, final HashSet<String> entries){ public Tag(final String name, final HashSet<String> entries){
tagHash = BookmarkHelper.tagHash(name); tagHash = BookmarkHelper.tagHash(name);
@ -570,12 +571,9 @@ public class bookmarksDB {
} }
public Tag(final String name){ public Tag(final String name){
tagHash=BookmarkHelper.tagHash(name); this(name, new HashSet<String>());
mem=new HashMap<String, String>();
//mem.put(URL_HASHES, "");
urlHashes=new HashSet<String>();
mem.put(TAG_NAME, name);
} }
public Map<String, String> getMap(){ public Map<String, String> getMap(){
mem.put(URL_HASHES, listManager.collection2string(this.urlHashes)); mem.put(URL_HASHES, listManager.collection2string(this.urlHashes));
return mem; return mem;
@ -615,11 +613,7 @@ public class bookmarksDB {
} }
public boolean hasPublicItems(){ public boolean hasPublicItems(){
final Iterator<String> it=getBookmarksIterator(this.getTagName(), false); return getBookmarksIterator(this.getTagName(), false).hasNext();
if(it.hasNext()){
return true;
}
return false;
} }
public void addUrl(final String urlHash){ public void addUrl(final String urlHash){
@ -651,16 +645,28 @@ public class bookmarksDB {
private String urlHash; private String urlHash;
private Set<String> tagNames; private Set<String> tagNames;
private long timestamp; private long timestamp;
Map<String, String> entry; private Map<String, String> entry;
public Bookmark(final String urlHash, final Map<String, String> map) { public Bookmark(final String urlHash, final Map<String, String> map) {
this.entry = map; entry = map;
this.urlHash = urlHash; this.urlHash = urlHash;
tagNames = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); tagNames = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
if (map.containsKey(BOOKMARK_TAGS)) tagNames.addAll(listManager.string2set(map.get(BOOKMARK_TAGS))); if (map.containsKey(BOOKMARK_TAGS)) tagNames.addAll(listManager.string2set(map.get(BOOKMARK_TAGS)));
loadTimestamp(); loadTimestamp();
} }
public Bookmark(final String urlHash, final String url) {
entry = new HashMap<String, String>();
this.urlHash = urlHash;
entry.put(BOOKMARK_URL, url);
tagNames = new HashSet<String>();
timestamp = System.currentTimeMillis();
}
public Bookmark(final String urlHash, final DigestURI url) {
this(urlHash, url.toNormalform(false, true));
}
public Bookmark(String url){ public Bookmark(String url){
entry = new HashMap<String, String>(); entry = new HashMap<String, String>();
if (!url.toLowerCase().startsWith("http://") && !url.toLowerCase().startsWith("https://")) { if (!url.toLowerCase().startsWith("http://") && !url.toLowerCase().startsWith("https://")) {
@ -687,22 +693,6 @@ public class bookmarksDB {
removeBookmark(this.urlHash); //prevent empty tags removeBookmark(this.urlHash); //prevent empty tags
} }
public Bookmark(final String urlHash, final DigestURI url) {
entry = new HashMap<String, String>();
this.urlHash=urlHash;
entry.put(BOOKMARK_URL, url.toNormalform(false, true));
tagNames=new HashSet<String>();
timestamp=System.currentTimeMillis();
}
public Bookmark(final String urlHash, final String url) {
entry = new HashMap<String, String>();
this.urlHash=urlHash;
entry.put(BOOKMARK_URL, url);
tagNames=new HashSet<String>();
timestamp=System.currentTimeMillis();
}
public Bookmark(final Map<String, String> map) throws MalformedURLException { public Bookmark(final Map<String, String> map) throws MalformedURLException {
this(new String((new DigestURI(map.get(BOOKMARK_URL), null)).hash()), map); this(new String((new DigestURI(map.get(BOOKMARK_URL), null)).hash()), map);
} }
@ -732,24 +722,26 @@ public class bookmarksDB {
public String getTagsString() { public String getTagsString() {
final String s[] = listManager.collection2string(getTags()).split(","); final String s[] = listManager.collection2string(getTags()).split(",");
String tagsString=""; final StringBuilder stringBuilder = new StringBuilder();
for (int i=0; i<s.length; i++){ for (final String element : s){
if(!s[i].startsWith("/")){ if(!element.startsWith("/")){
tagsString += s[i]+","; stringBuilder.append(element);
stringBuilder.append(",");
} }
} }
return tagsString; return stringBuilder.toString();
} }
public String getFoldersString(){ public String getFoldersString(){
final String s[] = listManager.collection2string(getTags()).split(","); final String s[] = listManager.collection2string(getTags()).split(",");
String foldersString=""; final StringBuilder stringBuilder = new StringBuilder();
for (int i=0; i<s.length; i++){ for (final String element : s){
if(s[i].startsWith("/")){ if(!element.startsWith("/")){
foldersString += s[i]+","; stringBuilder.append(element);
stringBuilder.append(",");
} }
} }
return foldersString; return stringBuilder.toString();
} }
public String getDescription(){ public String getDescription(){
@ -834,9 +826,7 @@ public class bookmarksDB {
public void setTags(final Set<String> tags2, final boolean local){ public void setTags(final Set<String> tags2, final boolean local){
tagNames = tags2; // TODO: check if this is safe tagNames = tags2; // TODO: check if this is safe
// tags.addAll(tags2); // in order for renameTag() to work I had to change this form 'add' to 'set' // tags.addAll(tags2); // in order for renameTag() to work I had to change this form 'add' to 'set'
final Iterator<String> it=tagNames.iterator(); for (final String tagName : tagNames) {
while(it.hasNext()){
final String tagName=it.next();
Tag tag = getTag(BookmarkHelper.tagHash(tagName)); Tag tag = getTag(BookmarkHelper.tagHash(tagName));
if (tag == null) { if (tag == null) {
tag = new Tag(tagName); tag = new Tag(tagName);
@ -882,10 +872,10 @@ public class bookmarksDB {
} }
public Bookmark next() { public Bookmark next() {
try { if (hasNext()) {
String s = new String(this.bookmarkIter.next()); String s = new String(this.bookmarkIter.next());
return getBookmark(s); return getBookmark(s);
} catch (final kelondroException e) { } else {
//resetDatabase(); //resetDatabase();
return null; return null;
} }
@ -913,8 +903,9 @@ public class bookmarksDB {
public int compare(final String obj1, final String obj2) { public int compare(final String obj1, final String obj2) {
final Bookmark bm1 = getBookmark(obj1); final Bookmark bm1 = getBookmark(obj1);
final Bookmark bm2 = getBookmark(obj2); final Bookmark bm2 = getBookmark(obj2);
if(bm1==null || bm2==null) if (bm1 == null || bm2 == null) {
return 0; //XXX: i think this should not happen? maybe this needs further tracing of the bug return 0; //XXX: i think this should not happen? maybe this needs further tracing of the bug
}
if (this.newestFirst){ if (this.newestFirst){
if (bm2.getTimeStamp() - bm1.getTimeStamp() >0) return 1; if (bm2.getTimeStamp() - bm1.getTimeStamp() >0) return 1;
return -1; return -1;
@ -951,9 +942,13 @@ public class bookmarksDB {
private static final long serialVersionUID = 4149185397646373251L; private static final long serialVersionUID = 4149185397646373251L;
public int compare(final Tag obj1, final Tag obj2) { public int compare(final Tag obj1, final Tag obj2) {
if (obj1.size() < obj2.size()) return 1; if (obj1.size() < obj2.size()) {
else if (obj1.getTagName().equals(obj2.getTagName())) return 0; return 1;
else return -1; } else if (obj1.getTagName().equals(obj2.getTagName())) {
return 0;
} else {
return -1;
}
} }
} }

@ -39,42 +39,42 @@ import net.yacy.document.parser.html.CharacterCoding;
public class diff { public class diff {
private final ArrayList <Part> parts = new ArrayList<Part>(); private final ArrayList <Part> parts = new ArrayList<Part>();
protected final Object[] o; protected final Object[] original;
protected final Object[] n; protected final Object[] changed;
/** /**
* @param o the original <code>String</code> * @param original the original <code>String</code>
* @param n the new <code>String</code> * @param changed the new <code>String</code>
* @throws NullPointerException if one of the arguments is <code>null</code> * @throws NullPointerException if one of the arguments is <code>null</code>
*/ */
public diff(final String o, final String n) { public diff(final String original, final String changed) {
this(o, n, 1); this(original, changed, 1);
} }
/** /**
* @param o the original <code>String</code> * @param original the original <code>String</code>
* @param n the new <code>String</code> * @param changed the new <code>String</code>
* @param minConsecutive the minimum number of consecutive equal characters in * @param minConsecutive the minimum number of consecutive equal characters in
* both Strings. Smaller seperations will only be performed on the end of either * both Strings. Smaller seperations will only be performed on the end of either
* String if needed * String if needed
* @throws NullPointerException if <code>o</code> or <code>n</code> is * @throws NullPointerException if <code>original</code> or <code>changed</code> is
* <code>null</code> * <code>null</code>
*/ */
public diff(final String o, final String n, final int minConsecutive) { public diff(final String original, final String changed, final int minConsecutive) {
if (o == null || n == null) throw new NullPointerException("neither o nor n must be null"); if (original == null || changed == null) throw new NullPointerException("input Strings must be null");
this.o = new Comparable[o.length()]; this.original = new Comparable[original.length()];
for (int i=0; i<o.length(); i++) for (int i=0; i<original.length(); i++)
this.o[i] = Character.valueOf(o.charAt(i)); this.original[i] = Character.valueOf(original.charAt(i));
this.n = new Comparable[n.length()]; this.changed = new Comparable[changed.length()];
for (int i=0; i<n.length(); i++) for (int i=0; i<changed.length(); i++)
this.n[i] = Character.valueOf(n.charAt(i)); this.changed[i] = Character.valueOf(changed.charAt(i));
parse((minConsecutive > 0) ? minConsecutive : 1); parse((minConsecutive > 0) ? minConsecutive : 1);
} }
public diff(final Object[] o, final Object[] n, final int minConsecutive) { public diff(final Object[] original, final Object[] changed, final int minConsecutive) {
if (o == null || n == null) throw new NullPointerException("neither o nor n must be null"); if (original == null || changed == null) throw new NullPointerException("input Objects must be null");
this.o = o; this.original = original;
this.n = n; this.changed = changed;
parse((minConsecutive > 0) ? minConsecutive : 1); parse((minConsecutive > 0) ? minConsecutive : 1);
} }
@ -102,10 +102,10 @@ public class diff {
* C| | | | | | | | | | | | | | | | |#| | * C| | | | | | | | | | | | | | | | |#| |
* E| | |#| | | | | | | | |#| | |#| | |#| * E| | |#| | | | | | | | |#| | |#| | |#|
*/ */
final boolean[][] matrix = new boolean[this.n.length][this.o.length]; final boolean[][] matrix = new boolean[this.changed.length][this.original.length];
for (int y=0; y<this.n.length; y++) for (int y=0; y<this.changed.length; y++)
for (int x=0; x<this.o.length; x++) for (int x=0; x<this.original.length; x++)
matrix[y][x] = this.o[x].equals(this.n[y]); matrix[y][x] = this.original[x].equals(this.changed[y]);
int s = 0, t = 0; int s = 0, t = 0;
int[] tmp; int[] tmp;
@ -114,7 +114,7 @@ public class diff {
this.parts.add(new Part(Part.UNCHANGED, tmp[0], s = tmp[0] + tmp[2])); this.parts.add(new Part(Part.UNCHANGED, tmp[0], s = tmp[0] + tmp[2]));
t = tmp[1] + tmp[2]; t = tmp[1] + tmp[2];
} }
addReplacementParts(s, t, this.o.length, this.n.length); addReplacementParts(s, t, this.original.length, this.changed.length);
} }
private void addReplacementParts(final int startx, final int starty, final int endx, final int endy) { private void addReplacementParts(final int startx, final int starty, final int endx, final int endy) {
@ -161,12 +161,12 @@ public class diff {
/** /**
* @return the original <code>Object[]</code> passed to this class on instantiation * @return the original <code>Object[]</code> passed to this class on instantiation
*/ */
public Object[] getOriginal() { return this.o; } public Object[] getOriginal() { return this.original; }
/** /**
* @return the new <code>Object[]</code> passed to this class on instantiation * @return the new <code>Object[]</code> passed to this class on instantiation
*/ */
public Object[] getNew() { return this.n; } public Object[] getNew() { return this.changed; }
/** /**
* A diff is composed of different parts. Each of these parts stands for an * A diff is composed of different parts. Each of these parts stands for an
@ -177,11 +177,12 @@ public class diff {
*/ */
public Part[] getParts() { return this.parts.toArray(new Part[this.parts.size()]); } public Part[] getParts() { return this.parts.toArray(new Part[this.parts.size()]); }
@Override
public String toString() { public String toString() {
final StringBuilder sb = new StringBuilder(this.parts.size() * 20); final StringBuilder sb = new StringBuilder(this.parts.size() * 20);
for (int j=0; j<this.parts.size(); j++) for (final Part part :parts)
sb.append(this.parts.get(j).toString()).append("\n"); sb.append(part.toString()).append("\n");
return new String(sb); return sb.toString();
} }
/** /**
@ -221,12 +222,12 @@ public class diff {
final StringBuilder sb = new StringBuilder(this.posNew - this.posOld); final StringBuilder sb = new StringBuilder(this.posNew - this.posOld);
if (this.action == ADDED) { if (this.action == ADDED) {
for (int i = this.posOld; i < this.posNew; i++) for (int i = this.posOld; i < this.posNew; i++)
sb.append(diff.this.n[i]); sb.append(diff.this.changed[i]);
} else { } else {
for (int i = this.posOld; i < this.posNew; i++) for (int i = this.posOld; i < this.posNew; i++)
sb.append(diff.this.o[i]); sb.append(diff.this.original[i]);
} }
return new String(sb); return sb.toString();
} }
/** /**
@ -237,6 +238,7 @@ public class diff {
* <dt>deleted</dt><dd>"<code>- STRING</code>"</dd> * <dt>deleted</dt><dd>"<code>- STRING</code>"</dd>
* </dl> * </dl>
*/ */
@Override
public String toString() { public String toString() {
return ((this.action == UNCHANGED) ? " " : return ((this.action == UNCHANGED) ? " " :
(this.action == ADDED) ? "+" : "-") + " " + getString(); (this.action == ADDED) ? "+" : "-") + " " + getString();
@ -246,21 +248,21 @@ public class diff {
public static String toHTML(final diff[] diffs) { public static String toHTML(final diff[] diffs) {
final StringBuilder sb = new StringBuilder(diffs.length * 60); final StringBuilder sb = new StringBuilder(diffs.length * 60);
diff.Part[] ps; diff.Part[] ps;
for (int i=0; i<diffs.length; i++) { for (diff d : diffs) {
sb.append("<p class=\"diff\">\n"); sb.append("<p class=\"diff\">\n");
ps = diffs[i].getParts(); ps = d.getParts();
for (int j=0; j<ps.length; j++) { for (diff.Part part :ps) {
sb.append("<span\nclass=\""); sb.append("<span\nclass=\"");
switch (ps[j].getAction()) { switch (part.getAction()) {
case diff.Part.UNCHANGED: sb.append("unchanged"); break; case diff.Part.UNCHANGED: sb.append("unchanged"); break;
case diff.Part.ADDED: sb.append("added"); break; case diff.Part.ADDED: sb.append("added"); break;
case diff.Part.DELETED: sb.append("deleted"); break; case diff.Part.DELETED: sb.append("deleted"); break;
} }
sb.append("\">").append(CharacterCoding.unicode2html(ps[j].getString(), true).replaceAll("\n", "<br />")); sb.append("\">").append(CharacterCoding.unicode2html(part.getString(), true).replaceAll("\n", "<br />"));
sb.append("</span>"); sb.append("</span>");
} }
sb.append("</p>"); sb.append("</p>");
} }
return new String(sb); return sb.toString();
} }
} }

@ -39,6 +39,7 @@ import net.yacy.repository.BlacklistFile;
import de.anomic.search.SearchEventCache; import de.anomic.search.SearchEventCache;
import de.anomic.search.Switchboard; import de.anomic.search.Switchboard;
import java.util.List;
// The Naming of the functions is a bit strange... // The Naming of the functions is a bit strange...
@ -101,17 +102,14 @@ public class listManager {
* "listName", <code>false</code> otherwise. * "listName", <code>false</code> otherwise.
*/ */
public static boolean listSetContains(final String setName, final String listName) { public static boolean listSetContains(final String setName, final String listName) {
final Set<String> Lists = getListSet(setName); return getListSet(setName).contains(listName);
return Lists.contains(listName);
} }
//================general Lists================== //================general Lists==================
public static String getListString(final String filename, final boolean withcomments) { public static String getListString(final String filename, final boolean withcomments) {
final File listFile = new File(listsPath ,filename); return FileUtils.getListString(new File(listsPath ,filename), withcomments);
return FileUtils.getListString(listFile, withcomments);
} }
//================Helper functions for collection conversion================== //================Helper functions for collection conversion==================
@ -142,15 +140,15 @@ public class listManager {
* @see listManager#string2vector(String) * @see listManager#string2vector(String)
*/ */
public static ArrayList<String> string2arraylist(final String string){ public static ArrayList<String> string2arraylist(final String string){
ArrayList<String> l; ArrayList<String> list;
if (string != null && string.length() > 0) { if (string != null && string.length() > 0) {
l = new ArrayList<String>(Arrays.asList(string.split(","))); list = new ArrayList<String>(Arrays.asList(string.split(",")));
} else { } else {
l = new ArrayList<String>(); list = new ArrayList<String>();
} }
return l; return list;
} }
/** /**
@ -199,12 +197,12 @@ public class listManager {
final String supportedBlacklistTypesStr = Blacklist.BLACKLIST_TYPES_STRING; final String supportedBlacklistTypesStr = Blacklist.BLACKLIST_TYPES_STRING;
final String[] supportedBlacklistTypes = supportedBlacklistTypesStr.split(","); final String[] supportedBlacklistTypes = supportedBlacklistTypesStr.split(",");
final ArrayList<BlacklistFile> blacklistFiles = new ArrayList<BlacklistFile>(supportedBlacklistTypes.length); final List<BlacklistFile> blacklistFiles = new ArrayList<BlacklistFile>(supportedBlacklistTypes.length);
for (int i=0; i < supportedBlacklistTypes.length; i++) { for (String supportedBlacklistType : supportedBlacklistTypes) {
final BlacklistFile blFile = new BlacklistFile( final BlacklistFile blFile = new BlacklistFile(
switchboard.getConfig( switchboard.getConfig(
supportedBlacklistTypes[i] + ".BlackLists", switchboard.getConfig("BlackLists.DefaultList", "url.default.black")), supportedBlacklistType + ".BlackLists", switchboard.getConfig("BlackLists.DefaultList", "url.default.black")),
supportedBlacklistTypes[i]); supportedBlacklistType);
blacklistFiles.add(blFile); blacklistFiles.add(blFile);
} }

@ -43,13 +43,13 @@ public class messageBoard {
private static final int categoryLength = 12; private static final int categoryLength = 12;
private static final String dateFormat = "yyyyMMddHHmmss"; private static final String dateFormat = "yyyyMMddHHmmss";
static SimpleDateFormat SimpleFormatter = new SimpleDateFormat(dateFormat, Locale.US); private static final SimpleDateFormat SimpleFormatter = new SimpleDateFormat(dateFormat, Locale.US);
static { static {
SimpleFormatter.setTimeZone(TimeZone.getTimeZone("GMT")); SimpleFormatter.setTimeZone(TimeZone.getTimeZone("GMT"));
} }
MapHeap database = null; private MapHeap database = null;
private int sn = 0; private int sn = 0;
public messageBoard(final File path) throws IOException { public messageBoard(final File path) throws IOException {
@ -104,20 +104,12 @@ public class messageBoard {
if (key.length() > categoryLength) key = key.substring(0, categoryLength); if (key.length() > categoryLength) key = key.substring(0, categoryLength);
while (key.length() < categoryLength) key += "_"; while (key.length() < categoryLength) key += "_";
key += dateString() + snString(); key += dateString() + snString();
if ((authorName == null) || (authorName.length() == 0)) authorName = "anonymous"; record.put("author", ((authorName == null) || (authorName.length() == 0)) ? authorName : "anonymous");
record.put("author", authorName); record.put("recipient", ((recName == null) || (recName.length() == 0)) ? recName : "anonymous");
if ((recName == null) || (recName.length() == 0)) recName = "anonymous"; record.put("ahash", (authorHash == null) ? authorHash : "");
record.put("recipient", recName); record.put("rhash", (recHash == null) ? recHash : "");
if (authorHash == null) authorHash = ""; record.put("subject", (subject == null) ? subject : "");
record.put("ahash", authorHash); record.put("message", (message == null) ? "" : Base64Order.enhancedCoder.encode(message));
if (recHash == null) recHash = "";
record.put("rhash", recHash);
if (subject == null) subject = "";
record.put("subject", subject);
if (message == null)
record.put("message", "");
else
record.put("message", Base64Order.enhancedCoder.encode(message));
record.put("read", "false"); record.put("read", "false");
} }
@ -158,13 +150,11 @@ public class messageBoard {
public String authorHash() { public String authorHash() {
final String a = record.get("ahash"); final String a = record.get("ahash");
if (a == null) return null;
return a; return a;
} }
public String recipientHash() { public String recipientHash() {
final String a = record.get("rhash"); final String a = record.get("rhash");
if (a == null) return null;
return a; return a;
} }
@ -224,9 +214,9 @@ public class messageBoard {
public class catIter implements Iterator<String> { public class catIter implements Iterator<String> {
Iterator<byte[]> allIter = null; private Iterator<byte[]> allIter = null;
String nextKey = null; private String nextKey = null;
String category = ""; private String category = "";
public catIter(final String category, final boolean up) throws IOException { public catIter(final String category, final boolean up) throws IOException {
this.allIter = database.keys(up, false); this.allIter = database.keys(up, false);

@ -39,12 +39,9 @@ import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Iterator; import java.util.Iterator;
import java.util.Vector;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -53,6 +50,9 @@ import net.yacy.kelondro.util.FileUtils;
import net.yacy.kelondro.util.Formatter; import net.yacy.kelondro.util.Formatter;
import de.anomic.server.serverSwitch; import de.anomic.server.serverSwitch;
import java.util.List;
import java.util.Map;
import java.util.Set;
/** /**
* Wordlist based translator * Wordlist based translator
@ -60,16 +60,14 @@ import de.anomic.server.serverSwitch;
* Uses a Property like file with phrases or single words to translate a string or a file * Uses a Property like file with phrases or single words to translate a string or a file
* */ * */
public class translator { public class translator {
public static String translate(final String source, final Hashtable<String, String> translationList){ public static String translate(final String source, final Map<String, String> translationTable){
final Enumeration<String> keys = translationList.keys(); final Set<String> keys = translationTable.keySet();
String result = source; String result = source;
String key = ""; for (String key : keys){
while(keys.hasMoreElements()){
key = keys.nextElement();
final Pattern pattern = Pattern.compile(key); final Pattern pattern = Pattern.compile(key);
final Matcher matcher = pattern.matcher(result); final Matcher matcher = pattern.matcher(result);
if (matcher.find()) { if (matcher.find()) {
result = matcher.replaceAll(translationList.get(key)); result = matcher.replaceAll(translationTable.get(key));
} else { } else {
//Filename not available, but it will be printed in Log //Filename not available, but it will be printed in Log
//after all untranslated Strings as "Translated file: " //after all untranslated Strings as "Translated file: "
@ -78,27 +76,24 @@ public class translator {
} }
return result; return result;
} }
/** /**
* Load multiple translationLists from one File. Each List starts with #File: relative/path/to/file * Load multiple translationLists from one File. Each List starts with #File: relative/path/to/file
* @param translationFile the File, which contains the Lists * @param translationFile the File, which contains the Lists
* @return a Hashtable, which contains for each File a Hashtable with translations. * @return a Hashtable, which contains for each File a Hashtable with translations.
*/ */
public static Hashtable<String, Hashtable<String, String>> loadTranslationsLists(final File translationFile){ public static Map<String, Map<String, String>> loadTranslationsLists(final File translationFile){
final Hashtable<String, Hashtable<String, String>> lists = new Hashtable<String, Hashtable<String, String>>(); //list of translationLists for different files. final Map<String, Map<String, String>> lists = new Hashtable<String, Map<String, String>>(); //list of translationLists for different files.
Hashtable<String, String> translationList = new Hashtable<String, String>(); //current Translation Table Map<String, String> translationList = new Hashtable<String, String>(); //current Translation Table
final ArrayList<String> list = FileUtils.getListArray(translationFile); final List<String> list = FileUtils.getListArray(translationFile);
final Iterator<String> it = list.iterator();
String line = "";
String[] splitted;
String forFile = ""; String forFile = "";
while(it.hasNext()){ for (final String line : list){
line = it.next();
if (line.length() == 0 || line.charAt(0) != '#'){ if (line.length() == 0 || line.charAt(0) != '#'){
splitted = line.split("==", 2); String[] split = line.split("==", 2);
if (splitted.length == 2) { if (split.length == 2) {
translationList.put(splitted[0], splitted[1]); translationList.put(split[0], split[1]);
//}else{ //Invalid line //}else{ //Invalid line
} }
} else if (line.startsWith("#File: ")) { } else if (line.startsWith("#File: ")) {
@ -122,122 +117,123 @@ public class translator {
} }
public static boolean translateFile(final File sourceFile, final File destFile, final File translationFile){ public static boolean translateFile(final File sourceFile, final File destFile, final File translationFile){
final Hashtable<String, String> translationList = loadTranslationsLists(translationFile).get(sourceFile.getName()); return translateFile(sourceFile, destFile, loadTranslationsLists(translationFile).get(sourceFile.getName()));
return translateFile(sourceFile, destFile, translationList);
} }
public static boolean translateFile(final File sourceFile, final File destFile, final Hashtable<String, String> translationList){ public static boolean translateFile(final File sourceFile, final File destFile, final Map<String, String> translationList){
String content = ""; StringBuilder content = new StringBuilder();
String line = "";
BufferedReader br = null; BufferedReader br = null;
try{ try{
br = new BufferedReader(new InputStreamReader(new FileInputStream(sourceFile),"UTF-8")); br = new BufferedReader(new InputStreamReader(new FileInputStream(sourceFile),"UTF-8"));
String line = null;
while( (line = br.readLine()) != null){ while( (line = br.readLine()) != null){
content += line + de.anomic.server.serverCore.CRLF_STRING; content.append(line).append(de.anomic.server.serverCore.CRLF_STRING);
} }
br.close(); br.close();
} catch(final IOException e) { } catch(final IOException e) {
return false; return false;
} finally { } finally {
if (br!=null)try{br.close();}catch(final Exception e){} if (br !=null) {
try {
br.close();
} catch (final Exception e) {}
}
} }
content = translate(content, translationList); content = new StringBuilder(translate(content.toString(), translationList));
BufferedWriter bw = null; BufferedWriter bw = null;
try{ try{
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFile),"UTF-8")); bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFile),"UTF-8"));
bw.write(content); bw.write(content.toString());
bw.close(); bw.close();
}catch(final IOException e){ }catch(final IOException e){
return false; return false;
} finally { } finally {
if(bw!=null)try{bw.close();}catch(final Exception e){} if(bw != null) {
try {
bw.close();
} catch (final Exception e) {}
}
} }
return true; return true;
} }
public static boolean translateFiles(final File sourceDir, final File destDir, final File baseDir, final File translationFile, final String extensions){ public static boolean translateFiles(final File sourceDir, final File destDir, final File baseDir, final File translationFile, final String extensions){
final Hashtable<String, Hashtable<String, String>> translationLists = loadTranslationsLists(translationFile); return translateFiles(sourceDir, destDir, baseDir, loadTranslationsLists(translationFile), extensions);
return translateFiles(sourceDir, destDir, baseDir, translationLists, extensions);
} }
public static boolean translateFiles(final File sourceDir, final File destDir, final File baseDir, final Hashtable<String, Hashtable<String, String>> translationLists, final String extensions){ public static boolean translateFiles(final File sourceDir, final File destDir, final File baseDir, final Map<String, Map<String, String>> translationLists, final String extensions){
destDir.mkdirs(); destDir.mkdirs();
final File[] sourceFiles = sourceDir.listFiles(); final File[] sourceFiles = sourceDir.listFiles();
final Vector<String> exts=listManager.string2vector(extensions); final List<String> exts = listManager.string2vector(extensions);
boolean rightExtension; boolean rightExtension;
Iterator<String> it;
String relativePath; String relativePath;
for(int i=0;i<sourceFiles.length;i++){ for(final File sourceFile : sourceFiles){
it=exts.iterator();
rightExtension=false; rightExtension=false;
while(it.hasNext()){ for(final String ext : exts){
if(sourceFiles[i].getName().endsWith(it.next())){ if(sourceFile.getName().endsWith(ext)){
rightExtension=true; rightExtension=true;
break; break;
} }
} }
if (rightExtension) { if (rightExtension) {
try { try {
relativePath=sourceFiles[i].getAbsolutePath().substring(baseDir.getAbsolutePath().length()+1); //+1 to get the "/" relativePath=sourceFile.getAbsolutePath().substring(baseDir.getAbsolutePath().length()+1); //+1 to get the "/"
relativePath = relativePath.replace(File.separatorChar, '/'); relativePath = relativePath.replace(File.separatorChar, '/');
} catch (final IndexOutOfBoundsException e){ } catch (final IndexOutOfBoundsException e){
Log.logSevere("TRANSLATOR", "Error creating relative Path for "+sourceFiles[i].getAbsolutePath()); Log.logSevere("TRANSLATOR", "Error creating relative Path for "+sourceFile.getAbsolutePath());
relativePath = "wrong path"; //not in translationLists relativePath = "wrong path"; //not in translationLists
} }
if (translationLists.containsKey(relativePath)) { if (translationLists.containsKey(relativePath)) {
Log.logInfo("TRANSLATOR", "Translating file: "+ relativePath); Log.logInfo("TRANSLATOR", "Translating file: "+ relativePath);
if(!translateFile( if(!translateFile(
sourceFiles[i] sourceFile,
, new File(destDir, sourceFiles[i].getName().replace('/', File.separatorChar)) new File(destDir, sourceFile.getName().replace('/', File.separatorChar)),
, translationLists.get(relativePath))){ translationLists.get(relativePath)))
{
Log.logSevere("TRANSLATOR", "File error while translating file "+relativePath); Log.logSevere("TRANSLATOR", "File error while translating file "+relativePath);
} }
//}else{ //}else{
//serverLog.logInfo("TRANSLATOR", "No translation for file: "+relativePath); //serverLog.logInfo("TRANSLATOR", "No translation for file: "+relativePath);
} }
} }
} }
return true; return true;
} }
public static boolean translateFilesRecursive(final File sourceDir, final File destDir, final File translationFile, final String extensions, final String notdir){ public static boolean translateFilesRecursive(final File sourceDir, final File destDir, final File translationFile, final String extensions, final String notdir){
final ArrayList<File> dirList=FileUtils.getDirsRecursive(sourceDir, notdir); final List<File> dirList=FileUtils.getDirsRecursive(sourceDir, notdir);
dirList.add(sourceDir); dirList.add(sourceDir);
final Iterator<File> it=dirList.iterator(); for (final File file : dirList){
File file=null;
File file2=null;
while(it.hasNext()){
file=it.next();
//cuts the sourcePath and prepends the destPath
file2=new File(destDir, file.getPath().substring(sourceDir.getPath().length()));
if(file.isDirectory() && !file.getName().equals(notdir)){ if(file.isDirectory() && !file.getName().equals(notdir)){
//cuts the sourcePath and prepends the destPath
File file2 = new File(destDir, file.getPath().substring(sourceDir.getPath().length()));
translateFiles(file, file2, sourceDir, translationFile, extensions); translateFiles(file, file2, sourceDir, translationFile, extensions);
} }
} }
return true; return true;
} }
public static HashMap<String, String> langMap(final serverSwitch env) { public static Map<String, String> langMap(final serverSwitch env) {
final String[] ms = env.getConfig("locale.lang", "").split(","); final String[] ms = env.getConfig("locale.lang", "").split(",");
final HashMap<String, String> map = new HashMap<String, String>(); final Map<String, String> map = new HashMap<String, String>();
int p; for (final String element : ms) {
for (int i = 0; i < ms.length; i++) { int p = element.indexOf('/');
p = ms[i].indexOf('/');
if (p > 0) if (p > 0)
map.put(ms[i].substring(0, p), ms[i].substring(p + 1)); map.put(element.substring(0, p), element.substring(p + 1));
} }
return map; return map;
} }
public static boolean changeLang(final serverSwitch env, final String langPath, final String lang) { public static boolean changeLang(final serverSwitch env, final String langPath, final String lang) {
boolean ret = false;
if ((lang.equals("default")) || (lang.equals("default.lng"))) { if ((lang.equals("default")) || (lang.equals("default.lng"))) {
env.setConfig("locale.language", "default"); env.setConfig("locale.language", "default");
return true; ret = true;
} } else {
final String htRootPath = env.getConfig("htRootPath", "htroot"); final String htRootPath = env.getConfig("htRootPath", "htroot");
final File sourceDir = new File(env.getRootPath(), htRootPath); final File sourceDir = new File(env.getRootPath(), htRootPath);
final File destDir = new File(env.getConfigPath("locale.translated_html","DATA/LOCALE/htroot"), lang.substring(0, lang.length() - 4));// cut final File destDir = new File(env.getConfigPath("locale.translated_html","DATA/LOCALE/htroot"), lang.substring(0, lang.length() - 4));// cut
@ -247,8 +243,7 @@ public class translator {
final File translationFile = new File(langPath, lang); final File translationFile = new File(langPath, lang);
//if (translator.translateFiles(sourceDir, destDir, translationFile, "html")) { //if (translator.translateFiles(sourceDir, destDir, translationFile, "html")) {
if(translator.translateFilesRecursive(sourceDir, destDir, if (translator.translateFilesRecursive(sourceDir, destDir, translationFile, "html,template,inc", "locale")) {
translationFile, "html,template,inc", "locale")){
env.setConfig("locale.language", lang.substring(0, lang.length() - 4)); env.setConfig("locale.language", lang.substring(0, lang.length() - 4));
Formatter.setLocale(env.getConfig("locale.language", "en")); Formatter.setLocale(env.getConfig("locale.language", "en"));
try { try {
@ -258,8 +253,9 @@ public class translator {
} catch (final IOException e) { } catch (final IOException e) {
// Error // Error
} }
return true; ret = true;
} }
return false; }
return ret;
} }
} }

@ -49,13 +49,13 @@ import de.anomic.http.server.RequestHeader;
public final class userDB { public final class userDB {
public static final int USERNAME_MAX_LENGTH = 128; private static final int USERNAME_MAX_LENGTH = 128;
public static final int USERNAME_MIN_LENGTH = 4; private static final int USERNAME_MIN_LENGTH = 4;
MapHeap userTable; private MapHeap userTable;
private final File userTableFile; private final File userTableFile;
HashMap<String, String> ipUsers = new HashMap<String, String>(); private final Map<String, String> ipUsers = new HashMap<String, String>();
HashMap<String, Object> cookieUsers = new HashMap<String, Object>(); private final Map<String, Object> cookieUsers = new HashMap<String, Object>();
public userDB(final File userTableFile) throws IOException { public userDB(final File userTableFile) throws IOException {
this.userTableFile = userTableFile; this.userTableFile = userTableFile;
@ -104,7 +104,7 @@ public final class userDB {
return new Entry(userName, record); return new Entry(userName, record);
} }
public Entry createEntry(final String userName, final HashMap<String, String> userProps) throws IllegalArgumentException{ public Entry createEntry(final String userName, final Map<String, String> userProps) throws IllegalArgumentException{
final Entry entry = new Entry(userName,userProps); final Entry entry = new Entry(userName,userProps);
return entry; return entry;
} }
@ -123,39 +123,31 @@ public final class userDB {
* use a ProxyAuth String to authenticate a user * use a ProxyAuth String to authenticate a user
* @param auth a base64 Encoded String, which contains "username:pw". * @param auth a base64 Encoded String, which contains "username:pw".
*/ */
public Entry proxyAuth(String auth) { public Entry proxyAuth(final String auth) {
if(auth==null) if(auth == null) {
return null; return null;
}
Entry entry = null; Entry entry = null;
auth=auth.trim().substring(6);
try{ final String[] tmp= Base64Order.standardCoder.decodeString(auth.trim().substring(6)).split(":");
auth=Base64Order.standardCoder.decodeString(auth);
}catch(final RuntimeException e){} //no valid Base64
final String[] tmp=auth.split(":");
if(tmp.length == 2){ if(tmp.length == 2){
entry = this.passwordAuth(tmp[0], tmp[1]); entry = this.passwordAuth(tmp[0], tmp[1]);
// if(entry != null){
//return entry;
// }else{ //wrong/no auth, so auth is removed from browser
/*FIXME: This cannot work
try{
entry.setProperty(Entry.LOGGED_OUT, "false");
}catch(IOException e){}*/
// }
return entry;
} }
return null; return entry;
} }
public Entry getUser(final RequestHeader header){ public Entry getUser(final RequestHeader header){
return getUser(header.get(RequestHeader.AUTHORIZATION), header.getHeaderCookies()); return getUser(header.get(RequestHeader.AUTHORIZATION), header.getHeaderCookies());
} }
public Entry getUser(final String auth, final String cookies){ public Entry getUser(final String auth, final String cookies){
Entry entry=null; Entry entry=null;
if(auth != null) if(auth != null) {
entry=proxyAuth(auth); entry=proxyAuth(auth);
if(entry == null) }
if(entry == null) {
entry=cookieAuth(cookies); entry=cookieAuth(cookies);
}
return entry; return entry;
} }
/** /**
@ -166,13 +158,15 @@ public final class userDB {
*/ */
public boolean hasAdminRight(final String auth, final String cookies) { public boolean hasAdminRight(final String auth, final String cookies) {
final Entry entry = getUser(auth, cookies); final Entry entry = getUser(auth, cookies);
if (entry != null) if (entry != null) {
return entry.hasAdminRight(); return entry.hasRight(Entry.ADMIN_RIGHT);
}
// else if(entry != null && cookieAdminAuth(cookies)) // else if(entry != null && cookieAdminAuth(cookies))
// return entry.hasAdminRight(); // return entry.hasAdminRight();
else else {
return false; return false;
} }
}
/** /**
* use a ProxyAuth String to authenticate a user and save the ip/username for ipAuth * use a ProxyAuth String to authenticate a user and save the ip/username for ipAuth
@ -188,6 +182,7 @@ public final class userDB {
this.ipUsers.put(ip, entry.getUserName()); this.ipUsers.put(ip, entry.getUserName());
return entry; return entry;
} }
/** /**
* authenticate a user by ip, if he had used proxyAuth in the last 10 Minutes * authenticate a user by ip, if he had used proxyAuth in the last 10 Minutes
* @param ip the IP of the User * @param ip the IP of the User
@ -221,6 +216,7 @@ public final class userDB {
} }
return null; return null;
} }
public Entry passwordAuth(final String user, final String password, final String ip){ public Entry passwordAuth(final String user, final String password, final String ip){
final Entry entry=passwordAuth(user, password); final Entry entry=passwordAuth(user, password);
if(entry == null){ if(entry == null){
@ -230,6 +226,7 @@ public final class userDB {
this.ipUsers.put(ip, entry.getUserName()); //XXX: This is insecure. TODO: use cookieauth this.ipUsers.put(ip, entry.getUserName()); //XXX: This is insecure. TODO: use cookieauth
return entry; return entry;
} }
public Entry md5Auth(final String user, final String md5) { public Entry md5Auth(final String user, final String md5) {
final Entry entry=this.getEntry(user); final Entry entry=this.getEntry(user);
if (entry != null && entry.getMD5EncodedUserPwd().equals(md5)) { if (entry != null && entry.getMD5EncodedUserPwd().equals(md5)) {
@ -245,6 +242,7 @@ public final class userDB {
} }
return null; return null;
} }
public Entry cookieAuth(final String cookieString){ public Entry cookieAuth(final String cookieString){
final String token = getLoginToken(cookieString); final String token = getLoginToken(cookieString);
if (cookieUsers.containsKey(token)) { if (cookieUsers.containsKey(token)) {
@ -254,21 +252,25 @@ public final class userDB {
} }
return null; return null;
} }
public boolean cookieAdminAuth(final String cookieString){ public boolean cookieAdminAuth(final String cookieString){
final String token = getLoginToken(cookieString); final String token = getLoginToken(cookieString);
if (cookieUsers.containsKey(token)) { if (cookieUsers.containsKey(token)) {
final Object entry = cookieUsers.get(token); final Object entry = cookieUsers.get(token);
if(entry instanceof String && entry.equals("admin")) if (entry instanceof String && entry.equals("admin")) {
return true; return true;
} }
}
return false; return false;
} }
public String getCookie(final Entry entry){ public String getCookie(final Entry entry){
final Random r = new Random(); final Random r = new Random();
final String token = Long.toString(Math.abs(r.nextLong()), 36); final String token = Long.toString(Math.abs(r.nextLong()), 36);
cookieUsers.put(token, entry); cookieUsers.put(token, entry);
return token; return token;
} }
public String getAdminCookie(){ public String getAdminCookie(){
final Random r = new Random(); final Random r = new Random();
final String token = Long.toString(Math.abs(r.nextLong()), 36); final String token = Long.toString(Math.abs(r.nextLong()), 36);
@ -278,15 +280,15 @@ public final class userDB {
public static String getLoginToken(final String cookies){ public static String getLoginToken(final String cookies){
final String[] cookie = cookies.split(";"); //TODO: Mozilla uses "; " final String[] cookie = cookies.split(";"); //TODO: Mozilla uses "; "
String[] pair; for (String c :cookie) {
for(int i=0;i<cookie.length;i++){ String[] pair = c.split("=");
pair=cookie[i].split("=");
if (pair[0].trim().equals("login")) { if (pair[0].trim().equals("login")) {
return pair[1].trim(); return pair[1].trim();
} }
} }
return ""; return "";
} }
public void adminLogout(final String logintoken){ public void adminLogout(final String logintoken){
if(cookieUsers.containsKey(logintoken)){ if(cookieUsers.containsKey(logintoken)){
//XXX: We could check, if its == "admin", but we want to logout anyway. //XXX: We could check, if its == "admin", but we want to logout anyway.
@ -327,8 +329,8 @@ public final class userDB {
public static final int PROXY_TIMELIMIT_REACHED = 3; public static final int PROXY_TIMELIMIT_REACHED = 3;
// this is a simple record structure that hold all properties of a user // this is a simple record structure that hold all properties of a user
Map<String, String> mem; private Map<String, String> mem;
String userName; private String userName;
private final Calendar oldDate, newDate; private final Calendar oldDate, newDate;
public Entry(final String userName, final Map<String, String> mem) throws IllegalArgumentException { public Entry(final String userName, final Map<String, String> mem) throws IllegalArgumentException {
@ -340,12 +342,14 @@ public final class userDB {
this.userName = userName.trim(); this.userName = userName.trim();
if (this.userName.length() < USERNAME_MIN_LENGTH) if (this.userName.length() < USERNAME_MIN_LENGTH)
throw new IllegalArgumentException("Username to short. Length should be >= " + USERNAME_MIN_LENGTH); throw new IllegalArgumentException("Username too short. Length should be >= " + USERNAME_MIN_LENGTH);
if (mem == null) this.mem = new HashMap<String, String>(); if (mem == null) this.mem = new HashMap<String, String>();
else this.mem = mem; else this.mem = mem;
if (mem == null || !mem.containsKey(AUTHENTICATION_METHOD))this.mem.put(AUTHENTICATION_METHOD,"yacy"); if (mem == null || !mem.containsKey(AUTHENTICATION_METHOD)) {
this.mem.put(AUTHENTICATION_METHOD,"yacy");
}
this.oldDate=Calendar.getInstance(); this.oldDate=Calendar.getInstance();
this.newDate=Calendar.getInstance(); this.newDate=Calendar.getInstance();
} }
@ -433,7 +437,7 @@ public final class userDB {
public int surfRight(){ public int surfRight(){
final long timeUsed=this.updateLastAccess(true); final long timeUsed=this.updateLastAccess(true);
if (!this.hasProxyRight()) if (!this.hasRight(Entry.PROXY_RIGHT))
return PROXY_NORIGHT; return PROXY_NORIGHT;
if(! (this.getTimeLimit() <= 0 || ( timeUsed < this.getTimeLimit())) ){ //no timelimit or timelimit not reached if(! (this.getTimeLimit() <= 0 || ( timeUsed < this.getTimeLimit())) ){ //no timelimit or timelimit not reached
@ -441,13 +445,15 @@ public final class userDB {
} }
return PROXY_ALLOK; return PROXY_ALLOK;
} }
public boolean canSurf(){ public boolean canSurf(){
if(this.surfRight()==PROXY_ALLOK) return true; return (this.surfRight() == PROXY_ALLOK) ? true : false;
return false;
} }
public long updateLastAccess(final boolean incrementTimeUsed) { public long updateLastAccess(final boolean incrementTimeUsed) {
return updateLastAccess(System.currentTimeMillis(), incrementTimeUsed); return updateLastAccess(System.currentTimeMillis(), incrementTimeUsed);
} }
public long updateLastAccess(final long timeStamp, final boolean incrementTimeUsed) { public long updateLastAccess(final long timeStamp, final boolean incrementTimeUsed) {
if (timeStamp < 0) throw new IllegalArgumentException(); if (timeStamp < 0) throw new IllegalArgumentException();
@ -554,15 +560,18 @@ public final class userDB {
public boolean hasBookmarkRight() { public boolean hasBookmarkRight() {
return this.hasRight(BOOKMARK_RIGHT); return this.hasRight(BOOKMARK_RIGHT);
} }
public boolean isLoggedOut(){ public boolean isLoggedOut(){
return (this.mem.containsKey(LOGGED_OUT)?this.mem.get(LOGGED_OUT).equals("true"):false); return (this.mem.containsKey(LOGGED_OUT)?this.mem.get(LOGGED_OUT).equals("true"):false);
} }
public void logout(final String ip, final String logintoken){ public void logout(final String ip, final String logintoken){
logout(ip); logout(ip);
if(cookieUsers.containsKey(logintoken)){ if(cookieUsers.containsKey(logintoken)){
cookieUsers.remove(logintoken); cookieUsers.remove(logintoken);
} }
} }
public void logout(final String ip) { public void logout(final String ip) {
try { try {
setProperty(LOGGED_OUT, "true"); setProperty(LOGGED_OUT, "true");
@ -573,19 +582,22 @@ public final class userDB {
Log.logException(e); Log.logException(e);
} }
} }
public void logout(){ public void logout(){
logout("xxxxxx"); logout("xxxxxx");
} }
@Override
public String toString() { public String toString() {
final StringBuilder str = new StringBuilder(); final StringBuilder str = new StringBuilder();
str.append((this.userName==null)?"null":this.userName)
.append(": "); str.append((this.userName==null)?"null":this.userName).append(": ");
if (this.mem != null) { if (this.mem != null) {
str.append(this.mem.toString()); str.append(this.mem.toString());
} }
return new String(str); return str.toString();
} }
} }
@ -609,6 +621,7 @@ public final class userDB {
this.userIter = userDB.this.userTable.keys(up, false); this.userIter = userDB.this.userTable.keys(up, false);
//this.nextEntry = null; //this.nextEntry = null;
} }
public boolean hasNext() { public boolean hasNext() {
try { try {
return this.userIter.hasNext(); return this.userIter.hasNext();
@ -617,6 +630,7 @@ public final class userDB {
return false; return false;
} }
} }
public Entry next() { public Entry next() {
try { try {
return getEntry(new String(this.userIter.next())); return getEntry(new String(this.userIter.next()));
@ -625,6 +639,7 @@ public final class userDB {
return null; return null;
} }
} }
public void remove() { public void remove() {
// if (this.nextEntry != null) { // if (this.nextEntry != null) {
// try { // try {
@ -634,6 +649,7 @@ public final class userDB {
// resetDatabase(); // resetDatabase();
// } // }
// } // }
throw new UnsupportedOperationException("Method not implemented yet.");
} }
} }

Loading…
Cancel
Save