Java 8 Friday Goodies: Lambdas and XML
At Data Geekery, we love Java. And as we’re really into jOOQ’s fluent API and query DSL, we’re absolutely thrilled about what Java 8 will bring to our ecosystem. We have blogged a couple of times about some nice Java 8 goodies, and now we feel it’s time to start a new blog series, the…
Java 8 Friday
Every Friday, we’re showing you a couple of nice new tutorial-style Java 8 features, which take advantage of lambda expressions, extension methods, and other great stuff. You’ll find the source code on GitHub.
Java 8 Goodie: Lambdas and XML
There isn’t too much that Java 8 can do to the existing SAX and DOM APIs. The SAX ContentHandler has too many abstract methods to qualify as a @FunctionalInterface, and DOM is a huge, verbose API specified by w3c, with little chance of adding new extension methods.
Luckily there is a small Open Source library called jOOX that allows for processing the w3c standard DOM API through a wrapper API that mimicks the popular jQuery library. jQuery leverages JavaScript’s language features by allowing users to pass functions to the API for DOM traversal. The same is the case with jOOX. Let’s have a closer look:
Assume that we’re using the following pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.jooq</groupId> <artifactId>java8-goodies</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.jooq</groupId> <artifactId>joox</artifactId> <version>1.2.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <fork>true</fork> <maxmem>512m</maxmem> <meminitial>256m</meminitial> <encoding>UTF-8</encoding> <source>1.8</source> <target>1.8</target> <debug>true</debug> <debuglevel>lines,vars,source</debuglevel> </configuration> </plugin> </plugins> </build> </project>
Let’s assume we wanted to know all the involved artifacts in Maven’s groupId:artifactId:version
notation. Here’s how we can do that with jOOX and lambda expressions:
$(new File("./pom.xml")).find("groupId") .each(ctx -> { System.out.println( $(ctx).text() + ":" + $(ctx).siblings("artifactId").text() + ":" + $(ctx).siblings("version").text() ); });
Executing the above yields:
org.jooq:java8-goodies:1.0-SNAPSHOT org.jooq:joox:1.2.0 org.apache.maven.plugins:maven-compiler-plugin:2.3.2
Let’s assume we only wanted to display those artifacts that don’t have SNAPSHOT
in their version numbers. Simply add a filter:
$(new File("./pom.xml")) .find("groupId") .filter(ctx -> $(ctx).siblings("version") .matchText(".*-SNAPSHOT") .isEmpty()) .each(ctx -> { System.out.println( $(ctx).text() + ":" + $(ctx).siblings("artifactId").text() + ":" + $(ctx).siblings("version").text()); });
This will now yield
org.jooq:joox:1.2.0 org.apache.maven.plugins:maven-compiler-plugin:2.3.2
We can also transform the XML content. For instance, if the target document doesn’t need to be a POM, we could replace the matched groupId
elements by an artifical artifact
element that contains the artifact name in Maven notation. Here’s how to do this:
$(new File("./pom.xml")) .find("groupId") .filter(ctx -> $(ctx).siblings("version") .matchText(".*-SNAPSHOT") .isEmpty()) .content(ctx -> $(ctx).text() + ":" + $(ctx).siblings("artifactId").text() + ":" + $(ctx).siblings("version").text() ) .rename("artifact") .each(ctx -> System.out.println(ctx));
The above puts new content in place of the previous one through .content()
, and then renames the groupId
elements to artifact
, before printing out the element. The result is:
<artifact>org.jooq:joox:1.2.0</artifact> <artifact>org.apache.maven.plugins:maven-compiler-plugin:2.3.2</artifact>
More goodies next week
What becomes immediately obvious is the fact that the lambda expert group’s choice of making all SAMs (Single Abstract Method interfaces) eligible for use with lambda expressions adds great value to pre-existing APIs. Quite a clever move.
But there are also new APIs. Last week, we have discussed how the existing JDK 1.2 File API can be improved through the use of lambdas. Some of our readers have expressed their concerns that the java.io
API has been largely replaced by java.nio
(nio as in New I/O). Next week, we’ll have a look at Java 8′s java.nnio
API (for new-new I/O ! ) and how it relates to the Java 8 Streams API.