001package org.andromda.maven.plugin.site;
002
003import java.io.File;
004import java.net.URL;
005import org.andromda.core.common.ResourceUtils;
006import org.apache.commons.lang.exception.ExceptionUtils;
007import org.apache.maven.plugin.AbstractMojo;
008import org.apache.maven.plugin.MojoExecutionException;
009import org.apache.maven.plugin.MojoFailureException;
010import org.apache.maven.project.MavenProject;
011
012/**
013 * Used to perform the transformation of the namespace XSL document to namespace.xml xdoc format
014 * within the site plugin.
015 *
016 * @phase pre-site
017 * @goal namespace-xsl
018 * @description runs AndroMDA site namespace xsl transformation
019 * @author Vance Karimi
020 */
021public class NamespaceTransformerMojo
022    extends AbstractMojo
023{
024    /**
025     * The name of the project injected from pom.xml
026     *
027     * @parameter default-value="${project.name}"
028     */
029    private String projectName;
030
031    /**
032     * Path to the project namespace.xml
033     *
034     * @parameter expression="${basedir}/src/main/resources/META-INF/andromda/namespace.xml"
035     */
036    private File namespaceDocumentPath;
037
038    /**
039     * Path to the project namespace transformation XSL
040     */
041    private static final String NAMESPACE_TRANSFORMATION_URI = "META-INF/xsl/namespace.xsl";
042
043    /** Not used
044     * @parameter expression="${basedir}/src/main/resources/META-INF/xsl/namespace.xsl"
045     */
046    @SuppressWarnings("unused")
047    private File namespaceTransformationPath;
048
049    /**
050     * Path to the project namespace document output
051     *
052     * @parameter expression="${basedir}/src/site/xdoc/namespace.xml"
053     */
054    private File namespaceOutputPath;
055
056    /**
057     * @parameter expression="${project}"
058     * @required
059     * @readonly
060     */
061    protected MavenProject project;
062
063    /**
064     * XSL Transformer
065     */
066    private XslTransformer xslTransformer;
067
068
069    /**
070     * @see org.apache.maven.plugin.Mojo#execute()
071     */
072    public void execute()
073        throws MojoExecutionException, MojoFailureException
074    {
075        this.getLog().info("-----------------------------------------------------------------------------");
076        this.getLog().info("  A n d r o M D A   S i t e   N a m e s p a c e   T r a n s f o r m a t i o n");
077        this.getLog().info("-----------------------------------------------------------------------------");
078
079        if (xslTransformer == null)
080        {
081            xslTransformer = new XslTransformer(projectName);
082        }
083
084        this.getLog().info("Transforming namespace " + this.namespaceDocumentPath);
085
086        try
087        {
088            final File namespaceDocumentFile = this.namespaceDocumentPath;
089            if (namespaceDocumentFile.exists() && namespaceDocumentFile.isFile())
090            {
091                final URL namespaceTransformationUri = ResourceUtils.getResource(NAMESPACE_TRANSFORMATION_URI);
092                xslTransformer.transform(namespaceDocumentPath.getAbsolutePath(), namespaceTransformationUri, namespaceOutputPath.getAbsolutePath());
093            }
094
095            this.getLog().info("Transformed namesapce " + this.namespaceOutputPath);
096            this.getLog().info("TRANSFORMING NAMESPACE SUCCESSFUL");
097        }
098        catch (final Throwable throwable)
099        {
100            if (throwable instanceof MojoExecutionException)
101            {
102                throw (MojoExecutionException)throwable;
103            }
104            throw new MojoExecutionException("An error occurred creating namespace site document '" +
105                this.project.getArtifactId() + '\'',
106                ExceptionUtils.getRootCause(throwable));
107        }
108    }
109
110}