View Javadoc

1   /*
2    * Copyright 2006 Outsource Cafe, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the 'License')
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an 'AS IS' BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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()) //log the Generator class, input instance and runtime
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  }