package org.apache.maven.doxia;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.reporting.MavenReport;
import org.apache.maven.reporting.MavenReportConfiguration;
import org.codehaus.doxia.site.renderer.SiteRenderer;

import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @goal site
 * @description Doxia plugin
 *
 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
 * @version $Id$
 */
public class DoxiaMojo
    extends AbstractMojo
{
    /**
     * @parameter expression="${basedir}/src/site"
     * @required
     */
    private String siteDirectory;

    /**
     * @parameter alias="workingDirectory" expression="${project.build.directory}/site-generated"
     * @required
     */
    private String generatedSiteDirectory;

    /**
     * @parameter expression="${project.build.directory}/site"
     * @required
     */
    private String outputDirectory;

    /**
     * @parameter alias="flavor"
     */
    private String flavour = "maven";

    /**
     * @parameter expression="${component.org.codehaus.doxia.site.renderer.SiteRenderer}"
     * @required
     * @readonly
     */
    private  SiteRenderer siteRenderer;

    /**
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    private MavenProject project;

    /**
     * @parameter expression="${reports}"
     * @required
     * @readonly
     */
    private Map reports;

    /**
     * @parameter expression="${localRepository}"
     * @required
     * @readonly
     */
    private ArtifactRepository localRepository;

    /**
     * @parameter expression="${project.remoteArtifactRepositories}"
     * @required
     * @readonly
     */
    private List remoteRepositories;

    public void execute()
        throws MojoExecutionException
    {
        try
        {
            MavenReportConfiguration config = new MavenReportConfiguration();

            config.setModel( project.getModel() );

            config.setOutputDirectory( new File( generatedSiteDirectory ) );

            if ( reports != null )
            {
                for ( Iterator i = reports.keySet().iterator(); i.hasNext(); )
                {
                    String reportKey = (String) i.next();

                    getLog().info( "Generate " + reportKey + " report." );

                    MavenReport report = (MavenReport) reports.get( reportKey );

                    report.setConfiguration( config );

                    report.generate();
                }
            }

            siteRenderer.render( siteDirectory, generatedSiteDirectory, outputDirectory, flavour );
        }
        catch ( Exception e )
        {
            // TODO: handle it better
            throw new MojoExecutionException( "Error during site generation", e );
        }
    }
}

@@ -1023,28 +1023,193 @@
 er;%0A
-%0Aimport java.io.File
+import org.codehaus.plexus.util.FileUtils;%0Aimport org.codehaus.plexus.util.StringInputStream;%0Aimport org.codehaus.plexus.util.StringUtils;%0A%0Aimport java.io.File;%0Aimport java.util.HashMap
 ;%0Aim
@@ -3508,16 +3508,688 @@
      %7D%0A%0A
+            File siteDescriptor = new File( siteDirectory, %22site.xml%22 );%0A            if ( !siteDescriptor.exists() )%0A            %7B%0A                throw new MojoExecutionException( %22The site descriptor is not present!%22 );%0A            %7D%0A            String siteDescriptorContent = FileUtils.fileRead( siteDescriptor );%0A            Map props = new HashMap();%0A            if ( reports != null )%0A            %7B%0A                props.put( %22reports%22, getReportsMenu() );%0A            %7D%0A            siteDescriptorContent = StringUtils.interpolate( siteDescriptorContent, props );%0A            StringInputStream siteDescriptorStream = new StringInputStream( siteDescriptorContent );%0A%0A
         
@@ -4276,16 +4276,71 @@
  flavour
+,%0A                                 siteDescriptorStream
  );%0A    
@@ -4522,10 +4522,759 @@
 %7D%0A    %7D%0A
+    private String getReportsMenu()%0A    %7B%0A        StringBuffer buffer = new StringBuffer();%0A        buffer.append( %22%3Cmenu name=%5C%22Project Documentation%5C%22%3E%5Cn%22 );%0A        buffer.append( %22    %3Citem name=%5C%22About %22 + project.getName() + %22%5C%22 href=%5C%22/index.html%5C%22/%3E%5Cn%22);%0A        buffer.append( %22    %3Citem name=%5C%22Project reports%5C%22 href=%5C%22/maven-reports.html%5C%22 collapse=%5C%22true%5C%22%3E%5Cn%22 );%0A%0A        for ( Iterator i = reports.keySet().iterator(); i.hasNext(); )%0A        %7B%0A            String reportKey = (String) i.next();%0A            buffer.append( %22        %3Citem name=%5C%22%22 + reportKey + %22%5C%22 href=%5C%22/%22 + reportKey + %22.html%5C%22/%3E%5Cn%22 );%0A        %7D%0A%0A        buffer.append( %22    %3C/item%3E%5Cn%22 );%0A        buffer.append( %22%3C/menu%3E%5Cn%22 );%0A        return buffer.toString();%0A    %7D%0A
 %7D%0A

Generate reports links in menu