From Documentation

Jump to: navigation, search

  • Author
    Riccardo Casatta Mail.png, Software Engineer @ Synaptica
  • Date
    May 18, 2010
  • Version



I'd like to share a tool built to save time creating Controller class using ZK. This is achieved by extracting component type and id from a zul file and importing it in a Controller class. Other frameworks, that I consider worse than ZK (like .NET), natively integrate this feature in the Controller class. After reading this small talk by Ashish Dasnurkar and after a comment on his works, I finally decide to write that tool. In ZK there are many ways to access components from Controller class (i.e. getFellow(), Composer, CDI…) but none of them save us the noisy part of writing component variable declaration. It is well-known that writing non-strictly necessary code is error-prone and cause of mistyping.

How it works

The tool has been built as a stand-alone standard zul file and it looks like this:


Using it is quite simple, after creating the zul file, and giving identifiers to the necessary components, just copy and paste all the zul source in the relative box, click the "Extract" button and then we have all the variables declaration to kick-start our controller class.


There is a pattern textbox that makes this tool quite flexible changing the output format in the result. The default pattern is the standard Java private variable declaration, another built-in pattern is the ZK CDI variable declaration. I believe changing the pattern is enough for variable declaration in other languages supported like groovy and others.

Source Code

The most relevant part in the source is the extract function that parses the zul file and, according to the pattern, prints out the result.

java.util.regex.Pattern p = java.util.regex.Pattern.compile("<(\\w*) [^>]*id=[\"|'](\\w*)[\"|'][^>]*");
public void extract() {
    	java.util.regex.Matcher m = p.matcher(zulFile.getValue());
	String message=resultPattern.getValue();
        StringBuffer sb = new StringBuffer();
        boolean result = m.find();
        while(result) {
		componentName = componentName.substring(0,1).toUpperCase() + componentName.substring(1);		
	        Object[] args = {componentName, componentId};
                result = m.find();

Why regular expression

I used a regular expression to parse the zul file, it sounds a simply and reasonable approach to me. Another approach could be to create the components via the Execution.createComponents() and iterating over component tree, but it will be a little more complex and involve security risk, so I choose the easiest way.

Conclusion and Further works

Hoping ZK team will integrate this feature directly in the next release of ZK Studio.


Copyright © Riccardo Casatta. This article is licensed under GNU Free Documentation License.