1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.javagen.agile.core;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.javagen.agile.core.model.Model;
22 import org.javagen.agile.core.util.StringUtil;
23
24 /***
25 * The <code>GeneratorPipeline</code> defines a code generation assembly line. It consists of
26 * a series of <code>Generator</code>'s called sequentially, each passing their output to the
27 * input of the next.
28 * <p>
29 * The <code>GeneratorPipeline</code> is itself a <code>Generator</code> allowing assemblages
30 * of <code>Generator</code>'s to be combined as a single task.
31 *
32 * @author Richard Easterling
33 */
34 public class GeneratorPipeline implements Generator {
35
36 protected List<Generator> pipeline;
37 protected Model startModel;
38 private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(GeneratorPipeline.class);
39
40 /***
41 * Entry point for starting the generator. If <code>startModel</code> is set, it
42 * will replace <code>input</code> as the pipeline seed model.
43 */
44 public Model gen(Model input) {
45 long loadTime = System.currentTimeMillis();
46 Model result = startModel!=null ? startModel : input;
47 for(Generator gen : getPipeline()) {
48 long startTime = System.currentTimeMillis();
49 result = gen.gen(result);
50 if (log.isInfoEnabled())
51 log.info(StringUtil.lastToken(gen.getClass().getName(), '.')+"("+result+") "+(System.currentTimeMillis()-startTime)+"msec");
52 }
53 if (log.isInfoEnabled())
54 log.info("JavaGen run time "+(System.currentTimeMillis()-loadTime)+"msec");
55 return result;
56 }
57
58 public List<Generator> getPipeline() {
59 return pipeline;
60 }
61
62 public void setPipeline(List<Generator> pipeline) {
63 this.pipeline = pipeline;
64 }
65
66 public void addGenerator(Generator generator) {
67 if (pipeline==null)
68 pipeline = new ArrayList<Generator>();
69 pipeline.add(generator);
70 }
71
72 public Model getStartModel() {
73 return startModel;
74 }
75
76 public void setStartModel(Model startModel) {
77 this.startModel = startModel;
78 }
79
80 }