Example 2

Replace all occurrences of strings matching the regular expression pattern dat(a)+ in the text file txt with the same string enclosed in <code> tags using a closure to generate the replacement text.

Groovy

import org.schmant.task.proxy.ReplaceSourceFileTF import org.schmant.task.text.TextReplaceTF new ReplaceSourceFileTF(). addSource(txt). setTaskFactory( new TextReplaceTF(). // Use a closure to return the replacement text. // // Even though we don't use the matcher argument, we have to define it for // Groovy to accept the call to the closure. addReplace("dat(a)+", { text, matcher -> "<code>" + text + "</code>" } )).run()

JavaScript

new ReplaceSourceFileTF(). addSource(txt). setTaskFactory( new TextReplaceTF(). // Use a closure to return the replacement text. We don't care about the // Matcher, so the closure takes only one argument. addReplace("dat(a)+", function(text) { return "<code>" + text + "</code>"; })).run();

JRuby

# Use a lambda to generate the replacement text. We can use a lambda since the # expression used only consists of one statement. Otherwise we would have had to # define a separate function object to use as in the next example. Schmant::ReplaceSourceFileTF::new. addSource($txt). setTaskFactory( Schmant::TextReplaceTF.new. addReplace("dat(a)+", lambda { |text, matcher| "<code>" + text + "</code>" } )).run

Jython

# Use a lambda expression to generate the replacement text. We can use a lambda # since the expression used only consists of one statement. Otherwise we would # have had to define a separate function object to use as in the next example. ReplaceSourceFileTF(). \ addSource(txt). \ setTaskFactory( \ TextReplaceTF(). \ addReplace("dat(a)+", lambda text, matcher: "<code>" + text + "</code>" )).run()

Example 3

Replace all occurrences of strings matching the regular expression pattern #insert:.*?# (for instance #insert:d2/f.txt# matches) in the file f with the contents of the file that the "tag" references. The "tag" files are stored in or under the directory d. The result is put in the file fp.

Groovy

import RelativeLocation import org.entityfs.util.* import org.schmant.task.text.TextReplaceTF new TextReplaceTF(). setSource(f). setTarget(fp). // Use a closure to return the replacement text. The closure will access the // variable "d" that is defined further up in the script (not included in the // example). // // We have to define the "matcher" parameter even though we don't use it. addReplace( "#insert:d2/f.txt#", { text, matcher -> // Extract the name of the file to insert def f = text.substring(8, text.length() - 1); return Files.readTextFile( Directories.getFile(d, new RelativeLocation(f))); }).run()

JavaScript

new TextReplaceTF(). setSource(f). setTarget(fp). // Use a closure to return the replacement text. The closure will access the // variable "d" that is defined further up in the script (not included in the // example). // // Since we don't use it, JavaScript lets us skip defining the second Matcher // argument. addReplace( "#insert:d2/f.txt#", function(text) { // Extract the name of the file to insert var f = text.substring(8, text.length - 1); return Files.readTextFile( Directories.getFile(d, new RelativeLocation(f))); }).run();

JRuby

# Use a Proc object to return the replacement text. The Proc object must have # the same number of arguments as it is called with, so we have to define the # matcher argument even though we don't use it. # # The Proc object will use the global variable "d" defined somewhere above in # the script (not shown in the example). Schmant::TextReplaceTF.new. setSource($f). setTarget($fp). addReplace( "#insert:d2/f.txt#", Proc.new { |text, matcher| # Dig out the file reference from the string f = text[8..(text.length - 2)] # Read and return the contents of the file. JRuby does not seem to like us # using "return" here. Just give the return value and it will be the value # returned from the function. Schmant::Files.readTextFile( Schmant::Directories.getFile( $d, Schmant::RelativeLocation.new(f))) }).run

Jython

# Use a function object to return the replacement text. The lambda function will # make use of the script variable "d" that is defined somewhere above in the # build script (not included in the example). # # A Python function object must have the same number of arguments as the caller # calls it with, in this case two. That means that we must have an argument for # the Matcher even though we don't use it in the function. def getReplace(text, matcher): # Dig out the file reference from the string f = text[8:len(text) - 1] return Files.readTextFile( \ Directories.getFile(d, RelativeLocation(f))) TextReplaceTF(). \ setSource(f). \ setTarget(fp). \ addReplace("#insert:d2/f.txt#", getReplace).run()

Example 4

In the file report.html, add the text " - confirmed!" to all occurrences of "wild hypothesis no n", where n is a digit. Use capturing group zero to repeat the whole matched text.

JavaScript

new TextReplaceTF(). setSource(new File("report.html")). setTarget(new File("report-final.html")). addReplace("wild hypothesis \\d+", "\\0 - confirmed!").run(); // Given the following report.html: // Jada jada, wild hypothesis 0 Jada jada wild hypothesis 1 // // report-final.html will contain (on one row): // Jada jada, wild hypothesis 0 - confirmed! Jada jada wild hypothesis 1 - \ // confirmed!

Example 5

"Auto-comment" each start tag in the file info.html. Use capturing group zero to repeat the whole matched text and capturing group one to repeat the name of the tag.

JavaScript

new ReplaceSourceFileTF(). setSource(new File("info.html")). setTaskFactory( new TextReplaceTF(). addReplace( // A regular expression that matches all start tags, independent of // case. Pattern.compile( "<\\s*(\\p{Alpha}+)\\s?(\\s|\\p{Print})*?>", Pattern.CASE_INSENSITIVE), "\\0<!-- A \\1 tag -->")).run(); // Given the following info.html: // <html> // <HEAD id="foo"> // <title>Title</title> // </HEAD> // <body type="large">foo</body> // </html> // // After the transformation, it will contain: // <html><!-- A html tag --> // <HEAD id="foo"><!-- A HEAD tag --> // <title><!-- A title tag -->Title</title> // </HEAD> // <body type="large"><!-- A body tag -->foo</body> // </html>

Example 6

Replace the ###city### and ###country### placeholders with values supplied in an EntityFS Properties object.

JavaScript

// The PropertyValueTextStrategy uses EntityFS' Properties object. p = new PropertiesImpl(); p.putStringValue("country", "Sweden"); p.putStringValue("city", "Stockholm"); new ReplaceSourceFileTF(). setSource(new File("weather_report.txt")). setTaskFactory( new TextReplaceTF(). addReplace( // A regular expression that matches our placeholder format ###x###. // Use capturing group 1 to extract the placeholder name. Pattern.compile("###(\\w+)###"), new PropertyValueTextStrategy( p, // Use capturing group 1 1, // We want an exception if we encounter an unknown property key. // Don't ignore missing properties. false))).run(); // Given the following weather_report.txt: // Forecast for ###city###, ###country###: // Cloudy, zero degrees, 50% chance of precipitation. // // After the transformation, it will contain: // Forecast for Stockholm, Sweden: // Cloudy, zero degrees, 50% chance of precipitation.


* An EntityFS-aware task is implemented using EntityFS. This means that it uses the filter settings of DirectoryView:s and also that it often can work with other file system implementations than File-based, such as the RAM file system.