001package org.andromda.maven.plugin.modelarchiver;
002
003import java.io.File;
004import org.apache.commons.io.FileUtils;
005import org.apache.commons.io.FilenameUtils;
006import org.apache.maven.plugin.MojoExecutionException;
007
008/**
009 * Builds archived model xml files.
010 *
011 * @author Bob Fields
012 * @version $Id: $
013 * @goal xml
014 * @phase package
015 * @description builds a versioned xml
016 */
017public class XmlArchiverMojo
018        extends BaseArchiveMojo
019{
020    private static final String ARTIFACT_TYPE = "xml";
021
022    /**
023     * The pattern of the model file(s) that should be versioned.
024     *
025     * @parameter expression=".*(\\.xml)"
026     * @required
027     * @readonly
028     */
029    private String modelFilePattern;
030
031    /**
032     * <p>execute</p>
033     *
034     * @throws org.apache.maven.plugin.MojoExecutionException
035     *          if any.
036     * @see org.apache.maven.plugin.Mojo#execute()
037     */
038    public void execute()
039            throws MojoExecutionException
040    {
041        if (getLog().isDebugEnabled())
042        {
043            getLog().debug(" ======= XmlArchiverMojo settings =======");
044            getLog().debug("modelSourceDirectory[" + modelSourceDirectory + ']');
045            getLog().debug("workDirectory[" + workDirectory + ']');
046            getLog().debug("outputDirectory[" + outputDirectory + ']');
047            getLog().debug("finalName[" + finalName + ']');
048        }
049
050        try
051        {
052            final File buildDirectory = this.workDirectory;
053            if (buildDirectory.exists())
054            {   // old files in directory are not automatically deleted.
055                deleteFiles(buildDirectory.getAbsolutePath(), ARTIFACT_TYPE);
056            }
057            else
058            {
059                buildDirectory.mkdirs();
060            }
061
062            //final String[] replacementExtensions =
063            //    this.replacementExtensions != null ? this.replacementExtensions.split(",\\s*") : new String[0];
064            if (modelSourceDirectory.exists())
065            {
066                final File[] modelFiles = modelSourceDirectory.listFiles();
067                for (final File file : modelFiles)
068                {
069                    if (file.isFile() && file.toString().matches(this.modelFilePattern))
070                    {
071                        final File newFile =
072                                new File(buildDirectory,
073                                        this.finalName + '.' + FilenameUtils.getExtension(file.getName()));
074                        getLog().info("File " + file + " copied  to " + newFile);
075                        FileUtils.copyFile(file, newFile);
076
077                        String contents = FileUtils.readFileToString(newFile);
078                        final String version = escapePattern(this.project.getVersion());
079                        /*if (replaceExtensions)
080                       {
081                           for (int ctr3 = 0; ctr3 < replacementExtensions.length; ctr3++)
082                           {
083                               final String extension = escapePattern(replacementExtensions[ctr3]);
084                               final String extensionPattern = "((\\-" + version + ")?)" + extension;
085                               final String newExtension = "\\-" + version + extension;
086                               contents = contents.replaceAll(
087                                       extensionPattern,
088                                       newExtension); */
089                        // Fix replacement error for standard UML profiles which follow the _Profile. naming convention.
090                        contents =
091                                contents.replaceAll(
092                                        "_Profile\\-" + version,
093                                        "_Profile");
094                        /*}
095                        }*/
096                        FileUtils.writeStringToFile(newFile, contents);
097
098                        setArtifactFile(newFile);
099                    }
100                }
101            }
102        }
103        catch (final Throwable throwable)
104        {
105            throw new MojoExecutionException("Error assembling model", throwable);
106        }
107    }
108}