Author: trygvis Date: Mon Jul 3 16:34:22 2006 New Revision: 418876 URL: http://svn.apache.org/viewvc?rev=418876&view=rev Log: o Adding a Maven plugin to render Doxia Books.
Added: maven/doxia/trunk/doxia-sandbox/doxia-maven-plugin/ (with props) maven/doxia/trunk/doxia-sandbox/doxia-maven-plugin/pom.xml maven/doxia/trunk/doxia-sandbox/doxia-maven-plugin/src/ maven/doxia/trunk/doxia-sandbox/doxia-maven-plugin/src/main/ maven/doxia/trunk/doxia-sandbox/doxia-maven-plugin/src/main/java/ maven/doxia/trunk/doxia-sandbox/doxia-maven-plugin/src/main/java/org/ maven/doxia/trunk/doxia-sandbox/doxia-maven-plugin/src/main/java/org/apache/ maven/doxia/trunk/doxia-sandbox/doxia-maven-plugin/src/main/java/org/apache/maven/ maven/doxia/trunk/doxia-sandbox/doxia-maven-plugin/src/main/java/org/apache/maven/doxia/ maven/doxia/trunk/doxia-sandbox/doxia-maven-plugin/src/main/java/org/apache/maven/doxia/plugin/ maven/doxia/trunk/doxia-sandbox/doxia-maven-plugin/src/main/java/org/apache/maven/doxia/plugin/Book.java maven/doxia/trunk/doxia-sandbox/doxia-maven-plugin/src/main/java/org/apache/maven/doxia/plugin/DoxiaRenderBooksMojo.java Propchange: maven/doxia/trunk/doxia-sandbox/doxia-maven-plugin/ ------------------------------------------------------------------------------ --- svn:ignore (added) +++ svn:ignore Mon Jul 3 16:34:22 2006 @@ -0,0 +1,8 @@ +target +*~ +*.log +.classpath +.project +*.ipr +*.iws +*.iml Added: maven/doxia/trunk/doxia-sandbox/doxia-maven-plugin/pom.xml URL: http://svn.apache.org/viewvc/maven/doxia/trunk/doxia-sandbox/doxia-maven-plugin/pom.xml?rev=418876&view=auto ============================================================================== --- maven/doxia/trunk/doxia-sandbox/doxia-maven-plugin/pom.xml (added) +++ maven/doxia/trunk/doxia-sandbox/doxia-maven-plugin/pom.xml Mon Jul 3 16:34:22 2006 @@ -0,0 +1,24 @@ +<project> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.maven.doxia</groupId> + <artifactId>doxia</artifactId> + <version>1.0-alpha-9-SNAPSHOT</version> + </parent> + <artifactId>doxia-maven-plugin</artifactId> + <version>1.0-alpha-1-SNAPSHOT</version> + <packaging>maven-plugin</packaging> + <name>Doxia Maven Plugin</name> + <dependencies> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-plugin-api</artifactId> + <version>2.0</version> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>doxia-book</artifactId> + <version>1.0-alpha-1-SNAPSHOT</version> + </dependency> + </dependencies> +</project> Added: maven/doxia/trunk/doxia-sandbox/doxia-maven-plugin/src/main/java/org/apache/maven/doxia/plugin/Book.java URL: http://svn.apache.org/viewvc/maven/doxia/trunk/doxia-sandbox/doxia-maven-plugin/src/main/java/org/apache/maven/doxia/plugin/Book.java?rev=418876&view=auto ============================================================================== --- maven/doxia/trunk/doxia-sandbox/doxia-maven-plugin/src/main/java/org/apache/maven/doxia/plugin/Book.java (added) +++ maven/doxia/trunk/doxia-sandbox/doxia-maven-plugin/src/main/java/org/apache/maven/doxia/plugin/Book.java Mon Jul 3 16:34:22 2006 @@ -0,0 +1,45 @@ +package org.apache.maven.doxia.plugin; + +import java.util.List; + +/** + * @author <a href="mailto:[EMAIL PROTECTED]">Trygve Laugstøl</a> + * @version $Id$ + */ +public class Book +{ + private String descriptor; + + private List formats; + + private String directory; + + private List includes; + + private List excludes; + + public String getDescriptor() + { + return descriptor; + } + + public List getFormats() + { + return formats; + } + + public String getDirectory() + { + return directory; + } + + public List getIncludes() + { + return includes; + } + + public List getExcludes() + { + return excludes; + } +} Added: maven/doxia/trunk/doxia-sandbox/doxia-maven-plugin/src/main/java/org/apache/maven/doxia/plugin/DoxiaRenderBooksMojo.java URL: http://svn.apache.org/viewvc/maven/doxia/trunk/doxia-sandbox/doxia-maven-plugin/src/main/java/org/apache/maven/doxia/plugin/DoxiaRenderBooksMojo.java?rev=418876&view=auto ============================================================================== --- maven/doxia/trunk/doxia-sandbox/doxia-maven-plugin/src/main/java/org/apache/maven/doxia/plugin/DoxiaRenderBooksMojo.java (added) +++ maven/doxia/trunk/doxia-sandbox/doxia-maven-plugin/src/main/java/org/apache/maven/doxia/plugin/DoxiaRenderBooksMojo.java Mon Jul 3 16:34:22 2006 @@ -0,0 +1,191 @@ +package org.apache.maven.doxia.plugin; + +import org.apache.maven.doxia.book.BookDoxia; +import org.apache.maven.doxia.book.BookDoxiaException; +import org.apache.maven.doxia.book.InvalidBookDescriptorException; +import org.apache.maven.doxia.book.services.validation.ValidationResult; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.codehaus.plexus.util.FileUtils; +import org.codehaus.plexus.util.StringUtils; + +import java.io.File; +import java.io.IOException; +import java.util.Iterator; +import java.util.List; + +/** + * @goal render-books + * + * @author <a href="mailto:[EMAIL PROTECTED]">Trygve Laugstøl</a> + * @version $Id$ + */ +public class DoxiaRenderBooksMojo + extends AbstractMojo +{ + // ---------------------------------------------------------------------- + // + // ---------------------------------------------------------------------- + + /** + * @parameter + */ + private List books; + + /** + * @parameter expression="${basedir}" + */ + private File basedir; + + /** + * @parameter expression="${project.reporting.outputDirectory}" + */ + private File outputDirectory; + + /** + * @component + */ + private BookDoxia bookDoxia; + + private static final String LINE_SEPARATOR = System.getProperty( "line.separator" ); + + // ---------------------------------------------------------------------- + // + // ---------------------------------------------------------------------- + + public void execute() + throws MojoExecutionException, MojoFailureException + { + for ( Iterator it = books.iterator(); it.hasNext(); ) + { + Book book = (Book) it.next(); + + // ---------------------------------------------------------------------- + // Validate + // ---------------------------------------------------------------------- + + if ( StringUtils.isEmpty( book.getDirectory() ) ) + { + throw new MojoFailureException( "Invalid configuration: The book is required to have a descriptor set." ); + } + + if ( StringUtils.isEmpty( book.getDirectory() ) ) + { + throw new MojoFailureException( "Invalid configuration: The book is required to have a directory set." ); + } + + if ( book.getFormats() == null || book.getFormats().size() == 0 ) + { + throw new MojoFailureException( "Invalid configuration: The book is required to have at least one format set." ); + } + + // ---------------------------------------------------------------------- + // + // ---------------------------------------------------------------------- + + File descriptor = new File( basedir, book.getDescriptor() ); + + String includes = ""; + + if ( book.getIncludes() != null ) + { + for ( Iterator j = book.getIncludes().iterator(); j.hasNext(); ) + { + String include = (String) j.next(); + + includes += include + ","; + } + } + else + { + includes = "**/*"; + } + + String excludes = ""; + + if ( book.getExcludes() != null ) + { + for ( Iterator j = book.getExcludes().iterator(); j.hasNext(); ) + { + String exclude = (String) j.next(); + + excludes += exclude + ","; + } + } + + // ---------------------------------------------------------------------- + // Find all the files to pass to the renderer. + // ---------------------------------------------------------------------- + + getLog().debug( "Locating files to include in the book:" ); + getLog().debug( "Basedir: " + basedir ); + getLog().debug( "Includes: " + includes ); + getLog().debug( "Excludes: " + excludes ); + + List files; + + try + { + files = FileUtils.getFiles( new File( basedir, book.getDirectory() ), includes, excludes ); + } + catch ( IOException e ) + { + throw new MojoExecutionException( "Error while looking for input files. " + + "Basedir=" + basedir.getAbsolutePath() + ", " + + "includes=" + includes + ", " + + "excludes=" + excludes, e ); + } + + for ( Iterator j = book.getFormats().iterator(); j.hasNext(); ) + { + String format = (String) j.next(); + + try + { + bookDoxia.renderBook( descriptor, format, files, new File( outputDirectory, format ) ); + } + catch ( InvalidBookDescriptorException e ) + { + throw new MojoFailureException( "Invalid book descriptor: " + LINE_SEPARATOR + + formatResult( e.getValidationResult() ) ); + } + catch ( BookDoxiaException e ) + { + throw new MojoExecutionException( "Error while generating book in format '" + format + "'.", e ); + } + } + } + } + + private String formatResult( ValidationResult result ) + { + StringBuffer buffer = new StringBuffer(); + + if ( result.getErrors().size() > 0 ) + { + buffer.append( "Validation errors:" ); + + for ( Iterator it = result.getErrors().iterator(); it.hasNext(); ) + { + String error = (String) it.next(); + + buffer.append( LINE_SEPARATOR ).append( " " ).append( error ); + } + } + + if ( result.getWarnings().size() > 0 ) + { + buffer.append( "Validation warnings:" ); + + for ( Iterator it = result.getWarnings().iterator(); it.hasNext(); ) + { + String error = (String) it.next(); + + buffer.append( LINE_SEPARATOR ).append( " " ).append( error ); + } + } + + return buffer.toString(); + } +}