fSPath

From Wikipedia, the free encyclopedia
Jump to: navigation, search
FSPath
Developer(s) Keith Bishop
Stable release 1.0-alpha / April 29, 2008 (2008-04-29)
Written in Java (programming language)
Type API
License GPL
Website http://sugar.sourceforge.net/fspath

FSPath is a lightweight API written in Java. The goal of FSPath is to make interacting with the file system simpler. It was first released in April 2008 by Keith Bishop.

FSPath is licensed under the GNU General Public License and is free and open source software.

Features[edit]

The notable features of the FSPath API are

  1. A query language for defining file system queries (Based on XPath).
  2. A fluent interface[1] to enable the chaining of different file system operations such as copying, renaming and deleting.
  3. A plug-in mechanism for developers to implement custom file system operations by use of callbacks.

Usage[edit]

When used in conjunction, these two features provide the software developer with a great deal of power and flexibility in return for very few keystrokes.

The example below shows a typical recursive search algorithm implemented in Java, for searching for a file by name.

 public List<File> listFiles(File currentDirectory) {
 
    List<File> fileList = new ArrayList<File>();
 
    File[] files = currentDirectory.getFiles();
 
    for(int i = 0; i < files.length; i++) {
 
        if (file[i].isDirectory()) {
          fileList.addAll(this.listFiles(files[i]));
 
        } else if (file[i].getName().endsWith(".txt")) {
          fileList.add(file[i]);
 
        }
    }
 
    return fileList;
 
  }

The code above can be substituted with the following FSPath API calls...

  FSPath fspath = FSPathFactory.newFSPath();
 
  FSPathResultList results = fspath.query("//file[contains(@name, '.txt')]");

FSPath's fluent interface enables simple manipulation of files by 'chaining' the desired file system operation onto the search query...

  FSPath fspath = FSPathFactory.newFSPath();
 
  FSPathResultList results = fspath.query("//file[contains(@name, '.txt')]").delete();

To perform custom behaviour on a file or set of files, the developer can implement the net.sf.sugar.fspath.Callback interface. The example below shows an anonymous inner class being used as a callback.

  fspath.query("/dir")
        .each(new Callback() {
                      public void call(FSPathResult result) {
                          File file = result.getFile();
                          System.out.println(file.getName() + " has : "
                          + file.listFiles().size() + " children");
                      }
                  });

Further reading[edit]

References[edit]

  1. ^ "Fluent interface". Retrieved 2008-11-18.