Author: vsiveton
Date: Sat Sep  5 14:19:51 2009
New Revision: 811653

URL: http://svn.apache.org/viewvc?rev=811653&view=rev
Log:
DOXIASITETOOLS-33: Add an option to include or not the TOC

o option to add the TOC at the beginning or the end of the doc

Modified:
    
maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/fo/FoPdfRenderer.java
    
maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/itext/ITextPdfRenderer.java
    
maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/resources/org/apache/maven/doxia/docrenderer/pdf/itext/TOC.xslt
    
maven/doxia/doxia/trunk/doxia-modules/doxia-module-fo/src/main/java/org/apache/maven/doxia/module/fo/FoAggregateSink.java

Modified: 
maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/fo/FoPdfRenderer.java
URL: 
http://svn.apache.org/viewvc/maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/fo/FoPdfRenderer.java?rev=811653&r1=811652&r2=811653&view=diff
==============================================================================
--- 
maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/fo/FoPdfRenderer.java
 (original)
+++ 
maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/fo/FoPdfRenderer.java
 Sat Sep  5 14:19:51 2009
@@ -120,17 +120,32 @@
                 getLogger().debug( "Loaded pdf config file: " + 
fOConfigFile.getAbsolutePath() );
             }
 
-            boolean addToc =
-                ( context != null && context.get( "includeTOC" ) != null ? ( 
(Boolean) context.get( "includeTOC" ) )
-                                                                               
                                     .booleanValue()
-                                : true );
-            sink.setDocumentModel( documentModel, addToc );
+            String generateTOC =
+                ( context != null && context.get( "generateTOC" ) != null ? 
context.get( "generateTOC" ).toString().trim()
+                                : "start" );
+            int tocPosition = 0;
+            if ( "start".equalsIgnoreCase( generateTOC ))
+            {
+                tocPosition = FoAggregateSink.TOC_START;
+            }
+            else if ( "end".equalsIgnoreCase( generateTOC ))
+            {
+                tocPosition = FoAggregateSink.TOC_END;
+            }
+            else
+            {
+                tocPosition = FoAggregateSink.TOC_NONE;
+            }
+            sink.setDocumentModel( documentModel, tocPosition );
 
             sink.beginDocument();
 
             sink.coverPage();
 
-            sink.toc();
+            if ( tocPosition == FoAggregateSink.TOC_START )
+            {
+                sink.toc();
+            }
 
             if ( ( documentModel.getToc() == null ) || ( 
documentModel.getToc().getItems() == null ) )
             {
@@ -145,6 +160,11 @@
                 mergeSourcesFromTOC( documentModel.getToc(), sink, context );
             }
 
+            if ( tocPosition == FoAggregateSink.TOC_END )
+            {
+                sink.toc();
+            }
+
             sink.endDocument();
         }
         finally

Modified: 
maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/itext/ITextPdfRenderer.java
URL: 
http://svn.apache.org/viewvc/maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/itext/ITextPdfRenderer.java?rev=811653&r1=811652&r2=811653&view=diff
==============================================================================
--- 
maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/itext/ITextPdfRenderer.java
 (original)
+++ 
maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/itext/ITextPdfRenderer.java
 Sat Sep  5 14:19:51 2009
@@ -180,15 +180,14 @@
             iTextFiles = parseTOCFiles( outputDirectory, documentModel, 
context );
         }
 
-        boolean addToc =
-            ( context != null && context.get( "includeTOC" ) != null ? ( 
(Boolean) context.get( "includeTOC" ) )
-                                                                               
                                 .booleanValue()
-                            : true );
+        String generateTOC =
+            ( context != null && context.get( "generateTOC" ) != null ? 
context.get( "generateTOC" ).toString()
+                            : "start" );
 
         File iTextFile = new File( outputDirectory, outputName + ".xml" );
         File iTextOutput = new File( outputDirectory, outputName + "." + 
getOutputExtension() );
         Document document = generateDocument( iTextFiles );
-        transform( documentModel, document, iTextFile, addToc );
+        transform( documentModel, document, iTextFile, generateTOC );
         generatePdf( iTextFile, iTextOutput );
     }
 
@@ -374,9 +373,10 @@
      * @param transformer the Transformer to set the parameters.
      * @param documentModel the DocumentModel to take the parameters from, 
could be null.
      * @param iTextFile the iTextFile not null for the relative paths.
-     * @param addToc to include or not the TOC.
+     * @param generateTOC not null, possible values are: 'none', 'start' and 
'end'.
      */
-    private void addTransformerParameters( Transformer transformer, 
DocumentModel documentModel, File iTextFile, boolean addToc )
+    private void addTransformerParameters( Transformer transformer, 
DocumentModel documentModel, File iTextFile,
+                                           String generateTOC )
     {
         if ( documentModel == null )
         {
@@ -384,7 +384,7 @@
         }
 
         // TOC
-        addTransformerParameter( transformer, "toc.add", Boolean.toString( 
addToc ) );
+        addTransformerParameter( transformer, "toc.position", generateTOC );
 
         // Meta parameters
         boolean hasNullMeta = false;
@@ -491,15 +491,15 @@
      * @param documentModel the DocumentModel to take the parameters from, 
could be null.
      * @param document the Document to transform.
      * @param iTextFile the resulting iText xml file.
-     * @param addToc to include or not the TOC.
+     * @param generateTOC not null, possible values are: 'none', 'start' and 
'end'.
      * @throws DocumentRendererException in case of a transformation error.
      */
-    private void transform( DocumentModel documentModel, Document document, 
File iTextFile, boolean addToc )
+    private void transform( DocumentModel documentModel, Document document, 
File iTextFile, String generateTOC )
         throws DocumentRendererException
     {
         Transformer transformer = initTransformer();
 
-        addTransformerParameters( transformer, documentModel, iTextFile, 
addToc );
+        addTransformerParameters( transformer, documentModel, iTextFile, 
generateTOC );
 
         // need a writer for StreamResult to prevent FileNotFoundException 
when iTextFile contains spaces
         Writer writer = null;

Modified: 
maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/resources/org/apache/maven/doxia/docrenderer/pdf/itext/TOC.xslt
URL: 
http://svn.apache.org/viewvc/maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/resources/org/apache/maven/doxia/docrenderer/pdf/itext/TOC.xslt?rev=811653&r1=811652&r2=811653&view=diff
==============================================================================
--- 
maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/resources/org/apache/maven/doxia/docrenderer/pdf/itext/TOC.xslt
 (original)
+++ 
maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/resources/org/apache/maven/doxia/docrenderer/pdf/itext/TOC.xslt
 Sat Sep  5 14:19:51 2009
@@ -22,7 +22,7 @@
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
 
     <!-- TOC Parameters -->
-    <xsl:param name="toc.add" />
+    <xsl:param name="toc.position" />
 
     <!-- MetaData Parameters -->
     <xsl:param name="meta.author" />
@@ -113,7 +113,7 @@
 
         <!-- Start TOC -->
         <xsl:choose>
-          <xsl:when test="$toc.add = 'true'">
+          <xsl:when test="$toc.position = 'start'">
             <newpage />
             <paragraph align="Center">
               <!-- TODO i18N -->
@@ -133,6 +133,25 @@
 
         <xsl:apply-templates select="*" mode="body" />
 
+        <!-- Start TOC -->
+        <xsl:choose>
+          <xsl:when test="$toc.position = 'end'">
+            <newpage />
+            <paragraph align="Center">
+              <!-- TODO i18N -->
+              <chunk font="Helvetica" size="24" fontstyle="bold" blue="0"
+                green="0" red="0">Table Of Contents</chunk>
+            </paragraph>
+            <paragraph align="Left" leading="24.0">
+              <newline />
+              <xsl:apply-templates select="*" mode="toc" />
+            </paragraph>
+          </xsl:when>
+          <xsl:otherwise>
+            <newpage />
+          </xsl:otherwise>
+        </xsl:choose>
+        <!-- End TOC -->
         </itext>
     </xsl:template>
 

Modified: 
maven/doxia/doxia/trunk/doxia-modules/doxia-module-fo/src/main/java/org/apache/maven/doxia/module/fo/FoAggregateSink.java
URL: 
http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-modules/doxia-module-fo/src/main/java/org/apache/maven/doxia/module/fo/FoAggregateSink.java?rev=811653&r1=811652&r2=811653&view=diff
==============================================================================
--- 
maven/doxia/doxia/trunk/doxia-modules/doxia-module-fo/src/main/java/org/apache/maven/doxia/module/fo/FoAggregateSink.java
 (original)
+++ 
maven/doxia/doxia/trunk/doxia-modules/doxia-module-fo/src/main/java/org/apache/maven/doxia/module/fo/FoAggregateSink.java
 Sat Sep  5 14:19:51 2009
@@ -67,8 +67,30 @@
  * @version $Id$
  * @since 1.1
  */
-public class FoAggregateSink extends FoSink
+public class FoAggregateSink
+    extends FoSink
 {
+    /**
+     * No Table Of Content.
+     * @see #setDocumentModel(DocumentModel, int)
+     */
+    public static int TOC_NONE = 0;
+
+    /**
+     * Table Of Content at the start of the document.
+     * @see #setDocumentModel(DocumentModel, int)
+     */
+    public static int TOC_START = 1;
+
+    /**
+     * Table Of Content at the end of the document.
+     * @see #setDocumentModel(DocumentModel, int)
+     */
+    public static int TOC_END = 2;
+
+    // TODO: make configurable
+    private static final String COVER_HEADER_HEIGHT = "1.5in";
+
     /** The document model to be used by this sink. */
     private DocumentModel docModel;
 
@@ -84,15 +106,12 @@
     /** Content in head is ignored in aggregated documents. */
     private boolean ignoreText;
 
-    /** Flag to include of not the toc */
-    private boolean addToc;
+    /** Current position of the TOC, see {...@link #TOC_POSITION} */
+    private int tocPosition;
 
     /** Used to get the current position in the TOC. */
     private final Stack tocStack = new Stack();
 
-    // TODO: make configurable
-    private static final String COVER_HEADER_HEIGHT = "1.5in";
-
     /**
      * Constructor.
      *
@@ -231,7 +250,6 @@
         }
     }
 
-
     /**
      * Sets the name of the current source document, relative to the source 
root.
      * Used to resolve links to other source documents.
@@ -246,36 +264,54 @@
     /**
      * Sets the DocumentModel to be used by this sink. The DocumentModel 
provides all the meta-information
      * required to render a document, eg settings for the cover page, table of 
contents, etc.
+     * <br/>
+     * By default, a TOC will be added at the beginning of the document.
      *
      * @param model the DocumentModel.
-     * @see #setDocumentModel(DocumentModel, boolean)
+     * @see #setDocumentModel(DocumentModel, String)
+     * @see #TOC_START
      */
     public void setDocumentModel( DocumentModel model )
     {
-        setDocumentModel( model, true );
+        setDocumentModel( model, TOC_START );
     }
 
     /**
      * Sets the DocumentModel to be used by this sink. The DocumentModel 
provides all the meta-information
      * required to render a document, eg settings for the cover page, table of 
contents, etc.
      *
-     * @param model the DocumentModel.
-     * @param addToc true to include the TOC in the sink.
+     * @param model the DocumentModel, could be null.
+     * @param tocPos should be one of these values: {...@link #TOC_NONE}, 
{...@link #TOC_START} and {...@link #TOC_END}.
      * @since 1.1.2
      */
-    public void setDocumentModel( DocumentModel model, boolean addToc )
+    public void setDocumentModel( DocumentModel model, int tocPos )
     {
         this.docModel = model;
-        this.addToc = addToc;
+        if ( !( tocPos == TOC_NONE || tocPos == TOC_START || tocPos == TOC_END 
) )
+        {
+            if ( getLog().isDebugEnabled() )
+            {
+                getLog().debug( "Unrecognized value for tocPosition: " + 
tocPos + ", using no toc." );
+            }
+            tocPos = TOC_NONE;
+        }
+        this.tocPosition = tocPos;
 
-        if ( this.addToc )
+        if ( this.docModel != null && this.docModel.getToc() != null && 
this.tocPosition != TOC_NONE )
         {
             DocumentTOCItem tocItem = new DocumentTOCItem();
             tocItem.setName( this.docModel.getToc().getName() );
             tocItem.setRef( "./toc" );
             List items = new LinkedList();
-            items.add( tocItem );
+            if ( this.tocPosition == TOC_START )
+            {
+                items.add( tocItem );
+            }
             items.addAll( this.docModel.getToc().getItems() );
+            if ( this.tocPosition == TOC_END )
+            {
+                items.add( tocItem );
+            }
 
             this.docModel.getToc().setItems( items );
         }
@@ -352,7 +388,7 @@
     }
 
     /** {...@inheritdoc} */
-    public void anchor( String name, SinkEventAttributes attributes  )
+    public void anchor( String name, SinkEventAttributes attributes )
     {
         if ( name == null )
         {
@@ -380,13 +416,13 @@
     }
 
     /** {...@inheritdoc} */
-    public void link( String name  )
+    public void link( String name )
     {
         link( name, null );
     }
 
     /** {...@inheritdoc} */
-    public void link( String name, SinkEventAttributes attributes  )
+    public void link( String name, SinkEventAttributes attributes )
     {
         if ( name == null )
         {
@@ -458,7 +494,7 @@
                 return;
             }
 
-            anchor = chopExtension ( anchor );
+            anchor = chopExtension( anchor );
 
             String base = docName.substring( 0, docName.lastIndexOf( "/" ) );
             anchor = base + "/" + anchor;
@@ -488,7 +524,7 @@
         return base + "/" + anchor;
     }
 
-    private  String chopExtension( String name )
+    private String chopExtension( String name )
     {
         String anchor = name;
 
@@ -504,13 +540,14 @@
 
                 if ( dot2 != -1 )
                 {
-                    anchor = anchor.substring( 0, dot ) + "#"
-                        + HtmlTools.encodeId( anchor.substring( hash + 1, dot2 
) );
+                    anchor =
+                        anchor.substring( 0, dot ) + "#" + HtmlTools.encodeId( 
anchor.substring( hash + 1, dot2 ) );
                 }
                 else
                 {
-                    anchor = anchor.substring( 0, dot ) + "#"
-                        + HtmlTools.encodeId( anchor.substring( hash + 1, 
anchor.length() ) );
+                    anchor =
+                        anchor.substring( 0, dot ) + "#"
+                            + HtmlTools.encodeId( anchor.substring( hash + 1, 
anchor.length() ) );
                 }
             }
             else
@@ -803,12 +840,8 @@
      */
     public void toc()
     {
-        if ( !this.addToc )
-        {
-            return;
-        }
-
-        if ( docModel == null || docModel.getToc() == null || 
docModel.getToc().getItems() == null )
+        if ( docModel == null || docModel.getToc() == null || 
docModel.getToc().getItems() == null
+            || this.tocPosition == TOC_NONE )
         {
             return;
         }
@@ -1107,7 +1140,6 @@
         writeSimpleTag( BLOCK_TAG );
         writeEndTag( TABLE_CELL_TAG );
 
-
         writeStartTag( TABLE_CELL_TAG, "number-columns-spanned", "2", 
"cover.border.left.bottom" );
         writeStartTag( BLOCK_TAG, "cover.subtitle" );
         write( subtitle == null ? ( version == null ? "" : " v. " + version ) 
: subtitle );
@@ -1140,7 +1172,7 @@
         writeEndTag( TABLE_ROW_TAG );
     }
 
-    private void writeCoverFooter( DocumentCover cover, DocumentMeta meta  )
+    private void writeCoverFooter( DocumentCover cover, DocumentMeta meta )
     {
         if ( cover == null && meta == null )
         {
@@ -1214,7 +1246,7 @@
 
         if ( atts == null )
         {
-            return new SinkEventAttributeSet( new String[] 
{SinkEventAttributes.HEIGHT, COVER_HEADER_HEIGHT} );
+            return new SinkEventAttributeSet( new String[] { 
SinkEventAttributes.HEIGHT, COVER_HEADER_HEIGHT } );
         }
 
         // FOP dpi: 72


Reply via email to