Author: vsiveton
Date: Sun Jan 18 07:59:51 2009
New Revision: 735477

URL: http://svn.apache.org/viewvc?rev=735477&view=rev
Log:
DOXIA-177: Invalid XHTML because of wrong position of table caption

o using a tempWriter to prewrite the xhtml content. The close() performs the 
change in the writer.
o fixed test case

Modified:
    
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/sink/XhtmlBaseSink.java
    
maven/doxia/doxia/trunk/doxia-core/src/test/java/org/apache/maven/doxia/sink/XhtmlBaseSinkTest.java

Modified: 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/sink/XhtmlBaseSink.java
URL: 
http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/sink/XhtmlBaseSink.java?rev=735477&r1=735476&r2=735477&view=diff
==============================================================================
--- 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/sink/XhtmlBaseSink.java
 (original)
+++ 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/sink/XhtmlBaseSink.java
 Sun Jan 18 07:59:51 2009
@@ -28,6 +28,7 @@
 import javax.swing.text.html.HTML.Tag;
 
 import org.apache.maven.doxia.markup.HtmlMarkup;
+import org.apache.maven.doxia.markup.Markup;
 import org.apache.maven.doxia.util.DoxiaUtils;
 import org.apache.maven.doxia.util.HtmlTools;
 import org.codehaus.plexus.util.StringUtils;
@@ -51,6 +52,10 @@
     /** The PrintWriter to write the result. */
     private PrintWriter writer;
 
+    /** The StringWriter to write the result temporary, so we could play with 
the output and fix XHTML
+     * like DOXIA-177. Calling the method {...@link #close()} is needed to 
perform the changes in the {...@link #writer}. */
+    private StringWriter tempWriter;
+
     /** Used to collect text events. */
     private StringBuffer buffer = new StringBuffer();
 
@@ -90,9 +95,6 @@
     /** Indicates that an image is part of a figure. */
     private boolean inFigure;
 
-    /** The StringWriter to write the table content (DOXIA-177). */
-    private StringWriter tableWriter;
-
     // ----------------------------------------------------------------------
     // Constructor
     // ----------------------------------------------------------------------
@@ -105,6 +107,7 @@
     public XhtmlBaseSink( Writer out )
     {
         this.writer = new PrintWriter( out );
+        this.tempWriter = new StringWriter();
     }
 
     // ----------------------------------------------------------------------
@@ -1085,8 +1088,6 @@
             paragraph_();
         }
 
-        tableWriter = new StringWriter();
-
         // start table with tableRows
         if ( attributes == null )
         {
@@ -1107,36 +1108,49 @@
     {
         writeEndTag( Tag.TABLE );
 
-        if ( tableWriter == null )
+        String content = tempWriter.toString();
+
+        String startTable =
+            new StringBuffer().append( Markup.LESS_THAN ).append( 
Tag.TABLE.toString() ).toString();
+
+        if ( content.lastIndexOf( startTable ) == -1 )
         {
-            throw new IllegalArgumentException( "table( SinkEventAttributes 
attributes ) was not called before." );
+            if ( getLog().isDebugEnabled() )
+            {
+                getLog().debug( "table() NOT call firstly" );
+            }
+            return;
         }
 
-        String content = tableWriter.toString();
-        tableWriter = null;
+        content = content.substring( content.lastIndexOf( startTable ) );
 
-        String startCaption = "<" + Tag.CAPTION.toString() + ">";
-        String endCaption = "</" + Tag.CAPTION.toString() + ">";
+        String startCaption =
+            new StringBuffer().append( Markup.LESS_THAN ).append( 
Tag.CAPTION.toString() )
+                              .append( Markup.GREATER_THAN ).toString();
+        String endCaption =
+            new StringBuffer().append( Markup.LESS_THAN ).append( Markup.SLASH 
).append( Tag.CAPTION.toString() )
+                              .append( Markup.GREATER_THAN ).toString();
 
-        if ( content.indexOf( startCaption ) == -1 && content.indexOf( 
endCaption ) == -1 )
-        {
-            write( content );
-        }
-        else
+        if ( content.indexOf( startCaption ) != -1 && content.indexOf( 
endCaption ) != -1 )
         {
             // DOXIA-177
             int iStartCaption = content.indexOf( startCaption );
             int iEndCaption = content.indexOf( endCaption ) + 
endCaption.length();
 
             String captionTag = content.substring( iStartCaption, iEndCaption 
);
-            content = StringUtils.replace( content, captionTag, "" );
+            String contentWithoutCaption = StringUtils.replace( content, 
captionTag, "" );
+
+            String startTr =
+                new StringBuffer().append( Markup.LESS_THAN ).append( 
Tag.TR.toString() ).toString();
 
             StringBuffer text = new StringBuffer();
-            text.append( content.substring( 0, content.indexOf( "<" + 
Tag.TR.toString() ) ) );
+            text.append( contentWithoutCaption.substring( 0, 
contentWithoutCaption.indexOf( startTr ) ) );
             text.append( captionTag );
-            text.append( content.substring( content.indexOf( "<" + 
Tag.TR.toString() ) ) );
+            text.append( contentWithoutCaption.substring( 
contentWithoutCaption.indexOf( startTr ) ) );
 
-            write( text.toString() );
+            String contentWithCaption = tempWriter.toString();
+            tempWriter = new StringWriter();
+            tempWriter.write( StringUtils.replace( contentWithCaption, 
content, text.toString() ) );
         }
     }
 
@@ -1784,6 +1798,8 @@
     /** {...@inheritdoc} */
     public void close()
     {
+        writer.write( tempWriter.toString() );
+        tempWriter = new StringWriter();
         writer.close();
     }
 
@@ -1838,14 +1854,7 @@
     /** {...@inheritdoc} */
     protected void write( String text )
     {
-        if ( tableWriter == null )
-        {
-            writer.write( unifyEOLs( text ) );
-        }
-        else
-        {
-            tableWriter.write( unifyEOLs( text ) );
-        }
+        tempWriter.write( unifyEOLs( text ) );
     }
 
 }

Modified: 
maven/doxia/doxia/trunk/doxia-core/src/test/java/org/apache/maven/doxia/sink/XhtmlBaseSinkTest.java
URL: 
http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-core/src/test/java/org/apache/maven/doxia/sink/XhtmlBaseSinkTest.java?rev=735477&r1=735476&r2=735477&view=diff
==============================================================================
--- 
maven/doxia/doxia/trunk/doxia-core/src/test/java/org/apache/maven/doxia/sink/XhtmlBaseSinkTest.java
 (original)
+++ 
maven/doxia/doxia/trunk/doxia-core/src/test/java/org/apache/maven/doxia/sink/XhtmlBaseSinkTest.java
 Sun Jan 18 07:59:51 2009
@@ -41,10 +41,9 @@
         // DOXIA-189
         XhtmlBaseSink sink = null;
 
+        Writer writer =  new StringWriter();
         try
         {
-            Writer writer =  new StringWriter();
-
             sink = new XhtmlBaseSink( writer );
 
             sink.paragraph();
@@ -54,11 +53,6 @@
             sink.italic_();
             sink.text( "." );
             sink.paragraph_();
-
-            String actual = writer.toString();
-            String expected = "<p>There should be no space before the 
<i>period</i>.</p>";
-
-            assertEquals( expected, actual );
         }
         finally
         {
@@ -67,6 +61,11 @@
                 sink.close();
             }
         }
+
+        String actual = writer.toString();
+        String expected = "<p>There should be no space before the 
<i>period</i>.</p>";
+
+        assertEquals( expected, actual );
     }
 
     /** @throws Exception */
@@ -75,41 +74,44 @@
     {
         // DOXIA-177
         XhtmlBaseSink sink = null;
+        Writer writer =  new StringWriter();
 
         try
         {
-            Writer writer =  new StringWriter();
-
             sink = new XhtmlBaseSink( writer );
 
             sink.table();
             sink.tableRows( new int[] {0}, false );
+            sink.tableCaption();
+            sink.text( "caption1" );
+            sink.tableCaption_();
+            sink.tableRow();
+            sink.tableCell();
+            sink.table();
+            sink.tableRows( new int[] {0}, false );
             sink.tableRow();
             sink.tableCell();
-// FIXME: include nested table
-//            sink.table();
-//            sink.tableRows( new int[] {0}, false );
-//            sink.tableRow();
-//            sink.tableCell();
-//            sink.text( "nestedTableCell" );
-//            sink.tableCell_();
-//            sink.tableRow_();
-//            sink.tableRows_();
-//            sink.table_();
+            sink.text( "nestedTableCell" );
+            sink.tableCell_();
+            sink.tableRow_();
+            sink.tableRows_();
+            sink.tableCaption();
+            sink.text( "caption2" );
+            sink.tableCaption_();
+            sink.table_();
             sink.tableCell_();
             sink.tableRow_();
             sink.tableRows_();
             sink.table_();
-
-            String actual = writer.toString();
-            //assertTrue( actual.indexOf( "nestedTableCell" ) != 1 );
         }
         finally
         {
-            if ( sink != null )
-            {
-                sink.close();
-            }
+            sink.close();
         }
+
+        String actual = writer.toString();
+        assertTrue( actual.indexOf( "nestedTableCell" ) != 1 );
+        assertTrue( actual.indexOf( 
"class=\"bodyTable\"><caption>caption1</caption><tr" ) != 1 );
+        assertTrue( actual.indexOf( 
"class=\"bodyTable\"><caption>caption2</caption><tr" ) != 1 );
     }
 }


Reply via email to