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.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  		//artifact = new DefaultArtifact();
55  	}
56  	
57  //	public TemplateEmitter(Artifact artifact) {
58  //		this.artifact = artifact;
59  //	}
60  //	
61  	////////////////////////////////////////////////////////////////////////////
62  	// Emitter interface:
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  	// ContextHolder inteface:
85  	////////////////////////////////////////////////////////////////////////////
86  	
87  	public Map<String,Object> getContext() {
88  		return context;
89  	}
90  
91  	////////////////////////////////////////////////////////////////////////////
92  	// getters and setters:
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 	/* (non-Javadoc)
105 	 * @see org.javagen.revgen.emitter.Emitter#getGatekeeper()
106 	 */
107 	public Predicate getEmitCondition() {
108 		return emitCondition;
109 	}
110 	/* (non-Javadoc)
111 	 * @see org.javagen.revgen.emitter.Emitter#setGatekeeper(org.javagen.revgen.emitter.Gatekeeper)
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 }