Author: trygvis Date: Mon Jul 3 16:34:53 2006 New Revision: 418877 URL: http://svn.apache.org/viewvc?rev=418877&view=rev Log: o More work done. Adding LaTeX book sink.
Added: maven/doxia/trunk/doxia-sandbox/doxia-book/src/main/java/org/apache/maven/doxia/book/services/renderer/LatexBookRenderer.java maven/doxia/trunk/doxia-sandbox/doxia-book/src/main/java/org/apache/maven/doxia/book/services/renderer/latex/ maven/doxia/trunk/doxia-sandbox/doxia-book/src/main/java/org/apache/maven/doxia/book/services/renderer/latex/LatexBookSink.java Modified: maven/doxia/trunk/doxia-sandbox/doxia-book/src/main/java/org/apache/maven/doxia/book/services/validation/DefaultBookValidator.java maven/doxia/trunk/doxia-sandbox/doxia-book/src/main/modello/book.mdo Added: maven/doxia/trunk/doxia-sandbox/doxia-book/src/main/java/org/apache/maven/doxia/book/services/renderer/LatexBookRenderer.java URL: http://svn.apache.org/viewvc/maven/doxia/trunk/doxia-sandbox/doxia-book/src/main/java/org/apache/maven/doxia/book/services/renderer/LatexBookRenderer.java?rev=418877&view=auto ============================================================================== --- maven/doxia/trunk/doxia-sandbox/doxia-book/src/main/java/org/apache/maven/doxia/book/services/renderer/LatexBookRenderer.java (added) +++ maven/doxia/trunk/doxia-sandbox/doxia-book/src/main/java/org/apache/maven/doxia/book/services/renderer/LatexBookRenderer.java Mon Jul 3 16:34:53 2006 @@ -0,0 +1,198 @@ +package org.apache.maven.doxia.book.services.renderer; + +import org.apache.maven.doxia.book.BookDoxiaException; +import org.apache.maven.doxia.book.services.renderer.latex.LatexBookSink; +import org.apache.maven.doxia.book.context.BookContext; +import org.apache.maven.doxia.book.model.Book; +import org.apache.maven.doxia.book.model.Chapter; +import org.apache.maven.doxia.book.model.Section; +import org.apache.maven.doxia.module.latex.LatexSink; +import org.apache.maven.doxia.parser.manager.ParserNotFoundException; +import org.apache.maven.doxia.parser.ParseException; +import org.apache.maven.doxia.Doxia; +import org.codehaus.plexus.util.StringUtils; +import org.codehaus.plexus.util.IOUtil; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.FileReader; +import java.io.FileNotFoundException; +import java.util.Iterator; +import java.util.List; +import java.util.ArrayList; +import java.util.Map; +import java.util.HashMap; + +/** + * @plexus.component role-hint="latex" + * + * @author <a href="mailto:[EMAIL PROTECTED]">Trygve Laugstøl</a> + * @version $Id$ + */ +public class LatexBookRenderer + implements BookRenderer +{ + /** + * @plexus.requirement + */ + private Doxia doxia; + + // ---------------------------------------------------------------------- + // BookRenderer Implementatino + // ---------------------------------------------------------------------- + + public void renderBook( BookContext context ) + throws BookDoxiaException + { + Book book = context.getBook(); + + if ( !context.getOutputDirectory().exists() ) + { + if ( !context.getOutputDirectory().mkdirs() ) + { + throw new BookDoxiaException( + "Could not make directory: " + context.getOutputDirectory().getAbsolutePath() + "." ); + } + } + + File bookFile = new File( context.getOutputDirectory(), book.getId() + ".tex" ); + + FileWriter fileWriter = null; + + try + { + fileWriter = new FileWriter( bookFile ); + + PrintWriter writer = new PrintWriter( fileWriter ); + + writeBook( book, context, writer ); + } + catch ( IOException e ) + { + throw new BookDoxiaException( "Error while opening file.", e ); + } + finally + { + IOUtil.close( fileWriter ); + } + } + + // ---------------------------------------------------------------------- + // Private + // ---------------------------------------------------------------------- + + private static class SectionInfo + { + private String id; + + private String title; + } + + private void writeBook( Book book, BookContext context, PrintWriter writer ) + throws IOException, BookDoxiaException + { + // ---------------------------------------------------------------------- + // Process all the section documents and collect their names + // ---------------------------------------------------------------------- + + Map sectionInfos = new HashMap(); + + for ( Iterator it = book.getChapters().iterator(); it.hasNext(); ) + { + Chapter chapter = (Chapter) it.next(); + + for ( Iterator j = chapter.getSections().iterator(); j.hasNext(); ) + { + Section section = (Section) j.next(); + + SectionInfo info = writeSection( section, context ); + + sectionInfos.put( info.id, info ); + } + } + + // ---------------------------------------------------------------------- + // Write the main .tex file + // ---------------------------------------------------------------------- + + writer.println( "\\documentclass{book}" ); + writer.println( "\\title{" + book.getTitle() + "}"); + + if ( StringUtils.isNotEmpty( book.getAuthor() ) ) + { + writer.println( "\\author{" + book.getAuthor() + "}" ); + } + + if ( StringUtils.isNotEmpty( book.getDate() ) ) + { + writer.println( "\\author{" + book.getDate() + "}" ); + } + + writer.print( IOUtil.toString( LatexSink.getDefaultSinkCommands() ) ); + writer.print( IOUtil.toString( LatexSink.getDefaultPreamble() ) ); + writer.println( "\\begin{document}"); + writer.println( "\\maketitle"); + writer.println( "\\tableofcontents"); +// writer.println( "\\listoffigures"); + + for ( Iterator it = book.getChapters().iterator(); it.hasNext(); ) + { + Chapter chapter = (Chapter) it.next(); + + writer.println( "\\chapter{" + chapter.getTitle() + "}" ); + + for ( Iterator j = chapter.getSections().iterator(); j.hasNext(); ) + { + Section section = (Section) j.next(); + + SectionInfo info = (SectionInfo) sectionInfos.get( section.getId() ); + + writer.println( "\\input{" + info.id + "}"); + } + } + + writer.println( "\\end{document}"); + } + + private SectionInfo writeSection( Section section, BookContext context ) + throws IOException, BookDoxiaException + { + File file = new File( context.getOutputDirectory(), (section.getId() + ".tex") ); + + FileWriter writer = new FileWriter( file ); + + LatexBookSink sink = new LatexBookSink( writer ); + + BookContext.BookFile bookFile = (BookContext.BookFile) context.getFiles().get( section.getId() ); + + if ( bookFile == null ) + { + throw new BookDoxiaException( "No document that matches section with id=" + section.getId() + "." ); + } + + try + { + doxia.parse( new FileReader( bookFile.getFile() ), bookFile.getParserId(), sink ); + } + catch ( ParserNotFoundException e ) + { + throw new BookDoxiaException( "Parser not found: " + bookFile.getParserId() + ".", e ); + } + catch ( ParseException e ) + { + throw new BookDoxiaException( "Error while parsing document: " + bookFile.getFile().getAbsolutePath() + ".", e ); + } + catch ( FileNotFoundException e ) + { + throw new BookDoxiaException( "Could not find document: " + bookFile.getFile().getAbsolutePath() + ".", e ); + } + + SectionInfo info = new SectionInfo(); + info.id = section.getId(); + info.title = sink.getTitle(); + + return info; + } +} Added: maven/doxia/trunk/doxia-sandbox/doxia-book/src/main/java/org/apache/maven/doxia/book/services/renderer/latex/LatexBookSink.java URL: http://svn.apache.org/viewvc/maven/doxia/trunk/doxia-sandbox/doxia-book/src/main/java/org/apache/maven/doxia/book/services/renderer/latex/LatexBookSink.java?rev=418877&view=auto ============================================================================== --- maven/doxia/trunk/doxia-sandbox/doxia-book/src/main/java/org/apache/maven/doxia/book/services/renderer/latex/LatexBookSink.java (added) +++ maven/doxia/trunk/doxia-sandbox/doxia-book/src/main/java/org/apache/maven/doxia/book/services/renderer/latex/LatexBookSink.java Mon Jul 3 16:34:53 2006 @@ -0,0 +1,70 @@ +package org.apache.maven.doxia.book.services.renderer.latex; + +import org.apache.maven.doxia.module.latex.LatexSink; + +import java.io.Writer; +import java.io.IOException; + +/** + * @author <a href="mailto:[EMAIL PROTECTED]">Trygve Laugstøl</a> + * @version $Id$ + */ +public class LatexBookSink + extends LatexSink +{ + private String text; + + private String title; + + // ---------------------------------------------------------------------- + // + // ---------------------------------------------------------------------- + + public LatexBookSink( Writer out ) + throws IOException + { + super( out, null, null, true ); + } + + // ---------------------------------------------------------------------- + // + // ---------------------------------------------------------------------- + + protected String getDocumentStart() + { + return ""; +// return "\\documentclass{book}"; + } + + protected String getDocumentBegin() + { + return null; + } + + // ---------------------------------------------------------------------- + // + // ---------------------------------------------------------------------- + + public void text( String text ) + { + this.text = text; + + super.text( text ); + } + + public void title_() + { + super.title_(); + + this.title = text; + } + + // ---------------------------------------------------------------------- + // + // ---------------------------------------------------------------------- + + public String getTitle() + { + return title; + } +} Modified: maven/doxia/trunk/doxia-sandbox/doxia-book/src/main/java/org/apache/maven/doxia/book/services/validation/DefaultBookValidator.java URL: http://svn.apache.org/viewvc/maven/doxia/trunk/doxia-sandbox/doxia-book/src/main/java/org/apache/maven/doxia/book/services/validation/DefaultBookValidator.java?rev=418877&r1=418876&r2=418877&view=diff ============================================================================== --- maven/doxia/trunk/doxia-sandbox/doxia-book/src/main/java/org/apache/maven/doxia/book/services/validation/DefaultBookValidator.java (original) +++ maven/doxia/trunk/doxia-sandbox/doxia-book/src/main/java/org/apache/maven/doxia/book/services/validation/DefaultBookValidator.java Mon Jul 3 16:34:53 2006 @@ -43,6 +43,8 @@ Chapter chapter = (Chapter) it.next(); validateChapter( result, chapter ); + + // TODO: Validate the chapter id } } @@ -62,9 +64,14 @@ return; } + if ( StringUtils.isEmpty( chapter.getTitle() ) ) + { + result.getErrors().add( "Missing title. Chapter id: " + chapter.getId() ); + } + if ( chapter.getSections().size() == 0 ) { - result.getErrors().add( "Each chapter has to contain at least one section. Chapter id: " + chapter.getId() ); + result.getErrors().add( "Chapter doesn't have any sections. Chapter id: " + chapter.getId() ); } } } Modified: maven/doxia/trunk/doxia-sandbox/doxia-book/src/main/modello/book.mdo URL: http://svn.apache.org/viewvc/maven/doxia/trunk/doxia-sandbox/doxia-book/src/main/modello/book.mdo?rev=418877&r1=418876&r2=418877&view=diff ============================================================================== --- maven/doxia/trunk/doxia-sandbox/doxia-book/src/main/modello/book.mdo (original) +++ maven/doxia/trunk/doxia-sandbox/doxia-book/src/main/modello/book.mdo Mon Jul 3 16:34:53 2006 @@ -23,7 +23,17 @@ <identifier>true</identifier> </field> <field> - <name>name</name> + <name>title</name> + <version>1.0.0</version> + <type>String</type> + </field> + <field> + <name>author</name> + <version>1.0.0</version> + <type>String</type> + </field> + <field> + <name>date</name> <version>1.0.0</version> <type>String</type> </field>