001    package org.apache.torque.mojo;
002    
003    import java.io.File;
004    import java.io.IOException;
005    
006    import org.apache.maven.plugin.MojoExecutionException;
007    import org.apache.torque.mojo.morph.FileMorphRequest;
008    import org.apache.torque.mojo.morph.MorphRequest;
009    import org.apache.torque.mojo.morph.Morpher;
010    import org.apache.torque.mojo.morph.VersionTableMorpher;
011    import org.codehaus.plexus.util.FileUtils;
012    
013    /**
014     * Morph the xml inside KS_DB_VERSION.xml so it has Maven friendly placeholders
015     * for version info
016     * 
017     * @goal morphversiontable
018     * @phase generate-sources
019     */
020    public class MorphVersionTableMojo extends AbstractMorphSingleMojo {
021    
022            /**
023             * The XML file that will contain the new information
024             * 
025             * @parameter expression="${newVersionXMLFile}" default-value=
026             *            "${project.build.directory}/generated-impex/xml/KS_DB_VERSION.xml"
027             * @required
028             */
029            private File newVersionXMLFile;
030    
031            /**
032             * The XML file that contains the old information
033             * 
034             * @parameter expression="${oldVersionXMLFile}" default-value=
035             *            "${project.build.directory}/generated-impex/xml/KS_DB_VERSION.xml"
036             * @required
037             */
038            private File oldVersionXMLFile;
039    
040            protected void beforeExecution() {
041                    setNewFile(newVersionXMLFile);
042                    setOldFile(oldVersionXMLFile);
043            }
044    
045            @Override
046            protected boolean isMorphNeeded() {
047                    // Only reason
048                    if (!getOldFile().exists()) {
049                            getLog().debug("file:" + getOldFile().getAbsolutePath() + " does not exist");
050                            return false;
051                    } else {
052                            return true;
053                    }
054            }
055    
056            @Override
057            protected void executeMojo() throws MojoExecutionException {
058                    getLog().info("------------------------------------------------------------------------");
059                    getLog().info("Converting version table XML file");
060                    getLog().info("------------------------------------------------------------------------");
061                    try {
062                            FileUtils.forceMkdir(new File(FileUtils.getPath(getNewFile().getAbsolutePath())));
063                            MorphRequest request = new FileMorphRequest(getOldFile(), getNewFile());
064                            request.setName(getOldFile().getName());
065                            request.setEncoding(getEncoding());
066                            Morpher morpher = getMorpher(request, getProject().getArtifactId());
067                            morpher.executeMorph();
068                    } catch (IOException e) {
069                            throw new MojoExecutionException("Unexpected error while attempting to morph " + getOldFile().getAbsolutePath(), e);
070                    }
071            }
072    
073            protected Morpher getMorpher(MorphRequest request, String artifactId) {
074                    VersionTableMorpher morpher = new VersionTableMorpher(request, artifactId);
075                    morpher.setProjectVersion(getProject().getVersion());
076                    return morpher;
077            }
078    
079            /**
080             * @return the newVersionXMLFile
081             */
082            public File getNewVersionXMLFile() {
083                    return newVersionXMLFile;
084            }
085    
086            /**
087             * @param newVersionXMLFile
088             *            the newVersionXMLFile to set
089             */
090            public void setNewVersionXMLFile(final File newVersionXMLFile) {
091                    this.newVersionXMLFile = newVersionXMLFile;
092            }
093    
094            /**
095             * @return the oldVersionXMLFile
096             */
097            public File getOldVersionXMLFile() {
098                    return oldVersionXMLFile;
099            }
100    
101            /**
102             * @param oldVersionXMLFile
103             *            the oldVersionXMLFile to set
104             */
105            public void setOldVersionXMLFile(final File oldVersionXMLFile) {
106                    this.oldVersionXMLFile = oldVersionXMLFile;
107            }
108    }