001    package org.apache.torque.mojo;
002    
003    import java.io.File;
004    import java.io.IOException;
005    
006    import org.apache.commons.configuration.PropertiesConfiguration;
007    import org.apache.commons.io.FileUtils;
008    import org.apache.maven.plugin.MojoExecutionException;
009    import org.apache.torque.task.TorqueDataModelTask;
010    import org.kuali.core.db.torque.Utils;
011    
012    /**
013     * Generates a DTD for the database tables from a schema XML file
014     *
015     * @goal datadtd
016     * @phase generate-sources
017     */
018    public class DataDtdMojo extends DataModelTaskMojo {
019        /** The context property for the name of the project. */
020        public static final String PROJECT_CONTEXT_PROPERTY = "project";
021    
022        /**
023         * @parameter expression="${antCompatibilityMode}" antCompatibilityMode="false"
024         */
025        boolean antCompatibilityMode;
026    
027        /**
028         * Only used if antCompatibilityMode is set to true. If so, the dtd that gets generated will be copied to
029         *
030         * @parameter expression="${copyToFile}" default-value="${basedir}/src/main/impex/data.dtd"
031         */
032        String copyToFile;
033    
034        /**
035         * Included here as a simple property to facilitate generating DTD's for other artifacts
036         *
037         * @parameter expression="${artifactId}" default-value="${project.artifactId}"
038         */
039        String artifactId;
040    
041        /**
042         * The directory in which the DTD will be generated
043         *
044         * @parameter property="outputDir" expression="${outputDir}"
045         * default-value="${project.build.directory}/generated-impex"
046         */
047        @SuppressWarnings("unused")
048        private String dummy1;
049    
050        /**
051         * The location where the report file will be generated, relative to outputDir.
052         *
053         * @parameter property="reportFile" expression="${reportFile}"
054         * default-value="../reports/report.${project.artifactId}.datadtd.generation"
055         */
056        @SuppressWarnings("unused")
057        private String dummy2;
058    
059        /**
060         * The location where the context property file for velocity will be generated.
061         *
062         * @parameter property="contextPropertiesPath" expression="${contextPropertiesPath}"
063         * default-value="${project.build.directory}/reports/context.datadtd.properties"
064         */
065        @SuppressWarnings("unused")
066        private String dummy3;
067    
068        /**
069         * The name of the project
070         *
071         * @parameter expression="${projectName}" default-value="impex"
072         */
073        private String projectName;
074    
075        /**
076         * The name of the schema.xml file to process
077         *
078         * @parameter expression="${schemaXMLFile}"
079         * default-value="${project.build.directory}/generated-impex/${project.artifactId}.xml"
080         * @required
081         */
082        private String schemaXMLFile;
083    
084        /**
085         * Returns the context properties for the Texen task.
086         *
087         * @return The PropertiesConfiguration containing all context properties, not null.
088         */
089        @Override
090        protected PropertiesConfiguration getMojoContextProperties() {
091            PropertiesConfiguration configuration = new PropertiesConfiguration();
092            configuration.addProperty(PROJECT_CONTEXT_PROPERTY, getProjectName());
093            configuration.addProperty("version", getProject().getVersion());
094            return configuration;
095        }
096    
097        protected void showConfig() {
098            getLog().info("Schema XML File: " + schemaXMLFile);
099            getLog().info("Ant Compatibility Mode: " + antCompatibilityMode);
100            getLog().info("Output Directory: " + getOutputDir());
101        }
102    
103        /**
104         * Configures the Texen task wrapped by this mojo
105         */
106        @Override
107        protected void configureTask() throws MojoExecutionException {
108            TorqueDataModelTask task = new TorqueDataModelTask();
109            setAntTask(task);
110            super.configureTask();
111            boolean exists = new Utils().isFileOrResource(getSchemaXMLFile());
112            if (!exists) {
113                throw new MojoExecutionException("Unable to locate: " + getSchemaXMLFile());
114            }
115            task.setXmlFile(getSchemaXMLFile());
116    
117        }
118    
119        /**
120         * Returns the path to the control template.
121         *
122         * @return "data/Control.vm"
123         */
124        @Override
125        protected String getControlTemplate() {
126            return "data/Control.vm";
127        }
128    
129        /**
130         * Returns the name of the project
131         *
132         * @return the name of the project.
133         */
134        public String getProjectName() {
135            return projectName;
136        }
137    
138        /**
139         * Sets the name of the project
140         *
141         * @param project
142         * the name of the project.
143         */
144        public void setProjectName(final String projectName) {
145            this.projectName = projectName;
146        }
147    
148        /**
149         * Returns the name of the xml file to process.
150         *
151         * @return the name of the xml file to process.
152         */
153        public String getSchemaXMLFile() {
154            return schemaXMLFile;
155        }
156    
157        /**
158         * Sets the name of the xml file to process.
159         *
160         * @param project
161         * the name of the xml file to process.
162         */
163        public void setSchemaXMLFile(final String xmlFile) {
164            this.schemaXMLFile = xmlFile;
165        }
166    
167        @Override
168        public void executeMojo() throws MojoExecutionException {
169            getLog().info("------------------------------------------------------------------------");
170            getLog().info("Generating database DTD");
171            getLog().info("------------------------------------------------------------------------");
172            showConfig();
173            super.executeMojo();
174            if (antCompatibilityMode) {
175                File srcFile = new File(getOutputDir() + FS + getArtifactId() + ".dtd");
176                File dstFile = new File(copyToFile);
177                getLog().info("Creating " + dstFile.getAbsolutePath() + " for Ant compatibility mode");
178                try {
179                    FileUtils.copyFile(srcFile, dstFile);
180                } catch (IOException e) {
181                    throw new MojoExecutionException("Error copying file", e);
182                }
183            }
184        }
185    
186        public boolean isAntCompatibilityMode() {
187            return antCompatibilityMode;
188        }
189    
190        public void setAntCompatibilityMode(final boolean antCompatibilityMode) {
191            this.antCompatibilityMode = antCompatibilityMode;
192        }
193    
194        public String getCopyToFile() {
195            return copyToFile;
196        }
197    
198        public void setCopyToFile(final String copyToFile) {
199            this.copyToFile = copyToFile;
200        }
201    
202        public String getArtifactId() {
203            return artifactId;
204        }
205    
206        public void setArtifactId(final String artifactId) {
207            this.artifactId = artifactId;
208        }
209    }