diff --git a/htroot/Crawler_p.java b/htroot/Crawler_p.java index b91376d2e..929dd4482 100644 --- a/htroot/Crawler_p.java +++ b/htroot/Crawler_p.java @@ -27,6 +27,7 @@ import java.io.File; import java.io.FileInputStream; +import java.io.IOException; import java.io.Writer; import java.net.MalformedURLException; import java.util.Date; @@ -45,6 +46,7 @@ import net.yacy.cora.services.federated.yacy.CacheStrategy; import net.yacy.document.parser.html.ContentScraper; import net.yacy.document.parser.html.TransformerWriter; import net.yacy.kelondro.data.meta.DigestURI; +import net.yacy.kelondro.index.RowSpaceExceededException; import net.yacy.kelondro.logging.Log; import net.yacy.kelondro.util.FileUtils; import net.yacy.peers.NewsPool; @@ -60,6 +62,7 @@ import de.anomic.data.BookmarkHelper; import de.anomic.data.BookmarksDB; import de.anomic.data.ListManager; import de.anomic.data.WorkTables; +import de.anomic.data.ymark.YMarkEntry; import de.anomic.server.serverObjects; import de.anomic.server.serverSwitch; @@ -318,7 +321,7 @@ public class Crawler_p { // get a scraper to get the title final ContentScraper scraper = sb.loader.parseResource(url, CacheStrategy.IFFRESH); final String title = scraper == null ? url.toNormalform(true, true) : scraper.getTitle(); - //final String description = scraper.getDescription(); + final String description = scraper.getDescription(); // stack url sb.crawler.removePassive(crawlingStartURL.hash()); // if there is an old entry, delete it @@ -368,17 +371,34 @@ public class Crawler_p { if (kk.length() > 0) tags.add(kk); } } - //if ("on".equals(post.get("createBookmark","off"))) { + String tagStr = tags.toString(); + if (tagStr.length() > 2 && tagStr.startsWith("[") && tagStr.endsWith("]")) tagStr = tagStr.substring(1, tagStr.length() - 2); + // we will create always a bookmark to use this to track crawled hosts + final YMarkEntry bmk = new YMarkEntry(); + bmk.put(YMarkEntry.BOOKMARK.URL.key(), url.toNormalform(true, false)); + bmk.put(YMarkEntry.BOOKMARK.TITLE.key(), title); + bmk.put(YMarkEntry.BOOKMARK.DESC.key(), description); + bmk.put(YMarkEntry.BOOKMARK.PUBLIC.key(), "false"); + bmk.put(YMarkEntry.BOOKMARK.TAGS.key(), tagStr); + bmk.put(YMarkEntry.BOOKMARK.FOLDERS.key(), "/crawlStart"); + + try { + sb.tables.bookmarks.addBookmark("admin", bmk, false, false); + } catch (final IOException e) { + Log.logException(e); + } catch (final RowSpaceExceededException e) { + } + final BookmarksDB.Bookmark bookmark = sb.bookmarksDB.createBookmark(crawlingStart, "admin"); - if (bookmark != null) { - bookmark.setProperty(BookmarksDB.Bookmark.BOOKMARK_TITLE, title /*post.get("bookmarkTitle", crawlingStart)*/); - bookmark.setOwner("admin"); - bookmark.setPublic(false); - bookmark.setTags(tags, true); - sb.bookmarksDB.saveBookmark(bookmark); - } - //} + if (bookmark != null) { + bookmark.setProperty(BookmarksDB.Bookmark.BOOKMARK_TITLE, title); + bookmark.setOwner("admin"); + bookmark.setPublic(false); + bookmark.setTags(tags, true); + sb.bookmarksDB.saveBookmark(bookmark); + } + // liftoff! prop.put("info", "8");//start msg prop.putHTML("info_crawlingURL", post.get("crawlingURL")); diff --git a/htroot/api/getpageinfo_p.java b/htroot/api/getpageinfo_p.java index c763d41a8..7f99b8d28 100755 --- a/htroot/api/getpageinfo_p.java +++ b/htroot/api/getpageinfo_p.java @@ -106,6 +106,7 @@ public class getpageinfo_p { final StringBuilder filter = new StringBuilder(uris.size() * 40); count = 0; for (final MultiProtocolURI uri: uris) { + if (uri == null) continue; links.append(';').append(uri.toNormalform(true, false)); filter.append('|').append(uri.getProtocol()).append("://").append(uri.getHost()).append(".*"); prop.putXML("links_" + count + "_link", uri.toNormalform(true, false)); diff --git a/htroot/compare_yacy.java b/htroot/compare_yacy.java index d11c26ed3..60e9c617d 100755 --- a/htroot/compare_yacy.java +++ b/htroot/compare_yacy.java @@ -9,7 +9,7 @@ // $LastChangedBy$ // // LICENSE -// +// // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or @@ -24,31 +24,31 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -import de.anomic.server.serverObjects; -import de.anomic.server.serverSwitch; -import de.anomic.server.servletProperties; - import java.util.HashMap; import java.util.Map; import net.yacy.cora.protocol.RequestHeader; import net.yacy.search.Switchboard; +import de.anomic.server.serverObjects; +import de.anomic.server.serverSwitch; +import de.anomic.server.servletProperties; public class compare_yacy { - - private static final String defaultsearch = "YaCy"; - private static final String[] order = {defaultsearch, "YaCy (local)", "bing.com", - "google.de", "google.com", "scroogle.org", + + private static final String defaultsearchL = "YaCy"; + private static final String defaultsearchR = "google.com"; + private static final String[] order = {defaultsearchL, "YaCy (local)", "bing.com", + "google.de", defaultsearchR, "scroogle.org", "metager.de", "metager2.de (web)", "metager2.de (international)", "yahoo.com", "romso.de", "search.live.com", "Wikipedia English", "Wikipedia Deutsch", "Sciencenet", "dbpedia", "wolfram alpha", "OAIster@OCLC", "oai.yacy.net"}; private static final Map searchengines = new HashMap(); static { - searchengines.put(defaultsearch, "yacysearch.html?display=2&verify=true&resource=global&query="); - searchengines.put("YaCy (local)", "yacysearch.html?display=2&verify=true&resource=local&query="); + searchengines.put(defaultsearchL, "yacysearch.html?display=2&resource=global&query="); + searchengines.put("YaCy (local)", "yacysearch.html?display=2&resource=local&query="); searchengines.put("bing.com", "http://www.bing.com/search?q="); - searchengines.put("google.de", "http://www.google.de/search?q="); - searchengines.put("google.com", "http://www.google.com/search?q="); + searchengines.put("google.de", "http://www.google.de/#fp=1&q="); + searchengines.put("google.com", "http://www.google.com/#fp=1&q="); searchengines.put("scroogle.org", "http://www.scroogle.org/cgi-bin/nbbw.cgi?Gw="); searchengines.put("metager.de", "http://www.metager.de/meta/cgi-bin/meta.ger1?eingabe="); searchengines.put("metager2.de (web)", "http://www.metager2.de/search.php?ses=web&q="); @@ -63,18 +63,18 @@ public class compare_yacy { searchengines.put("OAIster@OCLC", "http://oaister.worldcat.org/search?q="); searchengines.put("oai.yacy.net", "http://oai.yacy.net/yacysearch.html?verify=true&resource=local&nav=all&display=2&meanCount=5&query="); } - + public static serverObjects respond(final RequestHeader header, final serverObjects post, final serverSwitch env) { final Switchboard sb = (Switchboard) env; final boolean authenticated = sb.adminAuthenticated(header) >= 2; final int display = ((post == null) || (!authenticated)) ? 0 : post.getInt("display", 0); final servletProperties prop = new servletProperties(); - + prop.put("display", display); - - String default_left = sb.getConfig("compare_yacy.left", defaultsearch); - String default_right = sb.getConfig("compare_yacy.right", defaultsearch); - + + String default_left = sb.getConfig("compare_yacy.left", defaultsearchL); + String default_right = sb.getConfig("compare_yacy.right", defaultsearchR); + if (post != null) { if (searchengines.get(post.get("left", default_left)) != null) { default_left = post.get("left", default_left); @@ -85,7 +85,7 @@ public class compare_yacy { sb.setConfig("compare_yacy.right", default_right); } } - + prop.put("searchengines", order.length); String name; for (int i = 0; i < order.length; i++) { @@ -97,16 +97,16 @@ public class compare_yacy { prop.putHTML("search_left", searchengines.get(default_left)); prop.putHTML("search_right", searchengines.get(default_right)); - + if (post == null || post.get("query", "").length() == 0) { prop.put("search", 0); prop.put("search_query", ""); return prop; } - + prop.put("search", 1); prop.putHTML("search_query", post.get("query", "")); - + // return rewrite properties return prop; } diff --git a/source/de/anomic/data/BookmarkHelper.java b/source/de/anomic/data/BookmarkHelper.java index 8007e7d87..6119978d2 100644 --- a/source/de/anomic/data/BookmarkHelper.java +++ b/source/de/anomic/data/BookmarkHelper.java @@ -1,4 +1,4 @@ -// BookmarkHelper.java +// BookmarkHelper.java // ------------------------------------- // part of YACY // (C) by Michael Peter Christen; mc@yacy.net @@ -30,29 +30,22 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.io.Writer; +import java.net.MalformedURLException; import java.text.ParseException; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; +import java.util.Map.Entry; import java.util.Properties; import java.util.Set; import java.util.TreeSet; -import java.util.Map.Entry; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; -import org.w3c.dom.Document; -import org.w3c.dom.NamedNodeMap; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; - -import de.anomic.data.BookmarksDB.Bookmark; -import de.anomic.data.BookmarksDB.Tag; import net.yacy.cora.date.ISO8601Formatter; import net.yacy.cora.document.ASCII; import net.yacy.cora.document.MultiProtocolURI; @@ -64,10 +57,19 @@ import net.yacy.kelondro.data.word.Word; import net.yacy.kelondro.logging.Log; import net.yacy.kelondro.util.FileUtils; +import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +import de.anomic.data.BookmarksDB.Bookmark; +import de.anomic.data.BookmarksDB.Tag; + public class BookmarkHelper { public static String cleanTagsString(String tagsString) { - + // get rid of heading, trailing and double commas since they are useless while (tagsString.length() > 0 && tagsString.charAt(0) == ',') { tagsString = tagsString.substring(1); @@ -89,15 +91,15 @@ public class BookmarkHelper { tagsString = tagsString.replaceAll("//", "/"); } // space characters following a comma are removed - tagsString = tagsString.replaceAll(",\\s+", ","); - + tagsString = tagsString.replaceAll(",\\s+", ","); + return tagsString; } - + /** * returns an object of type String that contains a tagHash - * @param tagName an object of type String with the name of the tag. + * @param tagName an object of type String with the name of the tag. * tagName is converted to lower case before hash is generated! */ public static String tagHash(final String tagName){ @@ -108,30 +110,30 @@ public class BookmarkHelper { return UTF8.String(Word.word2hash(user+":"+tagName.toLowerCase())); } */ - + // -------------------------------------- // bookmarksDB's Import/Export functions // -------------------------------------- - - public static int importFromBookmarks(BookmarksDB db, final DigestURI baseURL, final String input, final String tag, final boolean importPublic){ + + public static int importFromBookmarks(final BookmarksDB db, final DigestURI baseURL, final String input, final String tag, final boolean importPublic){ try { // convert string to input stream final ByteArrayInputStream byteIn = new ByteArrayInputStream(UTF8.getBytes(input)); final InputStreamReader reader = new InputStreamReader(byteIn,"UTF-8"); - + // import stream return importFromBookmarks(db, baseURL, reader, tag, importPublic); - } catch (final UnsupportedEncodingException e) { + } catch (final UnsupportedEncodingException e) { return 0; - } + } } - - private static int importFromBookmarks(BookmarksDB db, final DigestURI baseURL, final InputStreamReader input, final String tag, final boolean importPublic){ - + + private static int importFromBookmarks(final BookmarksDB db, final DigestURI baseURL, final InputStreamReader input, final String tag, final boolean importPublic){ + int importCount = 0; - + Map links = new HashMap(); String title; MultiProtocolURI url; @@ -139,12 +141,12 @@ public class BookmarkHelper { final Set tags=ListManager.string2set(tag); //this allow multiple default tags try { //load the links - final ContentScraper scraper = new ContentScraper(baseURL); + final ContentScraper scraper = new ContentScraper(baseURL); //OutputStream os = new htmlFilterOutputStream(null, scraper, null, false); final Writer writer= new TransformerWriter(null,null,scraper, null, false); FileUtils.copy(input,writer); writer.close(); - links = scraper.getAnchors(); + links = scraper.getAnchors(); } catch (final IOException e) { Log.logWarning("BOOKMARKS", "error during load of links: "+ e.getClass() +" "+ e.getMessage());} for (final Entry link: links.entrySet()) { url = link.getKey(); @@ -153,28 +155,28 @@ public class BookmarkHelper { if ("".equals(title)) {//cannot be displayed title = url.toString(); } - bm = db.new Bookmark(url.toString()); + bm = db.new Bookmark(new DigestURI(url)); bm.setProperty(Bookmark.BOOKMARK_TITLE, title); bm.setTags(tags); bm.setPublic(importPublic); db.saveBookmark(bm); - + importCount++; } - + return importCount; } - - - public static int importFromXML(BookmarksDB db, final String input, final boolean importPublic){ + + + public static int importFromXML(final BookmarksDB db, final String input, final boolean importPublic){ // convert string to input stream final ByteArrayInputStream byteIn = new ByteArrayInputStream(UTF8.getBytes(input)); - + // import stream - return importFromXML(db, byteIn,importPublic); + return importFromXML(db, byteIn,importPublic); } - - private static int importFromXML(BookmarksDB db, final InputStream input, final boolean importPublic){ + + private static int importFromXML(final BookmarksDB db, final InputStream input, final boolean importPublic){ final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setNamespaceAware(false); @@ -182,15 +184,15 @@ public class BookmarkHelper { final DocumentBuilder builder = factory.newDocumentBuilder(); final Document doc = builder.parse(input); return parseXMLimport(db, doc, importPublic); - } catch (final ParserConfigurationException e) { + } catch (final ParserConfigurationException e) { } catch (final SAXException e) { } catch (final IOException e) { } return 0; - + } - - private static int parseXMLimport(BookmarksDB db, final Node doc, final boolean importPublic){ + + private static int parseXMLimport(final BookmarksDB db, final Node doc, final boolean importPublic){ int importCount = 0; if ("post".equals(doc.getNodeName())) { final NamedNodeMap attributes = doc.getAttributes(); @@ -198,7 +200,12 @@ public class BookmarkHelper { if("".equals(url)){ return 0; } - final Bookmark bm = db.new Bookmark(url); + Bookmark bm; + try { + bm = db.new Bookmark(url); + } catch (final MalformedURLException e1) { + return 0; + } String tagsString = ""; String title = ""; String description = ""; @@ -216,7 +223,7 @@ public class BookmarkHelper { time = attributes.getNamedItem("time").getNodeValue(); } Set tags = new HashSet(); - + if (title != null) { bm.setProperty(Bookmark.BOOKMARK_TITLE, title); } @@ -225,13 +232,13 @@ public class BookmarkHelper { } bm.setTags(tags, true); if(time != null){ - + Date parsedDate = null; try { parsedDate = ISO8601Formatter.FORMATTER.parse(time); } catch (final ParseException e) { parsedDate = new Date(); - } + } bm.setTimeStamp(parsedDate.getTime()); } if(description!=null){ @@ -239,7 +246,7 @@ public class BookmarkHelper { } bm.setPublic(importPublic); db.saveBookmark(bm); - + importCount++; } final NodeList children=doc.getChildNodes(); @@ -248,29 +255,29 @@ public class BookmarkHelper { importCount += parseXMLimport(db, children.item(i), importPublic); } } - + return importCount; } - - public static Iterator getFolderList(final String root, Iterator tagIterator) { - + + public static Iterator getFolderList(final String root, final Iterator tagIterator) { + final Set folders = new TreeSet(); String path = ""; Tag tag; - + while (tagIterator.hasNext()) { - tag=tagIterator.next(); + tag=tagIterator.next(); if (tag.getFriendlyName().startsWith(("/".equals(root) ? root : root+"/"))) { path = tag.getFriendlyName(); - path = BookmarkHelper.cleanTagsString(path); + path = BookmarkHelper.cleanTagsString(path); while(path.length() > 0 && !path.equals(root)){ - folders.add(path); + folders.add(path); path = path.replaceAll("(/.[^/]*$)", ""); // create missing folders in path - } + } } } if (!"/".equals(root)) { folders.add(root); } folders.add("\uffff"); - return folders.iterator(); + return folders.iterator(); } } diff --git a/source/de/anomic/data/BookmarksDB.java b/source/de/anomic/data/BookmarksDB.java index 630e3e559..f4f6ef039 100644 --- a/source/de/anomic/data/BookmarksDB.java +++ b/source/de/anomic/data/BookmarksDB.java @@ -1,4 +1,4 @@ -//bookmarksDB.java +//bookmarksDB.java //------------------------------------- //part of YACY //(C) by Michael Peter Christen; mc@yacy.net @@ -45,7 +45,7 @@ import net.yacy.kelondro.logging.Log; import net.yacy.kelondro.order.NaturalOrder; public class BookmarksDB { - + // ------------------------------------ // Declaration of Class-Attributes // ------------------------------------ @@ -55,27 +55,27 @@ public class BookmarksDB { private final static int SHOW_ALL = -1; // bookmarks - private MapHeap bookmarks; - + private final MapHeap bookmarks; + // tags - private ConcurrentHashMap tags; - - private BookmarkDate dates; - + private final ConcurrentHashMap tags; + + private final BookmarkDate dates; + // ------------------------------------ // bookmarksDB's class constructor // ------------------------------------ public BookmarksDB(final File bookmarksFile, final File datesFile) throws IOException { - + // bookmarks bookmarksFile.getParentFile().mkdirs(); //this.bookmarksTable = new kelondroMap(kelondroDyn.open(bookmarksFile, bufferkb * 1024, preloadTime, 12, 256, '_', true, false)); //this.bookmarksTable = new MapView(BLOBTree.toHeap(bookmarksFile, true, true, 12, 256, '_', NaturalOrder.naturalOrder, bookmarksFileNew), 1000, '_'); this.bookmarks = new MapHeap(bookmarksFile, 12, NaturalOrder.naturalOrder, 1024 * 64, 1000, ' '); - + // tags - tags = new ConcurrentHashMap(); + this.tags = new ConcurrentHashMap(); Log.logInfo("BOOKMARKS", "started init of tags from bookmarks.db..."); final Iterator it = new bookmarkIterator(true); Bookmark bookmark; @@ -95,7 +95,7 @@ public class BookmarksDB { putTag(tag); } } - Log.logInfo("BOOKMARKS", "finished init " + this.tags.size() + " tags using your "+bookmarks.size()+" bookmarks."); + Log.logInfo("BOOKMARKS", "finished init " + this.tags.size() + " tags using your "+this.bookmarks.size()+" bookmarks."); // dates final boolean datesExisted = datesFile.exists(); @@ -107,56 +107,61 @@ public class BookmarksDB { // ----------------------------------------------------- // bookmarksDB's functions for 'destructing' the class // ----------------------------------------------------- - + public void close(){ - bookmarks.close(); - tags.clear(); - dates.close(); + this.bookmarks.close(); + this.tags.clear(); + this.dates.close(); } - + // ----------------------------------------------------------- // bookmarksDB's functions for bookmarksTable / bookmarkCache // ----------------------------------------------------------- - + public Bookmark createBookmark(final String url, final String user){ if (url == null || url.length() == 0) return null; - final Bookmark bk = new Bookmark(url); - bk.setOwner(user); - return (bk.getUrlHash() == null || bk.toMap() == null) ? null : bk; + Bookmark bk; + try { + bk = new Bookmark(url); + bk.setOwner(user); + return (bk.getUrlHash() == null || bk.toMap() == null) ? null : bk; + } catch (final MalformedURLException e) { + return null; + } } - + // returning the number of bookmarks public int bookmarksSize(){ - return bookmarks.size(); + return this.bookmarks.size(); } - + // adding a bookmark to the bookmarksDB public void saveBookmark(final Bookmark bookmark){ try { - bookmarks.insert(ASCII.getBytes(bookmark.getUrlHash()), bookmark.entry); + this.bookmarks.insert(ASCII.getBytes(bookmark.getUrlHash()), bookmark.entry); } catch (final Exception e) { Log.logException(e); } } public String addBookmark(final Bookmark bookmark){ - this.saveBookmark(bookmark); + saveBookmark(bookmark); return bookmark.getUrlHash(); - + } - + public Bookmark getBookmark(final String urlHash){ try { - final Map map = bookmarks.get(ASCII.getBytes(urlHash)); + final Map map = this.bookmarks.get(ASCII.getBytes(urlHash)); return (map == null) ? null : new Bookmark(map); } catch (final IOException e) { Log.logException(e); return null; - } catch (RowSpaceExceededException e) { + } catch (final RowSpaceExceededException e) { Log.logException(e); return null; } } - + public boolean removeBookmark(final String urlHash){ final Bookmark bookmark = getBookmark(urlHash); if (bookmark == null) return false; //does not exist @@ -173,19 +178,19 @@ public class BookmarksDB { Bookmark b; try { b = getBookmark(urlHash); - bookmarks.delete(ASCII.getBytes(urlHash)); + this.bookmarks.delete(ASCII.getBytes(urlHash)); } catch (final IOException e) { b = null; } return b != null; } - + public Iterator getBookmarksIterator(final boolean priv) { final TreeSet set=new TreeSet(new bookmarkComparator(true)); Iterator it; try { it = new bookmarkIterator(true); - } catch (IOException e) { + } catch (final IOException e) { Log.logException(e); return set.iterator(); } @@ -199,7 +204,7 @@ public class BookmarksDB { } return set.iterator(); } - + public Iterator getBookmarksIterator(final String tagName, final boolean priv){ final TreeSet set=new TreeSet(new bookmarkComparator(true)); final String tagHash=BookmarkHelper.tagHash(tagName); @@ -222,16 +227,16 @@ public class BookmarksDB { } return set.iterator(); } - + // ------------------------------------------------- // bookmarksDB's functions for tagsTable / tagCache // ------------------------------------------------- - + // returning the number of tags public int tagsSize(){ return this.tags.size(); } - + /** * retrieve an object of type Tag from the the tagCache, if object is not cached return loadTag(hash) * @param hash an object of type String, containing a tagHash @@ -239,7 +244,7 @@ public class BookmarksDB { public Tag getTag(final String hash){ return this.tags.get(hash); //null if it does not exists } - + /** * store a Tag in tagsTable or remove an empty tag * @param tag an object of type Tag to be stored/removed @@ -252,15 +257,15 @@ public class BookmarksDB { this.tags.remove(tag.getTagHash()); } } - + public void removeTag(final String hash) { - tags.remove(hash); + this.tags.remove(hash); } - + public Iterator getTagIterator(final boolean priv) { return getTagIterator(priv, 1); } - + private Iterator getTagIterator(final boolean priv, final int c) { final Set set = new TreeSet((c == SORT_SIZE) ? tagSizeComparator : tagComparator); final Iterator it = this.tags.values().iterator(); @@ -271,23 +276,23 @@ public class BookmarksDB { if (priv ||tag.hasPublicItems()) { set.add(tag); } - } + } return set.iterator(); } - + public Iterator getTagIterator(final boolean priv, final int comp, final int max){ - if (max==SHOW_ALL) - return getTagIterator(priv, comp); + if (max==SHOW_ALL) + return getTagIterator(priv, comp); final Iterator it = getTagIterator(priv, SORT_SIZE); final TreeSet set=new TreeSet((comp == SORT_SIZE) ? tagSizeComparator : tagComparator); int count = 0; while (it.hasNext() && count<=max) { set.add(it.next()); count++; - } + } return set.iterator(); } - + public Iterator getTagIterator(final String tagName, final boolean priv, final int comp) { final TreeSet set=new TreeSet((comp == SORT_SIZE) ? tagSizeComparator : tagComparator); Iterator it=null; @@ -309,7 +314,7 @@ public class BookmarksDB { } return set.iterator(); } - + public Iterator getTagIterator(final String tagName, final boolean priv, final int comp, final int max) { if (max==SHOW_ALL) return getTagIterator(priv, comp); final Iterator it = getTagIterator(tagName, priv, SORT_SIZE); @@ -320,23 +325,23 @@ public class BookmarksDB { count++; } return set.iterator(); - } + } + - // ------------------------------------- // bookmarksDB's experimental functions // ------------------------------------- - + public boolean renameTag(final String oldName, final String newName){ - + 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 - + Bookmark bookmark; Set tagSet = new TreeSet(String.CASE_INSENSITIVE_ORDER); - for (String urlHash : urlHashes) { // looping through all bookmarks which were tagged with oldName + for (final String urlHash : urlHashes) { // looping through all bookmarks which were tagged with oldName bookmark = getBookmark(urlHash); tagSet = bookmark.getTags(); tagSet.remove(oldName); @@ -348,9 +353,9 @@ public class BookmarksDB { } return false; } - - public void addTag(final String selectTag, final String newTag) { - + + public void addTag(final String selectTag, final String newTag) { + Bookmark bookmark; for (final String urlHash : getTag(BookmarkHelper.tagHash(selectTag)).getUrlHashes()) { // looping through all bookmarks which were tagged with selectTag bookmark = getBookmark(urlHash); @@ -358,11 +363,11 @@ public class BookmarksDB { saveBookmark(bookmark); } } - + // -------------------------------------- // bookmarksDB's Subclasses // -------------------------------------- - + /** * Subclass of bookmarksDB, which provides the Tag object-type */ @@ -374,32 +379,32 @@ public class BookmarksDB { private Set urlHashes; public Tag(final String hash, final Map map){ - tagHash = hash; - mem = map; - if (mem.containsKey(URL_HASHES)) { - urlHashes = ListManager.string2set(mem.get(URL_HASHES)); + this.tagHash = hash; + this.mem = map; + if (this.mem.containsKey(URL_HASHES)) { + this.urlHashes = ListManager.string2set(this.mem.get(URL_HASHES)); } else { - urlHashes = new HashSet(); + this.urlHashes = new HashSet(); } } - + public Tag(final String name, final HashSet entries){ - tagHash = BookmarkHelper.tagHash(name); - mem = new HashMap(); + this.tagHash = BookmarkHelper.tagHash(name); + this.mem = new HashMap(); //mem.put(URL_HASHES, listManager.arraylist2string(entries)); - urlHashes = entries; - mem.put(TAG_NAME, name); + this.urlHashes = entries; + this.mem.put(TAG_NAME, name); } - + public Tag(final String name){ this(name, new HashSet()); } - + public Map getMap(){ - mem.put(URL_HASHES, ListManager.collection2string(this.urlHashes)); - return mem; + this.mem.put(URL_HASHES, ListManager.collection2string(this.urlHashes)); + return this.mem; } - + /** * get the lowercase Tagname */ @@ -410,11 +415,11 @@ public class BookmarksDB { return "";*/ return getFriendlyName().toLowerCase(); } - + public String getTagHash(){ - return tagHash; + return this.tagHash; } - + /** * @return the tag name, with all uppercase chars */ @@ -428,33 +433,33 @@ public class BookmarksDB { } return "notagname"; } - + public Set getUrlHashes(){ - return urlHashes; + return this.urlHashes; } - + public boolean hasPublicItems(){ - return getBookmarksIterator(this.getTagName(), false).hasNext(); + return getBookmarksIterator(getTagName(), false).hasNext(); } - + public void addUrl(final String urlHash){ - urlHashes.add(urlHash); + this.urlHashes.add(urlHash); } - + public void delete(final String urlHash){ - urlHashes.remove(urlHash); + this.urlHashes.remove(urlHash); } - + public int size(){ - return urlHashes.size(); + return this.urlHashes.size(); } } - + /** * Subclass of bookmarksDB, which provides the Bookmark object-type */ public class Bookmark { - + public static final String BOOKMARK_URL = "bookmarkUrl"; public static final String BOOKMARK_TITLE = "bookmarkTitle"; public static final String BOOKMARK_DESCRIPTION = "bookmarkDesc"; @@ -463,85 +468,70 @@ public class BookmarksDB { 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 final String urlHash; private Set tagNames; private long timestamp; - private Map entry; - + private final Map entry; + public Bookmark(final String urlHash, final Map map) { - entry = map; + this.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))); + this.tagNames = new TreeSet(String.CASE_INSENSITIVE_ORDER); + if (map.containsKey(BOOKMARK_TAGS)) this.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://")) { - url="http://"+url; - } - try { - this.urlHash = ASCII.String((new DigestURI(url)).hash()); - } catch (final MalformedURLException e) { - this.urlHash = null; - } - entry.put(BOOKMARK_URL, url); - this.timestamp=System.currentTimeMillis(); - tagNames=new HashSet(); + public Bookmark(final DigestURI url) { + this.entry = new HashMap(); + this.urlHash = ASCII.String(url.hash()); + this.entry.put(BOOKMARK_URL, url.toNormalform(false, true)); + this.tagNames = new HashSet(); + this.timestamp = System.currentTimeMillis(); final Bookmark oldBm=getBookmark(this.urlHash); if(oldBm!=null && oldBm.entry.containsKey(BOOKMARK_TIMESTAMP)){ - entry.put(BOOKMARK_TIMESTAMP, oldBm.entry.get(BOOKMARK_TIMESTAMP)); //preserve timestamp on edit + this.entry.put(BOOKMARK_TIMESTAMP, oldBm.entry.get(BOOKMARK_TIMESTAMP)); //preserve timestamp on edit }else{ - entry.put(BOOKMARK_TIMESTAMP, String.valueOf(System.currentTimeMillis())); - } - final BookmarkDate.Entry bmDate=dates.getDate(entry.get(BOOKMARK_TIMESTAMP)); + this.entry.put(BOOKMARK_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + } + final BookmarkDate.Entry bmDate=BookmarksDB.this.dates.getDate(this.entry.get(BOOKMARK_TIMESTAMP)); bmDate.add(this.urlHash); bmDate.setDatesTable(); - + removeBookmark(this.urlHash); //prevent empty tags } - + + public Bookmark(final String url) throws MalformedURLException { + this(new DigestURI((!url.toLowerCase().startsWith("http://") && !url.toLowerCase().startsWith("https://")) ? "http://" + url : url)); + } + public Bookmark(final Map map) throws MalformedURLException { this(ASCII.String((new DigestURI(map.get(BOOKMARK_URL))).hash()), map); } - + Map toMap() { - entry.put(BOOKMARK_TAGS, ListManager.collection2string(tagNames)); - entry.put(BOOKMARK_TIMESTAMP, String.valueOf(this.timestamp)); - return entry; + this.entry.put(BOOKMARK_TAGS, ListManager.collection2string(this.tagNames)); + this.entry.put(BOOKMARK_TIMESTAMP, String.valueOf(this.timestamp)); + return this.entry; } - + private void loadTimestamp() { - if(entry.containsKey(BOOKMARK_TIMESTAMP)) - this.timestamp=Long.parseLong(entry.get(BOOKMARK_TIMESTAMP)); + if(this.entry.containsKey(BOOKMARK_TIMESTAMP)) + this.timestamp=Long.parseLong(this.entry.get(BOOKMARK_TIMESTAMP)); } - + public String getUrlHash() { - return urlHash; + return this.urlHash; } - + public String getUrl() { - return entry.get(BOOKMARK_URL); + return this.entry.get(BOOKMARK_URL); } - + public Set getTags() { - return tagNames; + return this.tagNames; } - - public String getTagsString() { + + public String getTagsString() { final String s[] = ListManager.collection2string(getTags()).split(","); final StringBuilder stringBuilder = new StringBuilder(); for (final String element : s){ @@ -552,7 +542,7 @@ public class BookmarksDB { } return stringBuilder.toString(); } - + public String getFoldersString(){ final String s[] = ListManager.collection2string(getTags()).split(","); final StringBuilder stringBuilder = new StringBuilder(); @@ -564,73 +554,73 @@ public class BookmarksDB { } return stringBuilder.toString(); } - + public String getDescription(){ - if(entry.containsKey(BOOKMARK_DESCRIPTION)){ - return entry.get(BOOKMARK_DESCRIPTION); + if(this.entry.containsKey(BOOKMARK_DESCRIPTION)){ + return this.entry.get(BOOKMARK_DESCRIPTION); } return ""; } - + public String getTitle(){ - if(entry.containsKey(BOOKMARK_TITLE)){ - return entry.get(BOOKMARK_TITLE); + if(this.entry.containsKey(BOOKMARK_TITLE)){ + return this.entry.get(BOOKMARK_TITLE); } - return entry.get(BOOKMARK_URL); + return this.entry.get(BOOKMARK_URL); } - + public String getOwner(){ - if(entry.containsKey(BOOKMARK_OWNER)){ - return entry.get(BOOKMARK_OWNER); + if(this.entry.containsKey(BOOKMARK_OWNER)){ + return this.entry.get(BOOKMARK_OWNER); } return null; //null means admin } - + public void setOwner(final String owner){ - entry.put(BOOKMARK_OWNER, owner); + this.entry.put(BOOKMARK_OWNER, owner); } - + public boolean getPublic(){ - if(entry.containsKey(BOOKMARK_PUBLIC)){ - return "public".equals(entry.get(BOOKMARK_PUBLIC)); + if(this.entry.containsKey(BOOKMARK_PUBLIC)){ + return "public".equals(this.entry.get(BOOKMARK_PUBLIC)); } return false; } - + public boolean getFeed(){ - if(entry.containsKey(BOOKMARK_IS_FEED)){ - return "true".equals(entry.get(BOOKMARK_IS_FEED)); + if(this.entry.containsKey(BOOKMARK_IS_FEED)){ + return "true".equals(this.entry.get(BOOKMARK_IS_FEED)); } return false; } - + public void setPublic(final boolean isPublic){ if(isPublic){ - entry.put(BOOKMARK_PUBLIC, "public"); + this.entry.put(BOOKMARK_PUBLIC, "public"); } else { - entry.put(BOOKMARK_PUBLIC, "private"); + this.entry.put(BOOKMARK_PUBLIC, "private"); } } - + public void setFeed(final boolean isFeed){ if(isFeed){ - entry.put(BOOKMARK_IS_FEED, "true"); + this.entry.put(BOOKMARK_IS_FEED, "true"); }else{ - entry.put(BOOKMARK_IS_FEED, "false"); + this.entry.put(BOOKMARK_IS_FEED, "false"); } } - + public void setProperty(final String name, final String value){ - entry.put(name, value); + this.entry.put(name, value); //setBookmarksTable(); } - + public void addTag(final String tagName){ - tagNames.add(tagName); - setTags(tagNames); + this.tagNames.add(tagName); + setTags(this.tagNames); saveBookmark(this); } - + /** * set the Tags of the bookmark, and write them into the tags table. * @param tags2 a ArrayList with the tags @@ -638,16 +628,16 @@ public class BookmarksDB { public void setTags(final Set tags2){ setTags(tags2, true); } - + /** * set the Tags of the bookmark * @param tagNames ArrayList with the tagnames * @param local sets, whether the updated tags should be stored to tagsDB */ public void setTags(final Set tags2, final boolean local){ - tagNames = tags2; // TODO: check if this is safe + this.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' - for (final String tagName : tagNames) { + for (final String tagName : this.tagNames) { Tag tag = getTag(BookmarkHelper.tagHash(tagName)); if (tag == null) { tag = new Tag(tagName); @@ -661,55 +651,55 @@ public class BookmarksDB { } public long getTimeStamp(){ - return timestamp; + return this.timestamp; } - + public void setTimeStamp(final long ts){ this.timestamp = ts; } } - - + + /** * Subclass of bookmarksDB, which provides the bookmarkIterator object-type */ public class bookmarkIterator implements Iterator { Iterator bookmarkIter; - + public bookmarkIterator(final boolean up) throws IOException { //flushBookmarkCache(); //XXX: this will cost performance this.bookmarkIter = BookmarksDB.this.bookmarks.keys(up, false); //this.nextEntry = null; } - + public boolean hasNext() { return this.bookmarkIter.hasNext(); } - + public Bookmark next() { return getBookmark(UTF8.String(this.bookmarkIter.next())); } - + public void remove() { throw new UnsupportedOperationException(); } } - + /** * Comparator to sort objects of type Bookmark according to their timestamps */ public class bookmarkComparator implements Comparator { - + private final boolean newestFirst; - + /** * @param newestFirst newest first, or oldest first? */ public bookmarkComparator(final boolean newestFirst){ this.newestFirst = newestFirst; } - + public int compare(final String obj1, final String obj2) { final Bookmark bm1 = getBookmark(obj1); final Bookmark bm2 = getBookmark(obj2); @@ -724,15 +714,15 @@ public class BookmarksDB { return -1; } } - + public static final TagComparator tagComparator = new TagComparator(); public static final TagSizeComparator tagSizeComparator = new TagSizeComparator(); - + /** * Comparator to sort objects of type Tag according to their names */ public static class TagComparator implements Comparator, Serializable { - + /** * generated serial */ @@ -741,11 +731,11 @@ public class BookmarksDB { public int compare(final Tag obj1, final Tag obj2){ return obj1.getTagName().compareTo(obj2.getTagName()); } - + } - + public static class TagSizeComparator implements Comparator, Serializable { - + /** * generated serial */ @@ -760,10 +750,10 @@ public class BookmarksDB { return -1; } } - + } - + public BookmarkDate.Entry getDate(final String date) { - return dates.getDate(date); + return this.dates.getDate(date); } } \ No newline at end of file