Author: nextgens Date: 2007-11-28 13:45:30 +0000 (Wed, 28 Nov 2007) New Revision: 16009
Modified: trunk/freenet/src/freenet/clients/http/bookmark/Bookmark.java trunk/freenet/src/freenet/clients/http/bookmark/BookmarkCategory.java trunk/freenet/src/freenet/clients/http/bookmark/BookmarkItem.java trunk/freenet/src/freenet/clients/http/bookmark/BookmarkManager.java trunk/freenet/src/freenet/support/SimpleFieldSet.java Log: New bookmark code to please toad; see http://archives.freenetproject.org/message/20071120.231400.e598d958.en.html There is some code to import "current trunk's" bookmarks Modified: trunk/freenet/src/freenet/clients/http/bookmark/Bookmark.java =================================================================== --- trunk/freenet/src/freenet/clients/http/bookmark/Bookmark.java 2007-11-28 13:39:05 UTC (rev 16008) +++ trunk/freenet/src/freenet/clients/http/bookmark/Bookmark.java 2007-11-28 13:45:30 UTC (rev 16009) @@ -1,5 +1,7 @@ package freenet.clients.http.bookmark; +import freenet.support.SimpleFieldSet; + public abstract class Bookmark { protected String name; @@ -26,4 +28,6 @@ return false; } } + + public abstract SimpleFieldSet getSimpleFieldSet(); } Modified: trunk/freenet/src/freenet/clients/http/bookmark/BookmarkCategory.java =================================================================== --- trunk/freenet/src/freenet/clients/http/bookmark/BookmarkCategory.java 2007-11-28 13:39:05 UTC (rev 16008) +++ trunk/freenet/src/freenet/clients/http/bookmark/BookmarkCategory.java 2007-11-28 13:45:30 UTC (rev 16009) @@ -1,17 +1,25 @@ package freenet.clients.http.bookmark; +import freenet.node.FSParseException; import freenet.support.SimpleFieldSet; import java.util.Vector; import freenet.support.StringArray; public class BookmarkCategory extends Bookmark { + public static final String NAME = "BookmarkCategory"; private final Vector bookmarks = new Vector(); public BookmarkCategory(String name) { setName(name); } + + public BookmarkCategory(SimpleFieldSet sfs) throws FSParseException { + String aName = sfs.get("Name"); + if(aName == null) throw new FSParseException("No Name!"); + setName(aName); + } protected synchronized Bookmark addBookmark(Bookmark b) { if (b == null) { @@ -126,25 +134,10 @@ } - public SimpleFieldSet toSimpleFieldSet() { + public SimpleFieldSet getSimpleFieldSet() { SimpleFieldSet sfs = new SimpleFieldSet(true); - - BookmarkItems items = getItems(); - for (int i = 0; i < items.size(); i++) { - BookmarkItem item = items.get(i); - sfs.putSingle(String.valueOf(i), item.toString()); - } - - BookmarkCategories subCategories = getSubCategories(); - for (int i = 0; i < subCategories.size(); i++) { - BookmarkCategory category = subCategories.get(i); - SimpleFieldSet toPut = category.toSimpleFieldSet(); - if ("".equals(category.name) || toPut.isEmpty()) { - continue; - } - sfs.put(category.name, toPut); - } - + sfs.putSingle("Name", name); + sfs.put("Content", BookmarkManager.toSimpleFieldSet(this)); return sfs; } // Don't override equals(), two categories are equal if they have the same name and description. Modified: trunk/freenet/src/freenet/clients/http/bookmark/BookmarkItem.java =================================================================== --- trunk/freenet/src/freenet/clients/http/bookmark/BookmarkItem.java 2007-11-28 13:39:05 UTC (rev 16008) +++ trunk/freenet/src/freenet/clients/http/bookmark/BookmarkItem.java 2007-11-28 13:45:30 UTC (rev 16009) @@ -6,6 +6,7 @@ import freenet.keys.FreenetURI; import freenet.keys.USK; import freenet.l10n.L10n; +import freenet.node.FSParseException; import freenet.node.NodeClientCore; import freenet.node.useralerts.AbstractUserAlert; import freenet.node.useralerts.UserAlert; @@ -14,10 +15,11 @@ import freenet.support.HTMLEncoder; import freenet.support.HTMLNode; +import freenet.support.SimpleFieldSet; import java.net.MalformedURLException; public class BookmarkItem extends Bookmark { - + public static final String NAME = "Bookmark"; private FreenetURI key; private boolean updated; private boolean hasAnActivelink = false; @@ -54,6 +56,15 @@ this.alerts = uam; this.alert = new BookmarkUpdatedUserAlert(); } + + public BookmarkItem(SimpleFieldSet sfs, UserAlertManager uam) throws FSParseException, MalformedURLException { + this.name = sfs.get("Name"); + this.desc = sfs.get("Description"); + this.hasAnActivelink = sfs.getBoolean("hasAnActivelink"); + this.key = new FreenetURI(sfs.get("URI")); + this.alerts = uam; + this.alert = new BookmarkUpdatedUserAlert(); + } private class BookmarkUpdatedUserAlert extends AbstractUserAlert { @@ -204,4 +215,13 @@ public String getDescription() { return (desc == null ? "" : desc); } + + public SimpleFieldSet getSimpleFieldSet() { + SimpleFieldSet sfs = new SimpleFieldSet(true); + sfs.putSingle("Name", name); + sfs.putSingle("Description", desc); + sfs.put("hasAnActivelink", hasAnActivelink); + sfs.putSingle("URI", key.toString()); + return sfs; + } } Modified: trunk/freenet/src/freenet/clients/http/bookmark/BookmarkManager.java =================================================================== --- trunk/freenet/src/freenet/clients/http/bookmark/BookmarkManager.java 2007-11-28 13:39:05 UTC (rev 16008) +++ trunk/freenet/src/freenet/clients/http/bookmark/BookmarkManager.java 2007-11-28 13:45:30 UTC (rev 16009) @@ -13,6 +13,7 @@ import freenet.keys.FreenetURI; import freenet.keys.USK; import freenet.l10n.L10n; +import freenet.node.FSParseException; import freenet.node.NodeClientCore; import freenet.support.Logger; import freenet.support.SimpleFieldSet; @@ -354,20 +355,14 @@ public void storeBookmarks() { Logger.normal(this, "Attempting to save bookmarks to " + bookmarksFile.toString()); - SimpleFieldSet sfs; + SimpleFieldSet sfs = null; synchronized (bookmarks) { if (isSavingBookmarks) { return; } isSavingBookmarks = true; - - SimpleFieldSet toSave = MAIN_CATEGORY.toSimpleFieldSet(); - if (toSave.isEmpty()) { - isSavingBookmarks = false; - bookmarksFile.delete(); - return; - } - sfs = toSave; + + sfs = toSimpleFieldSet(); } FileOutputStream fos = null; try { @@ -389,13 +384,60 @@ } private void readBookmarks(BookmarkCategory category, SimpleFieldSet sfs) { - _innerReadBookmarks("", category, sfs); + try { + int bookmarkVersion = sfs.getInt("Version"); + + _innerReadBookmarks("", category, sfs); + } catch (FSParseException e) { + _innerReadTrunkBookmarks("", category, sfs); + } } private void _innerReadBookmarks(String prefix, BookmarkCategory category, SimpleFieldSet sfs) { + boolean hasBeenParsedWithoutAnyProblem = true; + boolean isRoot = ("".equals(prefix) && MAIN_CATEGORY.equals(category)); + synchronized (bookmarks) { + if(!isRoot) + putPaths(prefix + category.name + '/', category); + + try { + int nbBookmarks = sfs.getInt(BookmarkItem.NAME); + int nbCategories = sfs.getInt(BookmarkCategory.NAME); + + for(int i = 0; i < nbBookmarks; i++) { + SimpleFieldSet subset = sfs.getSubset(BookmarkItem.NAME + i); + try { + BookmarkItem item = new BookmarkItem(subset, node.alerts); + String name = (isRoot ? "" : prefix + category.name) + '/' + item.name; + putPaths(name, item); + category.addBookmark(item); + } catch(MalformedURLException e) { + throw new FSParseException(e); + } + } + + for(int i = 0; i < nbCategories; i++) { + SimpleFieldSet subset = sfs.getSubset(BookmarkCategory.NAME + i); + BookmarkCategory currentCategory = new BookmarkCategory(subset); + category.addBookmark(currentCategory); + String name = (isRoot ? "/" : (prefix + category.name + '/')); + _innerReadBookmarks(name, currentCategory, subset.getSubset("Content")); + } + + } catch(FSParseException e) { + Logger.error(this, "Error parsing the bookmarks file!", e); + hasBeenParsedWithoutAnyProblem = false; + } + + } + if(hasBeenParsedWithoutAnyProblem) + storeBookmarks(); + } + + private void _innerReadTrunkBookmarks(String prefix, BookmarkCategory category, SimpleFieldSet sfs) { boolean hasBeenParsedWithoutAnyProblem = true; boolean isRoot = ("".equals(prefix) && MAIN_CATEGORY.equals(category)); - synchronized (bookmarks) { + synchronized (bookmarks) { if(!isRoot) putPaths(prefix + category.name + '/', category); @@ -405,7 +447,7 @@ BookmarkCategory currentCategory = new BookmarkCategory(categories[i]); String name = prefix + category.name + '/'; category.addBookmark(currentCategory); - _innerReadBookmarks((isRoot ? "/" : name), currentCategory, subset); + _innerReadTrunkBookmarks((isRoot ? "/" : name), currentCategory, subset); } Iterator it = sfs.toplevelKeyIterator(); @@ -426,4 +468,34 @@ if(hasBeenParsedWithoutAnyProblem) storeBookmarks(); } + + public SimpleFieldSet toSimpleFieldSet() { + SimpleFieldSet sfs = new SimpleFieldSet(true); + + sfs.put("Version", 1); + synchronized (bookmarks) { + sfs.putAllOverwrite(BookmarkManager.toSimpleFieldSet(MAIN_CATEGORY)); + } + + return sfs; + } + + public static SimpleFieldSet toSimpleFieldSet(BookmarkCategory cat) { + SimpleFieldSet sfs = new SimpleFieldSet(true); + BookmarkCategories bc = cat.getSubCategories(); + + for(int i=0; i<bc.size(); i++) { + BookmarkCategory currentCat = bc.get(i); + sfs.put(BookmarkCategory.NAME+i, currentCat.getSimpleFieldSet()); + } + sfs.put(BookmarkCategory.NAME, bc.size()); + + + BookmarkItems bi = cat.getItems(); + for(int i=0; i<bi.size(); i++) + sfs.put(BookmarkItem.NAME+i, bi.get(i).getSimpleFieldSet()); + sfs.put(BookmarkItem.NAME, bi.size()); + + return sfs; + } } Modified: trunk/freenet/src/freenet/support/SimpleFieldSet.java =================================================================== --- trunk/freenet/src/freenet/support/SimpleFieldSet.java 2007-11-28 13:39:05 UTC (rev 16008) +++ trunk/freenet/src/freenet/support/SimpleFieldSet.java 2007-11-28 13:45:30 UTC (rev 16009) @@ -825,6 +825,14 @@ public boolean getBoolean(String key, boolean def) { return Fields.stringToBool(get(key), def); } + + public boolean getBoolean(String key) throws FSParseException { + try { + return Fields.stringToBool(get(key)); + } catch(NumberFormatException e) { + throw new FSParseException(e); + } + } public void put(String key, int[] value) { // FIXME this could be more efficient...
