Apache Groovy is a powerful multi-faceted programming language for the JVM platform
—
Configuration file parsing, XML processing, command-line interface building, and data structure utilities. These tools provide essential infrastructure for application configuration, data transformation, and user interface components.
ConfigSlurper provides a flexible way to parse configuration files written in Groovy syntax.
class ConfigSlurper {
/**
* Creates a ConfigSlurper with no environment set.
*/
ConfigSlurper();
/**
* Creates a ConfigSlurper for the specified environment.
*/
ConfigSlurper(String environment);
/**
* Parses configuration from a string.
*/
ConfigObject parse(String text);
/**
* Parses configuration from a URL.
*/
ConfigObject parse(URL location);
/**
* Parses configuration from a file.
*/
ConfigObject parse(File file);
/**
* Parses configuration from a Reader.
*/
ConfigObject parse(Reader reader);
/**
* Parses configuration from a Properties object.
*/
ConfigObject parse(Properties properties);
/**
* Sets the environment for conditional configuration.
*/
void setEnvironment(String environment);
/**
* Gets the current environment.
*/
String getEnvironment();
/**
* Sets the binding for the configuration script.
*/
void setBinding(Binding binding);
/**
* Gets the binding for the configuration script.
*/
Binding getBinding();
}
class ConfigObject extends LinkedHashMap<String, Object> implements Writable {
/**
* Creates an empty ConfigObject.
*/
ConfigObject();
/**
* Flattens nested configuration into dot-separated keys.
*/
ConfigObject flatten();
/**
* Flattens with a custom key separator.
*/
ConfigObject flatten(String separator);
/**
* Merges another ConfigObject into this one.
*/
ConfigObject merge(ConfigObject other);
/**
* Converts to Properties object.
*/
Properties toProperties();
/**
* Converts to Properties with a prefix.
*/
Properties toProperties(String prefix);
/**
* Writes the configuration to a Writer.
*/
Writer writeTo(Writer writer);
/**
* Checks if this ConfigObject is empty of meaningful values.
*/
boolean isEmpty();
/**
* Gets a nested value using dot notation.
*/
Object getProperty(String key);
/**
* Sets a nested value using dot notation.
*/
void setProperty(String key, Object value);
}CliBuilder provides a fluent API for parsing command line arguments.
class CliBuilder {
/**
* Creates a CliBuilder with default settings.
*/
CliBuilder();
/**
* Creates a CliBuilder with usage text.
*/
CliBuilder(String usage);
/**
* Defines a command line option.
*/
void addOption(String opt, String longOpt, boolean hasArg, String description);
/**
* Defines an option using builder pattern.
*/
OptionBuilder option(String opt);
/**
* Defines an option with short name only.
*/
void opt(String name, String description);
/**
* Defines an option with argument.
*/
void opt(String name, String argName, String description);
/**
* Defines an option with long name.
*/
void opt(String name, String longName, String description);
/**
* Parses command line arguments.
*/
OptionAccessor parse(String[] args);
/**
* Parses command line arguments with error handling.
*/
OptionAccessor parse(String[] args, boolean stopAtNonOption);
/**
* Prints usage information.
*/
void usage();
/**
* Gets the usage formatter.
*/
HelpFormatter getFormatter();
/**
* Sets the usage formatter.
*/
void setFormatter(HelpFormatter formatter);
/**
* Sets the header text for usage.
*/
void setHeader(String header);
/**
* Sets the footer text for usage.
*/
void setFooter(String footer);
/**
* Sets the program name for usage.
*/
void setProgramName(String programName);
}
interface OptionAccessor {
/**
* Checks if an option was provided.
*/
boolean hasOption(String opt);
/**
* Gets the value of an option.
*/
String getOptionValue(String opt);
/**
* Gets the value of an option with default.
*/
String getOptionValue(String opt, String defaultValue);
/**
* Gets all values for a multi-value option.
*/
String[] getOptionValues(String opt);
/**
* Gets the list of non-option arguments.
*/
List<?> getArgList();
/**
* Gets non-option arguments as array.
*/
String[] getArgs();
/**
* Gets all option properties as a Properties object.
*/
Properties getOptionProperties(String opt);
}XmlSlurper provides GPath-based XML parsing and navigation.
class XmlSlurper {
/**
* Creates an XmlSlurper with default settings.
*/
XmlSlurper();
/**
* Creates an XmlSlurper with validation enabled/disabled.
*/
XmlSlurper(boolean validating);
/**
* Creates an XmlSlurper with validation and namespace awareness.
*/
XmlSlurper(boolean validating, boolean namespaceAware);
/**
* Parses XML from a string.
*/
GPathResult parseText(String text);
/**
* Parses XML from a file.
*/
GPathResult parse(File file);
/**
* Parses XML from a URL.
*/
GPathResult parse(URL url);
/**
* Parses XML from an InputStream.
*/
GPathResult parse(InputStream input);
/**
* Parses XML from a Reader.
*/
GPathResult parse(Reader reader);
/**
* Sets the XML parser to use.
*/
void setXMLReader(XMLReader reader);
/**
* Gets the XML parser being used.
*/
XMLReader getXMLReader();
/**
* Sets an entity resolver.
*/
void setEntityResolver(EntityResolver resolver);
/**
* Sets an error handler.
*/
void setErrorHandler(ErrorHandler handler);
}
abstract class GPathResult implements Iterable<GPathResult>, Writable {
/**
* Gets the text content of this node.
*/
String text();
/**
* Gets the number of child nodes.
*/
int size();
/**
* Checks if this result is empty.
*/
boolean isEmpty();
/**
* Gets all child nodes.
*/
GPathResult children();
/**
* Gets the parent node.
*/
GPathResult parent();
/**
* Finds child nodes matching the closure condition.
*/
GPathResult find(Closure closure);
/**
* Finds all child nodes matching the closure condition.
*/
GPathResult findAll(Closure closure);
/**
* Gets an attribute value.
*/
String attribute(String name);
/**
* Gets all attributes.
*/
Map<String, String> attributes();
/**
* Gets the local name of this node.
*/
String name();
/**
* Gets the namespace URI of this node.
*/
String namespaceURI();
/**
* Converts to a Node object.
*/
Node convertToNode();
/**
* Gets child by index.
*/
GPathResult getAt(int index);
/**
* Gets child by name.
*/
GPathResult getProperty(String name);
}XmlParser creates Node trees for XML processing.
class XmlParser {
/**
* Creates an XmlParser with default settings.
*/
XmlParser();
/**
* Creates an XmlParser with validation enabled/disabled.
*/
XmlParser(boolean validating);
/**
* Creates an XmlParser with validation and namespace awareness.
*/
XmlParser(boolean validating, boolean namespaceAware);
/**
* Parses XML from a string.
*/
Node parseText(String text);
/**
* Parses XML from a file.
*/
Node parse(File file);
/**
* Parses XML from a URL.
*/
Node parse(URL url);
/**
* Parses XML from an InputStream.
*/
Node parse(InputStream input);
/**
* Parses XML from a Reader.
*/
Node parse(Reader reader);
/**
* Sets trimming of whitespace-only text nodes.
*/
void setTrimWhitespace(boolean trimWhitespace);
/**
* Gets whether whitespace-only text nodes are trimmed.
*/
boolean isTrimWhitespace();
/**
* Sets whether to keep ignorable whitespace.
*/
void setKeepIgnorableWhitespace(boolean keepIgnorableWhitespace);
}Node represents XML elements in a tree structure.
class Node implements Serializable {
/**
* Creates a Node with the given name.
*/
Node(String name);
/**
* Creates a Node with name and value.
*/
Node(String name, Object value);
/**
* Creates a Node with name and attributes.
*/
Node(String name, Map<String, String> attributes);
/**
* Creates a Node with name, attributes, and value.
*/
Node(String name, Map<String, String> attributes, Object value);
/**
* Gets the name of this node.
*/
Object name();
/**
* Gets the value of this node.
*/
Object value();
/**
* Sets the value of this node.
*/
void setValue(Object value);
/**
* Gets the attributes of this node.
*/
Map<String, String> attributes();
/**
* Gets an attribute value.
*/
Object attribute(String key);
/**
* Gets all child nodes.
*/
List<Node> children();
/**
* Gets the parent node.
*/
Node parent();
/**
* Gets the text content of this node.
*/
String text();
/**
* Gets child nodes with the given name.
*/
NodeList get(String key);
/**
* Gets child node at the given index.
*/
Object get(int index);
/**
* Appends a child node.
*/
Node append(Node child);
/**
* Removes a child node.
*/
boolean remove(Node child);
/**
* Adds a child node.
*/
void add(Node child);
/**
* Replaces this node with another.
*/
void replaceNode(Node replacement);
}
class NodeList extends ArrayList<Node> {
/**
* Gets the text content of all nodes.
*/
String text();
/**
* Gets the local text content (direct children only).
*/
List<String> localText();
/**
* Removes and returns the last element.
*/
Node pop();
}import groovy.util.ConfigSlurper;
import groovy.util.ConfigObject;
// Parse configuration from string
ConfigSlurper slurper = new ConfigSlurper("development");
String configText = """
database {
host = 'localhost'
port = 5432
username = 'admin'
}
environments {
development {
database.host = 'dev-server'
}
production {
database.host = 'prod-server'
}
}
""";
ConfigObject config = slurper.parse(configText);
System.out.println("Database host: " + config.getProperty("database.host"));
// Flatten configuration
ConfigObject flattened = config.flatten();
Properties props = flattened.toProperties();import groovy.util.CliBuilder;
import groovy.util.OptionAccessor;
// Build command line parser
CliBuilder cli = new CliBuilder("myapp [options] files...");
cli.setHeader("My Application");
cli.addOption("h", "help", false, "Show usage information");
cli.addOption("v", "verbose", false, "Enable verbose output");
cli.addOption("o", "output", true, "Output file name");
cli.addOption("c", "config", true, "Configuration file");
// Parse arguments
String[] args = {"-v", "-o", "result.txt", "input1.txt", "input2.txt"};
OptionAccessor options = cli.parse(args);
if (options.hasOption("help")) {
cli.usage();
return;
}
boolean verbose = options.hasOption("verbose");
String outputFile = options.getOptionValue("output", "default.out");
List<?> inputFiles = options.getArgList();
System.out.println("Verbose: " + verbose);
System.out.println("Output: " + outputFile);
System.out.println("Input files: " + inputFiles);import groovy.util.XmlSlurper;
import groovy.util.slurpersupport.GPathResult;
// Parse XML
XmlSlurper slurper = new XmlSlurper();
String xml = """
<catalog>
<book id="1">
<title>Groovy in Action</title>
<author>Dierk König</author>
<price>45.99</price>
</book>
<book id="2">
<title>Programming Groovy</title>
<author>Venkat Subramaniam</author>
<price>39.99</price>
</book>
</catalog>
""";
GPathResult catalog = slurper.parseText(xml);
// Navigate and extract data
System.out.println("Catalog has " + catalog.book.size() + " books");
for (GPathResult book : catalog.book) {
String id = book.attribute("id");
String title = book.title.text();
String author = book.author.text();
String price = book.price.text();
System.out.println("Book " + id + ": " + title + " by " + author + " - $" + price);
}
// Find specific books
GPathResult expensiveBooks = catalog.book.findAll { book ->
Double.parseDouble(book.price.text()) > 40.0;
};
System.out.println("Found " + expensiveBooks.size() + " expensive books");import groovy.util.XmlParser;
import groovy.util.Node;
import groovy.util.NodeList;
// Parse XML into Node tree
XmlParser parser = new XmlParser();
Node catalog = parser.parseText(xml);
// Navigate using Node API
System.out.println("Root name: " + catalog.name());
NodeList books = catalog.get("book");
for (Node book : books) {
Map<String, String> attrs = book.attributes();
String id = attrs.get("id");
String title = ((Node) book.get("title").get(0)).text();
String author = ((Node) book.get("author").get(0)).text();
System.out.println("Book " + id + ": " + title + " by " + author);
}Install with Tessl CLI
npx tessl i tessl/maven-org-codehaus-groovy--groovy