...I sent you an old version (security does not work!). Here is the
corrected version.
Lewis
/*
* $Header:
/home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/ui/WebdavSystemView.java,v
1.1.2.1 2004/10/11 08:18:14 luetzkendorf Exp $
* $Revision: 1.1.2.1 $
* $Date: 2004/10/11 08:18:14 $
*
* ====================================================================
*
* Copyright 1999-2002 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.webdav.ui;
import org.apache.commons.httpclient.HttpURL;
import org.apache.commons.httpclient.URIException;
import org.apache.webdav.lib.WebdavException;
import org.apache.webdav.lib.WebdavResource;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
/**
* WebdavSystemView.java
*/
public class WebdavSystemView extends FileSystemView {
private HttpURL rootURI = null;
private String rootPath = null;
private WebdavFile homedir = null;
private static final String newFolderString =
UIManager.getString("FileChooser.other.newFolder");
static FileSystemView fsv = null;
public WebdavSystemView(URL rootURL) throws Exception {
try {
rootPath = rootURL.getPath();
rootURI = new HttpURL(rootURL.toString());
rootURI.setPath(null);
// Create home directory object
homedir = new WebdavFile(new HttpURL(rootURI,
rootPath));
} catch (Throwable e) {
throw new Exception("Unable to access dav server.");
}
}
private static HttpURL uriToHttpURL(HttpURL uri, String relative)
throws URIException {
if (!(uri.getScheme().equals("http") ||
uri.getScheme().equals("https"))) {
throw new URIException("Unknown protocol in URL " +
uri);
}
return new HttpURL(uri, relative.replaceAll(" ", "%20"));
}
public void disconnect(WebdavResource webdavResource) throws
java.lang.UnknownError {
try {
webdavResource.close();
} catch (Exception e) {
System.err.println(e.toString());
throw new UnknownError();
}
}
public WebdavResource connect() throws java.lang.IllegalAccessError {
try {
return new WebdavResource(new HttpURL(rootURI,
rootPath));
} catch (Exception e) {
System.err.println(e.toString());
throw new IllegalAccessError();
}
}
public static FileSystemView getFileSystemView() {
try {
if (fsv == null) {
fsv = new WebdavSystemView(new
URL("http://guest:[EMAIL PROTECTED]:8080/slide/files"));
}
return fsv;
} catch (Exception e) {
System.err.println(e.toString());
return null;
}
}
/**
* Returns a File object constructed in dir from the given filename.
*/
public File createFileObject(File dir, String filename) {
File file = null;
try {
if (dir == null) {
file = new File(filename);
} else {
HttpURL url = uriToHttpURL(rootURI,
dir.getPath() + WebdavFile.davSeparatorChar + filename);
file = new WebdavFile(url,
WebdavResource.NOACTION);
}
} catch (IOException e) {
System.err.println(e.toString());
}
return file;
}
/**
* Returns a File object constructed from the given path string.
*/
public File createFileObject(String path) {
File f = new File(path);
if (isFileSystemRoot(f)) {
f = createFileSystemRoot(f);
}
return f;
}
/**
* Creates a new folder with a default folder name.
*/
public File createNewFolder(File containingDir) throws IOException {
WebdavResource webdavResource = null;
try {
if (containingDir == null) {
throw new IOException("Containing directory is
null:");
}
HttpURL url = uriToHttpURL(rootURI,
containingDir.getPath() + WebdavFile.davSeparatorChar + newFolderString);
// Need to add user info so has access for queries
WebdavFile newFolder = new WebdavFile(url,
WebdavResource.NOACTION);
webdavResource = connect();
if
(webdavResource.mkcolMethod(newFolder.getAbsolutePath())) {
return newFolder;
} else {
System.err.println("failed.");
System.err.println(webdavResource.getStatusMessage());
throw new
IOException(webdavResource.getStatusMessage());
}
} catch (IOException e) {
throw e;
} catch (Exception e) {
System.err.println(e.toString());
e.printStackTrace();
return null;
} finally {
disconnect(webdavResource);
}
}
/**
* Returns all root partitions on this system. For example, on
* Windows, this would be the "Desktop" folder, while on DOS this
* would be the A: through Z: drives.
*/
public File[] getRoots() {
try {
return new WebdavFile[]{this.homedir};
} catch (Exception e) {
System.err.println(e.toString());
e.printStackTrace();
return null;
}
}
/**
* Returns true if the file (directory) can be visited.
* Returns false if the directory cannot be traversed.
*
* @param f the <code>File</code>
* @return <code>true</code> if the file/directory can be traversed,
* otherwise <code>false</code>
* @see javax.swing.JFileChooser#isTraversable
* @see javax.swing.filechooser.FileView#isTraversable
*/
public Boolean isTraversable(File f) {
try {
return f.isDirectory() ? Boolean.TRUE : Boolean.FALSE;
} catch (Exception e) {
System.err.println(e.toString());
e.printStackTrace();
return Boolean.FALSE;
}
}
/**
* Name of a file, directory, or folder as it would be displayed in
* a system file browser. Example from Windows: the "M:\" directory
* displays as "CD-ROM (M:)"
* <p/>
* The default implementation gets information from the ShellFolder
* class.
*
* @param f a <code>File</code> object
* @return the file name as it would be displayed by a native file
* chooser
* @see javax.swing.JFileChooser#getName
*/
public String getSystemDisplayName(File f) {
try {
return f.getName();
} catch (Exception e) {
System.err.println(e.toString());
e.printStackTrace();
return null;
}
}
/**
* Type description for a file, directory, or folder as it would be
* displayed in
* a system file browser. Example from Windows: the "Desktop" folder
* is desribed as "Desktop".
* <p/>
* Override for platforms with native ShellFolder implementations.
*
* @param f a <code>File</code> object
* @return the file type description as it would be displayed by a
* native file chooser or null if no native information is
* available.
* @see javax.swing.JFileChooser#getTypeDescription
*/
public String getSystemTypeDescription(File f) {
return null;
}
/**
* Checks if <code>f</code> represents a real directory or file as
* opposed to a special folder such as <code>"Desktop"</code>. Used by
UI
* classes to decide if a folder is selectable when doing directory
* choosing.
*
* @param f a <code>File</code> object
* @return <code>true</code> if <code>f</code> is a real file or
directory.
*/
public boolean isFileSystem(File f) {
return true;
}
/**
* Returns whether a file is hidden or not.
*/
public boolean isHiddenFile(File f) {
return f.isHidden();
}
/**
* Is dir the root of a tree in the file system, such as a drive
* or partition. Example: Returns true for "C:\" on Windows 98.
*
* @param dir a <code>File</code> object representing a directory
* @return <code>true</code> if <code>f</code> is a root of a filesystem
* @see #isRoot
*/
public boolean isFileSystemRoot(File dir) {
try {
return (rootPath.equals(dir.getPath()));
}
catch (Exception e) {
System.err.println("isFileSystemRoot" + e.toString());
e.printStackTrace();
return false;
}
}
/**
* Used by UI classes to decide whether to display a special icon
* for drives or partitions, e.g. a "hard disk" icon.
* <p/>
* The default implementation has no way of knowing, so always returns
* false.
*
* @param dir a directory
* @return <code>false</code> always
*/
public boolean isDrive(File dir) {
return false;
}
/**
* Used by UI classes to decide whether to display a special icon
* for a floppy disk. Implies isDrive(dir).
* <p/>
* The default implementation has no way of knowing, so always returns
* false.
*
* @param dir a directory
* @return <code>false</code> always
*/
public boolean isFloppyDrive(File dir) {
return false;
}
/**
* Used by UI classes to decide whether to display a special icon
* for a computer node, e.g. "My Computer" or a network server.
* <p/>
* The default implementation has no way of knowing, so always returns
* false.
*
* @param dir a directory
* @return <code>false</code> always
*/
public boolean isComputerNode(File dir) {
return false;
}
// Providing default implementations for the remaining methods
// because most OS file systems will likely be able to use this
// code. If a given OS can't, override these methods in its
// implementation.
public File getHomeDirectory() {
return this.homedir;
}
/**
* Return the user's default starting directory for the file chooser.
*
* @return a <code>File</code> object representing the default
* starting folder
*/
public File getDefaultDirectory() {
return this.homedir;
}
/**
* Gets the list of shown (i.e. not hidden) files.
*/
public File[] getFiles(File dir, boolean useFileHiding) {
WebdavResource webdavResource = null;
try {
String filenames[];
WebdavFile files[];
HttpURL url;
String path;
String localDir;
webdavResource = connect();
path = dir.getPath();
if (!path.endsWith("/")) {
path += "/";
}
webdavResource.setPath(path);
filenames = webdavResource.list();
files = new WebdavFile[filenames.length];
for (int i = 0; i < filenames.length; i++) {
localDir = dir.getPath();
if (!localDir.endsWith("/")) localDir =
localDir + "/";
url = new HttpURL(rootURI, localDir +
filenames[i]);
files[i] = new WebdavFile(url);
}
return files;
} catch (Exception e) {
System.err.println(e.toString());
e.printStackTrace();
return null;
} finally {
disconnect(webdavResource);
}
}
/**
* Returns the parent directory of <code>dir</code>.
*
* @param dir the <code>File</code> being queried
* @return the parent directory of <code>dir</code>, or
* <code>null</code> if <code>dir</code> is <code>null</code>
*/
public File getParentDirectory(File dir) {
if (dir == null) {
return null;
} else if (dir.equals(this.homedir)) {
return this.homedir;
} else {
return dir.getParentFile();
}
}
/**
* Creates a new <code>File</code> object for <code>f</code> with
* correct behavior for a file system root directory.
*
* @param f a <code>File</code> object representing a file system root
* directory, for example "/" on Unix or "C:\" on Windows.
* @return a new <code>File</code> object
*/
protected File createFileSystemRoot(File f) {
try {
return new FileSystemRoot((WebdavFile) f);
} catch (Exception e) {
System.err.println("createFileSystemRoot : " +
e.toString());
return null;
}
}
protected class WebdavFile extends org.apache.webdav.lib.WebdavFile {
private WebdavResource webdavResource = null;
public WebdavFile(HttpURL pathUrl) throws
URIException, IOException {
this(pathUrl, WebdavResource.BASIC);
}
public WebdavFile(HttpURL pathUrl, int action) throws
URIException, IOException {
super(pathUrl);
this.webdavResource = new WebdavResource(pathUrl,
action, 0);
}
public String getPath() {
String path = null;
try {
URI uri = new URI(super.getPath().replaceAll("
", "%20"));
path = uri.getPath();
} catch (URISyntaxException e) {
System.err.println("getPath() Exception =>" +
e);
}
return path;
}
public String getParent() {
try {
String path =
webdavResource.getHttpURL().getPath();
// If we are at the root already
if (rootPath.equalsIgnoreCase(path)) {
return null;
} else {
// otherwise call original
return super.getParent();
}
}
catch (Exception e) {
System.err.println(e.toString());
e.printStackTrace();
return null;
}
}
public File getParentFile() {
try {
String parent = getParent();
if (parent == null)
return null;
HttpURL url = new HttpURL(rootURI, parent);
return new WebdavFile(url);
} catch (Exception e) {
System.err.println(e.toString());
e.printStackTrace();
return null;
}
}
public boolean exists() {
return this.webdavResource.exists();
}
public boolean isDirectory() {
return this.webdavResource.isCollection();
}
public boolean createNewFile() {
try {
return webdavResource.putMethod("");
} catch (Exception e) {
throw new WebdavException(e);
} finally {
closeRes(webdavResource);
}
}
private void closeRes(WebdavResource res) {
try {
if (res != null) {
res.close();
}
} catch (Exception e) {
throw new WebdavException(e);
}
}
}
class FileSystemRoot extends WebdavFile {
public FileSystemRoot(WebdavFile webdavFile) throws
URIException,
IOException {
super(new HttpURL(rootURI, rootPath));
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]