VelocityContext context = new VelocityContext();
context.put("generator",this);
Template template = getTemplate();
// Put common properties
for (Iterator iter = config.getValidators().iterator(); iter.hasNext();) {
ValidatorBean validator = (ValidatorBean) iter.next();
TagBean tag = validator.getTag();
if ( null != tag && null !=tag.getClassname() && tag.isGenerate()) {
info("Create tag class file "+validator.getClassname());
context.put("validator", validator);
context.put("tag", validator.getTag());
context.put("package", validator.getTag().getPackageName());
Set<String> importClasses = new HashSet<String>();
for (Iterator it = validator.getProperties().iterator(); it
.hasNext();) {
PropertyBean property = (PropertyBean) it.next();
// For non-primitive types, add import declaration.
if (!property.isSimpleType() && !property.isHidden()) {
importClasses.add(property.getClassname());
}
}
importClasses.add(validator.getTag().getSuperclass());
context.put("imports", importClasses);
String resultPath = validator.getTag().getClassname().replace('.', '/')
+ ".java";
File javaFile = new File(getDestDir(), resultPath);
File javaDir = javaFile.getParentFile();
if (!javaDir.exists()) {
javaDir.mkdirs();
}
try {
if (javaFile.exists()) {
javaFile.delete();
}
Writer out = new BufferedWriter(new FileWriter(javaFile));
template.merge(context, out);
out.flush();
out.close();
} catch (Exception e) {
throw new GeneratorException(
"Error generating JSP Tag Java file for validator " + validator.getName(), e);
}
}
}
}