//============================================================================== // FDirectory.java //============================================================================== package tribble.search.disk; // System imports import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.lang.Exception; import java.lang.String; import java.lang.System; import java.lang.UnsupportedOperationException; import java.net.MalformedURLException; import java.net.URL; import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; // Local imports import tribble.search.DocumentI; import tribble.search.DocumentFilterI; import tribble.search.DocumentSearcherI; /******************************************************************************* * File directory searcher. * * @version $Revision: 1.3 $ $Date: 2001/06/18 03:01:54 $ * @since 2001-03-02 * @author * David R. Tribble * (david@tribble.com). *
* Copyright * ©2001 by David R. Tribble, all rights reserved.
* * @see FFile */ public class FDirectory implements DocumentSearcherI { // Identification /** Revision information. */ static final String REV = "@(#)tribble/search/disk/FDirectory.java $Revision: 1.3 $ $Date: 2001/06/18 03:01:54 $\n"; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Public constants /** Series number. */ public static final int SERIES = 200; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Protected variables /** Directory pathname. */ protected File m_path; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Public constructors /*************************************************************************** * Default constructor. * * @since 1.1, 2001-05-17 */ public FDirectory() { // Do nothing } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Public methods /*************************************************************************** * Initialize this directory searcher. * *

* Note: This method does not actually do anything. * * @param parms * A table containing name/value pairs with which to initialize this * directory searcher. This can be null. * * @since 1.1, 2001-05-10 */ public void initialize(Hashtable parms) //implements Tribble.Search.DocumentSearcherI { // Do nothing } /*************************************************************************** * Open a given pathname for this directory searcher. * * @param path * A pathname specifying the location of a directory to open and search. * This name must specify a directory filename on the local machine. * * @throws IOException * Thrown if the specified path does not exist or is not accessible, or if it * is not a directory, or if it is malformed, or if some other error occurs. * * @since 1.1, 2001-05-05 */ public void open(String path) throws IOException //implements Tribble.Search.DocumentSearcherI { File fname; // Open the pathname fname = new File(path); // Check that the filename is a directory if (!fname.isDirectory()) throw new IOException("Not a directory: " + fname.toString()); // Save the directory name info m_path = fname; } /*************************************************************************** * Open a given URL for this directory searcher. * * @param path * A URL specifying the location of the directory to open and search. * This URL must specify a directory filename on the local machine, and must * begin with the substring "file://". * * @throws MalformedURLException * Thrown if the specified URL is malformed. * * @throws IOException * Thrown if the specified URL does not exist or is not accessible, or if it * is malformed, or if some other error occurs. * * @since 1.1, 2001-03-02 */ public void open(URL path) throws IOException { File fname; // Extract the filename from the URL fname = new File(path.getPath(), path.getFile()); // Check args if (!path.getProtocol().equals("file://")) throw new MalformedURLException("URL must begin with \"file://\": " + fname.toString()); // Check that the filename is a directory if (!fname.isDirectory()) throw new IOException("Not a directory: " + fname.toString()); // Save the directory name info m_path = fname; } /*************************************************************************** * Close this directory searcher. * * @throws IOException * Thrown if an error occurs. * * @since 1.1, 2001-03-02 */ public void close() throws IOException //implements Tribble.Search.DocumentSearcherI { // Clear the directory name info m_path = null; } /*************************************************************************** * Retrieve file entries from this directory matching given search criteria. * * @param filt * A filter specifying file searching criteria. This can be null, in which * case all possible file entries for this searcher are returned. * * @return * An {@link FIterator} object containing an enumeration of {@link FFile} * objects representing the list of directory files found by this searcher, * or null if there are no matching files to be found. * * @throws IOException * Thrown if the specified criteria are malformed, or if some other error * occurs. * * @since 1.2, 2001-06-15 */ public Enumeration find(DocumentFilterI filt) throws IOException, Exception //implements Tribble.Search.DocumentSearcherI { String[] dirlist; Vector list; FIterator iter; // Get the complete list of all entries for this directory dirlist = m_path.list(); // Build a list of file entries from the directory list list = new Vector(dirlist.length); for (int i = 0; i < dirlist.length; i++) { File fname; FFile ent; // Create the next file entry fname = new File(m_path, dirlist[i]); ent = new FFile(this, fname); // Filter the file entry if (filt == null || filt.accept(ent)) list.add(ent); } // Check for zero matching entries if (list.size() == 0) return (null); // Create an iterator for the directory list iter = new FIterator(this, list); return (iter); } /*************************************************************************** * Create a new file entry for this directory searcher. * * @param name * The name of the new file entry to create in this directory. * * @param append * Specifies whether or not to append written data to the end of the file. * * @return * A new file entry corresponding to the newly created file entry. * * @throws Exception * Thrown if the file name is malformed, or if the file already exists, or if * some other error occurs. * * @since 1.2, 2001-06-15 */ public DocumentI create(String name, boolean append) throws IOException { File fname; FileOutputStream out; FFile fil; // Create/open a new output file for writing fname = new File(m_path, name); out = new FileOutputStream(fname.getAbsolutePath(), append); // Create a file entry for the newly created output file fil = new FFile(this, fname); fil.setOutput(out); return (fil); } /*************************************************************************** * Determine if new file entries can be created by this searcher. * * @return * True, always. * * @since 1.1, 2001-06-02 */ public boolean createSupported() { // Is supported return (true); } /*************************************************************************** * Determine if file entries can be written to this searcher. * * @return * True, always. * * @since 1.1, 2001-06-02 */ public boolean outputSupported() { // Is supported return (true); } } // End FDirectory.java