From 2bc459252e0af9dd20db96e26ee06ba643ff7e1f Mon Sep 17 00:00:00 2001 From: low012 Date: Tue, 13 Apr 2010 01:16:09 +0000 Subject: [PATCH] *) changes for better code readability git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@6797 6c8d7289-2bf4-0310-a012-ef5d649a1542 --- htroot/Blog.java | 15 +- htroot/ConfigLanguage_p.java | 6 +- source/de/anomic/data/BookmarkHelper.java | 2 +- source/de/anomic/data/DidYouMean.java | 324 ++++++------- source/de/anomic/data/DidYouMeanLibrary.java | 94 ++-- source/de/anomic/data/LibraryProvider.java | 34 +- source/de/anomic/data/MimeTable.java | 10 +- source/de/anomic/data/SitemapParser.java | 3 + source/de/anomic/data/URLAnalysis.java | 163 +++---- source/de/anomic/data/URLLicense.java | 8 +- source/de/anomic/data/WorkTables.java | 6 +- source/de/anomic/data/blogBoard.java | 319 +++++++------ source/de/anomic/data/blogBoardComments.java | 212 +++++---- source/de/anomic/data/bookmarksDB.java | 457 +++++++++---------- source/de/anomic/data/diff.java | 92 ++-- source/de/anomic/data/listManager.java | 24 +- source/de/anomic/data/messageBoard.java | 46 +- source/de/anomic/data/translator.java | 316 +++++++------ source/de/anomic/data/userDB.java | 242 +++++----- 19 files changed, 1212 insertions(+), 1161 deletions(-) diff --git a/htroot/Blog.java b/htroot/Blog.java index 62f2bb9cd..5886a5dd5 100644 --- a/htroot/Blog.java +++ b/htroot/Blog.java @@ -32,7 +32,6 @@ import java.io.UnsupportedEncodingException; import java.text.SimpleDateFormat; -import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Iterator; @@ -49,6 +48,8 @@ import de.anomic.server.serverObjects; import de.anomic.server.serverSwitch; import de.anomic.yacy.yacyNewsPool; import de.anomic.yacy.yacyNewsRecord; +import java.util.List; +import java.util.Map; public class Blog { @@ -127,9 +128,8 @@ public class Blog { if(hasRights && post.containsKey("delete") && post.get("delete").equals("sure")) { page = sb.blogDB.readBlogEntry(pagename); - final Iterator i = page.getComments().iterator(); - while(i.hasNext()) { - sb.blogCommentDB.delete(i.next()); + for (final String comment : page.getComments()) { + sb.blogCommentDB.delete(comment); } sb.blogDB.deleteBlogEntry(pagename); pagename = DEFAULT_PAGE; @@ -149,7 +149,7 @@ public class Blog { } Date date = null; - ArrayList comments = null; + List comments = null; //set name for new entry or date for old entry 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)); // create a news message - final HashMap map = new HashMap(); + final Map map = new HashMap(); map.put("page", pagename); map.put("subject", StrSubject.replace(',', ' ')); map.put("author", StrAuthor.replace(',', ' ')); @@ -195,8 +195,7 @@ public class Blog { else { prop.put("mode", "3"); //access denied (no rights) } - } - else if(post.containsKey("preview")) { + } else if(post.containsKey("preview")) { //preview the page if(hasRights) { prop.put("mode", "2");//preview diff --git a/htroot/ConfigLanguage_p.java b/htroot/ConfigLanguage_p.java index ac9bc3f1c..6a5510022 100644 --- a/htroot/ConfigLanguage_p.java +++ b/htroot/ConfigLanguage_p.java @@ -34,7 +34,6 @@ import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; -import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -51,6 +50,7 @@ import de.anomic.search.Switchboard; import de.anomic.server.serverObjects; import de.anomic.server.serverSwitch; import java.util.Collections; +import java.util.Map; public class ConfigLanguage_p { @@ -134,7 +134,7 @@ public class ConfigLanguage_p { //reread language files langFiles = FileUtils.getDirListing(langPath, LANG_FILENAME_FILTER); Collections.sort(langFiles); - final HashMap langNames = translator.langMap(env); + final Map langNames = translator.langMap(env); String langKey, langName; //virtual entry @@ -143,7 +143,7 @@ public class ConfigLanguage_p { prop.put("langlist_0_selected", "selected=\"selected\""); int count = 0; - for(String langFile : langFiles){ + for(final String langFile : langFiles){ //+1 because of the virtual entry "default" at top langKey = langFile.substring(0, langFile.length() -4); langName = langNames.get(langKey); diff --git a/source/de/anomic/data/BookmarkHelper.java b/source/de/anomic/data/BookmarkHelper.java index d2d62e57c..2be4cf829 100644 --- a/source/de/anomic/data/BookmarkHelper.java +++ b/source/de/anomic/data/BookmarkHelper.java @@ -162,7 +162,7 @@ public class BookmarkHelper { } - public static int importFromXML(bookmarksDB db, final String input, final boolean importPublic){ + public static int importFromXML(bookmarksDB db, final String input, final boolean importPublic){ try { // convert string to input stream final ByteArrayInputStream byteIn = new ByteArrayInputStream(input.getBytes("UTF-8")); diff --git a/source/de/anomic/data/DidYouMean.java b/source/de/anomic/data/DidYouMean.java index a241b377b..155d3162c 100644 --- a/source/de/anomic/data/DidYouMean.java +++ b/source/de/anomic/data/DidYouMean.java @@ -29,55 +29,55 @@ import net.yacy.kelondro.rwi.IndexCell; */ 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', 'q','r','s','t','u','v','w','x','y','z','\u00e4','\u00f6','\u00fc','\u00df'}; - private static final String poisonString = "\n"; - public static final int availableCPU = Runtime.getRuntime().availableProcessors(); - protected static final wordLengthComparator wlComp = new wordLengthComparator(); + private static final String POISON_STRING = "\n"; + public static final int AVAILABLE_CPU = Runtime.getRuntime().availableProcessors(); + protected static final wordLengthComparator WORD_LENGTH_COMPARATOR = new wordLengthComparator(); - protected final IndexCell index; + protected final IndexCell index; protected String word; - protected int wordLen; - protected LinkedBlockingQueue guessGen, guessLib; - protected long timeLimit; - protected boolean createGen; // keeps the value 'true' as long as no entry in guessLib is written - protected final SortedSet resultSet; + protected int wordLen; + protected LinkedBlockingQueue guessGen, guessLib; + protected long timeLimit; + protected boolean createGen; // keeps the value 'true' as long as no entry in guessLib is written + protected final SortedSet resultSet; - /** - * @param index a termIndex - most likely retrieved from a switchboard object. - * @param sort true/false - sorts the resulting TreeSet by index.count(); Warning: this causes heavy i/o. - */ - public DidYouMean(final IndexCell index) { - this.resultSet = Collections.synchronizedSortedSet(new TreeSet(wlComp)); - this.word = ""; - this.wordLen = 0; - this.index = index; - this.guessGen = new LinkedBlockingQueue(); - this.guessLib = new LinkedBlockingQueue(); - this.createGen = true; - } + /** + * @param index a termIndex - most likely retrieved from a switchboard object. + * @param sort true/false - sorts the resulting TreeSet by index.count(); Warning: this causes heavy i/o. + */ + public DidYouMean(final IndexCell index) { + this.resultSet = Collections.synchronizedSortedSet(new TreeSet(WORD_LENGTH_COMPARATOR)); + this.word = ""; + this.wordLen = 0; + this.index = index; + this.guessGen = new LinkedBlockingQueue(); + this.guessLib = new LinkedBlockingQueue(); + this.createGen = true; + } - public void reset() { - this.resultSet.clear(); - this.guessGen.clear(); - this.guessLib.clear(); - } + public void reset() { + this.resultSet.clear(); + this.guessGen.clear(); + this.guessLib.clear(); + } - /** - * get a single suggestion - * @param word - * @param timeout - * @return - */ - public String getSuggestion(final String word, long timeout) { - Set s = getSuggestions(word, timeout); - if (s == null || s.isEmpty()) return null; - return s.iterator().next(); - } + /** + * get a single suggestion + * @param word + * @param timeout + * @return + */ + public String getSuggestion(final String word, long timeout) { + Set s = getSuggestions(word, timeout); + if (s == null || s.isEmpty()) return null; + return s.iterator().next(); + } - /** + /** * get a single suggestion with additional sort * @param word * @param timeout @@ -89,40 +89,40 @@ public class DidYouMean { return s.iterator().next(); } - /** - * get suggestions for a given word. The result is first ordered using a term size ordering, - * and a subset of the result is sorted again with a IO-intensive order based on the index size - * @param word - * @param timeout - * @param preSortSelection the number of words that participate in the IO-intensive sort - * @return - */ - public SortedSet getSuggestions(final String word, long timeout, int preSortSelection) { - if (word.indexOf(' ') > 0) return getSuggestions(word.split(" "), timeout, preSortSelection, this.index); - long startTime = System.currentTimeMillis(); - SortedSet preSorted = getSuggestions(word, timeout); - long timelimit = 2 * System.currentTimeMillis() - startTime + timeout; + /** + * get suggestions for a given word. The result is first ordered using a term size ordering, + * and a subset of the result is sorted again with a IO-intensive order based on the index size + * @param word + * @param timeout + * @param preSortSelection the number of words that participate in the IO-intensive sort + * @return + */ + public SortedSet getSuggestions(final String word, long timeout, int preSortSelection) { + if (word.indexOf(' ') > 0) return getSuggestions(word.split(" "), timeout, preSortSelection, this.index); + long startTime = System.currentTimeMillis(); + SortedSet preSorted = getSuggestions(word, timeout); + long timelimit = 2 * System.currentTimeMillis() - startTime + timeout; if (System.currentTimeMillis() > timelimit) return preSorted; SortedSet countSorted = Collections.synchronizedSortedSet(new TreeSet(new indexSizeComparator())); int wc = index.count(Word.word2hash(word)); // all counts must be greater than this int c0; for (String s: preSorted) { - if (System.currentTimeMillis() > timelimit) break; - if (preSortSelection <= 0) break; - c0 = index.count(Word.word2hash(s)); - if (c0 > wc) countSorted.add(s); - preSortSelection--; - } - return countSorted; - } + if (System.currentTimeMillis() > timelimit) break; + if (preSortSelection <= 0) break; + c0 = index.count(Word.word2hash(s)); + if (c0 > wc) countSorted.add(s); + preSortSelection--; + } + return countSorted; + } - /** - * return a string that is a suggestion list for the list of given words - * @param words - * @param timeout - * @param preSortSelection - * @return - */ + /** + * return a string that is a suggestion list for the list of given words + * @param words + * @param timeout + * @param preSortSelection + * @return + */ @SuppressWarnings("unchecked") public static SortedSet getSuggestions(final String[] words, long timeout, int preSortSelection, final IndexCell index) { SortedSet[] s = new SortedSet[words.length]; @@ -144,25 +144,25 @@ public class DidYouMean { return result; } - /** - * This method triggers the producer and consumer threads of the DidYouMean object. - * @param word a String with a single word - * @param timeout execution time in ms. - * @return a Set<String> with word variations contained in term index. - */ - public SortedSet getSuggestions(final String word, long timeout) { - long startTime = System.currentTimeMillis(); - this.timeLimit = startTime + timeout; - this.word = word.toLowerCase(); - this.wordLen = word.length(); - - // create one consumer thread that checks the guessLib queue - // for occurrences in the index. If the producers are started next, their - // results can be consumers directly - Consumer[] consumers = new Consumer[availableCPU]; + /** + * This method triggers the producer and consumer threads of the DidYouMean object. + * @param word a String with a single word + * @param timeout execution time in ms. + * @return a Set<String> with word variations contained in term index. + */ + public SortedSet getSuggestions(final String word, long timeout) { + long startTime = System.currentTimeMillis(); + this.timeLimit = startTime + timeout; + this.word = word.toLowerCase(); + this.wordLen = word.length(); + + // create one consumer thread that checks the guessLib queue + // for occurrences in the index. If the producers are started next, their + // results can be consumers directly + Consumer[] consumers = new Consumer[AVAILABLE_CPU]; consumers[0] = new Consumer(); consumers[0].start(); - + // get a single recommendation for the word without altering the word Set libr = LibraryProvider.dymLib.recommend(word); for (String t: libr) { @@ -172,13 +172,13 @@ public class DidYouMean { } catch (InterruptedException e) {} } - // create and start producers + // create and start producers // the CPU load to create the guessed words is very low, but the testing // against the library may be CPU intensive. Since it is possible to test // words in the library concurrently, it is a good idea to start separate threads - Thread[] producers = new Thread[4]; - producers[0] = new ChangingOneLetter(); - producers[1] = new AddingOneLetter(); + Thread[] producers = new Thread[4]; + producers[0] = new ChangingOneLetter(); + producers[1] = new AddingOneLetter(); producers[2] = new DeletingOneLetter(); producers[3] = new ReversingTwoConsecutiveLetters(); for (Thread t: producers) t.start(); @@ -197,54 +197,55 @@ public class DidYouMean { // if there is not any entry in guessLib, then transfer all entries from the // guessGen to guessLib if (createGen) try { - this.guessGen.put(poisonString); + this.guessGen.put(POISON_STRING); 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) {} // put poison into guessLib to terminate 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 - for (Consumer c: consumers) - try { c.join(); } catch (InterruptedException e) {} + for (Consumer c: consumers) + try { c.join(); } catch (InterruptedException e) {} - // we don't want the given word in the result - this.resultSet.remove(word.toLowerCase()); - - // finished - Log.logInfo("DidYouMean", "found "+this.resultSet.size()+" terms; execution time: " - +(System.currentTimeMillis()-startTime)+"ms"+ " - remaining queue size: "+guessLib.size()); - - return this.resultSet; + // we don't want the given word in the result + this.resultSet.remove(word.toLowerCase()); + + // finished + Log.logInfo("DidYouMean", "found "+this.resultSet.size()+" terms; execution time: " + +(System.currentTimeMillis()-startTime)+"ms"+ " - remaining queue size: "+guessLib.size()); + + return this.resultSet; - } + } - public void test(String s) throws InterruptedException { - Set libr = LibraryProvider.dymLib.recommend(s); - libr.addAll(LibraryProvider.geoDB.recommend(s)); - if (!libr.isEmpty()) createGen = false; - for (String t: libr) guessLib.put(t); - if (createGen) guessGen.put(s); - } + public void test(final String s) throws InterruptedException { + Set libr = LibraryProvider.dymLib.recommend(s); + libr.addAll(LibraryProvider.geoDB.recommend(s)); + if (!libr.isEmpty()) createGen = false; + for (String t: libr) guessLib.put(t); + if (createGen) guessGen.put(s); + } - /** + /** * DidYouMean's producer thread that changes one letter (e.g. bat/cat) for a given term * based on the given alphabet and puts it on the blocking queue, to be 'consumed' by a consumer thread.

* Note: the loop runs (alphabet.length * len) tests. - */ - public class ChangingOneLetter extends Thread { + */ + public class ChangingOneLetter extends Thread { - public void run() { - for (int i = 0; i < wordLen; i++) try { - for (char c: alphabet) { - test(word.substring(0, i) + c + word.substring(i + 1)); - if (System.currentTimeMillis() > timeLimit) return; - } - } catch (InterruptedException e) {} - } - } + @Override + public void run() { + for (int i = 0; i < wordLen; i++) try { + for (char c: ALPHABET) { + test(word.substring(0, i) + c + word.substring(i + 1)); + if (System.currentTimeMillis() > timeLimit) return; + } + } catch (InterruptedException e) {} + } + } /** * DidYouMean's producer thread that deletes extra letters (e.g. frog/fog) for a given term @@ -253,12 +254,14 @@ public class DidYouMean { */ protected class DeletingOneLetter extends Thread { - public void run() { - for (int i = 0; i < wordLen; i++) try { - test(word.substring(0, i) + word.substring(i+1)); - if (System.currentTimeMillis() > timeLimit) return; - } catch (InterruptedException e) {} - } + @Override + public void run() { + for (int i = 0; i < wordLen; i++) try { + test(word.substring(0, i) + word.substring(i+1)); + if (System.currentTimeMillis() > timeLimit) return; + } catch (InterruptedException e) {} + } + } /** @@ -268,14 +271,15 @@ public class DidYouMean { */ protected class AddingOneLetter extends Thread { - public void run() { - for (int i = 0; i <= wordLen; i++) try { - for (char c: alphabet) { - test(word.substring(0, i) + c + word.substring(i)); - if (System.currentTimeMillis() > timeLimit) return; - } - } catch (InterruptedException e) {} - } + @Override + public void run() { + for (int i = 0; i <= wordLen; i++) try { + for (char c: ALPHABET) { + test(word.substring(0, i) + c + word.substring(i)); + if (System.currentTimeMillis() > timeLimit) return; + } + } catch (InterruptedException e) {} + } } /** @@ -285,12 +289,14 @@ public class DidYouMean { */ protected class ReversingTwoConsecutiveLetters extends Thread { - public void run() { - 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)); - if (System.currentTimeMillis() > timeLimit) return; - } catch (InterruptedException e) {} - } + @Override + public void run() { + 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)); + if (System.currentTimeMillis() > timeLimit) return; + } catch (InterruptedException e) {} + } + } /** @@ -300,15 +306,16 @@ public class DidYouMean { */ class Consumer extends Thread { - public void run() { - String s; - try { - while ((s = guessLib.take()) != poisonString) { - if (index.has(Word.word2hash(s))) resultSet.add(s); - if (System.currentTimeMillis() > timeLimit) return; - } - } catch (InterruptedException e) {} - } + @Override + public void run() { + String s; + try { + while (!(s = guessLib.take()).equals(POISON_STRING)) { + if (index.has(Word.word2hash(s))) resultSet.add(s); + if (System.currentTimeMillis() > timeLimit) return; + } + } catch (InterruptedException e) {} + } } /** @@ -316,11 +323,12 @@ public class DidYouMean { * Warning: this causes heavy i/o */ protected class indexSizeComparator implements Comparator { - public int compare(final String o1, final String o2) { - final int i1 = index.count(Word.word2hash(o1)); - final int i2 = index.count(Word.word2hash(o2)); - if (i1 == i2) return wlComp.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 + + public int compare(final String o1, final String o2) { + final int i1 = index.count(Word.word2hash(o1)); + final int i2 = index.count(Word.word2hash(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 } } @@ -329,12 +337,14 @@ public class DidYouMean { * This is the default order if the indexSizeComparator is not used */ protected static class wordLengthComparator implements Comparator { + public int compare(final String o1, final String o2) { final int i1 = o1.length(); final int i2 = o2.length(); if (i1 == i2) return o1.compareTo(o2); return (i1 > i2) ? 1 : -1; // '>' is correct, because the shortest word shall be first - } + } + } } diff --git a/source/de/anomic/data/DidYouMeanLibrary.java b/source/de/anomic/data/DidYouMeanLibrary.java index 88e8461ee..262124d33 100644 --- a/source/de/anomic/data/DidYouMeanLibrary.java +++ b/source/de/anomic/data/DidYouMeanLibrary.java @@ -1,8 +1,8 @@ // 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 // -// This is a part of YaCy +// This is ret part of YaCy // // $LastChangedDate$ // $LastChangedRevision$ @@ -20,7 +20,7 @@ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // -// You should have received a copy of the GNU General Public License +// You should have received ret copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA @@ -41,7 +41,7 @@ import java.util.zip.GZIPInputStream; 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 { @@ -50,13 +50,13 @@ public class DidYouMeanLibrary { private TreeSet dict, tcid; /** - * create a new dictionary + * create ret new dictionary * This loads all files that ends with '.words' * The files must have one word per line * 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; reload(); } @@ -68,23 +68,25 @@ public class DidYouMeanLibrary { this.dict = new TreeSet(); this.tcid = new TreeSet(); if (dictionaryPath == null || !dictionaryPath.exists()) return; - String[] files = dictionaryPath.list(); - for (String f: files) { + final String[] files = dictionaryPath.list(); + for (final String f: files) { if (f.endsWith(".words")) try { - importFile(new File(dictionaryPath, f)); + inputStream(new File(dictionaryPath, f)); } catch (IOException e) { Log.logException(e); } } } - private void importFile(File f) throws IOException { - InputStream is = new FileInputStream(f); - if (f.getName().endsWith(".gz")) is = new GZIPInputStream(is); - BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF-8")); + private void inputStream(final File file) throws IOException { + InputStream is = new FileInputStream(file); + if (file.getName().endsWith(".gz")) { + is = new GZIPInputStream(is); + } + final BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); String l; try { - while ((l = r.readLine()) != null) { + while ((l = reader.readLine()) != null) { if (l.length() == 0 || l.charAt(0) == '#') continue; l = l.trim().toLowerCase(); this.dict.add(l); @@ -95,30 +97,31 @@ public class DidYouMeanLibrary { } } - private static String reverse(String s) { - StringBuilder r = new StringBuilder(s.length()); - for (int i = s.length() - 1; i >= 0; i--) r.append(s.charAt(i)); - return r.toString(); + private static String reverse(final String s) { + StringBuilder sb = new StringBuilder(s.length()); + for (int i = s.length() - 1; i >= 0; i--) sb.append(s.charAt(i)); + 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 - * @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 recommend(String s) { - Set a = new HashSet(); - s = s.trim().toLowerCase(); - SortedSet t = this.dict.tailSet(s); - for (String r: t) { - if (r.startsWith(s)) a.add(r); else break; + public Set recommend(final String s) { + String string = new String(s); + Set ret = new HashSet(); + string = string.trim().toLowerCase(); + SortedSet t = this.dict.tailSet(string); + for (final String r: t) { + if (r.startsWith(string)) ret.add(r); else break; } - s = reverse(s); - t = this.tcid.tailSet(s); - for (String r: t) { - if (r.startsWith(s)) a.add(reverse(r)); else break; + string = reverse(string); + t = this.tcid.tailSet(string); + for (final String r: t) { + 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 * @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()); // 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 @@ -134,21 +137,22 @@ public class DidYouMeanLibrary { /** * 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 * @param s the given word * @return true if the library supports the word */ - public boolean supports(String s) { - s = s.trim().toLowerCase(); - SortedSet t = this.dict.tailSet(s); - for (String r: t) { - if (s.startsWith(r)) return true; else break; + public boolean supports(final String s) { + String string = new String(s); + string = string.trim().toLowerCase(); + SortedSet t = this.dict.tailSet(string); + for (final String r: t) { + if (string.startsWith(r)) return true; else break; } - s = reverse(s); - t = this.tcid.tailSet(s); - for (String r: t) { - if (s.startsWith(r)) return true; else break; + string = reverse(string); + t = this.tcid.tailSet(string); + for (final String r: t) { + if (string.startsWith(r)) return true; else break; } 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 * is not relevant for the construction of artificially constructed words * If this property returns true, all other words must be in the dictionary * @param minimumWords * @return */ - public boolean isRelevant(int minimumWords) { + public boolean isRelevant(final int minimumWords) { return this.dict.size() >= minimumWords; } diff --git a/source/de/anomic/data/LibraryProvider.java b/source/de/anomic/data/LibraryProvider.java index bae38fe50..3f71d441d 100644 --- a/source/de/anomic/data/LibraryProvider.java +++ b/source/de/anomic/data/LibraryProvider.java @@ -49,8 +49,8 @@ public class LibraryProvider { public static DidYouMeanLibrary dymLib = new DidYouMeanLibrary(null); public static OpenGeoDB geoDB = new OpenGeoDB(null); - public static File dictSource = null; - public static File dictRoot = null; + private static File dictSource = null; + private static File dictRoot = null; /** * initialize the LibraryProvider as static class. @@ -61,7 +61,7 @@ public class LibraryProvider { * @param pathToSource * @param pathToDICTIONARIES */ - public static void initialize(File rootPath) { + public static void initialize(final File rootPath) { dictSource = new File(rootPath, path_to_source_dictionaries); if (!dictSource.exists()) dictSource.mkdirs(); dictRoot = rootPath; @@ -91,21 +91,21 @@ public class LibraryProvider { } 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(); dymLib = new DidYouMeanLibrary(dymDict); } public static void integrateDeReWo() { // 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(); - File pathToSource = new File(dictRoot, path_to_source_dictionaries); - 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 pathToSource = new File(dictRoot, path_to_source_dictionaries); + final File derewoInput = new File(pathToSource, "derewo-v-30000g-2007-12-31-0.1.txt"); + final File derewoOutput = new File(dymDict, "derewo-v-30000g-2007-12-31-0.1.words"); if (!derewoOutput.exists() && derewoInput.exists()) { // create the translation of the derewo file (which is easy in this case) - ArrayList derewo = loadDeReWo(derewoInput, true); + final ArrayList derewo = loadDeReWo(derewoInput, true); try { writeWords(derewoOutput, derewo); } catch (IOException e) { @@ -138,20 +138,20 @@ public class LibraryProvider { } */ - private static Set sortUnique(List list) { - TreeSet s = new TreeSet(); - for (String t: list) s.add(t); + private static Set sortUnique(final List list) { + final Set s = new TreeSet(); + for (final String t: list) s.add(t); return s; } - private static void writeWords(File f, ArrayList list) throws IOException { - Set s = sortUnique(list); - PrintWriter w = new PrintWriter(new BufferedWriter(new FileWriter(f))); - for (String t: s) w.println(t); + private static void writeWords(final File f, final ArrayList list) throws IOException { + final Set s = sortUnique(list); + final PrintWriter w = new PrintWriter(new BufferedWriter(new FileWriter(f))); + for (final String t: s) w.println(t); w.close(); } - private static ArrayList loadDeReWo(final File file, boolean toLowerCase) { + private static ArrayList loadDeReWo(final File file, final boolean toLowerCase) { final ArrayList list = new ArrayList(); BufferedReader reader = null; try { diff --git a/source/de/anomic/data/MimeTable.java b/source/de/anomic/data/MimeTable.java index c3a76911f..4a0438863 100644 --- a/source/de/anomic/data/MimeTable.java +++ b/source/de/anomic/data/MimeTable.java @@ -11,7 +11,7 @@ public class MimeTable { private static final Properties mimeTable = new Properties(); - public static void init(File mimeFile) { + public static void init(final File mimeFile) { if (mimeTable.isEmpty()) { // load the mime table BufferedInputStream mimeTableInputStream = null; @@ -34,19 +34,19 @@ public class MimeTable { return mimeTable.isEmpty(); } - public static String ext2mime(String ext) { + public static String ext2mime(final String 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); } - public static String url2mime(DigestURI url, String dfltMime) { + public static String url2mime(final DigestURI url, final String dfltMime) { return ext2mime(url.getFileExtension(), dfltMime); } - public static String url2mime(DigestURI url) { + public static String url2mime(final DigestURI url) { return ext2mime(url.getFileExtension()); } } diff --git a/source/de/anomic/data/SitemapParser.java b/source/de/anomic/data/SitemapParser.java index f594bdedb..6f1aad1b7 100644 --- a/source/de/anomic/data/SitemapParser.java +++ b/source/de/anomic/data/SitemapParser.java @@ -224,6 +224,7 @@ public class SitemapParser extends DefaultHandler { * @param qName qualified name * @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 { this.currentElement = qName; @@ -242,6 +243,7 @@ public class SitemapParser extends DefaultHandler { * @throws SAXException * @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 { 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 { if (this.currentElement.equalsIgnoreCase(SITEMAP_URL_LOC)) { // TODO: we need to decode the URL here diff --git a/source/de/anomic/data/URLAnalysis.java b/source/de/anomic/data/URLAnalysis.java index 361c93e10..ceb874b0e 100644 --- a/source/de/anomic/data/URLAnalysis.java +++ b/source/de/anomic/data/URLAnalysis.java @@ -71,7 +71,7 @@ public class URLAnalysis { * processes to analyse URL lists */ - public static DigestURI poison = null; + private static DigestURI poison = null; static { try { poison = new DigestURI("http://poison.org/poison", null); @@ -82,18 +82,19 @@ public class URLAnalysis { public static class splitter extends Thread { - ArrayBlockingQueue in; - ConcurrentHashMap out; + private ArrayBlockingQueue in; + private ConcurrentHashMap out; - public splitter(ArrayBlockingQueue in, ConcurrentHashMap out) { + public splitter(final ArrayBlockingQueue in, final ConcurrentHashMap out) { this.in = in; this.out = out; } + @Override public void run() { try { DigestURI url; - Pattern p = Pattern.compile("~|\\(|\\)|\\+|-|@|:|%|\\.|;|_"); + final Pattern p = Pattern.compile("~|\\(|\\)|\\+|-|@|:|%|\\.|;|_"); while (true) { try { url = in.take(); @@ -109,9 +110,9 @@ public class URLAnalysis { } } - private void update(String[] s) { + private void update(final String[] s) { Integer c; - for (String t: s) { + for (final String t: s) { if (t.length() == 0) continue; c = out.get(t); out.put(t, (c == null) ? 1 : c.intValue() + 1); @@ -119,46 +120,46 @@ public class URLAnalysis { } } - public static void cleanup(ConcurrentHashMap stat) { + public static void cleanup(final ConcurrentHashMap stat) { Map.Entry entry; int c, low = Integer.MAX_VALUE; Iterator> i = stat.entrySet().iterator(); while (i.hasNext()) { - entry = i.next(); - c = entry.getValue().intValue(); - if (c == 1) { - i.remove(); - } else { - if (c < low) low = c; - } + entry = i.next(); + c = entry.getValue().intValue(); + if (c == 1) { + i.remove(); + } else { + if (c < low) low = c; + } } i = stat.entrySet().iterator(); while (i.hasNext()) { - entry = i.next(); - c = entry.getValue().intValue(); - if (c == low) { - i.remove(); - } + entry = i.next(); + c = entry.getValue().intValue(); + if (c == low) { + i.remove(); + } } Runtime.getRuntime().gc(); } - public static void genstat(String urlfile) { + public static void genstat(final String urlfile) { - boolean gz = urlfile.endsWith(".gz"); - String analysis = (gz) ? urlfile.substring(0, urlfile.length() - 3) + ".stats.gz" : urlfile + ".stats"; - long cleanuplimit = Math.max(50 * 1024 * 1024, MemoryControl.available() / 8); + final boolean gz = urlfile.endsWith(".gz"); + final String analysis = (gz) ? urlfile.substring(0, urlfile.length() - 3) + ".stats.gz" : urlfile + ".stats"; + final long cleanuplimit = Math.max(50 * 1024 * 1024, MemoryControl.available() / 8); // start threads - ArrayBlockingQueue in = new ArrayBlockingQueue(1000); - ConcurrentHashMap out = new ConcurrentHashMap(); - for (int i = 0; i < Runtime.getRuntime().availableProcessors(); i++) new splitter(in, out).start(); - splitter spl = new splitter(in, out); + final ArrayBlockingQueue in = new ArrayBlockingQueue(1000); + final ConcurrentHashMap out = new ConcurrentHashMap(); + for (int i = 0, available = Runtime.getRuntime().availableProcessors(); i < available; i++) new splitter(in, out).start(); + final splitter spl = new splitter(in, out); spl.start(); // put urls in queue - File infile = new File(urlfile); - File outfile = new File(analysis); + final File infile = new File(urlfile); + final File outfile = new File(analysis); BufferedReader reader = null; long time = System.currentTimeMillis(); long start = time; @@ -202,7 +203,7 @@ public class URLAnalysis { // stop 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); } catch (InterruptedException e) { Log.logException(e); @@ -215,10 +216,10 @@ public class URLAnalysis { // generate statistics System.out.println("start processing results"); - TreeMap results = new TreeMap(); + final TreeMap results = new TreeMap(); count = 0; Map.Entry entry; - Iterator> i = out.entrySet().iterator(); + final Iterator> i = out.entrySet().iterator(); while (i.hasNext()) { entry = i.next(); 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)); if (gz) os = new GZIPOutputStream(os); count = 0; - for (Map.Entry e: results.entrySet()) { + for (final Map.Entry e: results.entrySet()) { os.write(e.getKey().getBytes()); os.write(new byte[]{'\t'}); os.write(Integer.toString(e.getValue()).getBytes()); @@ -255,15 +256,15 @@ public class URLAnalysis { System.out.println("finished"); } - public static void genhost(String urlfile) { + public static void genhost(final String urlfile) { - boolean gz = urlfile.endsWith(".gz"); - String trunk = (gz) ? urlfile.substring(0, urlfile.length() - 3) + ".host" : urlfile + ".host"; - HashSet hosts = new HashSet(); - File infile = new File(urlfile); + final boolean gz = urlfile.endsWith(".gz"); + final String trunk = (gz) ? urlfile.substring(0, urlfile.length() - 3) + ".host" : urlfile + ".host"; + final HashSet hosts = new HashSet(); + final File infile = new File(urlfile); BufferedReader reader = null; long time = System.currentTimeMillis(); - long start = time; + final long start = time; int count = 0; System.out.println("start processing"); @@ -297,9 +298,9 @@ public class URLAnalysis { // copy everything into a TreeSet to order it System.out.println("start processing results"); - TreeSet results = new TreeSet(); + final TreeSet results = new TreeSet(); count = 0; - Iterator i = hosts.iterator(); + final Iterator i = hosts.iterator(); while (i.hasNext()) { results.add(i.next()); count++; @@ -316,7 +317,7 @@ public class URLAnalysis { System.out.println("finished"); } - private static void writeSet(String trunk, boolean gz, Set set) { + private static void writeSet(final String trunk, final boolean gz, final Set set) { // write hosts System.out.println("start writing results"); @@ -326,7 +327,7 @@ public class URLAnalysis { OutputStream os = new BufferedOutputStream(new FileOutputStream(outfile)); if (gz) os = new GZIPOutputStream(os); int count = 0; - for (String h: set) { + for (final String h: set) { os.write(h.getBytes()); os.write(new byte[]{'\n'}); count++; @@ -343,18 +344,18 @@ public class URLAnalysis { System.out.println("finished writing results"); } - public static void sortsplit(String urlfile) { + public static void sortsplit(final String urlfile) { - boolean gz = urlfile.endsWith(".gz"); - String trunk = ((gz) ? urlfile.substring(0, urlfile.length() - 3) : urlfile) + ".sort"; - File infile = new File(urlfile); - TreeSet urls = new TreeSet(); + final boolean gz = urlfile.endsWith(".gz"); + final String trunk = ((gz) ? urlfile.substring(0, urlfile.length() - 3) : urlfile) + ".sort"; + final File infile = new File(urlfile); + final TreeSet urls = new TreeSet(); BufferedReader reader = null; long time = System.currentTimeMillis(); - long start = time; + final long start = time; int count = 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"); try { @@ -397,9 +398,9 @@ public class URLAnalysis { System.out.println("finished"); } - public static void incell(File cellPath, String statisticPath) { + public static void incell(final File cellPath, final String statisticPath) { try { - HandleMap idx = ReferenceContainerArray.referenceHashes( + final HandleMap idx = ReferenceContainerArray.referenceHashes( cellPath, Segment.wordReferenceFactory, 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"); - 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); - HandleSet hs = new HandleSet(URIMetadataRow.rowdef.primaryKeyLength, URIMetadataRow.rowdef.objectOrder, 1000000); + final HandleMap idx = new HandleMap(URIMetadataRow.rowdef.primaryKeyLength, URIMetadataRow.rowdef.objectOrder, 4, new File(statisticFile), 0); + final MetadataRepository mr = new MetadataRepository(new File(metadataPath), "text.urlmd", false, false); + final HandleSet hs = new HandleSet(URIMetadataRow.rowdef.primaryKeyLength, URIMetadataRow.rowdef.objectOrder, 1000000); System.out.println("INDEX DIFF URL-COL loaded dump, starting diff"); - long start = System.currentTimeMillis(); + final long start = System.currentTimeMillis(); long update = start - 7000; - int c = 0; - for (byte[] refhash: mr) { + int count = 0; + for (final byte[] refhash: mr) { if (idx.get(refhash) == -1) { // the key exists as urlhash in the URL database, but not in the collection as referenced urlhash hs.put(refhash); } - c++; + count++; 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(); } } mr.close(); System.out.println("INDEX DIFF URL-COL finished diff, starting dump to " + diffFile); - c = 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"); - return c; + count = hs.dump(new File(diffFile)); + 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 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 - System.out.println("URL EXPORT startup"); - 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); + System.out.println("URL EXPORT startup"); + final MetadataRepository mr = new MetadataRepository(new File(metadataPath), "text.urlmd", false, false); + 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"); - Export e = mr.export(new File(export), ".*", hs, format, false); + final Export e = mr.export(new File(export), ".*", hs, format, false); try { e.join(); } 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"); } - public static void delete(String metadataPath, String diffFile) throws IOException, RowSpaceExceededException { - System.out.println("URL DELETE startup"); - MetadataRepository mr = new MetadataRepository(new File(metadataPath), "text.urlmd", false, false); - int mrSize = mr.size(); - HandleSet hs = new HandleSet(URIMetadataRow.rowdef.primaryKeyLength, URIMetadataRow.rowdef.objectOrder, new File(diffFile), 0); + public static void delete(final String metadataPath, final String diffFile) throws IOException, RowSpaceExceededException { + System.out.println("URL DELETE startup"); + final MetadataRepository mr = new MetadataRepository(new File(metadataPath), "text.urlmd", false, false); + final int mrSize = mr.size(); + 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); - for (byte[] refhash: hs) { + for (final byte[] refhash: hs) { mr.remove(refhash); } 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) { // generate a statistics about common words in file, store to .stat // example: @@ -548,9 +549,9 @@ public class URLAnalysis { System.exit(0); // kill remaining threads } - private static final String num(int i) { - String s = Integer.toString(i); - while (s.length() < 9) s = "0" + s; - return s; + private static final String num(final int i) { + StringBuffer s = new StringBuffer(Integer.toString(i)); + while (s.length() < 9) s.insert(0, "0"); + return s.toString(); } } diff --git a/source/de/anomic/data/URLLicense.java b/source/de/anomic/data/URLLicense.java index 9a42a37de..39544b83f 100644 --- a/source/de/anomic/data/URLLicense.java +++ b/source/de/anomic/data/URLLicense.java @@ -56,10 +56,10 @@ public class URLLicense { public String aquireLicense(final DigestURI url) { // generate license key - String license = ""; - if (url == null) return license; - while (license.length() < keylen) license += Integer.toHexString(random.nextInt()); - license = license.substring(0, keylen); + StringBuilder stringBuilder = new StringBuilder(); + if (url == null) return stringBuilder.toString(); + while (stringBuilder.length() < keylen) stringBuilder.append(Integer.toHexString(random.nextInt())); + String license = stringBuilder.substring(0, keylen); // store reference to url with license key permissions.put(license, url); aging.add(license); diff --git a/source/de/anomic/data/WorkTables.java b/source/de/anomic/data/WorkTables.java index 7b499736f..4b5d149a7 100644 --- a/source/de/anomic/data/WorkTables.java +++ b/source/de/anomic/data/WorkTables.java @@ -50,12 +50,12 @@ public class WorkTables extends Tables { public final static String TABLE_ROBOTS_NAME = "robots"; - public WorkTables(File workPath) { + public WorkTables(final File workPath) { super(workPath, 12); } - public void recordAPICall(final serverObjects post, final String servletName, String type, String comment) { - String apiurl = /*"http://localhost:" + getConfig("port", "8080") +*/ "/" + servletName + "?" + post.toString(); + public void recordAPICall(final serverObjects post, final String servletName, final String type, final String comment) { + final String apiurl = /*"http://localhost:" + getConfig("port", "8080") +*/ "/" + servletName + "?" + post.toString(); try { super.insert( TABLE_API_NAME, diff --git a/source/de/anomic/data/blogBoard.java b/source/de/anomic/data/blogBoard.java index 78940209c..6e52f4bfd 100644 --- a/source/de/anomic/data/blogBoard.java +++ b/source/de/anomic/data/blogBoard.java @@ -43,6 +43,7 @@ import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import net.yacy.kelondro.blob.MapHeap; +import net.yacy.kelondro.index.RowSpaceExceededException; import net.yacy.kelondro.logging.Log; import net.yacy.kelondro.order.Base64Order; import net.yacy.kelondro.order.NaturalOrder; @@ -55,19 +56,19 @@ import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import de.anomic.data.wiki.wikiBoard; +import java.util.List; +import java.util.Set; 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 { new File(actpath.getParent()).mkdir(); - if (database == null) { - //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 MapView(BLOBTree.toHeap(actpath, true, true, keyLength, recordSize, '_', NaturalOrder.naturalOrder, newFile), 500, '_'); + database = new MapHeap(actpath, KEY_LENGTH, NaturalOrder.naturalOrder, 1024 * 64, 500, '_'); } public int size() { @@ -92,17 +93,11 @@ public class blogBoard { } private static String normalize(final String key) { - if (key == null) return "null"; - return key.trim().toLowerCase(); + return (key == null) ? "null" : key.trim().toLowerCase(); } - public static String webalize(String key) { - if (key == null) return "null"; - key = key.trim().toLowerCase(); - int p; - while ((p = key.indexOf(" ")) >= 0) - key = key.substring(0, p) + "%20" + key.substring(p +1); - return key; + public static String webalize(final String key) { + return (key == null) ? "null": key.trim().toLowerCase().replaceAll(" ", "%20"); } 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 subject * @param author @@ -121,7 +116,7 @@ public class blogBoard { * @param commentMode possible params are: 0 - no comments allowed, 1 - comments allowed, 2 - comments moderated * @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 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 comments, final String commentMode) { return new BlogEntry(normalize(key), subject, author, ip, date, page, comments, commentMode); } @@ -129,115 +124,124 @@ public class blogBoard { * writes a new page and return the key */ public String writeBlogEntry(final BlogEntry page) { + String ret = null; try { database.put(page.key, page.record); - return page.key; - } catch (final Exception e) { - Log.logException(e); - return null; + ret = page.key; + } catch (IOException ex) { + Log.logException(ex); + } catch (RowSpaceExceededException ex) { + Log.logException(ex); } + return ret; } public BlogEntry readBlogEntry(final String key) { return readBlogEntry(key, database); } - private BlogEntry readBlogEntry(String key, final MapHeap base) { - key = normalize(key); - if (key.length() > keyLength) key = key.substring(0, keyLength); + private BlogEntry readBlogEntry(final String key, final MapHeap base) { + final String normalized = normalize(key); Map record; try { - record = base.get(key); + record = base.get(normalized.substring(0, Math.min(normalized.length(), KEY_LENGTH))); } catch (final IOException e) { record = null; } - if (record == null) - return newEntry(key, "".getBytes(), "anonymous".getBytes(), "127.0.0.1", new Date(), "".getBytes(), null, null); - return new BlogEntry(key, record); + return (record == null) ? + newEntry(key, new byte[0], "anonymous".getBytes(), "127.0.0.1", new Date(), new byte[0], null, null) : + new BlogEntry(key, record); } public boolean importXML(final String input) { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { - final DocumentBuilder builder = factory.newDocumentBuilder(); - final Document doc = builder.parse(new ByteArrayInputStream(input.getBytes("UTF-8"))); - return parseXMLimport(doc); - } catch (final ParserConfigurationException e) { - } catch (final SAXException e) { - } catch (final IOException e) {} + final DocumentBuilder builder = factory.newDocumentBuilder(); + return parseXMLimport(builder.parse(new ByteArrayInputStream(input.getBytes("UTF-8")))); + } catch (final ParserConfigurationException ex) { + Log.logException(ex); + } catch (final SAXException ex) { + Log.logException(ex); + } catch (final IOException ex) { + Log.logException(ex); + } return false; } private boolean parseXMLimport(final Document doc) { - if(!doc.getDocumentElement().getTagName().equals("blog")) - return false; + if(!doc.getDocumentElement().getTagName().equals("blog")) { + return false; + } final NodeList items = doc.getDocumentElement().getElementsByTagName("item"); - if(items.getLength() == 0) - return false; + if(items.getLength() == 0) { + return false; + } - for(int i=0;i keys(final boolean up) throws IOException { @@ -261,26 +265,28 @@ public class blogBoard { public int compare(final String obj1, final String obj2) { final BlogEntry blogEntry1 = readBlogEntry(obj1); final BlogEntry blogEntry2 = readBlogEntry(obj2); - if(blogEntry1 == null || blogEntry2 == null) + if (blogEntry1 == null || blogEntry2 == null) return 0; 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; } - if (Long.parseLong(blogEntry1.getTimestamp()) - Long.parseLong(blogEntry2.getTimestamp()) > 0) + if (Long.parseLong(blogEntry1.getTimestamp()) - Long.parseLong(blogEntry2.getTimestamp()) > 0) { return 1; + } return -1; } } public Iterator getBlogIterator(final boolean priv){ - final TreeSet set = new TreeSet(new BlogComparator(true)); + final Set set = new TreeSet(new BlogComparator(true)); final Iterator iterator = blogIterator(true); BlogEntry blogEntry; - while(iterator.hasNext()){ - blogEntry=iterator.next(); - if(priv || blogEntry.isPublic()){ + while (iterator.hasNext()) { + blogEntry = iterator.next(); + if (priv || blogEntry.isPublic()) { set.add(blogEntry.getKey()); } } @@ -333,6 +339,7 @@ public class blogBoard { // //resetDatabase(); // } // } + throw new UnsupportedOperationException("Method not implemented yet."); } } @@ -341,7 +348,7 @@ public class blogBoard { String key; Map record; - public BlogEntry(final String nkey, final byte[] subject, final byte[] author, final String ip, final Date date, final byte[] page, final ArrayList 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 comments, final String commentMode) { record = new HashMap(); setKey(nkey); setDate(date); @@ -361,14 +368,16 @@ public class blogBoard { BlogEntry(final String key, final Map record) { this.key = key; this.record = record; - if (this.record.get("comments")==null) this.record.put("comments", listManager.collection2string(new ArrayList())); - if (this.record.get("commentMode")==null || this.record.get("commentMode").equals("")) this.record.put("commentMode", "2"); + if (this.record.get("comments") == null) { + this.record.put("comments", listManager.collection2string(new ArrayList())); + } + if (this.record.get("commentMode") == null || this.record.get("commentMode").equals("")) { + this.record.put("commentMode", "2"); + } } private void setKey(final String key) { - if (key.length() > keyLength) - this.key = key.substring(0, keyLength); - this.key = key; + this.key = key.substring(0, Math.min(key.length(), KEY_LENGTH)); } public String getKey() { @@ -377,42 +386,50 @@ public class blogBoard { public byte[] getSubject() { 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); - if (b == null) return "".getBytes(); - return b; + return (b == null) ? new byte[0] : b; } private void setSubject(final byte[] subject) { - if (subject == null) + if (subject == null) { record.put("subject",""); - else + } else { record.put("subject", Base64Order.enhancedCoder.encode(subject)); + } } public Date getDate() { try { final String date = record.get("date"); 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 DateFormatter.parseShortSecond(date); - } catch (final ParseException e) { + } catch (final ParseException ex) { return new Date(); } } - private void setDate(Date date) { - if(date == null) - date = new Date(); - record.put("date", DateFormatter.formatShortSecond(date)); + private void setDate(final Date date) { + Date ret = date; + if (ret == null) { + ret = new Date(); + } + record.put("date", DateFormatter.formatShortSecond(ret)); } public String getTimestamp() { final String timestamp = record.get("date"); 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 timestamp; @@ -420,76 +437,79 @@ public class blogBoard { public byte[] getAuthor() { 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); - if (b == null) return "".getBytes(); - return b; + return (b == null) ? new byte[0] : b; } private void setAuthor(final byte[] author) { - if (author == null) + if (author == null) record.put("author",""); - else - record.put("author", Base64Order.enhancedCoder.encode(author)); + else + record.put("author", Base64Order.enhancedCoder.encode(author)); } public int getCommentsSize() { // This ist a Bugfix for Version older than 4443. - if(record.get("comments").startsWith(",")) { + if (record.get("comments").startsWith(",")) { record.put("comments", record.get("comments").substring(1)); writeBlogEntry(this); } - final ArrayList commentsize = listManager.string2arraylist(record.get("comments")); + final List commentsize = listManager.string2arraylist(record.get("comments")); return commentsize.size(); } - public ArrayList getComments() { - final ArrayList comments = listManager.string2arraylist(record.get("comments")); - return comments; + public List getComments() { + return listManager.string2arraylist(record.get("comments")); } - private void setComments(final ArrayList comments) { - if (comments == null) + private void setComments(final List comments) { + if (comments == null) { record.put("comments", listManager.collection2string(new ArrayList())); - else + } else { record.put("comments", listManager.collection2string(comments)); + } } public String getIp() { final String ip = record.get("ip"); - if (ip == null) return "127.0.0.1"; - return ip; + return (ip == null) ? "127.0.0.1" : ip; } - private void setIp(String ip) { - if ((ip == null) || (ip.length() == 0)) - ip = ""; - record.put("ip", ip); + private void setIp(final String ip) { + String ret = ip; + if ((ret == null) || (ret.length() == 0)) + ret = ""; + record.put("ip", ret); } public byte[] getPage() { 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); - if (page_as_byte == null) return "".getBytes(); - return page_as_byte; + return (page_as_byte == null) ? new byte[0] : page_as_byte; } private void setPage(final byte[] page) { - if (page == null) + if (page == null) { record.put("page", ""); - else + } else { record.put("page", Base64Order.enhancedCoder.encode(page)); + } } public void addComment(final String commentID) { - final ArrayList comments = listManager.string2arraylist(record.get("comments")); + final List comments = listManager.string2arraylist(record.get("comments")); comments.add(commentID); record.put("comments", listManager.collection2string(comments)); } public boolean removeComment(final String commentID) { - final ArrayList comments = listManager.string2arraylist(record.get("comments")); + final List comments = listManager.string2arraylist(record.get("comments")); final boolean success = comments.remove(commentID); record.put("comments", listManager.collection2string(comments)); return success; @@ -507,19 +527,16 @@ public class blogBoard { } private void setCommentMode(final String mode) { - if (mode == null) + if (mode == null) { record.put("commentMode", "2"); - else + } else { record.put("commentMode", mode); + } } public boolean isPublic() { final String privacy = record.get("privacy"); - if (privacy == null) - return true; - if(privacy.equalsIgnoreCase("public")) - return true; - return false; + return (privacy == null || privacy.equalsIgnoreCase("public")) ? true : false; } } diff --git a/source/de/anomic/data/blogBoardComments.java b/source/de/anomic/data/blogBoardComments.java index 21cc0cd63..a6b88d83c 100644 --- a/source/de/anomic/data/blogBoardComments.java +++ b/source/de/anomic/data/blogBoardComments.java @@ -56,22 +56,20 @@ import de.anomic.data.wiki.wikiBoard; public class blogBoardComments { - public static final int keyLength = 64; - private static final String dateFormat = "yyyyMMddHHmmss"; + private final static int KEY_LENGTH = 64; + 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 { - SimpleFormatter.setTimeZone(TimeZone.getTimeZone("GMT")); + SIMPLE_DATE_FORMATTER.setTimeZone(TimeZone.getTimeZone("GMT")); } private MapHeap database = null; public blogBoardComments(final File actpath) throws IOException { new File(actpath.getParent()).mkdir(); - if (database == null) { - //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 MapView(BLOBTree.toHeap(actpath, true, true, keyLength, recordSize, '_', NaturalOrder.naturalOrder, newFile), 500, '_'); + database = new MapHeap(actpath, KEY_LENGTH, NaturalOrder.naturalOrder, 1024 * 64, 500, '_'); } public int size() { @@ -83,29 +81,33 @@ public class blogBoardComments { } static String dateString(final Date date) { - synchronized (SimpleFormatter) { - return SimpleFormatter.format(date); + synchronized (SIMPLE_DATE_FORMATTER) { + return SIMPLE_DATE_FORMATTER.format(date); } } private static String normalize(final String key) { - if (key == null) return "null"; - return key.trim().toLowerCase(); + return (key == null) ? "null" : key.trim().toLowerCase(); } - public static String webalize(String key) { - if (key == null) return "null"; - key = key.trim().toLowerCase(); + public static String webalize(final String key) { + if (key == null) { + return "null"; + } + String ret = key.trim().toLowerCase(); int p; - while ((p = key.indexOf(" ")) >= 0) - key = key.substring(0, p) + "%20" + key.substring(p +1); - return key; + while ((p = ret.indexOf(" ")) >= 0) + ret = ret.substring(0, p) + "%20" + key.substring(p +1); + return ret; } + public String guessAuthor(final String 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) { return new CommentEntry(normalize(key), subject, author, ip, date, page); } + public String write(final CommentEntry page) { // writes a new page and returns key try { @@ -120,99 +122,115 @@ public class blogBoardComments { //System.out.println("DEBUG: read from blogBoardComments"); return read(key, database); } - private CommentEntry read(String key, final MapHeap base) { - key = normalize(key); - if (key.length() > keyLength) key = key.substring(0, keyLength); + + private CommentEntry read(final String key, final MapHeap base) { + String copyOfKey = normalize(key); + copyOfKey = copyOfKey.substring(0, Math.min(copyOfKey.length(), KEY_LENGTH)); Map record; try { - record = base.get(key); + record = base.get(copyOfKey); } catch (final IOException e) { record = null; } - if (record == null) return newEntry(key, "".getBytes(), "anonymous".getBytes(), "127.0.0.1", new Date(), "".getBytes()); - return new CommentEntry(key, record); + return (record == null) ? + 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) { + boolean ret = false; final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { - final DocumentBuilder builder = factory.newDocumentBuilder(); - final Document doc = builder.parse(new ByteArrayInputStream(input.getBytes())); - return parseXMLimport(doc); - } catch (final ParserConfigurationException e) { - } catch (final SAXException e) { - } catch (final IOException e) {} + final DocumentBuilder builder = factory.newDocumentBuilder(); + final Document doc = builder.parse(new ByteArrayInputStream(input.getBytes())); + ret = parseXMLimport(doc); + } + catch (final ParserConfigurationException e) {} + catch (final SAXException e) {} + catch (final IOException e) {} - return false; + return ret; } + private boolean parseXMLimport(final Document doc) { - if(!doc.getDocumentElement().getTagName().equals("blog")) - return false; + if(!doc.getDocumentElement().getTagName().equals("blog")) { + return false; + } final NodeList items = doc.getDocumentElement().getElementsByTagName("item"); - if(items.getLength() == 0) - return false; + if(items.getLength() == 0) { + return false; + } - for(int i=0;i keys(final boolean up) throws IOException { return database.keys(up, false); } @@ -238,17 +256,19 @@ public class blogBoardComments { CommentEntry(final String key, final Map record) { this.key = key; this.record = record; - if (this.record.get("comments")==null) this.record.put("comments", listManager.collection2string(new ArrayList())); + if (this.record.get("comments") == null) { + this.record.put("comments", listManager.collection2string(new ArrayList())); + } } public String getKey() { return key; } + private void setKey(final String var) { - key = var; - if (key.length() > keyLength) - key = var.substring(0, keyLength); + key = var.substring(0, Math.min(var.length(), KEY_LENGTH)); } + private void setSubject(final byte[] subject) { if (subject == null) record.put("subject",""); @@ -259,7 +279,7 @@ public class blogBoardComments { final String subject = record.get("subject"); if (subject == null) return new byte[0]; 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; } 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"); return new Date(); } - synchronized (SimpleFormatter) { - return SimpleFormatter.parse(date); + synchronized (SIMPLE_DATE_FORMATTER) { + return SIMPLE_DATE_FORMATTER.parse(date); } } catch (final ParseException e) { return new Date(); @@ -302,7 +322,7 @@ public class blogBoardComments { return new byte[0]; final byte[] author_byte = Base64Order.enhancedCoder.decode(author); if (author_byte == null) - return "".getBytes(); + return new byte[0]; return author_byte; } private void setIp(String ip) { @@ -328,7 +348,7 @@ public class blogBoardComments { return new byte[0]; final byte[] page_byte = Base64Order.enhancedCoder.decode(page); if (page_byte == null) - return "".getBytes(); + return new byte[0]; return page_byte; } /** diff --git a/source/de/anomic/data/bookmarksDB.java b/source/de/anomic/data/bookmarksDB.java index 67d96ff5f..16ded41d8 100644 --- a/source/de/anomic/data/bookmarksDB.java +++ b/source/de/anomic/data/bookmarksDB.java @@ -65,21 +65,21 @@ public class bookmarksDB { // Declaration of Class-Attributes // ------------------------------------ - final static int SORT_ALPHA = 1; - final static int SORT_SIZE = 2; - final static int SHOW_ALL = -1; - final static String SLEEP_TIME = "3600000"; // default sleepTime: check for recrawls every hour - - // bookmarks - MapHeap bookmarks; + //final static int SORT_ALPHA = 1; + private final static int SORT_SIZE = 2; + private final static int SHOW_ALL = -1; + private final static String SLEEP_TIME = "3600000"; // default sleepTime: check for recrawls every hour + + // bookmarks + private MapHeap bookmarks; // tags - ConcurrentHashMap tags; + private ConcurrentHashMap tags; // autoReCrawl private final BusyThread autoReCrawl; - BookmarkDate dates; + private BookmarkDate dates; // ------------------------------------ // bookmarksDB's class constructor @@ -99,15 +99,15 @@ public class bookmarksDB { final Iterator it = new bookmarkIterator(true); Bookmark bookmark; Tag tag; - String[] tags; + String[] tagArray; while(it.hasNext()){ - bookmark=it.next(); - tags = BookmarkHelper.cleanTagsString(bookmark.getTagsString() + bookmark.getFoldersString()).split(","); + bookmark = it.next(); + tagArray = BookmarkHelper.cleanTagsString(bookmark.getTagsString() + bookmark.getFoldersString()).split(","); tag = null; - for (int i = 0; i < tags.length; i++) { - tag = getTag(BookmarkHelper.tagHash(tags[i])); + for (final String element : tagArray) { + tag = getTag(BookmarkHelper.tagHash(element)); if (tag == null) { - tag = new Tag(tags[i]); + tag = new Tag(element); } tag.addUrl(bookmark.getUrlHash()); putTag(tag); @@ -122,9 +122,9 @@ public class bookmarksDB { if (!datesExisted) this.dates.init(new bookmarkIterator(true)); // 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); - 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, sleepTime, sleepTime, Long.parseLong(sb.getConfig("autoReCrawl_memprereq" , "-1")) ); @@ -132,8 +132,8 @@ public class bookmarksDB { } // ----------------------------------------------------- - // bookmarksDB's functions for 'destructing' the class - // ----------------------------------------------------- + // bookmarksDB's functions for 'destructing' the class + // ----------------------------------------------------- public void close(){ bookmarks.close(); @@ -142,63 +142,64 @@ public class bookmarksDB { } // ----------------------------------------------------- - // bookmarksDB's functions for autoReCrawl - // ----------------------------------------------------- + // bookmarksDB's functions for autoReCrawl + // ----------------------------------------------------- public boolean autoReCrawl() { // 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; try { - BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(f))); - Log.logInfo("BOOKMARKS", "autoReCrawl - reading schedules from " + f); - while( null != (s = in.readLine()) ) { - if (s.length() > 0 && s.charAt(0) != '#') { - String parser[] = s.split("\t"); - if (parser.length == 13) { - 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]), - Boolean.parseBoolean(parser[8]), Boolean.parseBoolean(parser[9]), - Boolean.parseBoolean(parser[10]), Boolean.parseBoolean(parser[11]), - Boolean.parseBoolean(parser[12]), CrawlProfile.CACHE_STRATEGY_IFFRESH - ); - } - if (parser.length == 14) { + final BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file))); + Log.logInfo("BOOKMARKS", "autoReCrawl - reading schedules from " + file); + while( null != (s = in.readLine()) ) { + if (s.length() > 0 && s.charAt(0) != '#') { + final String parser[] = s.split("\t"); + if (parser.length == 13) { + 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]), + Boolean.parseBoolean(parser[8]), Boolean.parseBoolean(parser[9]), + Boolean.parseBoolean(parser[10]), Boolean.parseBoolean(parser[11]), + Boolean.parseBoolean(parser[12]), CrawlProfile.CACHE_STRATEGY_IFFRESH + ); + } + if (parser.length == 14) { 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]), - Boolean.parseBoolean(parser[8]), Boolean.parseBoolean(parser[9]), - Boolean.parseBoolean(parser[10]), Boolean.parseBoolean(parser[11]), - Boolean.parseBoolean(parser[12]), Integer.parseInt(parser[13]) - ); + Integer.parseInt(parser[5]), Integer.parseInt(parser[6]), Boolean.parseBoolean(parser[7]), + Boolean.parseBoolean(parser[8]), Boolean.parseBoolean(parser[9]), + Boolean.parseBoolean(parser[10]), Boolean.parseBoolean(parser[11]), + Boolean.parseBoolean(parser[12]), Integer.parseInt(parser[13]) + ); } - } - } - in.close(); + } + } + in.close(); } catch( FileNotFoundException ex ) { - try { - Log.logInfo("BOOKMARKS", "autoReCrawl - creating new autoReCrawl.conf"); - File inputFile = new File(Switchboard.getSwitchboard().getRootPath(),"defaults/autoReCrawl.conf"); - File outputFile = new File(Switchboard.getSwitchboard().getRootPath(),"DATA/SETTINGS/autoReCrawl.conf"); - FileReader i = new FileReader(inputFile); - FileWriter o = new FileWriter(outputFile); - int c; - while ((c = i.read()) != -1) - o.write(c); - i.close(); - o.close(); - autoReCrawl(); - return true; - } catch( FileNotFoundException e ) { - Log.logSevere("BOOKMARKS", "autoReCrawl - file not found error: defaults/autoReCrawl.conf", e); - return false; - } catch (IOException e) { - Log.logSevere("BOOKMARKS", "autoReCrawl - IOException: defaults/autoReCrawl.conf", e); - return false; - } + try { + Log.logInfo("BOOKMARKS", "autoReCrawl - creating new autoReCrawl.conf"); + final File inputFile = new File(Switchboard.getSwitchboard().getRootPath(),"defaults/autoReCrawl.conf"); + final File outputFile = new File(Switchboard.getSwitchboard().getRootPath(),"DATA/SETTINGS/autoReCrawl.conf"); + final FileReader i = new FileReader(inputFile); + final FileWriter o = new FileWriter(outputFile); + int c; + while ((c = i.read()) != -1) { + o.write(c); + } + i.close(); + o.close(); + autoReCrawl(); + return true; + } catch( FileNotFoundException e ) { + Log.logSevere("BOOKMARKS", "autoReCrawl - file not found error: defaults/autoReCrawl.conf", e); + return false; + } catch (IOException e) { + Log.logSevere("BOOKMARKS", "autoReCrawl - IOException: defaults/autoReCrawl.conf", e); + return false; + } } catch( Exception ex ) { - Log.logSevere("BOOKMARKS", "autoReCrawl - error reading " + f, ex); - return false; + Log.logSevere("BOOKMARKS", "autoReCrawl - error reading " + file, ex); + return false; } return true; } @@ -207,30 +208,30 @@ public class bookmarksDB { int crawlingDomFilterDepth, int crawlingDomMaxPages, boolean crawlingQ, boolean indexText, boolean indexMedia, boolean crawlOrder, boolean xsstopw, boolean storeHTCache, int cacheStrategy) { - Switchboard sb = Switchboard.getSwitchboard(); - Iterator bit=getBookmarksIterator(folder, true); - Log.logInfo("BOOKMARKS", "autoReCrawl - processing: "+folder); + final Switchboard sb = Switchboard.getSwitchboard(); + final Iterator bit = getBookmarksIterator(folder, true); + Log.logInfo("BOOKMARKS", "autoReCrawl - processing: "+folder); - boolean xdstopw = xsstopw; - boolean xpstopw = xsstopw; + final boolean xdstopw = xsstopw; + final boolean xpstopw = xsstopw; - while(bit.hasNext()) { + while(bit.hasNext()) { - Bookmark bm = getBookmark(bit.next()); - long sleepTime = Long.parseLong(sb.getConfig("autoReCrawl_idlesleep" , SLEEP_TIME)); - long interTime = (System.currentTimeMillis()-bm.getTimeStamp())%schedule; + final Bookmark bm = getBookmark(bit.next()); + final long sleepTime = Long.parseLong(sb.getConfig("autoReCrawl_idlesleep" , SLEEP_TIME)); + final long interTime = (System.currentTimeMillis()-bm.getTimeStamp())%schedule; - Date date=new Date(bm.getTimeStamp()); - Log.logInfo("BOOKMARKS", "autoReCrawl - checking schedule for: "+"["+DateFormatter.formatISO8601(date)+"] "+bm.getUrl()); + final Date date = new Date(bm.getTimeStamp()); + Log.logInfo("BOOKMARKS", "autoReCrawl - checking schedule for: "+"["+DateFormatter.formatISO8601(date)+"] "+bm.getUrl()); - if (interTime >= 0 && interTime < sleepTime) { - try { - int pos = 0; - // set crawlingStart to BookmarkUrl - String crawlingStart = bm.getUrl(); - String newcrawlingMustMatch = crawlingfilter; + if (interTime >= 0 && interTime < sleepTime) { + try { + int pos = 0; + // set crawlingStart to BookmarkUrl + final String crawlingStart = bm.getUrl(); + String newcrawlingMustMatch = crawlingfilter; - DigestURI crawlingStartURL = new DigestURI(crawlingStart, null); + final DigestURI crawlingStartURL = new DigestURI(crawlingStart, null); // set the crawling filter if (newcrawlingMustMatch.length() < 2) newcrawlingMustMatch = ".*"; // avoid that all urls are filtered out if bad value was submitted @@ -241,18 +242,19 @@ public class bookmarksDB { if (crawlingStart!= null && newcrawlingMustMatch.equals("sub") && (pos = crawlingStart.lastIndexOf("/")) > 0) { newcrawlingMustMatch = crawlingStart.substring(0, pos + 1) + ".*"; } - - // check if the crawl filter works correctly - Pattern.compile(newcrawlingMustMatch); + + // check if the crawl filter works correctly + Pattern.compile(newcrawlingMustMatch); - byte[] urlhash = crawlingStartURL.hash(); + final byte[] urlhash = crawlingStartURL.hash(); + sb.indexSegments.urlMetadata(Segments.Process.LOCALCRAWLING).remove(urlhash); sb.crawlQueues.noticeURL.removeByURLHash(urlhash); sb.crawlQueues.errorURL.remove(urlhash); // stack url 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, newcrawlingMustMatch, CrawlProfile.MATCH_BAD_URL, @@ -263,19 +265,19 @@ public class bookmarksDB { storeHTCache, true, crawlOrder, xsstopw, xdstopw, xpstopw, cacheStrategy); sb.crawlStacker.enqueueEntry(new Request( sb.peers.mySeed().hash.getBytes(), - crawlingStartURL, - null, - "CRAWLING-ROOT", - new Date(), - null, - pe.handle(), - 0, - 0, - 0 - )); - Log.logInfo("BOOKMARKS", "autoReCrawl - adding crawl profile for: " + crawlingStart); - // serverLog.logInfo("BOOKMARKS", "autoReCrawl - crawl filter is set to: " + newcrawlingfilter); - // generate a YaCyNews if the global flag was set + crawlingStartURL, + null, + "CRAWLING-ROOT", + new Date(), + null, + pe.handle(), + 0, + 0, + 0 + )); + Log.logInfo("BOOKMARKS", "autoReCrawl - adding crawl profile for: " + crawlingStart); + // serverLog.logInfo("BOOKMARKS", "autoReCrawl - crawl filter is set to: " + newcrawlingfilter); + // generate a YaCyNews if the global flag was set if (crawlOrder) { Map m = new HashMap(pe.map()); // must be cloned m.remove("specificDepth"); @@ -292,15 +294,14 @@ public class bookmarksDB { m.put("intention", "Automatic ReCrawl!"); sb.peers.newsPool.publishMyNews(yacyNewsRecord.newRecord(sb.peers.mySeed(), yacyNewsPool.CATEGORY_CRAWL_START, m)); } - } catch (MalformedURLException e1) {} - } // if - } // while(bit.hasNext()) - return; + } catch (MalformedURLException e1) {} + } // if + } // while(bit.hasNext()) } // } autoReCrawl() // ----------------------------------------------------------- - // bookmarksDB's functions for bookmarksTable / bookmarkCache - // ----------------------------------------------------------- + // bookmarksDB's functions for bookmarksTable / bookmarkCache + // ----------------------------------------------------------- public Bookmark createBookmark(final String url, final String user){ if (url == null || url.length() == 0) return null; @@ -317,13 +318,13 @@ public class bookmarksDB { // adding a bookmark to the bookmarksDB public void saveBookmark(final Bookmark bookmark){ try { - bookmarks.put(bookmark.getUrlHash(), bookmark.entry); + bookmarks.put(bookmark.getUrlHash(), bookmark.entry); } catch (final Exception e) { Log.logException(e); } } public String addBookmark(final Bookmark bookmark){ - saveBookmark(bookmark); + this.saveBookmark(bookmark); return bookmark.getUrlHash(); } @@ -331,8 +332,7 @@ public class bookmarksDB { public Bookmark getBookmark(final String urlHash){ try { final Map map = bookmarks.get(urlHash); - if (map == null) return null; - return new Bookmark(map); + return (map == null) ? new Bookmark(map) : null; } catch (final IOException e) { return null; } @@ -340,10 +340,10 @@ public class bookmarksDB { public boolean removeBookmark(final String urlHash){ final Bookmark bookmark = getBookmark(urlHash); - if(bookmark == null) return false; //does not exist - final Set tags = bookmark.getTags(); + if (bookmark == null) return false; //does not exist + final Set tagSet = bookmark.getTags(); bookmarksDB.Tag tag=null; - final Iterator it=tags.iterator(); + final Iterator it=tagSet.iterator(); while(it.hasNext()){ tag=getTag(BookmarkHelper.tagHash(it.next())); if(tag!=null){ @@ -442,7 +442,7 @@ public class bookmarksDB { } private Iterator getTagIterator(final boolean priv, final int c) { - final TreeSet set=new TreeSet((c == SORT_SIZE) ? tagSizeComparator : tagComparator); + final Set set = new TreeSet((c == SORT_SIZE) ? tagSizeComparator : tagComparator); final Iterator it = this.tags.values().iterator(); Tag tag; while (it.hasNext()) { @@ -474,11 +474,11 @@ public class bookmarksDB { final Iterator bit=getBookmarksIterator(tagName, priv); Bookmark bm; Tag tag; - Set tags; + Set tagSet; while(bit.hasNext()){ bm=getBookmark(bit.next()); - tags = bm.getTags(); - it = tags.iterator(); + tagSet = bm.getTags(); + it = tagSet.iterator(); while (it.hasNext()) { tag=getTag(BookmarkHelper.tagHash(it.next()) ); if(priv ||tag.hasPublicItems()){ @@ -510,16 +510,16 @@ public class bookmarksDB { final Tag oldTag=getTag(BookmarkHelper.tagHash(oldName)); if (oldTag != null) { - final Set urlHashes = oldTag.getUrlHashes(); // preserve urlHashes of oldTag - removeTag(BookmarkHelper.tagHash(oldName)); // remove oldHash from TagsDB - final Iterator it = urlHashes.iterator(); + final Set urlHashes = oldTag.getUrlHashes(); // preserve urlHashes of oldTag + removeTag(BookmarkHelper.tagHash(oldName)); // remove oldHash from TagsDB + Bookmark bookmark; - Set tags = new TreeSet(String.CASE_INSENSITIVE_ORDER); - while (it.hasNext()) { // looping through all bookmarks which were tagged with oldName - bookmark = getBookmark(it.next()); - tags = bookmark.getTags(); - tags.remove(oldName); - bookmark.setTags(tags, true); // might not be needed, but doesn't hurt + Set tagSet = new TreeSet(String.CASE_INSENSITIVE_ORDER); + for (String urlHash : urlHashes) { // looping through all bookmarks which were tagged with oldName + bookmark = getBookmark(urlHash); + tagSet = bookmark.getTags(); + tagSet.remove(oldName); + bookmark.setTags(tagSet, true); // might not be needed, but doesn't hurt if(!newName.equals("")) bookmark.addTag(newName); saveBookmark(bookmark); } @@ -529,10 +529,10 @@ public class bookmarksDB { } public void addTag(final String selectTag, final String newTag) { - final Iterator it = getTag(BookmarkHelper.tagHash(selectTag)).getUrlHashes().iterator(); // get urlHashes for selectTag + Bookmark bookmark; - while (it.hasNext()) { // looping through all bookmarks which were tagged with selectTag - bookmark = getBookmark(it.next()); + for (final String urlHash : getTag(BookmarkHelper.tagHash(selectTag)).getUrlHashes()) { // looping through all bookmarks which were tagged with selectTag + bookmark = getBookmark(urlHash); bookmark.addTag(newTag); saveBookmark(bookmark); } @@ -546,36 +546,34 @@ public class bookmarksDB { * Subclass of bookmarksDB, which provides the Tag object-type */ public class Tag { - public static final String URL_HASHES="urlHashes"; - public static final String TAG_NAME="tagName"; + public static final String URL_HASHES = "urlHashes"; + public static final String TAG_NAME = "tagName"; private final String tagHash; private final Map mem; private Set urlHashes; public Tag(final String hash, final Map map){ - tagHash=hash; - mem=map; - if(mem.containsKey(URL_HASHES)) + tagHash = hash; + mem = map; + if (mem.containsKey(URL_HASHES)) { urlHashes = listManager.string2set(mem.get(URL_HASHES)); - else + } else { urlHashes = new HashSet(); + } } public Tag(final String name, final HashSet entries){ - tagHash=BookmarkHelper.tagHash(name); - mem=new HashMap(); + tagHash = BookmarkHelper.tagHash(name); + mem = new HashMap(); //mem.put(URL_HASHES, listManager.arraylist2string(entries)); - urlHashes=entries; + urlHashes = entries; mem.put(TAG_NAME, name); } public Tag(final String name){ - tagHash=BookmarkHelper.tagHash(name); - mem=new HashMap(); - //mem.put(URL_HASHES, ""); - urlHashes=new HashSet(); - mem.put(TAG_NAME, name); + this(name, new HashSet()); } + public Map getMap(){ mem.put(URL_HASHES, listManager.collection2string(this.urlHashes)); return mem; @@ -615,11 +613,7 @@ public class bookmarksDB { } public boolean hasPublicItems(){ - final Iterator it=getBookmarksIterator(this.getTagName(), false); - if(it.hasNext()){ - return true; - } - return false; + return getBookmarksIterator(this.getTagName(), false).hasNext(); } public void addUrl(final String urlHash){ @@ -640,30 +634,42 @@ public class bookmarksDB { */ public class Bookmark { - public static final String BOOKMARK_URL="bookmarkUrl"; - public static final String BOOKMARK_TITLE="bookmarkTitle"; - public static final String BOOKMARK_DESCRIPTION="bookmarkDesc"; - public static final String BOOKMARK_TAGS="bookmarkTags"; - public static final String BOOKMARK_PUBLIC="bookmarkPublic"; - public static final String BOOKMARK_TIMESTAMP="bookmarkTimestamp"; - public static final String BOOKMARK_OWNER="bookmarkOwner"; - public static final String BOOKMARK_IS_FEED="bookmarkIsFeed"; + public static final String BOOKMARK_URL = "bookmarkUrl"; + public static final String BOOKMARK_TITLE = "bookmarkTitle"; + public static final String BOOKMARK_DESCRIPTION = "bookmarkDesc"; + public static final String BOOKMARK_TAGS = "bookmarkTags"; + public static final String BOOKMARK_PUBLIC = "bookmarkPublic"; + public static final String BOOKMARK_TIMESTAMP = "bookmarkTimestamp"; + public static final String BOOKMARK_OWNER = "bookmarkOwner"; + public static final String BOOKMARK_IS_FEED = "bookmarkIsFeed"; private String urlHash; private Set tagNames; private long timestamp; - Map entry; + private Map entry; public Bookmark(final String urlHash, final Map map) { - this.entry = map; - this.urlHash=urlHash; + entry = map; + this.urlHash = urlHash; tagNames = new TreeSet(String.CASE_INSENSITIVE_ORDER); if (map.containsKey(BOOKMARK_TAGS)) tagNames.addAll(listManager.string2set(map.get(BOOKMARK_TAGS))); loadTimestamp(); } - + + public Bookmark(final String urlHash, final String url) { + entry = new HashMap(); + this.urlHash = urlHash; + entry.put(BOOKMARK_URL, url); + tagNames = new HashSet(); + timestamp = System.currentTimeMillis(); + } + + public Bookmark(final String urlHash, final DigestURI url) { + this(urlHash, url.toNormalform(false, true)); + } + public Bookmark(String url){ entry = new HashMap(); - if(!url.toLowerCase().startsWith("http://") && !url.toLowerCase().startsWith("https://")){ + if (!url.toLowerCase().startsWith("http://") && !url.toLowerCase().startsWith("https://")) { url="http://"+url; } try { @@ -687,22 +693,6 @@ public class bookmarksDB { removeBookmark(this.urlHash); //prevent empty tags } - public Bookmark(final String urlHash, final DigestURI url) { - entry = new HashMap(); - this.urlHash=urlHash; - entry.put(BOOKMARK_URL, url.toNormalform(false, true)); - tagNames=new HashSet(); - timestamp=System.currentTimeMillis(); - } - - public Bookmark(final String urlHash, final String url) { - entry = new HashMap(); - this.urlHash=urlHash; - entry.put(BOOKMARK_URL, url); - tagNames=new HashSet(); - timestamp=System.currentTimeMillis(); - } - public Bookmark(final Map map) throws MalformedURLException { this(new String((new DigestURI(map.get(BOOKMARK_URL), null)).hash()), map); } @@ -731,25 +721,27 @@ public class bookmarksDB { } public String getTagsString() { - final String s[] = listManager.collection2string(getTags()).split(","); - String tagsString=""; - for (int i=0; i tags2, final boolean local){ 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' - final Iterator it=tagNames.iterator(); - while(it.hasNext()){ - final String tagName=it.next(); - Tag tag=getTag(BookmarkHelper.tagHash(tagName)); - if(tag == null){ - tag=new Tag(tagName); + for (final String tagName : tagNames) { + Tag tag = getTag(BookmarkHelper.tagHash(tagName)); + if (tag == null) { + tag = new Tag(tagName); } tag.addUrl(getUrlHash()); - if(local){ + if (local) { putTag(tag); } } @@ -854,7 +844,7 @@ public class bookmarksDB { } public void setTimeStamp(final long ts){ - this.timestamp=ts; + this.timestamp = ts; } } @@ -882,10 +872,10 @@ public class bookmarksDB { } public Bookmark next() { - try { + if (hasNext()) { String s = new String(this.bookmarkIter.next()); return getBookmark(s); - } catch (final kelondroException e) { + } else { //resetDatabase(); return null; } @@ -907,19 +897,20 @@ public class bookmarksDB { * @param newestFirst newest first, or oldest first? */ public bookmarkComparator(final boolean newestFirst){ - this.newestFirst=newestFirst; + this.newestFirst = newestFirst; } public int compare(final String obj1, final String obj2) { - final Bookmark bm1=getBookmark(obj1); - final Bookmark bm2=getBookmark(obj2); - if(bm1==null || bm2==null) - return 0; //XXX: i think this should not happen? maybe this needs further tracing of the bug - if(this.newestFirst){ - if(bm2.getTimeStamp() - bm1.getTimeStamp() >0) return 1; + final Bookmark bm1 = getBookmark(obj1); + final Bookmark bm2 = getBookmark(obj2); + if (bm1 == null || bm2 == null) { + return 0; //XXX: i think this should not happen? maybe this needs further tracing of the bug + } + if (this.newestFirst){ + if (bm2.getTimeStamp() - bm1.getTimeStamp() >0) return 1; return -1; } - if(bm1.getTimeStamp() - bm2.getTimeStamp() >0) return 1; + if (bm1.getTimeStamp() - bm2.getTimeStamp() > 0) return 1; return -1; } } @@ -933,12 +924,12 @@ public class bookmarksDB { public static class TagComparator implements Comparator, Serializable { /** - * generated serial - */ - private static final long serialVersionUID = 3105057490088903930L; + * generated serial + */ + private static final long serialVersionUID = 3105057490088903930L; - public int compare(final Tag obj1, final Tag obj2){ - return obj1.getTagName().compareTo(obj2.getTagName()); + public int compare(final Tag obj1, final Tag obj2){ + return obj1.getTagName().compareTo(obj2.getTagName()); } } @@ -946,14 +937,18 @@ public class bookmarksDB { public static class TagSizeComparator implements Comparator, Serializable { /** - * generated serial - */ - private static final long serialVersionUID = 4149185397646373251L; + * generated serial + */ + private static final long serialVersionUID = 4149185397646373251L; - public int compare(final Tag obj1, final Tag obj2) { - if (obj1.size() < obj2.size()) return 1; - else if (obj1.getTagName().equals(obj2.getTagName())) return 0; - else return -1; + public int compare(final Tag obj1, final Tag obj2) { + if (obj1.size() < obj2.size()) { + return 1; + } else if (obj1.getTagName().equals(obj2.getTagName())) { + return 0; + } else { + return -1; + } } } diff --git a/source/de/anomic/data/diff.java b/source/de/anomic/data/diff.java index 95309e58f..ccf3c1c86 100644 --- a/source/de/anomic/data/diff.java +++ b/source/de/anomic/data/diff.java @@ -39,42 +39,42 @@ import net.yacy.document.parser.html.CharacterCoding; public class diff { private final ArrayList parts = new ArrayList(); - protected final Object[] o; - protected final Object[] n; + protected final Object[] original; + protected final Object[] changed; /** - * @param o the original String - * @param n the new String + * @param original the original String + * @param changed the new String * @throws NullPointerException if one of the arguments is null */ - public diff(final String o, final String n) { - this(o, n, 1); + public diff(final String original, final String changed) { + this(original, changed, 1); } /** - * @param o the original String - * @param n the new String + * @param original the original String + * @param changed the new String * @param minConsecutive the minimum number of consecutive equal characters in * both Strings. Smaller seperations will only be performed on the end of either * String if needed - * @throws NullPointerException if o or n is + * @throws NullPointerException if original or changed is * null */ - public diff(final String o, final String n, final int minConsecutive) { - if (o == null || n == null) throw new NullPointerException("neither o nor n must be null"); - this.o = new Comparable[o.length()]; - for (int i=0; i 0) ? minConsecutive : 1); } - public diff(final Object[] o, final Object[] n, final int minConsecutive) { - if (o == null || n == null) throw new NullPointerException("neither o nor n must be null"); - this.o = o; - this.n = n; + public diff(final Object[] original, final Object[] changed, final int minConsecutive) { + if (original == null || changed == null) throw new NullPointerException("input Objects must be null"); + this.original = original; + this.changed = changed; parse((minConsecutive > 0) ? minConsecutive : 1); } @@ -102,10 +102,10 @@ public class diff { * C| | | | | | | | | | | | | | | | |#| | * E| | |#| | | | | | | | |#| | |#| | |#| */ - final boolean[][] matrix = new boolean[this.n.length][this.o.length]; - for (int y=0; yObject[] passed to this class on instantiation */ - public Object[] getOriginal() { return this.o; } + public Object[] getOriginal() { return this.original; } /** * @return the new Object[] 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 @@ -177,11 +177,12 @@ public class diff { */ public Part[] getParts() { return this.parts.toArray(new Part[this.parts.size()]); } + @Override public String toString() { final StringBuilder sb = new StringBuilder(this.parts.size() * 20); - for (int j=0; jdeleted

"- STRING"
* */ + @Override public String toString() { return ((this.action == UNCHANGED) ? " " : (this.action == ADDED) ? "+" : "-") + " " + getString(); @@ -246,21 +248,21 @@ public class diff { public static String toHTML(final diff[] diffs) { final StringBuilder sb = new StringBuilder(diffs.length * 60); diff.Part[] ps; - for (int i=0; i\n"); - ps = diffs[i].getParts(); - for (int j=0; j").append(CharacterCoding.unicode2html(ps[j].getString(), true).replaceAll("\n", "
")); + sb.append("\">").append(CharacterCoding.unicode2html(part.getString(), true).replaceAll("\n", "
")); sb.append(""); } sb.append("

"); } - return new String(sb); + return sb.toString(); } } \ No newline at end of file diff --git a/source/de/anomic/data/listManager.java b/source/de/anomic/data/listManager.java index c77af1872..3fa7e7126 100644 --- a/source/de/anomic/data/listManager.java +++ b/source/de/anomic/data/listManager.java @@ -39,6 +39,7 @@ import net.yacy.repository.BlacklistFile; import de.anomic.search.SearchEventCache; import de.anomic.search.Switchboard; +import java.util.List; // The Naming of the functions is a bit strange... @@ -101,17 +102,14 @@ public class listManager { * "listName", false otherwise. */ public static boolean listSetContains(final String setName, final String listName) { - final Set Lists = getListSet(setName); - - return Lists.contains(listName); + return getListSet(setName).contains(listName); } //================general Lists================== public static String getListString(final String filename, final boolean withcomments) { - final File listFile = new File(listsPath ,filename); - return FileUtils.getListString(listFile, withcomments); + return FileUtils.getListString(new File(listsPath ,filename), withcomments); } //================Helper functions for collection conversion================== @@ -142,15 +140,15 @@ public class listManager { * @see listManager#string2vector(String) */ public static ArrayList string2arraylist(final String string){ - ArrayList l; + ArrayList list; if (string != null && string.length() > 0) { - l = new ArrayList(Arrays.asList(string.split(","))); + list = new ArrayList(Arrays.asList(string.split(","))); } else { - l = new ArrayList(); + list = new ArrayList(); } - return l; + return list; } /** @@ -199,12 +197,12 @@ public class listManager { final String supportedBlacklistTypesStr = Blacklist.BLACKLIST_TYPES_STRING; final String[] supportedBlacklistTypes = supportedBlacklistTypesStr.split(","); - final ArrayList blacklistFiles = new ArrayList(supportedBlacklistTypes.length); - for (int i=0; i < supportedBlacklistTypes.length; i++) { + final List blacklistFiles = new ArrayList(supportedBlacklistTypes.length); + for (String supportedBlacklistType : supportedBlacklistTypes) { final BlacklistFile blFile = new BlacklistFile( switchboard.getConfig( - supportedBlacklistTypes[i] + ".BlackLists", switchboard.getConfig("BlackLists.DefaultList", "url.default.black")), - supportedBlacklistTypes[i]); + supportedBlacklistType + ".BlackLists", switchboard.getConfig("BlackLists.DefaultList", "url.default.black")), + supportedBlacklistType); blacklistFiles.add(blFile); } diff --git a/source/de/anomic/data/messageBoard.java b/source/de/anomic/data/messageBoard.java index d8f4d1a37..3dba25512 100644 --- a/source/de/anomic/data/messageBoard.java +++ b/source/de/anomic/data/messageBoard.java @@ -43,13 +43,13 @@ public class messageBoard { private static final int categoryLength = 12; 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 { SimpleFormatter.setTimeZone(TimeZone.getTimeZone("GMT")); } - MapHeap database = null; + private MapHeap database = null; private int sn = 0; public messageBoard(final File path) throws IOException { @@ -93,7 +93,7 @@ public class messageBoard { public class entry { String key; // composed by category and date - Map record; // contains author, target hash, subject and message + Map record; // contains author, target hash, subject and message public entry(final String category, String authorName, String authorHash, @@ -104,20 +104,12 @@ public class messageBoard { if (key.length() > categoryLength) key = key.substring(0, categoryLength); while (key.length() < categoryLength) key += "_"; key += dateString() + snString(); - if ((authorName == null) || (authorName.length() == 0)) authorName = "anonymous"; - record.put("author", authorName); - if ((recName == null) || (recName.length() == 0)) recName = "anonymous"; - record.put("recipient", recName); - if (authorHash == null) authorHash = ""; - record.put("ahash", authorHash); - 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("author", ((authorName == null) || (authorName.length() == 0)) ? authorName : "anonymous"); + record.put("recipient", ((recName == null) || (recName.length() == 0)) ? recName : "anonymous"); + record.put("ahash", (authorHash == null) ? authorHash : ""); + record.put("rhash", (recHash == null) ? recHash : ""); + record.put("subject", (subject == null) ? subject : ""); + record.put("message", (message == null) ? "" : Base64Order.enhancedCoder.encode(message)); record.put("read", "false"); } @@ -147,31 +139,29 @@ public class messageBoard { public String author() { final String a = record.get("author"); if (a == null) return "anonymous"; - return a; + return a; } public String recipient() { final String a = record.get("recipient"); if (a == null) return "anonymous"; - return a; + return a; } public String authorHash() { final String a = record.get("ahash"); - if (a == null) return null; - return a; + return a; } public String recipientHash() { final String a = record.get("rhash"); - if (a == null) return null; - return a; + return a; } public String subject() { final String s = record.get("subject"); if (s == null) return ""; - return s; + return s; } public byte[] message() { @@ -224,9 +214,9 @@ public class messageBoard { public class catIter implements Iterator { - Iterator allIter = null; - String nextKey = null; - String category = ""; + private Iterator allIter = null; + private String nextKey = null; + private String category = ""; public catIter(final String category, final boolean up) throws IOException { this.allIter = database.keys(up, false); @@ -237,7 +227,7 @@ public class messageBoard { public void findNext() { while (allIter.hasNext()) { nextKey = new String(allIter.next()); - if (this.category==null || nextKey.startsWith(this.category)) return; + if (this.category == null || nextKey.startsWith(this.category)) return; } nextKey = null; } diff --git a/source/de/anomic/data/translator.java b/source/de/anomic/data/translator.java index 36cb3e7fd..74bfeee1c 100644 --- a/source/de/anomic/data/translator.java +++ b/source/de/anomic/data/translator.java @@ -39,12 +39,9 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; -import java.util.ArrayList; -import java.util.Enumeration; import java.util.HashMap; import java.util.Hashtable; import java.util.Iterator; -import java.util.Vector; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -53,6 +50,9 @@ import net.yacy.kelondro.util.FileUtils; import net.yacy.kelondro.util.Formatter; import de.anomic.server.serverSwitch; +import java.util.List; +import java.util.Map; +import java.util.Set; /** * Wordlist based translator @@ -60,206 +60,202 @@ import de.anomic.server.serverSwitch; * Uses a Property like file with phrases or single words to translate a string or a file * */ public class translator { - public static String translate(final String source, final Hashtable translationList){ - final Enumeration keys = translationList.keys(); - String result = source; - String key = ""; - while(keys.hasMoreElements()){ - key = keys.nextElement(); - final Pattern pattern = Pattern.compile(key); - final Matcher matcher = pattern.matcher(result); - if (matcher.find()) { - result = matcher.replaceAll(translationList.get(key)); - } else { - //Filename not available, but it will be printed in Log - //after all untranslated Strings as "Translated file: " - if (Log.isFine("TRANSLATOR")) Log.logFine("TRANSLATOR", "Unused String: "+key); - } - } - return result; - } - /** - * Load multiple translationLists from one File. Each List starts with #File: relative/path/to/file - * @param translationFile the File, which contains the Lists - * @return a Hashtable, which contains for each File a Hashtable with translations. - */ - public static Hashtable> loadTranslationsLists(final File translationFile){ - final Hashtable> lists = new Hashtable>(); //list of translationLists for different files. - Hashtable translationList = new Hashtable(); //current Translation Table - - final ArrayList list = FileUtils.getListArray(translationFile); - final Iterator it = list.iterator(); - String line = ""; - String[] splitted; - String forFile=""; + public static String translate(final String source, final Map translationTable){ + final Set keys = translationTable.keySet(); + String result = source; + for (String key : keys){ + final Pattern pattern = Pattern.compile(key); + final Matcher matcher = pattern.matcher(result); + if (matcher.find()) { + result = matcher.replaceAll(translationTable.get(key)); + } else { + //Filename not available, but it will be printed in Log + //after all untranslated Strings as "Translated file: " + if (Log.isFine("TRANSLATOR")) Log.logFine("TRANSLATOR", "Unused String: "+key); + } + } + return result; + } + + /** + * Load multiple translationLists from one File. Each List starts with #File: relative/path/to/file + * @param translationFile the File, which contains the Lists + * @return a Hashtable, which contains for each File a Hashtable with translations. + */ + public static Map> loadTranslationsLists(final File translationFile){ + final Map> lists = new Hashtable>(); //list of translationLists for different files. + Map translationList = new Hashtable(); //current Translation Table - while(it.hasNext()){ - line = it.next(); - if (line.length() == 0 || line.charAt(0) != '#'){ - splitted = line.split("==", 2); - if (splitted.length == 2) { - translationList.put(splitted[0], splitted[1]); - //}else{ //Invalid line - } - } else if (line.startsWith("#File: ")) { - if (!forFile.equals("")){ - lists.put(forFile, translationList); - } - if (line.charAt(6)==' ') { - forFile=line.substring(7); - } else { - forFile=line.substring(6); - } - if (lists.containsKey(forFile)) { - translationList=lists.get(forFile); - } else { - translationList=new Hashtable(); - } - } - } - lists.put(forFile, translationList); - return lists; - } + final List list = FileUtils.getListArray(translationFile); + String forFile = ""; - public static boolean translateFile(final File sourceFile, final File destFile, final File translationFile){ - final Hashtable translationList = loadTranslationsLists(translationFile).get(sourceFile.getName()); - return translateFile(sourceFile, destFile, translationList); - } + for (final String line : list){ + if (line.length() == 0 || line.charAt(0) != '#'){ + String[] split = line.split("==", 2); + if (split.length == 2) { + translationList.put(split[0], split[1]); + //}else{ //Invalid line + } + } else if (line.startsWith("#File: ")) { + if (!forFile.equals("")){ + lists.put(forFile, translationList); + } + if (line.charAt(6) == ' ') { + forFile=line.substring(7); + } else { + forFile=line.substring(6); + } + if (lists.containsKey(forFile)) { + translationList=lists.get(forFile); + } else { + translationList = new Hashtable(); + } + } + } + lists.put(forFile, translationList); + return lists; + } + + public static boolean translateFile(final File sourceFile, final File destFile, final File translationFile){ + return translateFile(sourceFile, destFile, loadTranslationsLists(translationFile).get(sourceFile.getName())); + } - public static boolean translateFile(final File sourceFile, final File destFile, final Hashtable translationList){ + public static boolean translateFile(final File sourceFile, final File destFile, final Map translationList){ - String content = ""; - String line = ""; + StringBuilder content = new StringBuilder(); BufferedReader br = null; - try{ - br = new BufferedReader(new InputStreamReader(new FileInputStream(sourceFile),"UTF-8")); - while( (line = br.readLine()) != null){ - content += line + de.anomic.server.serverCore.CRLF_STRING; - } - br.close(); - }catch(final IOException e){ - return false; - } finally { - if (br!=null)try{br.close();}catch(final Exception e){} + try{ + br = new BufferedReader(new InputStreamReader(new FileInputStream(sourceFile),"UTF-8")); + String line = null; + while( (line = br.readLine()) != null){ + content.append(line).append(de.anomic.server.serverCore.CRLF_STRING); + } + br.close(); + } catch(final IOException e) { + return false; + } finally { + if (br !=null) { + try { + br.close(); + } catch (final Exception e) {} + } } - content = translate(content, translationList); + content = new StringBuilder(translate(content.toString(), translationList)); BufferedWriter bw = null; - try{ - bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFile),"UTF-8")); - bw.write(content); - bw.close(); - }catch(final IOException e){ - return false; - } finally { - if(bw!=null)try{bw.close();}catch(final Exception e){} + try{ + bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFile),"UTF-8")); + bw.write(content.toString()); + bw.close(); + }catch(final IOException e){ + return false; + } finally { + 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){ - final Hashtable> translationLists = loadTranslationsLists(translationFile); - return translateFiles(sourceDir, destDir, baseDir, translationLists, extensions); - } + public static boolean translateFiles(final File sourceDir, final File destDir, final File baseDir, final File translationFile, final String extensions){ + return translateFiles(sourceDir, destDir, baseDir, loadTranslationsLists(translationFile), extensions); + } - public static boolean translateFiles(final File sourceDir, final File destDir, final File baseDir, final Hashtable> translationLists, final String extensions){ - destDir.mkdirs(); - final File[] sourceFiles = sourceDir.listFiles(); - final Vector exts=listManager.string2vector(extensions); + public static boolean translateFiles(final File sourceDir, final File destDir, final File baseDir, final Map> translationLists, final String extensions){ + destDir.mkdirs(); + final File[] sourceFiles = sourceDir.listFiles(); + final List exts = listManager.string2vector(extensions); boolean rightExtension; - Iterator it; String relativePath; - for(int i=0;i dirList=FileUtils.getDirsRecursive(sourceDir, notdir); + final List dirList=FileUtils.getDirsRecursive(sourceDir, notdir); dirList.add(sourceDir); - final Iterator it=dirList.iterator(); - 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())); + for (final File file : dirList){ 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); } } return true; } - public static HashMap langMap(final serverSwitch env) { + public static Map langMap(final serverSwitch env) { final String[] ms = env.getConfig("locale.lang", "").split(","); - final HashMap map = new HashMap(); - int p; - for (int i = 0; i < ms.length; i++) { - p = ms[i].indexOf('/'); + final Map map = new HashMap(); + for (final String element : ms) { + int p = element.indexOf('/'); 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; } public static boolean changeLang(final serverSwitch env, final String langPath, final String lang) { + boolean ret = false; + if ((lang.equals("default")) || (lang.equals("default.lng"))) { env.setConfig("locale.language", "default"); - return true; - } - final String htRootPath = env.getConfig("htRootPath", "htroot"); - 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 - // .lng - //File destDir = new File(env.getRootPath(), htRootPath + "/locale/" + lang.substring(0, lang.length() - 4));// cut - // .lng - final File translationFile = new File(langPath, lang); + ret = true; + } else { + final String htRootPath = env.getConfig("htRootPath", "htroot"); + 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 + // .lng + //File destDir = new File(env.getRootPath(), htRootPath + "/locale/" + lang.substring(0, lang.length() - 4));// cut + // .lng + final File translationFile = new File(langPath, lang); - //if (translator.translateFiles(sourceDir, destDir, translationFile, "html")) { - if(translator.translateFilesRecursive(sourceDir, destDir, - translationFile, "html,template,inc", "locale")){ - env.setConfig("locale.language", lang.substring(0, lang.length() - 4)); - Formatter.setLocale(env.getConfig("locale.language", "en")); - try { - final BufferedWriter bw = new BufferedWriter(new PrintWriter(new FileWriter(new File(destDir, "version")))); - bw.write(env.getConfig("svnRevision", "Error getting Version")); - bw.close(); - } catch (final IOException e) { - // Error + //if (translator.translateFiles(sourceDir, destDir, translationFile, "html")) { + if (translator.translateFilesRecursive(sourceDir, destDir, translationFile, "html,template,inc", "locale")) { + env.setConfig("locale.language", lang.substring(0, lang.length() - 4)); + Formatter.setLocale(env.getConfig("locale.language", "en")); + try { + final BufferedWriter bw = new BufferedWriter(new PrintWriter(new FileWriter(new File(destDir, "version")))); + bw.write(env.getConfig("svnRevision", "Error getting Version")); + bw.close(); + } catch (final IOException e) { + // Error + } + ret = true; } - return true; } - return false; + return ret; } } diff --git a/source/de/anomic/data/userDB.java b/source/de/anomic/data/userDB.java index 2cee3c711..48c6cff1e 100644 --- a/source/de/anomic/data/userDB.java +++ b/source/de/anomic/data/userDB.java @@ -49,13 +49,13 @@ import de.anomic.http.server.RequestHeader; public final class userDB { - public static final int USERNAME_MAX_LENGTH = 128; - public static final int USERNAME_MIN_LENGTH = 4; + private static final int USERNAME_MAX_LENGTH = 128; + private static final int USERNAME_MIN_LENGTH = 4; - MapHeap userTable; + private MapHeap userTable; private final File userTableFile; - HashMap ipUsers = new HashMap(); - HashMap cookieUsers = new HashMap(); + private final Map ipUsers = new HashMap(); + private final Map cookieUsers = new HashMap(); public userDB(final File userTableFile) throws IOException { this.userTableFile = userTableFile; @@ -104,7 +104,7 @@ public final class userDB { return new Entry(userName, record); } - public Entry createEntry(final String userName, final HashMap userProps) throws IllegalArgumentException{ + public Entry createEntry(final String userName, final Map userProps) throws IllegalArgumentException{ final Entry entry = new Entry(userName,userProps); return entry; } @@ -120,42 +120,34 @@ public final class userDB { } /** - * use a ProxyAuth String to authenticate a user - * @param auth a base64 Encoded String, which contains "username:pw". - */ - public Entry proxyAuth(String auth) { - if(auth==null) + * use a ProxyAuth String to authenticate a user + * @param auth a base64 Encoded String, which contains "username:pw". + */ + public Entry proxyAuth(final String auth) { + if(auth == null) { return null; - Entry entry=null; - auth=auth.trim().substring(6); - - try{ - auth=Base64Order.standardCoder.decodeString(auth); - }catch(final RuntimeException e){} //no valid Base64 - final String[] tmp=auth.split(":"); + } + Entry entry = null; + + final String[] tmp= Base64Order.standardCoder.decodeString(auth.trim().substring(6)).split(":"); if(tmp.length == 2){ - 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; - } - public Entry getUser(final RequestHeader header){ - return getUser(header.get(RequestHeader.AUTHORIZATION), header.getHeaderCookies()); + entry = this.passwordAuth(tmp[0], tmp[1]); } - public Entry getUser(final String auth, final String cookies){ + return entry; + } + + public Entry getUser(final RequestHeader header){ + return getUser(header.get(RequestHeader.AUTHORIZATION), header.getHeaderCookies()); + } + + public Entry getUser(final String auth, final String cookies){ Entry entry=null; - if(auth != null) + if(auth != null) { entry=proxyAuth(auth); - if(entry == null) + } + if(entry == null) { entry=cookieAuth(cookies); + } return entry; } /** @@ -166,33 +158,36 @@ public final class userDB { */ public boolean hasAdminRight(final String auth, final String cookies) { final Entry entry = getUser(auth, cookies); - if (entry != null) - return entry.hasAdminRight(); + if (entry != null) { + return entry.hasRight(Entry.ADMIN_RIGHT); + } // else if(entry != null && cookieAdminAuth(cookies)) // return entry.hasAdminRight(); - else + else { return false; + } } - /** - * use a ProxyAuth String to authenticate a user and save the ip/username for ipAuth - * @param auth a base64 Encoded String, which contains "username:pw". - * @param ip an ip. - */ - public Entry proxyAuth(final String auth, final String ip){ - final Entry entry=proxyAuth(auth); - if(entry == null){ - return null; - } + /** + * use a ProxyAuth String to authenticate a user and save the ip/username for ipAuth + * @param auth a base64 Encoded String, which contains "username:pw". + * @param ip an ip. + */ + public Entry proxyAuth(final String auth, final String ip) { + final Entry entry=proxyAuth(auth); + if(entry == null){ + return null; + } entry.updateLastAccess(false); this.ipUsers.put(ip, entry.getUserName()); return entry; - } - /** - * authenticate a user by ip, if he had used proxyAuth in the last 10 Minutes - * @param ip the IP of the User - */ - public Entry ipAuth(final String ip) { + } + + /** + * authenticate a user by ip, if he had used proxyAuth in the last 10 Minutes + * @param ip the IP of the User + */ + public Entry ipAuth(final String ip) { if(this.ipUsers.containsKey(ip)){ final String user=this.ipUsers.get(ip); final Entry entry=this.getEntry(user); @@ -203,10 +198,10 @@ public final class userDB { return entry; //All OK } return null; - } + } public Entry passwordAuth(final String user, final String password) { - final Entry entry=this.getEntry(user); + final Entry entry = this.getEntry(user); final String md5pwd; if (entry != null && (md5pwd = entry.getMD5EncodedUserPwd()) != null && md5pwd.equals(Digest.encodeMD5Hex(user+":"+password))) { if (entry.isLoggedOut()){ @@ -221,6 +216,7 @@ public final class userDB { } return null; } + public Entry passwordAuth(final String user, final String password, final String ip){ final Entry entry=passwordAuth(user, password); if(entry == null){ @@ -230,45 +226,51 @@ public final class userDB { this.ipUsers.put(ip, entry.getUserName()); //XXX: This is insecure. TODO: use cookieauth return entry; } + public Entry md5Auth(final String user, final String md5) { final Entry entry=this.getEntry(user); if (entry != null && entry.getMD5EncodedUserPwd().equals(md5)) { if (entry.isLoggedOut()){ try { - entry.setProperty(Entry.LOGGED_OUT, "false"); + entry.setProperty(Entry.LOGGED_OUT, "false"); } catch (final Exception e) { Log.logException(e); } return null; } - return entry; + return entry; } - return null; + return null; } + public Entry cookieAuth(final String cookieString){ - final String token=getLoginToken(cookieString); - if(cookieUsers.containsKey(token)){ - final Object entry=cookieUsers.get(token); - if(entry instanceof Entry) //String would mean static Admin + final String token = getLoginToken(cookieString); + if (cookieUsers.containsKey(token)) { + final Object entry = cookieUsers.get(token); + if (entry instanceof Entry) //String would mean static Admin return (Entry)entry; } return null; } + public boolean cookieAdminAuth(final String cookieString){ - final String token=getLoginToken(cookieString); - if(cookieUsers.containsKey(token)){ - final Object entry=cookieUsers.get(token); - if(entry instanceof String && entry.equals("admin")) + final String token = getLoginToken(cookieString); + if (cookieUsers.containsKey(token)) { + final Object entry = cookieUsers.get(token); + if (entry instanceof String && entry.equals("admin")) { return true; + } } return false; } + public String getCookie(final Entry entry){ final Random r = new Random(); final String token = Long.toString(Math.abs(r.nextLong()), 36); cookieUsers.put(token, entry); return token; } + public String getAdminCookie(){ final Random r = new Random(); final String token = Long.toString(Math.abs(r.nextLong()), 36); @@ -277,16 +279,16 @@ public final class userDB { } public static String getLoginToken(final String cookies){ - final String[] cookie=cookies.split(";"); //TODO: Mozilla uses "; " - String[] pair; - for(int i=0;i mem; - String userName; - private final Calendar oldDate, newDate; + private Map mem; + private String userName; + private final Calendar oldDate, newDate; public Entry(final String userName, final Map mem) throws IllegalArgumentException { if ((userName == null) || (userName.length() == 0)) @@ -340,14 +342,16 @@ public final class userDB { this.userName = userName.trim(); 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(); else this.mem = mem; - if (mem == null || !mem.containsKey(AUTHENTICATION_METHOD))this.mem.put(AUTHENTICATION_METHOD,"yacy"); - this.oldDate=Calendar.getInstance(); - this.newDate=Calendar.getInstance(); + if (mem == null || !mem.containsKey(AUTHENTICATION_METHOD)) { + this.mem.put(AUTHENTICATION_METHOD,"yacy"); + } + this.oldDate=Calendar.getInstance(); + this.newDate=Calendar.getInstance(); } public String getUserName() { @@ -433,7 +437,7 @@ public final class userDB { public int surfRight(){ final long timeUsed=this.updateLastAccess(true); - if (!this.hasProxyRight()) + if (!this.hasRight(Entry.PROXY_RIGHT)) return PROXY_NORIGHT; if(! (this.getTimeLimit() <= 0 || ( timeUsed < this.getTimeLimit())) ){ //no timelimit or timelimit not reached @@ -441,13 +445,15 @@ public final class userDB { } return PROXY_ALLOK; } - public boolean canSurf(){ - if(this.surfRight()==PROXY_ALLOK) return true; - return false; - } + + public boolean canSurf(){ + return (this.surfRight() == PROXY_ALLOK) ? true : false; + } + 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) { if (timeStamp < 0) throw new IllegalArgumentException(); @@ -458,25 +464,25 @@ public final class userDB { if ((lastAccess == null)||((lastAccess != null)&&(timeStamp-lastAccess.longValue()>=1000*60))) { //1 minute //this.mem.put(TIME_USED,Long.toString(newTimeUsed = ++oldTimeUsed)); newTimeUsed++; - if(lastAccess != null){ - this.oldDate.setTime(new Date(lastAccess.longValue())); - this.newDate.setTime(new Date(System.currentTimeMillis())); - if( - this.oldDate.get(Calendar.DAY_OF_MONTH) != this.newDate.get(Calendar.DAY_OF_MONTH) || - this.oldDate.get(Calendar.MONTH) != this.newDate.get(Calendar.MONTH) || - this.oldDate.get(Calendar.YEAR) != this.newDate.get(Calendar.YEAR) - ){ //new Day, reset time - newTimeUsed=0; - } + if(lastAccess != null){ + this.oldDate.setTime(new Date(lastAccess.longValue())); + this.newDate.setTime(new Date(System.currentTimeMillis())); + if( + this.oldDate.get(Calendar.DAY_OF_MONTH) != this.newDate.get(Calendar.DAY_OF_MONTH) || + this.oldDate.get(Calendar.MONTH) != this.newDate.get(Calendar.MONTH) || + this.oldDate.get(Calendar.YEAR) != this.newDate.get(Calendar.YEAR) + ){ //new Day, reset time + newTimeUsed=0; + } }else{ //no access so far - newTimeUsed=0; - } - this.mem.put(TIME_USED,Long.toString(newTimeUsed)); - this.mem.put(LAST_ACCESS,Long.toString(timeStamp)); //update Timestamp + newTimeUsed=0; + } + this.mem.put(TIME_USED,Long.toString(newTimeUsed)); + this.mem.put(LAST_ACCESS,Long.toString(timeStamp)); //update Timestamp } - }else{ - this.mem.put(LAST_ACCESS,Long.toString(timeStamp)); //update Timestamp - } + } else { + this.mem.put(LAST_ACCESS,Long.toString(timeStamp)); //update Timestamp + } try { userDB.this.userTable.put(getUserName(), this.mem); @@ -503,7 +509,7 @@ public final class userDB { return (this.mem.containsKey(propName)?this.mem.get(propName):defaultValue); } public boolean hasRight(final String rightName){ - return (this.mem.containsKey(rightName)?this.mem.get(rightName).equals("true"):false); + return (this.mem.containsKey(rightName)?this.mem.get(rightName).equals("true"):false); } /** * @deprecated use hasRight(UPLOAD_RIGHT) instead @@ -554,38 +560,44 @@ public final class userDB { public boolean hasBookmarkRight() { return this.hasRight(BOOKMARK_RIGHT); } + public boolean isLoggedOut(){ return (this.mem.containsKey(LOGGED_OUT)?this.mem.get(LOGGED_OUT).equals("true"):false); } + public void logout(final String ip, final String logintoken){ logout(ip); if(cookieUsers.containsKey(logintoken)){ cookieUsers.remove(logintoken); } } + public void logout(final String ip) { try { - setProperty(LOGGED_OUT, "true"); - if (ipUsers.containsKey(ip)){ - ipUsers.remove(ip); - } + setProperty(LOGGED_OUT, "true"); + if (ipUsers.containsKey(ip)){ + ipUsers.remove(ip); + } } catch (final Exception e) { Log.logException(e); } } + public void logout(){ - logout("xxxxxx"); + logout("xxxxxx"); } + + @Override public String toString() { 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) { 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.nextEntry = null; } + public boolean hasNext() { try { return this.userIter.hasNext(); @@ -617,6 +630,7 @@ public final class userDB { return false; } } + public Entry next() { try { return getEntry(new String(this.userIter.next())); @@ -625,6 +639,7 @@ public final class userDB { return null; } } + public void remove() { // if (this.nextEntry != null) { // try { @@ -634,6 +649,7 @@ public final class userDB { // resetDatabase(); // } // } + throw new UnsupportedOperationException("Method not implemented yet."); } }