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 property="outputDir" expression="${outputDir}"
60       * default-value="${project.build.directory}/generated-impex"
61       */
62      @SuppressWarnings("unused")
63      private String dummy1;
64  
65      /**
66       * The location where the report file will be generated, relative to outputDir.
67       *
68       * @parameter property="reportFile" expression="${reportFile}"
69       * default-value="../reports/report.${project.artifactId}.datadtd.generation"
70       */
71      @SuppressWarnings("unused")
72      private String dummy2;
73  
74      /**
75       * The location where the context property file for velocity will be generated.
76       *
77       * @parameter property="contextPropertiesPath" expression="${contextPropertiesPath}"
78       * default-value="${project.build.directory}/reports/context.datadtd.properties"
79       */
80      @SuppressWarnings("unused")
81      private String dummy3;
82  
83      /**
84       * The name of the project
85       *
86       * @parameter expression="${projectName}" default-value="impex"
87       */
88      private String projectName;
89  
90      /**
91       * The name of the schema.xml file to process
92       *
93       * @parameter expression="${schemaXMLFile}"
94       * default-value="${project.build.directory}/generated-impex/${project.artifactId}.xml"
95       * @required
96       */
97      private String schemaXMLFile;
98  
99      /**
100      * Returns the context properties for the Texen task.
101      *
102      * @return The PropertiesConfiguration containing all context properties, not null.
103      */
104     @Override
105     protected PropertiesConfiguration getMojoContextProperties() {
106         PropertiesConfiguration configuration = new PropertiesConfiguration();
107         configuration.addProperty(PROJECT_CONTEXT_PROPERTY, getProjectName());
108         configuration.addProperty("version", getProject().getVersion());
109         return configuration;
110     }
111 
112     protected void showConfig() {
113         getLog().info("Schema XML File: " + schemaXMLFile);
114         getLog().info("Ant Compatibility Mode: " + antCompatibilityMode);
115         getLog().info("Output Directory: " + getOutputDir());
116     }
117 
118     /**
119      * Configures the Texen task wrapped by this mojo
120      */
121     @Override
122     protected void configureTask() throws MojoExecutionException {
123         TorqueDataModelTask task = new TorqueDataModelTask();
124         setAntTask(task);
125         super.configureTask();
126         boolean exists = new Utils().isFileOrResource(getSchemaXMLFile());
127         if (!exists) {
128             throw new MojoExecutionException("Unable to locate: " + getSchemaXMLFile());
129         }
130         task.setXmlFile(getSchemaXMLFile());
131 
132     }
133 
134     /**
135      * Returns the path to the control template.
136      *
137      * @return "data/Control.vm"
138      */
139     @Override
140     protected String getControlTemplate() {
141         return "data/Control.vm";
142     }
143 
144     /**
145      * Returns the name of the project
146      *
147      * @return the name of the project.
148      */
149     public String getProjectName() {
150         return projectName;
151     }
152 
153     /**
154      * Sets the name of the project
155      *
156      * @param project
157      * the name of the project.
158      */
159     public void setProjectName(final String projectName) {
160         this.projectName = projectName;
161     }
162 
163     /**
164      * Returns the name of the xml file to process.
165      *
166      * @return the name of the xml file to process.
167      */
168     public String getSchemaXMLFile() {
169         return schemaXMLFile;
170     }
171 
172     /**
173      * Sets the name of the xml file to process.
174      *
175      * @param project
176      * the name of the xml file to process.
177      */
178     public void setSchemaXMLFile(final String xmlFile) {
179         this.schemaXMLFile = xmlFile;
180     }
181 
182     @Override
183     public void executeMojo() throws MojoExecutionException {
184         getLog().info("------------------------------------------------------------------------");
185         getLog().info("Generating database DTD");
186         getLog().info("------------------------------------------------------------------------");
187         showConfig();
188         super.executeMojo();
189         if (antCompatibilityMode) {
190             File srcFile = new File(getOutputDir() + FS + getArtifactId() + ".dtd");
191             File dstFile = new File(copyToFile);
192             getLog().info("Creating " + dstFile.getAbsolutePath() + " for Ant compatibility mode");
193             try {
194                 FileUtils.copyFile(srcFile, dstFile);
195             } catch (IOException e) {
196                 throw new MojoExecutionException("Error copying file", e);
197             }
198         }
199     }
200 
201     public boolean isAntCompatibilityMode() {
202         return antCompatibilityMode;
203     }
204 
205     public void setAntCompatibilityMode(final boolean antCompatibilityMode) {
206         this.antCompatibilityMode = antCompatibilityMode;
207     }
208 
209     public String getCopyToFile() {
210         return copyToFile;
211     }
212 
213     public void setCopyToFile(final String copyToFile) {
214         this.copyToFile = copyToFile;
215     }
216 
217     public String getArtifactId() {
218         return artifactId;
219     }
220 
221     public void setArtifactId(final String artifactId) {
222         this.artifactId = artifactId;
223     }
224 }