View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.apache.torque.mojo;
17  
18  import java.io.File;
19  import java.io.IOException;
20  
21  import org.apache.commons.configuration.PropertiesConfiguration;
22  import org.apache.commons.io.FileUtils;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.torque.task.TorqueDataModelTask;
25  import org.kuali.core.db.torque.Utils;
26  
27  /**
28   * Generates a DTD for the database tables from a schema XML file
29   * 
30   * @goal datadtd
31   * @phase generate-sources
32   */
33  public class DataDtdMojo extends DataModelTaskMojo {
34  	/** The context property for the name of the project. */
35  	public static final String PROJECT_CONTEXT_PROPERTY = "project";
36  
37  	/**
38  	 * @parameter expression="${antCompatibilityMode}" antCompatibilityMode="false"
39  	 */
40  	boolean antCompatibilityMode;
41  
42  	/**
43  	 * Only used if antCompatibilityMode is set to true. If so, the dtd that gets generated will be copied to
44  	 * 
45  	 * @parameter expression="${copyToFile}" default-value="${basedir}/src/main/impex/data.dtd"
46  	 */
47  	String copyToFile;
48  
49  	/**
50  	 * Included here as a simple property to facilitate generating DTD's for other artifacts
51  	 * 
52  	 * @parameter expression="${artifactId}" default-value="${project.artifactId}"
53  	 */
54  	String artifactId;
55  
56  	/**
57  	 * The directory in which the DTD will be generated
58  	 * 
59  	 * @parameter expression="${outputDir}" default-value="${project.build.directory}/generated-impex"
60  	 */
61  	@SuppressWarnings("unused")
62  	private String dummy1;
63  
64  	/**
65  	 * The location where the report file will be generated, relative to outputDir.
66  	 * 
67  	 * @parameter expression="${reportFile}" default-value="../reports/report.${project.artifactId}.datadtd.generation"
68  	 */
69  	@SuppressWarnings("unused")
70  	private String dummy2;
71  
72  	/**
73  	 * The location where the context property file for velocity will be generated.
74  	 * 
75  	 * @parameter expression="${contextPropertiesPath}" default-value="${project.build.directory}/reports/context.datadtd.properties"
76  	 */
77  	@SuppressWarnings("unused")
78  	private String dummy3;
79  
80  	/**
81  	 * The name of the project
82  	 * 
83  	 * @parameter expression="${projectName}" default-value="impex"
84  	 */
85  	private String projectName;
86  
87  	/**
88  	 * The name of the schema.xml file to process
89  	 * 
90  	 * @parameter expression="${schemaXMLFile}" default-value="${project.build.directory}/generated-impex/${project.artifactId}.xml"
91  	 * @required
92  	 */
93  	private String schemaXMLFile;
94  
95  	/**
96  	 * Returns the context properties for the Texen task.
97  	 * 
98  	 * @return The PropertiesConfiguration containing all context properties, not null.
99  	 */
100 	@Override
101 	protected PropertiesConfiguration getMojoContextProperties() {
102 		PropertiesConfiguration configuration = new PropertiesConfiguration();
103 		configuration.addProperty(PROJECT_CONTEXT_PROPERTY, getProjectName());
104 		configuration.addProperty("version", getProject().getVersion());
105 		return configuration;
106 	}
107 
108 	protected void showConfig() {
109 		getLog().info("Schema XML File: " + schemaXMLFile);
110 		getLog().info("Ant Compatibility Mode: " + antCompatibilityMode);
111 		getLog().info("Output Directory: " + getOutputDir());
112 	}
113 
114 	/**
115 	 * Configures the Texen task wrapped by this mojo
116 	 */
117 	@Override
118 	protected void configureTask() throws MojoExecutionException {
119 		TorqueDataModelTask task = new TorqueDataModelTask();
120 		setAntTask(task);
121 		super.configureTask();
122 		boolean exists = new Utils().isFileOrResource(getSchemaXMLFile());
123 		if (!exists) {
124 			throw new MojoExecutionException("Unable to locate: " + getSchemaXMLFile());
125 		}
126 		task.setXmlFile(getSchemaXMLFile());
127 
128 	}
129 
130 	/**
131 	 * Returns the path to the control template.
132 	 * 
133 	 * @return "data/Control.vm"
134 	 */
135 	@Override
136 	protected String getControlTemplate() {
137 		return "data/Control.vm";
138 	}
139 
140 	/**
141 	 * Returns the name of the project
142 	 * 
143 	 * @return the name of the project.
144 	 */
145 	public String getProjectName() {
146 		return projectName;
147 	}
148 
149 	/**
150 	 * Sets the name of the project
151 	 * 
152 	 * @param project
153 	 *            the name of the project.
154 	 */
155 	public void setProjectName(final String projectName) {
156 		this.projectName = projectName;
157 	}
158 
159 	/**
160 	 * Returns the name of the xml file to process.
161 	 * 
162 	 * @return the name of the xml file to process.
163 	 */
164 	public String getSchemaXMLFile() {
165 		return schemaXMLFile;
166 	}
167 
168 	/**
169 	 * Sets the name of the xml file to process.
170 	 * 
171 	 * @param project
172 	 *            the name of the xml file to process.
173 	 */
174 	public void setSchemaXMLFile(final String xmlFile) {
175 		this.schemaXMLFile = xmlFile;
176 	}
177 
178 	@Override
179 	public void executeMojo() throws MojoExecutionException {
180 		getLog().info("------------------------------------------------------------------------");
181 		getLog().info("Generating database DTD");
182 		getLog().info("------------------------------------------------------------------------");
183 		showConfig();
184 		super.executeMojo();
185 		if (antCompatibilityMode) {
186 			File srcFile = new File(getOutputDir() + FS + getArtifactId() + ".dtd");
187 			File dstFile = new File(copyToFile);
188 			getLog().info("Creating " + dstFile.getAbsolutePath() + " for Ant compatibility mode");
189 			try {
190 				FileUtils.copyFile(srcFile, dstFile);
191 			} catch (IOException e) {
192 				throw new MojoExecutionException("Error copying file", e);
193 			}
194 		}
195 	}
196 
197 	public boolean isAntCompatibilityMode() {
198 		return antCompatibilityMode;
199 	}
200 
201 	public void setAntCompatibilityMode(final boolean antCompatibilityMode) {
202 		this.antCompatibilityMode = antCompatibilityMode;
203 	}
204 
205 	public String getCopyToFile() {
206 		return copyToFile;
207 	}
208 
209 	public void setCopyToFile(final String copyToFile) {
210 		this.copyToFile = copyToFile;
211 	}
212 
213 	public String getArtifactId() {
214 		return artifactId;
215 	}
216 
217 	public void setArtifactId(final String artifactId) {
218 		this.artifactId = artifactId;
219 	}
220 }