Groovy is an object-oriented programming language for the Java Platform as an alternative to the Java programming language. It is a dynamic language with features similar to those of Python, Ruby, Perl, and Smalltalk. It can be used as a scripting language for the Java Platform.
Groovy uses a Java-like curly bracket syntax which is dynamically compiled to Java Virtual Machine bytecodes and which works seamlessly with other Java code and libraries. The Groovy compiler can be used to generate standard Java bytecode to be used by any Java project. Most Java code is valid Groovy syntax and can be used dynamically as a scripting language.
Groovy is currently undergoing standardization via the Java Community Process under JSR 241 Groovy 1.0 was released on January 2,2007.
Standard Java (Java 5+)
if (item.length() <= 4) {
System.out.println(item);
}
}
Groovy
One noteworthy feature of Groovy is its native support for various markup languages such as XML and HTML, accomplished via an inline DOM syntax. This feature enables the definition and manipulation of many types of heterogeneous data assets with a uniform and concise syntax and programming methodology. For example:
the following Groovy code ...
import groovy.xml.MarkupBuilder
def writer = new StringWriter()
def myXMLDoc = new MarkupBuilder(writer)
myXMLDoc.workbook {
worksheet(caption:"Employees") {
row(fname:"John", lname:"McDoe")
row(fname:"Nancy", lname:"Davolio")
}
worksheet(caption:"Products") {
row(name:"Veeblefeetzer", id:"sku34510")
row(name:"Prune Unit Zappa", id:"sku3a550")
}
}
println writer.toString()
... produces the XML result:
For the sake of comparison, Java code to produce the equivalent XML is shown below. Note that, unlike the Groovy example, each XML element and attribute is created with an explicit method call.
equivalent Java code:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document;
import org.w3c.dom.Element; public class XMLFun { public static void main(String[] args) throws Exception {
TransformerFactory factory = TransformerFactory.newInstance();
factory.setAttribute("indent-number", 2);
Transformer trans = factory.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes"); DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element workbook = doc.createElement("workbook");
doc.appendChild(workbook);
Element worksheet = doc.createElement("worksheet");
worksheet.setAttribute("caption", "Employees");
workbook.appendChild(worksheet);
Element row = doc.createElement("row");
row.setAttribute("fname", "John");
row.setAttribute("lname", "McDoe");
worksheet.appendChild(row);
row = doc.createElement("row");
row.setAttribute("fname", "Nancy");
row.setAttribute("lname", "Davolio");
worksheet.appendChild(row); worksheet = doc.createElement("worksheet");
worksheet.setAttribute("caption", "Products");
workbook.appendChild(worksheet);
row = doc.createElement("row");
row.setAttribute("name", "Veeblefeetzer");
row.setAttribute("id", "sku34510");
worksheet.appendChild(row);
row = doc.createElement("row");
row.setAttribute("name", "Prune Unit Zappa");
row.setAttribute("id", "sku3a550");
worksheet.appendChild(row); StringWriter writer = new StringWriter();
StreamResult out = new StreamResult(writer);
DOMSource dsource = new DOMSource(doc);
trans.transform(dsource, out);
System.out.println(writer.toString());
}
}
