001    /**
002     * Copyright 2004-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.apache.torque.mojo;
017    
018    import java.io.File;
019    import java.io.IOException;
020    
021    import org.apache.commons.configuration.PropertiesConfiguration;
022    import org.apache.commons.io.FileUtils;
023    import org.apache.maven.plugin.MojoExecutionException;
024    import org.apache.torque.task.TorqueDataModelTask;
025    import org.kuali.core.db.torque.Utils;
026    
027    /**
028     * Generates a DTD for the database tables from a schema XML file
029     *
030     * @goal datadtd
031     * @phase generate-sources
032     */
033    public class DataDtdMojo extends DataModelTaskMojo {
034        /** The context property for the name of the project. */
035        public static final String PROJECT_CONTEXT_PROPERTY = "project";
036    
037        /**
038         * @parameter expression="${antCompatibilityMode}" antCompatibilityMode="false"
039         */
040        boolean antCompatibilityMode;
041    
042        /**
043         * Only used if antCompatibilityMode is set to true. If so, the dtd that gets generated will be copied to
044         *
045         * @parameter expression="${copyToFile}" default-value="${basedir}/src/main/impex/data.dtd"
046         */
047        String copyToFile;
048    
049        /**
050         * Included here as a simple property to facilitate generating DTD's for other artifacts
051         *
052         * @parameter expression="${artifactId}" default-value="${project.artifactId}"
053         */
054        String artifactId;
055    
056        /**
057         * The directory in which the DTD will be generated
058         *
059         * @parameter property="outputDir" expression="${outputDir}"
060         * default-value="${project.build.directory}/generated-impex"
061         */
062        @SuppressWarnings("unused")
063        private String dummy1;
064    
065        /**
066         * The location where the report file will be generated, relative to outputDir.
067         *
068         * @parameter property="reportFile" expression="${reportFile}"
069         * default-value="../reports/report.${project.artifactId}.datadtd.generation"
070         */
071        @SuppressWarnings("unused")
072        private String dummy2;
073    
074        /**
075         * The location where the context property file for velocity will be generated.
076         *
077         * @parameter property="contextPropertiesPath" expression="${contextPropertiesPath}"
078         * default-value="${project.build.directory}/reports/context.datadtd.properties"
079         */
080        @SuppressWarnings("unused")
081        private String dummy3;
082    
083        /**
084         * The name of the project
085         *
086         * @parameter expression="${projectName}" default-value="impex"
087         */
088        private String projectName;
089    
090        /**
091         * The name of the schema.xml file to process
092         *
093         * @parameter expression="${schemaXMLFile}"
094         * default-value="${project.build.directory}/generated-impex/${project.artifactId}.xml"
095         * @required
096         */
097        private String schemaXMLFile;
098    
099        /**
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    }