Zk javaコードジェネレータ

23187 ワード

たとえばindex.zulページがあります.コードは次のとおりです.




给这个主程序类传5个参数,就可以得到最终的java文件。
java生成器代码:

package com.yourcompany.applications;
import com.yourcompany.generators.ZKWindowFileGenerator;
public class ZKWindowFileGeneratorMain {

/**
* Method : main
*
* @param args
*/
public static void main(String[] args) {
if (args.length != 5) {
System.out.println("Usage: java ZKWindowFileGeneratorMain inputFileDir inputFileName outputFileDir outputFileName packageName");
System.exit(0);
}

String inputFileDir = args[0]; //
String inputFileName = args[1];// .zul
String outputFileDir = args[2];//
String outputFileName = args[3]; // .java
String packageName = args[4]; //java

ZKWindowFileGenerator generator = null;
generator = new ZKWindowFileGenerator(inputFileDir, inputFileName, outputFileDir, outputFileName, packageName);

try {
generator.generate();
} catch (Exception e) {
// Already printed out and logged in the DHgZKWindowFileGenerator
// class.
}
}
}



package com.yourcompany.generators;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ZKCodeGenerator
{
private String nl = "
";


/**
*
* Method : generateMethodComment
* @param methodName
* @param description
* @param params
* @param hasReturnType
* @return
*/
protected String generateMethodComment(String methodName,
String description,
String params[],
boolean hasReturnType)
{
StringBuffer comment = new StringBuffer();

comment.append(" /**" + nl);
comment.append(" * Method : " + methodName + nl );
if (description.length() > 70)
{
String commentString = description;
while (commentString.length() > 0)
{
// Grab the first 50 characters
int length = commentString.length();
int endIndex = 69;
if (length < 69)
endIndex = length;
String stringToPrint = commentString.substring(0,endIndex);
comment.append(" * " + stringToPrint + nl);
// Remove the part that we just used.

commentString = commentString.substring(endIndex);
}
}
else
comment.append(" * " + description + nl);

if (params != null)
{
for (int i=0;i {
String param = params[i];
comment.append(" * @param " + param + nl);
}
}

if (hasReturnType)
{
comment.append(" * @return " + nl);
}
comment.append(" */" + nl);
return comment.toString();
}



/**
*
* Method : readHeaderCommentsFromFile
* @param fileName - this should include full path information to the file. Ex. /home/root/headers/headerFile.txt
* @return
* @throws IOException
*/
protected String readHeaderCommentsFromFile(String fileName) throws IOException
{
BufferedReader in = new BufferedReader(new FileReader(fileName));
StringBuffer header = new StringBuffer();
String currentLine;
while ((currentLine = in.readLine()) != null)
{
header.append(currentLine + "
");
}
in.close();
return header.toString();
}



/**
*
* Method : getHeaderComments
* @param project
* @param fileName
* @param packageName
* @param className
* @param author
* @param locationOfUse
* @param type
* @param description
* @return
*/
protected String getHeaderComments(String project,
String fileName,
String packageName,
String className,
String author,
String locationOfUse,
String type,
String description)
{
String nl = new String("
");
StringBuffer buff = new StringBuffer();

buff.append("/**" + nl);
buff.append("* _YourCompany_ Copyright 2007. All Rights Reserved." + nl);
buff.append("* " + nl);
buff.append("* Project : " + project + nl);
buff.append("* Filename : " + fileName + nl);
buff.append("* Package : " + packageName + nl);
buff.append("* Class : " + className + nl);
buff.append("* Create Date : " + nl);
buff.append("* Author : " + author + nl);
buff.append("*" + nl);
buff.append("* Location of Use : " + locationOfUse + nl);
buff.append("*" + nl);
buff.append("* Type : " + type + nl);
buff.append("* " + nl);
buff.append("* Description: " + description + nl);
buff.append("* " + nl);
buff.append("*/" + nl);

return buff.toString();
}

/**
*
* Method : getHeaderComments
* @return
*/
protected String getHeaderComments()
{
String nl = new String("
");
StringBuffer buff = new StringBuffer();

buff.append("/**" + nl);
buff
.append("* _YourCompany_ Copyright 2007. All Rights Reserved."
+ nl);
buff.append("* " + nl);
buff.append("* Project : " + nl);
buff.append("* Filename : " + nl);
buff.append("* Package : " + nl);
buff.append("* Class : " + nl);
buff.append("* Create Date : " + nl);
buff.append("* Author : " + nl);
buff.append("*" + nl);
buff.append("* Location of Use : " + nl);
buff.append("*" + nl);
buff.append("* Type : " + nl);
buff.append("* " + nl);
buff.append("* Description: TODO" + nl);
buff.append("* " + nl);
buff.append("*/" + nl);

return buff.toString();
}


}


/**
* Description:
*
* This class will take in a ZK .zul file and from that file generate the class that
* extends the zk Window class. It will generate the member variables, the widget lookups in the
* onCreate method, and all callback methods that are defined in the .zul file. This will save alot
* of time dealing with the very tedious part of creating .zul files and the corresponding java file.
*
*/
package com.yourcompany.generators;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class ZKWindowFileGenerator extends ZKCodeGenerator {
private static Logger logger = Logger.getLogger(ZKWindowFileGenerator.class);

private String className = null;

private String zkFileName = null;

private String zkFileDirectory = null;

private String outputFilePackage = null;

private String outputFileName = null;

private String outputDirectory = null;

private FileWriter writer = null;

private Vector components = new Vector();

/**
* Constructor
*
* @param zkFileName
*/
public ZKWindowFileGenerator(String zkFileDirectory, String zkFileName, String outputDirectory,
String outputFileName, String outputFilePackage) {
super();
this.zkFileName = zkFileName;
this.zkFileDirectory = zkFileDirectory;
this.outputFileName = outputFileName;
this.outputDirectory = outputDirectory;
this.outputFilePackage = outputFilePackage;

}

public void generate() throws Exception {
parseInputFile();
generateOutputFile();
}

/**
* Method : parseInputFile
*
*/
private void parseInputFile() throws Exception {
String fileName = null;
fileName = zkFileDirectory + zkFileName;

SAXBuilder builder = new SAXBuilder();

try {
Document doc = builder.build(fileName);
Element root = doc.getRootElement();
this.processDocument(root, 0);

}
// indicates a well-formedness error
catch (JDOMException e) {
System.out.println(fileName + " is not well-formed.");
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e);
}

}

/**
* Method: processDocument Processes the the zul file as DOM tree to get the
* components with theire id's and callBackMethod
*
* @param current
* node
* @param depth
* Used to go deeper in DOM tree (recursive)
*/
public void processDocument(Element current, int depth) {

// if component has no id then ignore it
if (current.getAttributeValue("id") != null) {
String idString = current.getAttributeValue("id");
String componentName = current.getName();
String callbackMethod = "";

// Get callBackMethod if there is any

if (componentName.equals("button")) {
if (current.getAttributeValue("onClick") != null) {
String callBackString = current.getAttributeValue("onClick");
int indexOfDotInCallBack = callBackString.indexOf(".");
callbackMethod = callBackString.substring(indexOfDotInCallBack + 1);
}

}
if (componentName.equals("listbox")) {
if (current.getAttributeValue("onSelect") != null) {
String callBackString = current.getAttributeValue("onSelect");
int indexOfDotInCallBack = callBackString.indexOf(".");
callbackMethod = callBackString.substring(indexOfDotInCallBack + 1);
}

}
if (componentName.equals("checkbox") || componentName.equals("radiogroup")) {
if (current.getAttributeValue("onCheck") != null) {
String callBackString = current.getAttributeValue("onCheck");
int indexOfDotInCallBack = callBackString.indexOf(".");
callbackMethod = callBackString.substring(indexOfDotInCallBack + 1);
}
}

ZKComponentHolder componentHolder = new ZKComponentHolder(componentName, idString, callbackMethod);
components.add(componentHolder);
}

List children = current.getChildren();
Iterator iterator = children.iterator();
while (iterator.hasNext()) {
Element child = (Element) iterator.next();

processDocument(child, depth + 1);

}

}

/**
*
* Method : generateOutputFile
*/
private void generateOutputFile() throws Exception {
setClassName();
String fileName = outputDirectory + outputFileName;
writer = new FileWriter(fileName);

writer.write(getHeaderComments() + "


");

writer.write("package " + outputFilePackage + ";


");

writer.write(generateImportStatements() + "


");

writer.write("class " + className + " extends Window
");
writer.write("{

");

generateMemberVariables();
generateOnCreateMethod();
generateCallbackMethods();
generateInitializeComponentsMethod();
generateInitializeListBoxMethods();

writer.write("}
");
writer.flush();
writer.close();
}

/**
*
* Method : setClassName
*/
private void setClassName() {
// int indexOfDot = zkFileName.indexOf(".");
// Use the name of output (java) file instead of zul file as class name
int indexOfDot = outputFileName.indexOf(".");
className = outputFileName.substring(0, indexOfDot);
}

/**
*
* Method : generateImportStatemens
*
* @return
*/
private String generateImportStatements() {
StringBuffer buff = new StringBuffer();
buff.append("import org.apache.log4j.Logger;
");
buff.append("import org.zkoss.zk.ui.Executions;
");
buff.append("import org.zkoss.zul.Button;
");
buff.append("import org.zkoss.zul.Messagebox;
");
buff.append("import org.zkoss.zul.Textbox;
");
buff.append("import org.zkoss.zul.Intbox;
");
buff.append("import org.zkoss.zul.Window;
");
buff.append("import org.zkoss.zul.Listbox;
");
buff.append("import org.zkoss.zul.Radiogroup;
");
buff.append("import org.zkoss.zul.Checkbox;

");
return buff.toString();
}

/**
*
* Method : generateMemberVariables
*
* @throws Exception
*/
private void generateMemberVariables() throws Exception {
writer.write(" private static Logger logger = Logger.getLogger(" + className + ".class);
");

Iterator it = components.iterator();
while (it.hasNext()) {
ZKComponentHolder holder = (ZKComponentHolder) it.next();
System.out.println("Type = " + holder.type + " Id = " + holder.id + " Callback = " + holder.callbackMethod);
if (holder.type.compareTo("listbox") == 0) {
writer.write(" private Listbox " + holder.id + " = null;
");
} else if (holder.type.compareTo("textbox") == 0) {
writer.write(" private Textbox " + holder.id + " = null;
");
} else if (holder.type.compareTo("intbox") == 0) {
writer.write(" private Intbox " + holder.id + " = null;
");
} else if (holder.type.compareTo("button") == 0) {
writer.write(" private Button " + holder.id + " = null;
");
} else if (holder.type.compareTo("radiogroup") == 0) {
writer.write(" private Radiogroup " + holder.id + " = null;
");
} else if (holder.type.compareTo("checkbox") == 0) {
writer.write(" private Checkbox " + holder.id + " = null;
");
}
}
writer.write("


");
}

/**
*
* Method : generateOnCreateMethod
*
* @throws Exception
*/
private void generateOnCreateMethod() throws Exception {
writer.write(generateMethodComment("onCreate", "", null, false));
writer.write(" public void onCreate()
");
writer.write(" {
");
writer.write(" initializeComponents();
");

Iterator it = components.iterator();
while (it.hasNext()) {
ZKComponentHolder holder = (ZKComponentHolder) it.next();

if (holder.type.compareTo("listbox") == 0) {
String id = holder.id;
String firstChar = id.substring(0, 1);
id = firstChar.toUpperCase() + id.substring(1);
writer.write(" initialize" + id + "ListBox();
");
}

}

writer.write(" }



");
}

/**
*
* Method : generateInitializeComponentsMethod
*
* @throws Exception
*/
private void generateInitializeComponentsMethod() throws Exception {
writer.write(generateMethodComment("initializeComponents", "", null, false));
writer.write(" public void initializeComponents()
");
writer.write(" {
");
Iterator it = components.iterator();
while (it.hasNext()) {
ZKComponentHolder holder = (ZKComponentHolder) it.next();

if (holder.type.compareTo("listbox") == 0) {
writer.write(" " + holder.id + " = (Listbox)this.getFellow(\"" + holder.id + "\");
");
} else if (holder.type.compareTo("textbox") == 0) {
writer.write(" " + holder.id + " = (Textbox)this.getFellow(\"" + holder.id + "\");
");
} else if (holder.type.compareTo("intbox") == 0) {
writer.write(" " + holder.id + " = (Intbox)this.getFellow(\"" + holder.id + "\");
");
} else if (holder.type.compareTo("button") == 0) {
writer.write(" " + holder.id + " = (Button)this.getFellow(\"" + holder.id + "\");
");
} else if (holder.type.compareTo("radiogroup") == 0) {
writer.write(" " + holder.id + " = (Radiogroup)this.getFellow(\"" + holder.id + "\");
");
} else if (holder.type.compareTo("checkbox") == 0) {
writer.write(" " + holder.id + " = (Checkbox)this.getFellow(\"" + holder.id + "\");
");
}
}

writer.write(" }



");
}

/**
*
* Method : generateInitializeListBoxMethods
*
* @throws Exception
*/
private void generateInitializeListBoxMethods() throws Exception {
Iterator it = components.iterator();
while (it.hasNext()) {
ZKComponentHolder holder = (ZKComponentHolder) it.next();

if (holder.type.compareTo("listbox") == 0) {
String id = holder.id;
String firstChar = id.substring(0, 1);
id = firstChar.toUpperCase() + id.substring(1);
writer.write(generateMethodComment("initialize" + id + "ListBox", "", null, false));
writer.write(" public void initialize" + id + "ListBox()
");
writer.write(" {
");

writer.write(" }



");
}

}

}

/**
*
* Method : generateCallbackMethods
*
* @throws Exception
*/
private void generateCallbackMethods() throws Exception {
Iterator it = components.iterator();
while (it.hasNext()) {
ZKComponentHolder holder = (ZKComponentHolder) it.next();
if (holder.callbackMethod.length() > 0) {
writer.write(generateMethodComment(holder.callbackMethod, "", null, false));
writer.write(" public void " + holder.callbackMethod + "
");
writer.write(" {

");
writer.write(" }

");
}
}
}

/**
* Embedded class for holding component info
*/
protected class ZKComponentHolder {
public String type;

public String id;

public String callbackMethod;

/**
* Constructor
*
* @param type
* @param id
* @param callbackMethod
*/
public ZKComponentHolder(String type, String id, String callbackMethod) {
super();
this.type = type;
this.id = id;
this.callbackMethod = callbackMethod;
}

}

/**
* Getter for outputFileName
*
* @return Returns the outputFileName.
*/
public String getOutputFileName() {
return this.outputFileName;
}

/**
* Setter for outputFileName
*
* @param outputFileName
* The outputFileName to set.
*/
public void setOutputFileName(String outputFileName) {
this.outputFileName = outputFileName;
}

/**
* Getter for writer
*
* @return Returns the writer.
*/
public FileWriter getWriter() {
return this.writer;
}

/**
* Setter for writer
*
* @param writer
* The writer to set.
*/
public void setWriter(FileWriter writer) {
this.writer = writer;
}

}


次の5つのパラメータを指定します.
args[0] = "D:/javacreator/zul/example/";
args[1] = "index.zul";
args[2] = "D:/javacreator/java/example/";
args[3] = "Hello.java";
args[4] = "com.yourcompany.example";
最後のD:/javacreator/java/example/ディレクトリで生成されたHello.javaファイルコード:

/**
* _YourCompany_ Copyright 2007. All Rights Reserved.
*
* Project :
* Filename :
* Package :
* Class :
* Create Date :
* Author :
*
* Location of Use :
*
* Type :
*
* Description: TODO
*
*/

package com.yourcompany.example;

import org.apache.log4j.Logger;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zul.Button;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Window;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Radiogroup;
import org.zkoss.zul.Checkbox;

class Hello extends Window
{

private static Logger logger = Logger.getLogger(Hello.class);
private Button hellowWorldButton = null;

/**
* Method : onCreate
*
*/
public void onCreate()
{
initializeComponents();
}


/**
* Method : hellowWorldButtonOnClickHandler()
*
*/
public void hellowWorldButtonOnClickHandler()
{

}

/**
* Method : initializeComponents
*
*/
public void initializeComponents()
{
hellowWorldButton = (Button)this.getFellow("hellowWorldButton");
}
}