Using EntityFS in a Groovy program

Revision History
Revision 1.02009.05.29

Table of Contents

Combining filters
Grep with filters
Filters in switch statements

This document contains some notes on how to best use EntityFS from a Groovy program.

All Filter:s extending AbstractConvenientFilter (which all filters bundled with EntityFS do) can be combined using Groovy's & (and), | (or) and ^ (xor) operators. A filter can be negated using the ~ (bitwise negate) operator.


Filter:s extending AbstractConvenientFilter can also be used with a collection's grep method to select the elements in the collection matching the filter.

Example 2. Using grep to select files in a collection

import org.entityfs.util.Directories
import org.entityfs.util.filter.entity.*

// List all files in the directory d
def files = Directories.listEntities(d)

// Use grep to return all XML files that contain the <foo> element.
def filter = new EFileNameExtensionFilter(".xml") &
  new FGrepFilter("<foo>")

// Get all XML files
def xmlFiles = files.grep(filter)

Filters can be used in Groovy switch statements. In the example below, a switch statement is used with an entity filter to log different statements for directories that contain less than or exactly or more than four XML files.