1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.javagen.agile.core.emitter;
17
18 import java.io.File;
19 import java.util.Map;
20
21 import org.javagen.agile.core.context.Context;
22 import org.javagen.agile.core.context.ContextHolder;
23 import org.javagen.agile.core.context.DefaultContext;
24 import org.javagen.agile.core.emitter.logic.Predicate;
25 import org.javagen.agile.core.emitter.template.TemplateGenerator;
26
27 /***
28 * This emitter utillizes a template engine to generate artifacts.
29 * It has the following attributes:
30 * <pre>
31 1) a template instance
32 2) an Artifact instance
33 3) an optional local context
34 4) a reference to a TemplateGenerator
35 5) a optional emitCondition Predicate
36 * </pre>
37 *
38 * @author Richard Easterling
39 */
40 public class TemplateEmitter implements ContextHolder, Emitter {
41
42 protected String templatePath;
43 protected Map<String,Object> context;
44 protected Predicate emitCondition;
45 protected Artifact artifact;
46 protected TemplateGenerator templateGenerator;
47 protected String sourceDirectoryProperty;
48 protected String relativeFilePathProperty;
49
50
51 private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(TemplateEmitter.class);
52
53 public TemplateEmitter() {
54
55 }
56
57
58
59
60
61
62
63
64
65 public void emit(Context context) {
66 assert templateGenerator!=null;
67 assert templatePath!=null;
68 assert context!=null;
69 if (artifact==null) {
70 setArtifact( new DefaultArtifact(sourceDirectoryProperty, relativeFilePathProperty) );
71 }
72 File outputDirectory = artifact.outputDirectory(context);
73 File relativeFilePath = artifact.outputFileName(context);
74 String path = relativeFilePath.getPath();
75 File outputFile = new File(outputDirectory, path);
76 boolean emitIt = (emitCondition==null || emitCondition.eval(outputFile, context));
77 if (log.isInfoEnabled())
78 log.info((emitIt?"EMIT: ":"SKIP: ")+outputFile.getAbsolutePath());
79 if (emitIt)
80 templateGenerator.gen(templatePath, outputFile, context);
81 }
82
83
84
85
86
87 public Map<String,Object> getContext() {
88 return context;
89 }
90
91
92
93
94
95 public void setContext(Map<String,Object> context) {
96 this.context = context;
97 }
98 public String getTemplatePath() {
99 return templatePath;
100 }
101 public void setTemplatePath(String templatePath) {
102 this.templatePath = templatePath;
103 }
104
105
106
107 public Predicate getEmitCondition() {
108 return emitCondition;
109 }
110
111
112
113 public void setEmitCondition(Predicate predicate) {
114 this.emitCondition = predicate;
115 }
116
117 public void putContext(String key, Object value) {
118 if (context == null) {
119 context = new DefaultContext();
120 }
121 context.put(key, value);
122
123 }
124
125 public Artifact getArtifact() {
126 return artifact;
127 }
128
129 public void setArtifact(Artifact artifact) {
130 this.artifact = artifact;
131 }
132
133 public TemplateGenerator getTemplateGenerator() {
134 return templateGenerator;
135 }
136
137 public void setTemplateGenerator(TemplateGenerator templateGenerator) {
138 this.templateGenerator = templateGenerator;
139 }
140
141 public String getRelativeFilePathProperty() {
142 return relativeFilePathProperty;
143 }
144
145 public void setRelativeFilePathProperty(String relativeFilePathProperty) {
146 this.relativeFilePathProperty = relativeFilePathProperty;
147 }
148
149 public String getSourceDirectoryProperty() {
150 return sourceDirectoryProperty;
151 }
152
153 public void setSourceDirectoryProperty(String sourceDirectoryProperty) {
154 this.sourceDirectoryProperty = sourceDirectoryProperty;
155 }
156
157 }