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