Author: nextgens
Date: 2007-11-30 17:12:36 +0000 (Fri, 30 Nov 2007)
New Revision: 16140

Modified:
   trunk/freenet/src/freenet/clients/http/bookmark/BookmarkManager.java
Log:
indent

Modified: trunk/freenet/src/freenet/clients/http/bookmark/BookmarkManager.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/bookmark/BookmarkManager.java        
2007-11-30 17:02:21 UTC (rev 16139)
+++ trunk/freenet/src/freenet/clients/http/bookmark/BookmarkManager.java        
2007-11-30 17:12:36 UTC (rev 16140)
@@ -29,15 +29,15 @@
 import java.util.Iterator;

 public class BookmarkManager {
-    public static SimpleFieldSet DEFAULT_BOOKMARKS = null;
-    private final NodeClientCore node;
-    private final USKUpdatedCallback uskCB = new USKUpdatedCallback();
-    public static final BookmarkCategory MAIN_CATEGORY = new 
BookmarkCategory("/");
-    private final HashMap bookmarks = new HashMap();
-    private final File bookmarksFile = new 
File("bookmarks.dat").getAbsoluteFile();
-    private final File backupBookmarksFile = new 
File(bookmarksFile.getParentFile(), bookmarksFile.getName()+".bak");
-    private boolean isSavingBookmarks = false;

+       public static SimpleFieldSet DEFAULT_BOOKMARKS = null;
+       private final NodeClientCore node;
+       private final USKUpdatedCallback uskCB = new USKUpdatedCallback();
+       public static final BookmarkCategory MAIN_CATEGORY = new 
BookmarkCategory("/");
+       private final HashMap bookmarks = new HashMap();
+       private final File bookmarksFile = new 
File("bookmarks.dat").getAbsoluteFile();
+       private final File backupBookmarksFile = new 
File(bookmarksFile.getParentFile(), bookmarksFile.getName() + ".bak");
+       private boolean isSavingBookmarks = false;
        static {
                String name = 
"freenet/clients/http/staticfiles/defaultbookmarks.dat";
                InputStream in = null;
@@ -55,455 +55,435 @@
                }
        }

-    public BookmarkManager(NodeClientCore n, SimpleFieldSet oldConfig) {
-       putPaths("/", MAIN_CATEGORY);
-        this.node = n;
+       public BookmarkManager(NodeClientCore n, SimpleFieldSet oldConfig) {
+               putPaths("/", MAIN_CATEGORY);
+               this.node = n;

-        try {
-            //TODO: remove
-            String[] oldBookmarks = null;
-            if(oldConfig != null) {
-               try {
-                       String o = oldConfig.get("fproxy.bookmarks");
-                       if (o == null) {
-                               oldBookmarks = null;
-                       } else {
-                               oldBookmarks = StringArrOption.stringToArray(o);
-                       }
-               } catch (URLEncodedFormatException e) {
-                       Logger.error(this, "Not possible to migrate: caught " + 
e, e);
-                       oldBookmarks = null;
-               }
-            }
-            if (oldBookmarks != null) {
-                migrateOldBookmarks(oldBookmarks);
-                storeBookmarks();
-            }
+               try {
+                       //TODO: remove
+                       String[] oldBookmarks = null;
+                       if(oldConfig != null)
+                               try {
+                                       String o = 
oldConfig.get("fproxy.bookmarks");
+                                       if(o == null)
+                                               oldBookmarks = null;
+                                       else
+                                               oldBookmarks = 
StringArrOption.stringToArray(o);
+                               } catch(URLEncodedFormatException e) {
+                                       Logger.error(this, "Not possible to 
migrate: caught " + e, e);
+                                       oldBookmarks = null;
+                               }
+                       if(oldBookmarks != null) {
+                               migrateOldBookmarks(oldBookmarks);
+                               storeBookmarks();
+                       }

-            // Read the backup file if necessary
-            if(!bookmarksFile.exists() || bookmarksFile.length() == 0)
-                throw new IOException();
-            Logger.normal(this, "Attempting to read the bookmark file from " + 
bookmarksFile.toString());
-            SimpleFieldSet sfs = SimpleFieldSet.readFrom(bookmarksFile, false, 
true);
-            readBookmarks(MAIN_CATEGORY, sfs);
-        } catch (MalformedURLException mue) {
-        } catch (IOException ioe) {
-            Logger.error(this, "Error reading the bookmark file (" + 
bookmarksFile.toString() + "):" + ioe.getMessage(), ioe);
-            
-            try {
-                if (backupBookmarksFile.exists() && 
backupBookmarksFile.canRead() && backupBookmarksFile.length() > 0) {
-                    Logger.normal(this, "Attempting to read the backup 
bookmark file from " + backupBookmarksFile.toString());
-                    SimpleFieldSet sfs = 
SimpleFieldSet.readFrom(backupBookmarksFile, false, true);
-                    readBookmarks(MAIN_CATEGORY, sfs);
-                } else {
-                    Logger.normal(this, "We couldn't find the backup either! - 
"+FileUtil.getCanonicalFile(backupBookmarksFile));
-                   // restore the default bookmark set
-                   readBookmarks(MAIN_CATEGORY, DEFAULT_BOOKMARKS);
+                       // Read the backup file if necessary
+                       if(!bookmarksFile.exists() || bookmarksFile.length() == 
0)
+                               throw new IOException();
+                       Logger.normal(this, "Attempting to read the bookmark 
file from " + bookmarksFile.toString());
+                       SimpleFieldSet sfs = 
SimpleFieldSet.readFrom(bookmarksFile, false, true);
+                       readBookmarks(MAIN_CATEGORY, sfs);
+               } catch(MalformedURLException mue) {
+               } catch(IOException ioe) {
+                       Logger.error(this, "Error reading the bookmark file (" 
+ bookmarksFile.toString() + "):" + ioe.getMessage(), ioe);
+
+                       try {
+                               if(backupBookmarksFile.exists() && 
backupBookmarksFile.canRead() && backupBookmarksFile.length() > 0) {
+                                       Logger.normal(this, "Attempting to read 
the backup bookmark file from " + backupBookmarksFile.toString());
+                                       SimpleFieldSet sfs = 
SimpleFieldSet.readFrom(backupBookmarksFile, false, true);
+                                       readBookmarks(MAIN_CATEGORY, sfs);
+                               } else {
+                                       Logger.normal(this, "We couldn't find 
the backup either! - " + FileUtil.getCanonicalFile(backupBookmarksFile));
+                                       // restore the default bookmark set
+                                       readBookmarks(MAIN_CATEGORY, 
DEFAULT_BOOKMARKS);
+                               }
+                       } catch(IOException e) {
+                               Logger.error(this, "Error reading the backup 
bookmark file !" + e.getMessage(), e);
+                       }
                }
-            } catch (IOException e) {
-                Logger.error(this, "Error reading the backup bookmark file !" 
+ e.getMessage(), e);
-            }
-        }
-    }
-    
-    public void reAddDefaultBookmarks() {
-           BookmarkCategory bc = new 
BookmarkCategory("DefaultBookmarks-"+System.currentTimeMillis());
-           _innerReadBookmarks("/", bc, DEFAULT_BOOKMARKS);
-           addBookmark("/", bc);
-           putPaths("/", bc);
-    }
+       }

-    private void registerProtectedItems(String prefix, BookmarkCategory 
current) {
-       BookmarkCategories categories = current.getSubCategories();
-       for(int i=0; i<categories.size(); i++) {
-               BookmarkCategory cat = categories.get(i);
-               String name = ("".equals(prefix) ? "" : prefix+'/') + cat.name;
-               addBookmark(prefix, cat);
-               registerProtectedItems(name, cat);
+       public void reAddDefaultBookmarks() {
+               BookmarkCategory bc = new BookmarkCategory("DefaultBookmarks-" 
+ System.currentTimeMillis());
+               _innerReadBookmarks("/", bc, DEFAULT_BOOKMARKS);
+               addBookmark("/", bc);
+               putPaths("/", bc);
        }
-       
-       BookmarkItems items = current.getItems();
-       for(int i=0; i<items.size(); i++)
-               addBookmark(prefix, items.get(i));
-    }
-    
-    private void migrateOldBookmarks(String[] newVals) {
-       if(Logger.shouldLog(Logger.MINOR, this))
-               Logger.minor(this, "Migrating bookmarks: 
"+StringArray.toString(newVals));
-        //FIXME: for some reason that doesn't work... if someone wants to fix 
it ;)
-        Pattern pattern = Pattern.compile("/(.*/)([^/]*)=([A-Z]{3}@.*).*");
-        FreenetURI key;
-        for (int i = 0; i < newVals.length; i++) {
-            try {
-                Matcher matcher = pattern.matcher(newVals[i]);
-                if (matcher.matches()) {
-                    makeParents(matcher.group(1));
-                    key = new FreenetURI(matcher.group(3));
-                    String title = matcher.group(2);
-                    boolean hasAnActiveLink = false;
-                    if(title.endsWith("=|")) {
-                       hasAnActiveLink = true;
-                       title = title.substring(0, title.length()-2);
-                    } else if(title.endsWith("=")) {
-                       title = title.substring(0, title.length()-1);
-                    }
-                    addBookmark(matcher.group(1), new BookmarkItem(key,
-                            title, "", hasAnActiveLink, node.alerts));
-                }
-            } catch (MalformedURLException e) {
-            }
-        }
-    }

-    private class USKUpdatedCallback implements USKCallback {
+       private void registerProtectedItems(String prefix, BookmarkCategory 
current) {
+               BookmarkCategories categories = current.getSubCategories();
+               for(int i = 0; i < categories.size(); i++) {
+                       BookmarkCategory cat = categories.get(i);
+                       String name = ("".equals(prefix) ? "" : prefix + '/') + 
cat.name;
+                       addBookmark(prefix, cat);
+                       registerProtectedItems(name, cat);
+               }

-        public void onFoundEdition(long edition, USK key) {
-            BookmarkItems items = MAIN_CATEGORY.getAllItems();
-            for (int i = 0; i < items.size(); i++) {
-                if (!"USK".equals(items.get(i).getKeyType())) {
-                    continue;
-                }
+               BookmarkItems items = current.getItems();
+               for(int i = 0; i < items.size(); i++)
+                       addBookmark(prefix, items.get(i));
+       }

-                try {
-                    FreenetURI furi = new FreenetURI(items.get(i).getKey());
-                    USK usk = USK.create(furi);
+       private void migrateOldBookmarks(String[] newVals) {
+               if(Logger.shouldLog(Logger.MINOR, this))
+                       Logger.minor(this, "Migrating bookmarks: " + 
StringArray.toString(newVals));
+               //FIXME: for some reason that doesn't work... if someone wants 
to fix it ;)
+               Pattern pattern = 
Pattern.compile("/(.*/)([^/]*)=([A-Z]{3}@.*).*");
+               FreenetURI key;
+               for(int i = 0; i < newVals.length; i++)
+                       try {
+                               Matcher matcher = pattern.matcher(newVals[i]);
+                               if(matcher.matches()) {
+                                       makeParents(matcher.group(1));
+                                       key = new FreenetURI(matcher.group(3));
+                                       String title = matcher.group(2);
+                                       boolean hasAnActiveLink = false;
+                                       if(title.endsWith("=|")) {
+                                               hasAnActiveLink = true;
+                                               title = title.substring(0, 
title.length() - 2);
+                                       } else if(title.endsWith("="))
+                                               title = title.substring(0, 
title.length() - 1);
+                                       addBookmark(matcher.group(1), new 
BookmarkItem(key,
+                                               title, "", hasAnActiveLink, 
node.alerts));
+                               }
+                       } catch(MalformedURLException e) {
+                       }
+       }

-                    if (usk.equals(key, false)) {
-                        items.get(i).setEdition(key.suggestedEdition, node);
-                        break;
-                    }
-                } catch (MalformedURLException mue) {
-                }
-            }
-            storeBookmarks();
-        }
-    }
+       private class USKUpdatedCallback implements USKCallback {

-    public String l10n(String key) {
-        return L10n.getString("BookmarkManager." + key);
-    }
+               public void onFoundEdition(long edition, USK key) {
+                       BookmarkItems items = MAIN_CATEGORY.getAllItems();
+                       for(int i = 0; i < items.size(); i++) {
+                               if(!"USK".equals(items.get(i).getKeyType()))
+                                       continue;

-    public String parentPath(String path) {
-        if (path.equals("/")) {
-            return "/";
-        }
+                               try {
+                                       FreenetURI furi = new 
FreenetURI(items.get(i).getKey());
+                                       USK usk = USK.create(furi);

-        return path.substring(0, path.substring(0, path.length() - 
1).lastIndexOf("/")) + "/";
-    }
+                                       if(usk.equals(key, false)) {
+                                               
items.get(i).setEdition(key.suggestedEdition, node);
+                                               break;
+                                       }
+                               } catch(MalformedURLException mue) {
+                               }
+                       }
+                       storeBookmarks();
+               }
+       }

-    public Bookmark getBookmarkByPath(String path) {
-        synchronized (bookmarks) {
-            return (Bookmark) bookmarks.get(path);
-        }
-    }
+       public String l10n(String key) {
+               return L10n.getString("BookmarkManager." + key);
+       }

-    public BookmarkCategory getCategoryByPath(String path) {
-        Bookmark cat = getBookmarkByPath(path.trim());
-        if (cat instanceof BookmarkCategory) {
-            return (BookmarkCategory) cat;
-        }
+       public String parentPath(String path) {
+               if(path.equals("/"))
+                       return "/";

-        return null;
-    }
+               return path.substring(0, path.substring(0, path.length() - 
1).lastIndexOf("/")) + "/";
+       }

-    public BookmarkItem getItemByPath(String path) {
-        if (getBookmarkByPath(path.trim()) instanceof BookmarkItem) {
-            return (BookmarkItem) getBookmarkByPath(path);
-        }
+       public Bookmark getBookmarkByPath(String path) {
+               synchronized(bookmarks) {
+                       return (Bookmark) bookmarks.get(path);
+               }
+       }

-        return null;
-    }
+       public BookmarkCategory getCategoryByPath(String path) {
+               Bookmark cat = getBookmarkByPath(path.trim());
+               if(cat instanceof BookmarkCategory)
+                       return (BookmarkCategory) cat;

-    public void addBookmark(String parentPath, Bookmark bookmark) {
-       if(Logger.shouldLog(Logger.MINOR, this))
-               Logger.minor(this, "Adding bookmark "+bookmark+" to 
"+parentPath);
-        BookmarkCategory parent = getCategoryByPath(parentPath);
-        parent.addBookmark(bookmark);
-        putPaths(parentPath + bookmark.getName() + ((bookmark instanceof 
BookmarkCategory) ? "/" : ""),
-                bookmark);
+               return null;
+       }

-        if (bookmark instanceof BookmarkItem && ((BookmarkItem) 
bookmark).getKeyType().equals("USK")) {
-            try {
-                USK u = ((BookmarkItem) bookmark).getUSK();
-                this.node.uskManager.subscribe(u, this.uskCB, true, this);
-            } catch (MalformedURLException mue) {}
-        }
-    }
+       public BookmarkItem getItemByPath(String path) {
+               if(getBookmarkByPath(path.trim()) instanceof BookmarkItem)
+                       return (BookmarkItem) getBookmarkByPath(path);

-    public void renameBookmark(String path, String newName) {
-        Bookmark bookmark = getBookmarkByPath(path);
+               return null;
+       }

-        String oldName = bookmark.getName();
-        String oldPath = '/' + oldName + '/';
-        String newPath = oldPath.substring(0, oldPath.indexOf(oldName)) + 
newName;
+       public void addBookmark(String parentPath, Bookmark bookmark) {
+               if(Logger.shouldLog(Logger.MINOR, this))
+                       Logger.minor(this, "Adding bookmark " + bookmark + " to 
" + parentPath);
+               BookmarkCategory parent = getCategoryByPath(parentPath);
+               parent.addBookmark(bookmark);
+               putPaths(parentPath + bookmark.getName() + ((bookmark 
instanceof BookmarkCategory) ? "/" : ""),
+                       bookmark);

-        bookmark.setName(newName);
-        synchronized (bookmarks) {
-            bookmarks.remove(path);
-        }
-        if (path.charAt(path.length() - 1) != '/') {
-            int lastIndexOfSlash = path.lastIndexOf('/');
-            newPath = path.substring(0, lastIndexOfSlash) + newPath;
-        } else {
-            newPath += '/';
-        }
-        synchronized (bookmarks) {
-            bookmarks.put(newPath, bookmark);
-        }
-        storeBookmarks();
-    }
+               if(bookmark instanceof BookmarkItem && ((BookmarkItem) 
bookmark).getKeyType().equals("USK"))
+                       try {
+                               USK u = ((BookmarkItem) bookmark).getUSK();
+                               this.node.uskManager.subscribe(u, this.uskCB, 
true, this);
+                       } catch(MalformedURLException mue) {
+                       }
+       }

-    public void moveBookmark(String bookmarkPath, String newParentPath) {
-        Bookmark b = getBookmarkByPath(bookmarkPath);
-        addBookmark(newParentPath, b);
+       public void renameBookmark(String path, String newName) {
+               Bookmark bookmark = getBookmarkByPath(path);

-        getCategoryByPath(parentPath(bookmarkPath)).removeBookmark(b);
-        removePaths(bookmarkPath);
-    }
+               String oldName = bookmark.getName();
+               String oldPath = '/' + oldName + '/';
+               String newPath = oldPath.substring(0, oldPath.indexOf(oldName)) 
+ newName;

-    public void removeBookmark(String path) {
-        Bookmark bookmark = getBookmarkByPath(path);
-        if (bookmark == null) {
-            return;
-        }
+               bookmark.setName(newName);
+               synchronized(bookmarks) {
+                       bookmarks.remove(path);
+               }
+               if(path.charAt(path.length() - 1) != '/') {
+                       int lastIndexOfSlash = path.lastIndexOf('/');
+                       newPath = path.substring(0, lastIndexOfSlash) + newPath;
+               } else
+                       newPath += '/';
+               synchronized(bookmarks) {
+                       bookmarks.put(newPath, bookmark);
+               }
+               storeBookmarks();
+       }

-        if (bookmark instanceof BookmarkCategory) {
-            BookmarkCategory cat = (BookmarkCategory) bookmark;
-            for (int i = 0; i < cat.size(); i++) {
-                removeBookmark(path + cat.get(i).getName() + ((cat.get(i) 
instanceof BookmarkCategory) ? "/"
-                        : ""));
-            }
-        } else {
-            if (((BookmarkItem) bookmark).getKeyType().equals("USK")) {
-                try {
-                    USK u = ((BookmarkItem) bookmark).getUSK();
-                    this.node.uskManager.unsubscribe(u, this.uskCB, true);
-                } catch (MalformedURLException mue) {
-                }
-            }
-        }
+       public void moveBookmark(String bookmarkPath, String newParentPath) {
+               Bookmark b = getBookmarkByPath(bookmarkPath);
+               addBookmark(newParentPath, b);

-        getCategoryByPath(parentPath(path)).removeBookmark(bookmark);
-        synchronized (bookmarks) {
-            bookmarks.remove(path);
-        }
-    }
+               getCategoryByPath(parentPath(bookmarkPath)).removeBookmark(b);
+               removePaths(bookmarkPath);
+       }

-    public void moveBookmarkUp(String path, boolean store) {
-        BookmarkCategory parent = getCategoryByPath(parentPath(path));
-        parent.moveBookmarkUp(getBookmarkByPath(path));
+       public void removeBookmark(String path) {
+               Bookmark bookmark = getBookmarkByPath(path);
+               if(bookmark == null)
+                       return;

-        if (store) {
-            storeBookmarks();
-        }
-    }
+               if(bookmark instanceof BookmarkCategory) {
+                       BookmarkCategory cat = (BookmarkCategory) bookmark;
+                       for(int i = 0; i < cat.size(); i++)
+                               removeBookmark(path + cat.get(i).getName() + 
((cat.get(i) instanceof BookmarkCategory) ? "/"
+                                       : ""));
+               } else
+                       if(((BookmarkItem) bookmark).getKeyType().equals("USK"))
+                               try {
+                                       USK u = ((BookmarkItem) 
bookmark).getUSK();
+                                       this.node.uskManager.unsubscribe(u, 
this.uskCB, true);
+                               } catch(MalformedURLException mue) {
+                               }

-    public void moveBookmarkDown(String path, boolean store) {
-        BookmarkCategory parent = getCategoryByPath(parentPath(path));
-        parent.moveBookmarkDown(getBookmarkByPath(path));
+               getCategoryByPath(parentPath(path)).removeBookmark(bookmark);
+               synchronized(bookmarks) {
+                       bookmarks.remove(path);
+               }
+       }

-        if (store) {
-            storeBookmarks();
-        }
-    }
+       public void moveBookmarkUp(String path, boolean store) {
+               BookmarkCategory parent = getCategoryByPath(parentPath(path));
+               parent.moveBookmarkUp(getBookmarkByPath(path));

-    private BookmarkCategory makeParents(String path) {
-        boolean isInPath = false;
-        synchronized (bookmarks) {
-            isInPath = bookmarks.containsKey(path);
-        }
-        if (isInPath) {
-            return getCategoryByPath(path);
-        } else {
+               if(store)
+                       storeBookmarks();
+       }

-            int index = path.substring(0, path.length() - 1).lastIndexOf("/");
-            String name = path.substring(index + 1, path.length() - 1);
+       public void moveBookmarkDown(String path, boolean store) {
+               BookmarkCategory parent = getCategoryByPath(parentPath(path));
+               parent.moveBookmarkDown(getBookmarkByPath(path));

-            BookmarkCategory cat = new BookmarkCategory(name);
-            makeParents(parentPath(path));
-            addBookmark(parentPath(path), cat);
+               if(store)
+                       storeBookmarks();
+       }

-            return cat;
-        }
-    }
+       private BookmarkCategory makeParents(String path) {
+               boolean isInPath = false;
+               synchronized(bookmarks) {
+                       isInPath = bookmarks.containsKey(path);
+               }
+               if(isInPath)
+                       return getCategoryByPath(path);
+               else {

-    private void putPaths(String path, Bookmark b) {
-        synchronized (bookmarks) {
-            bookmarks.put(path, b);
-        }
-        if (b instanceof BookmarkCategory) {
-            for (int i = 0; i < ((BookmarkCategory) b).size(); i++) {
-                Bookmark child = ((BookmarkCategory) b).get(i);
-                putPaths(path + child.getName() + (child instanceof 
BookmarkItem ? "" : "/"), child);
-            }
-        }
+                       int index = path.substring(0, path.length() - 
1).lastIndexOf("/");
+                       String name = path.substring(index + 1, path.length() - 
1);

-    }
+                       BookmarkCategory cat = new BookmarkCategory(name);
+                       makeParents(parentPath(path));
+                       addBookmark(parentPath(path), cat);

-    private void removePaths(String path) {
-        if (getBookmarkByPath(path) instanceof BookmarkCategory) {
-            BookmarkCategory cat = getCategoryByPath(path);
-            for (int i = 0; i < cat.size(); i++) {
-                removePaths(path + cat.get(i).getName() + (cat.get(i) 
instanceof BookmarkCategory ? "/" : ""));
-            }
-        }
-        bookmarks.remove(path);
-    }
+                       return cat;
+               }
+       }

-    public FreenetURI[] getBookmarkURIs() {
-        BookmarkItems items = MAIN_CATEGORY.getAllItems();
-        FreenetURI[] uris = new FreenetURI[items.size()];
-        for (int i = 0; i < items.size(); i++) {
-            uris[i] = items.get(i).getURI();
-        }
+       private void putPaths(String path, Bookmark b) {
+               synchronized(bookmarks) {
+                       bookmarks.put(path, b);
+               }
+               if(b instanceof BookmarkCategory)
+                       for(int i = 0; i < ((BookmarkCategory) b).size(); i++) {
+                               Bookmark child = ((BookmarkCategory) b).get(i);
+                               putPaths(path + child.getName() + (child 
instanceof BookmarkItem ? "" : "/"), child);
+                       }

-        return uris;
-    }
+       }

-    public void storeBookmarks() {
-        Logger.normal(this, "Attempting to save bookmarks to " + 
bookmarksFile.toString());
-        SimpleFieldSet sfs = null;
-        synchronized (bookmarks) {
-            if (isSavingBookmarks) {
-                return;
-            }
-            isSavingBookmarks = true;
-           
-            sfs = toSimpleFieldSet();
-        }
-        FileOutputStream fos = null;
-        try {
-            fos = new FileOutputStream(backupBookmarksFile);
-            sfs.writeTo(fos);
-            fos.close();
-            if (!FileUtil.renameTo(backupBookmarksFile, bookmarksFile)) {
-                Logger.error(this, "Unable to rename " + 
backupBookmarksFile.toString() + " to " + bookmarksFile.toString());
-            }
-        } catch (IOException ioe) {
-            Logger.error(this, "An error has occured saving the bookmark file 
:" + ioe.getMessage(), ioe);
-        } finally {
-            Closer.close(fos);
-            
-            synchronized (bookmarks) {
-                isSavingBookmarks = false;
-            }
-        }
-    }
+       private void removePaths(String path) {
+               if(getBookmarkByPath(path) instanceof BookmarkCategory) {
+                       BookmarkCategory cat = getCategoryByPath(path);
+                       for(int i = 0; i < cat.size(); i++)
+                               removePaths(path + cat.get(i).getName() + 
(cat.get(i) instanceof BookmarkCategory ? "/" : ""));
+               }
+               bookmarks.remove(path);
+       }

-    private void readBookmarks(BookmarkCategory category, SimpleFieldSet sfs) {
-       try {
-           int bookmarkVersion = sfs.getInt("Version");
-           
-           _innerReadBookmarks("", category, sfs);
-       } catch (FSParseException e) {
-           _innerReadTrunkBookmarks("", category, sfs);
+       public FreenetURI[] getBookmarkURIs() {
+               BookmarkItems items = MAIN_CATEGORY.getAllItems();
+               FreenetURI[] uris = new FreenetURI[items.size()];
+               for(int i = 0; i < items.size(); i++)
+                       uris[i] = items.get(i).getURI();
+
+               return uris;
        }
-    }
-    
-    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);
-                   }
+       public void storeBookmarks() {
+               Logger.normal(this, "Attempting to save bookmarks to " + 
bookmarksFile.toString());
+               SimpleFieldSet sfs = null;
+               synchronized(bookmarks) {
+                       if(isSavingBookmarks)
+                               return;
+                       isSavingBookmarks = true;
+
+                       sfs = toSimpleFieldSet();
                }
+               FileOutputStream fos = null;
+               try {
+                       fos = new FileOutputStream(backupBookmarksFile);
+                       sfs.writeTo(fos);
+                       fos.close();
+                       if(!FileUtil.renameTo(backupBookmarksFile, 
bookmarksFile))
+                               Logger.error(this, "Unable to rename " + 
backupBookmarksFile.toString() + " to " + bookmarksFile.toString());
+               } catch(IOException ioe) {
+                       Logger.error(this, "An error has occured saving the 
bookmark file :" + ioe.getMessage(), ioe);
+               } finally {
+                       Closer.close(fos);

-               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"));
+                       synchronized(bookmarks) {
+                               isSavingBookmarks = false;
+                       }
                }
+       }

-           } catch(FSParseException e) {
-               Logger.error(this, "Error parsing the bookmarks file!", e);
-               hasBeenParsedWithoutAnyProblem = false;
-           }
-           
-        }
-        if(hasBeenParsedWithoutAnyProblem)
-            storeBookmarks();
-    }
-    
-    /* FIXME: remove that kludge when 1076 is out */
-    private void _innerReadTrunkBookmarks(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);
-            
-            String[] categories = sfs.namesOfDirectSubsets();
-            for (int i = 0; i < categories.length; i++) {
-                SimpleFieldSet subset = sfs.subset(categories[i]);
-                BookmarkCategory currentCategory = new 
BookmarkCategory(categories[i]);
-                String name = prefix + category.name + '/';
-                category.addBookmark(currentCategory);
-                _innerReadTrunkBookmarks((isRoot ? "/" : name), 
currentCategory, subset);
-            }
-                        
-            Iterator it = sfs.toplevelKeyIterator();
-            while (it.hasNext()) {
-                String key = (String) it.next();
-                String line = sfs.get(key);
-                try {
-                    BookmarkItem item = new BookmarkItem(line, node.alerts);
-                    String name = (isRoot ? "" : prefix + category.name) + '/' 
+item.name;
-                    putPaths(name, item);
-                    category.addBookmark(item);
-                } catch (MalformedURLException e) {
-                    Logger.error(this, "Error while adding one of the 
bookmarks :" + e.getMessage(), e);
-                    hasBeenParsedWithoutAnyProblem = false;
-                }
-            }
-        }
-        if(hasBeenParsedWithoutAnyProblem)
-            storeBookmarks();
-    }
-    
-    public SimpleFieldSet toSimpleFieldSet() {
-       SimpleFieldSet sfs = new SimpleFieldSet(true);
+       private void readBookmarks(BookmarkCategory category, SimpleFieldSet 
sfs) {
+               try {
+                       int bookmarkVersion = sfs.getInt("Version");

-       sfs.put("Version", 1);
-       synchronized (bookmarks) {
-           
sfs.putAllOverwrite(BookmarkManager.toSimpleFieldSet(MAIN_CATEGORY));
+                       _innerReadBookmarks("", category, sfs);
+               } catch(FSParseException e) {
+                       _innerReadTrunkBookmarks("", category, sfs);
+               }
        }
-       
-       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());
+
+       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();
        }
-       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;
-    }
+
+       /* FIXME: remove that kludge when 1076 is out */
+       private void _innerReadTrunkBookmarks(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);
+
+                       String[] categories = sfs.namesOfDirectSubsets();
+                       for(int i = 0; i < categories.length; i++) {
+                               SimpleFieldSet subset = 
sfs.subset(categories[i]);
+                               BookmarkCategory currentCategory = new 
BookmarkCategory(categories[i]);
+                               String name = prefix + category.name + '/';
+                               category.addBookmark(currentCategory);
+                               _innerReadTrunkBookmarks((isRoot ? "/" : name), 
currentCategory, subset);
+                       }
+
+                       Iterator it = sfs.toplevelKeyIterator();
+                       while(it.hasNext()) {
+                               String key = (String) it.next();
+                               String line = sfs.get(key);
+                               try {
+                                       BookmarkItem item = new 
BookmarkItem(line, node.alerts);
+                                       String name = (isRoot ? "" : prefix + 
category.name) + '/' + item.name;
+                                       putPaths(name, item);
+                                       category.addBookmark(item);
+                               } catch(MalformedURLException e) {
+                                       Logger.error(this, "Error while adding 
one of the bookmarks :" + e.getMessage(), e);
+                                       hasBeenParsedWithoutAnyProblem = false;
+                               }
+                       }
+               }
+               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;
+       }
 }


Reply via email to