001 /*
002 * This file is part of muCommander, http://www.mucommander.com
003 * Copyright (C) 2002-2008 Maxence Bernard
004 *
005 * muCommander is free software; you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation; either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * muCommander is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program. If not, see <http://www.gnu.org/licenses/>.
017 */
018
019 package com.mucommander.bookmark.file;
020
021 import com.mucommander.bookmark.Bookmark;
022 import com.mucommander.bookmark.BookmarkManager;
023 import com.mucommander.file.AbstractFile;
024 import com.mucommander.file.FileFactory;
025 import com.mucommander.file.FileURL;
026 import com.mucommander.file.ProtocolProvider;
027
028 import java.io.IOException;
029
030 /**
031 * This class is the provider for the bookmark filesystem implemented by {@link com.mucommander.bookmark.file.BookmarkFile}.
032 *
033 * @author Nicolas Rinaudo
034 * @see com.mucommander.bookmark.file.BookmarkFile
035 */
036 public class BookmarkProtocolProvider implements ProtocolProvider {
037
038 public AbstractFile getFile(FileURL url) throws IOException {
039 // If the URL contains a path but no host, it's illegal.
040 // If it contains neither host nor path, we're browsing bookmarks://
041 if(url.getHost() == null) {
042 if(url.getPath().equals("/"))
043 return new BookmarkRoot(url);
044 throw new IOException();
045 }
046
047 // If the URL contains a host, look it up in the bookmark list and use that
048 // as the root of the returned path.
049 else {
050 Bookmark bookmark;
051 // If the bookmark doesn't exist, but a path is specified, throws an exception.
052 // Otherwise, returns the requested bookmark.
053 if((bookmark = BookmarkManager.getBookmark(url.getHost())) == null) {
054 if(!url.getPath().equals("/"))
055 throw new IOException();
056 return new BookmarkFile(new Bookmark(url.getHost(), url.getPath()));
057 }
058
059 // If the bookmark exists, and a path is specified, creates a new path
060 // from the bookmark's location and the specified path.
061 if(!url.getPath().equals("/"))
062 return FileFactory.getFile(bookmark.getLocation() + url.getPath());
063
064 // Otherwise, creates a new bookmark file.
065 return new BookmarkFile(bookmark);
066 }
067 }
068 }