01: package com.avaje.util.codegen;
02:
03: import java.io.FileWriter;
04: import java.io.IOException;
05: import java.util.ArrayList;
06: import java.util.Iterator;
07:
08: import org.apache.velocity.VelocityContext;
09:
10: import com.avaje.lib.util.Dnode;
11:
12: public class BeanWriter implements Contants {
13:
14: /**
15: * The name of the velocity template.
16: */
17: String beanTemplate = "beanGenerator.vm";
18:
19: String embeddedBeanTemplate = "embeddedIdGenerator.vm";
20:
21: /**
22: * The properties that should not ben included in the bean. This is because
23: * they are on a supertype class.
24: */
25: ArrayList ignoreList = new ArrayList();
26:
27: TemplateManager velocityManager = new TemplateManager();
28:
29: GenerateConfiguration config;
30:
31: /**
32: * Create the BeanWriter.
33: */
34: public BeanWriter(GenerateConfiguration config) {
35: this .config = config;
36: }
37:
38: /**
39: * Add a property to be ignored. Typically the property will exist in a
40: * super type that the generated bean extends.
41: */
42: public void addIgnoreProperty(String propertyName) {
43: ignoreList.add(propertyName);
44: }
45:
46: public void removeIgnoreProperty(String propertyName) {
47: ignoreList.remove(propertyName);
48: }
49:
50: public Iterator ignoreProperties() {
51: return ignoreList.iterator();
52: }
53:
54: protected boolean ignoreProperty(String propertyName) {
55: return (ignoreList.contains(propertyName));
56: }
57:
58: public void write(GenerateInfo info) throws IOException {
59:
60: info.setImports();
61:
62: String template = beanTemplate;
63:
64: Dnode beanTree = info.getBeanTree();
65: if (beanTree.getAttribute("embeddedIdBean") != null) {
66: template = embeddedBeanTemplate;
67: }
68: VelocityContext context = new VelocityContext();
69: context.put("dnode", beanTree);
70:
71: SourceCode beanSource = info.getBeanSource();
72: if (beanSource != null) {
73: context.put("beansource", beanSource);
74: }
75:
76: if (config.isWriteMode()) {
77:
78: String beanSourceFile = info.getFullFileName();
79: FileWriter writer = new FileWriter(beanSourceFile, false);
80:
81: velocityManager.merge(template, context, writer);
82:
83: writer.flush();
84: writer.close();
85: }
86:
87: info.getLog().add(info);
88: }
89:
90: }
|