Home:
» Download
» Current release
» Documentation
» FAQ
» Licensing
» News
» Mailing lists
» Please help!



Built by Schmant

Get EntityFS at SourceForge.net. Fast, secure and Free Open Source software downloads

XML file copying example


This is an example that demonstrates the benefits of using EntityFS over Java's java.io.File for implementing a simple XML copying routine.

Assuming the requirement:
  Search the directory hierarchy under d for XML files edited in the last 24 hours. Copy them to the ’today’ catalog.

Here is how it can be implemented using standard Java:

Queue<File> q = new LinkedList<File>();
q.addAll(Arrays.asList(d.listFiles()));
// We cannot use an Iterator here since we'll be modifying the queue while
// iterating over it.
while(!q.isEmpty()) 
{
  // Take the first element in the queue
  File f = q.remove();
  if (f.isDirectory())
  {
    // Add contents of the directory to the queue
    q.addAll(Arrays.asList(f.listFiles()));
  }
  else if (f.isFile() &&
    f.getName().endsWith("xml") &&
    isInLast24Hours(f.lastModified()))
  {
    // Copy the XML file to the today directory
    copy(f, today);
  }
}
This requires us to write our own isInLast24Hours and copy methods. Implementing copy forces us to dig down into Java NIO or to juggle temporary byte arrays.

Here is how the same requirement can be implemented using EntityFS:

Iterator<EntityView> search = Directories.getDepthLastIterator(d);

// Create filters that match the recently modified XML files
ConvenientEntityFilter xmlExt = new EFileNameExtensionFilter("xml");
ConvenientEntityFilter recent = 
  new EntityRecentModificationFilter(24, TimeUnit.HOURS);

// Create a filtering iterator that filters entities returned from the depth
// last iterator
Iterator<EntityView> xmls = 
  new FilteringIterator<EntityView>(search, xmlExt.and(recent)));

// Copy all XML files returned from the iterator to the today directory
while (xmls.hasNext()) {
  Entities.copy(xmls.next(), today);
}

Here, benefits from using EntityFS include:

To the front page.