You are viewing a plain text version of this content. The canonical link for it is here.
Posted to doxia-commits@maven.apache.org by lt...@apache.org on 2008/03/09 20:39:29 UTC

svn commit: r635336 - in /maven/doxia/doxia/trunk: doxia-core/src/main/java/org/apache/maven/doxia/parser/ doxia-core/src/main/java/org/apache/maven/doxia/sink/ doxia-core/src/test/java/org/apache/maven/doxia/parser/ doxia-core/src/test/java/org/apache...

Author: ltheussl
Date: Sun Mar  9 12:39:26 2008
New Revision: 635336

URL: http://svn.apache.org/viewvc?rev=635336&view=rev
Log:
[DOXIA-75] Clean up figure handling in XhtmlBaseParser/Sink. Only the new methods that take a SinkEventAttributeSet are modified, the old methods are kept backward compatible.

Modified:
    maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/XhtmlBaseParser.java
    maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/sink/SinkUtils.java
    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/parser/XhtmlBaseParserTest.java
    maven/doxia/doxia/trunk/doxia-core/src/test/java/org/apache/maven/doxia/sink/SinkTestDocument.java
    maven/doxia/doxia/trunk/doxia-modules/doxia-module-xhtml/src/main/java/org/apache/maven/doxia/module/xhtml/XhtmlParser.java

Modified: maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/XhtmlBaseParser.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/XhtmlBaseParser.java?rev=635336&r1=635335&r2=635336&view=diff
==============================================================================
--- maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/XhtmlBaseParser.java (original)
+++ maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/XhtmlBaseParser.java Sun Mar  9 12:39:26 2008
@@ -26,6 +26,7 @@
 import org.apache.maven.doxia.sink.Sink;
 import org.apache.maven.doxia.sink.SinkEventAttributeSet;
 
+import org.apache.maven.doxia.sink.SinkEventAttributes;
 import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.xml.pull.XmlPullParser;
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
@@ -59,6 +60,9 @@
     /** Verbatim level, increased whenever a <pre> tag is encountered. */
     private int verbatimLevel;
 
+    /** Used to recognize the case of img inside figure. */
+    private boolean inFigure;
+
     /**
      * <p>
      *   Goes through a common list of possible html start tags. These include only tags that can go into
@@ -128,7 +132,22 @@
         }
         else if ( parser.getName().equals( Tag.P.toString() ) )
         {
-            sink.paragraph( attribs );
+            if ( !inFigure )
+            {
+                sink.paragraph( attribs );
+            }
+        }
+        else if ( parser.getName().equals( Tag.DIV.toString() ) )
+        {
+            String divclass = parser.getAttributeValue( null, Attribute.CLASS.toString() );
+
+            if ( "figure".equals( divclass ) )
+            {
+                this.inFigure = true;
+                SinkEventAttributeSet atts = new SinkEventAttributeSet( attribs );
+                atts.removeAttribute( SinkEventAttributes.CLASS );
+                sink.figure( atts );
+            }
         }
         /*
          * The PRE element tells visual user agents that the enclosed text is
@@ -218,7 +237,14 @@
         else if ( ( parser.getName().equals( Tag.I.toString() ) )
                 || ( parser.getName().equals( Tag.EM.toString() ) ) )
         {
-            sink.italic( attribs );
+            if ( inFigure )
+            {
+                sink.figureCaption( attribs );
+            }
+            else
+            {
+                sink.italic( attribs );
+            }
         }
         else if ( ( parser.getName().equals( Tag.CODE.toString() ) )
                 || ( parser.getName().equals( Tag.SAMP.toString() ) )
@@ -333,31 +359,11 @@
         else if ( parser.getName().equals( Tag.IMG.toString() ) )
         {
             String src = parser.getAttributeValue( null, Attribute.SRC.toString() );
-            String title = parser.getAttributeValue( null, Attribute.TITLE.toString() );
-            String alt = parser.getAttributeValue( null, Attribute.ALT.toString() );
-
-            sink.figure();
 
             if ( src != null )
             {
                 sink.figureGraphics( src, attribs );
             }
-
-            // TODO: remove, see DOXIA-75
-            if ( title != null )
-            {
-                sink.figureCaption();
-                sink.text( title );
-                sink.figureCaption_();
-            }
-            else if ( alt != null )
-            {
-                sink.figureCaption();
-                sink.text( alt );
-                sink.figureCaption_();
-            }
-
-            sink.figure_();
         }
         else
         {
@@ -385,7 +391,18 @@
 
         if ( parser.getName().equals( Tag.P.toString() ) )
         {
-            sink.paragraph_();
+            if ( !inFigure )
+            {
+                sink.paragraph_();
+            }
+        }
+        else if ( parser.getName().equals( Tag.DIV.toString() ) )
+        {
+            if ( inFigure )
+            {
+                sink.figure_();
+                this.inFigure = false;
+            }
         }
         else if ( parser.getName().equals( Tag.PRE.toString() ) )
         {
@@ -434,7 +451,14 @@
         else if ( ( parser.getName().equals( Tag.I.toString() ) )
                 || ( parser.getName().equals( Tag.EM.toString() ) ) )
         {
-            sink.italic_();
+            if ( inFigure )
+            {
+                sink.figureCaption_();
+            }
+            else
+            {
+                sink.italic_();
+            }
         }
         else if ( ( parser.getName().equals( Tag.CODE.toString() ) )
                 || ( parser.getName().equals( Tag.SAMP.toString() ) )

Modified: maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/sink/SinkUtils.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/sink/SinkUtils.java?rev=635336&r1=635335&r2=635336&view=diff
==============================================================================
--- maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/sink/SinkUtils.java (original)
+++ maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/sink/SinkUtils.java Sun Mar  9 12:39:26 2008
@@ -171,28 +171,30 @@
      * ie it can be appended directly to an xml start tag. Attribute values that are itself
      * AttributeSets are ignored, all other keys and values are written as Strings.
      *
-     * @param att The AttributeSet.
+     * @param att The AttributeSet. May be null, in which case an empty String is returned.
      * @return the AttributeSet as a String in a form that can be appended to an xml start tag.
      */
     public static String getAttributeString( AttributeSet att )
     {
+        if ( att == null )
+        {
+            return "";
+        }
+
         StringBuffer sb = new StringBuffer();
 
-        if ( att != null )
+        Enumeration names = att.getAttributeNames();
+
+        while ( names.hasMoreElements() )
         {
-            Enumeration names = att.getAttributeNames();
+            Object key = names.nextElement();
+            Object value = att.getAttribute( key );
 
-            while ( names.hasMoreElements() )
+            // AttributeSets are ignored
+            if ( !(value instanceof AttributeSet) )
             {
-                Object key = names.nextElement();
-                Object value = att.getAttribute( key );
-
-                // AttributeSets are ignored
-                if ( !(value instanceof AttributeSet) )
-                {
-                    sb.append( Markup.SPACE ).append( key.toString() ).append( Markup.EQUAL )
-                        .append( Markup.QUOTE ).append( value.toString() ).append( Markup.QUOTE );
-                }
+                sb.append( Markup.SPACE ).append( key.toString() ).append( Markup.EQUAL )
+                    .append( Markup.QUOTE ).append( value.toString() ).append( Markup.QUOTE );
             }
         }
 

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=635336&r1=635335&r2=635336&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 Mar  9 12:39:26 2008
@@ -72,6 +72,15 @@
     /** used to store attributes passed to table(). */
     private MutableAttributeSet tableAttributes;
 
+    /** Used to distinguish old-style figure handling. */
+    private boolean legacyFigure;
+
+    /** Used to distinguish old-style figure handling. */
+    private boolean legacyFigureCaption;
+
+    /** Indicates that an image is part of a figure. */
+    private boolean inFigure;
+
     // ----------------------------------------------------------------------
     // Constructor
     // ----------------------------------------------------------------------
@@ -703,10 +712,14 @@
     /**
      * {@inheritDoc}
      * @see javax.swing.text.html.HTML.Tag#IMG
+     * @deprecated Use {@link figure(SinkEventAttributes)}, this method is only kept for
+     * backward compatibility. Note that the behavior is different though, as this method
+     * writes an img tag, while correctly the img tag should be written by  figureGraphics().
      */
     public void figure()
     {
         write( String.valueOf( LESS_THAN ) + Tag.IMG );
+        legacyFigure = true;
     }
 
     /**
@@ -715,19 +728,44 @@
      */
     public void figure( SinkEventAttributes attributes )
     {
+        inFigure = true;
+
         MutableAttributeSet atts = SinkUtils.filterAttributes(
                 attributes, SinkUtils.SINK_BASE_ATTRIBUTES  );
 
-        write( String.valueOf( LESS_THAN ) + Tag.IMG + SinkUtils.getAttributeString( atts ) );
+        if ( atts == null )
+        {
+            atts = new SinkEventAttributeSet( 1 );
+        }
+        
+        if ( !atts.isDefined( SinkEventAttributes.CLASS ) )
+        {
+            atts.addAttribute( SinkEventAttributes.CLASS, "figure" );
+        }
+        
+        writeStartTag( Tag.DIV, atts );
     }
 
     /** {@inheritDoc} */
     public void figure_()
     {
-        write( String.valueOf( SPACE ) + SLASH + GREATER_THAN );
+        if ( legacyFigure )
+        {
+            write( String.valueOf( SPACE ) + SLASH + GREATER_THAN );
+            legacyFigure = false;
+        }
+        else
+        {
+            writeEndTag( Tag.DIV );
+            inFigure = false;
+        }
     }
 
-    /** {@inheritDoc} */
+    /** {@inheritDoc}
+     * @deprecated Use {@link figureGraphics(String,SinkEventAttributes)},
+     * this method is only kept for backward compatibility. Note that the behavior is
+     * different though, as this method does not write the img tag, only the src attribute.
+     */
     public void figureGraphics( String name )
     {
         write( String.valueOf( SPACE ) + Attribute.SRC + EQUAL + QUOTE + name + QUOTE );
@@ -736,34 +774,73 @@
     /** {@inheritDoc} */
     public void figureGraphics( String src, SinkEventAttributes attributes )
     {
-        MutableAttributeSet atts = SinkUtils.filterAttributes(
-                attributes, SinkUtils.SINK_IMG_ATTRIBUTES  );
-        // TODO: remove, see DOXIA-75
-        atts.removeAttribute( Attribute.ALT.toString() );
-        atts.removeAttribute( Attribute.TITLE.toString() );
-        atts.removeAttribute( Attribute.SRC.toString() );
-
-        write( String.valueOf( SPACE ) + Attribute.SRC + EQUAL
-                + QUOTE + src + QUOTE + SinkUtils.getAttributeString( atts ) );
+        if ( inFigure )
+        {
+            MutableAttributeSet atts = new SinkEventAttributeSet( 1 );
+            atts.addAttribute( SinkEventAttributes.ALIGN, "center" );
+            
+            writeStartTag( Tag.P, atts );
+        }
+        
+        int count = ( attributes == null ? 1 : attributes.getAttributeCount() + 1 );
+        
+        MutableAttributeSet atts = new SinkEventAttributeSet( count );
+        
+        atts.addAttribute( Attribute.SRC, src );
+        atts.addAttributes( SinkUtils.filterAttributes(
+                attributes, SinkUtils.SINK_IMG_ATTRIBUTES ) );
+        
+        writeStartTag( Tag.IMG, atts, true );
+        
+        if ( inFigure )
+        {
+            writeEndTag( Tag.P );
+        }
     }
 
-    /** {@inheritDoc} */
+    /** {@inheritDoc}
+     * @deprecated Use {@link figureCaption(SinkEventAttributes)},
+     * this method is only kept for backward compatibility. Note that the behavior is
+     * different though, as this method only writes an alt attribute.
+     */
     public void figureCaption()
     {
         write( String.valueOf( SPACE ) + Attribute.ALT + EQUAL + QUOTE );
+        legacyFigureCaption = true;
     }
 
     /** {@inheritDoc} */
     public void figureCaption( SinkEventAttributes attributes )
     {
-        // TODO: see DOXIA-75
-        write( String.valueOf( SPACE ) + Attribute.ALT + EQUAL + QUOTE );
+        if ( legacyFigureCaption )
+        {
+            write( String.valueOf( SPACE ) + Attribute.ALT + EQUAL + QUOTE );
+            legacyFigureCaption = false;
+        }
+        else
+        {
+            SinkEventAttributeSet atts = new SinkEventAttributeSet( 1 );
+            atts.addAttribute( SinkEventAttributes.ALIGN, "center" );
+            atts.addAttributes( SinkUtils.filterAttributes(
+                attributes, SinkUtils.SINK_BASE_ATTRIBUTES  ) );
+            
+            paragraph( atts );
+            italic();
+        }
     }
 
     /** {@inheritDoc} */
     public void figureCaption_()
     {
-        write( String.valueOf( QUOTE ) );
+        if ( legacyFigureCaption )
+        {
+            write( String.valueOf( QUOTE ) );
+        }
+        else
+        {
+            italic_();
+            paragraph_();
+        }
     }
 
     /**

Modified: maven/doxia/doxia/trunk/doxia-core/src/test/java/org/apache/maven/doxia/parser/XhtmlBaseParserTest.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-core/src/test/java/org/apache/maven/doxia/parser/XhtmlBaseParserTest.java?rev=635336&r1=635335&r2=635336&view=diff
==============================================================================
--- maven/doxia/doxia/trunk/doxia-core/src/test/java/org/apache/maven/doxia/parser/XhtmlBaseParserTest.java (original)
+++ maven/doxia/doxia/trunk/doxia-core/src/test/java/org/apache/maven/doxia/parser/XhtmlBaseParserTest.java Sun Mar  9 12:39:26 2008
@@ -98,12 +98,7 @@
 
         Iterator it = sink.getEventList().iterator();
 
-        assertEquals( "figure", ( (SinkEventElement) it.next() ).getName() );
         assertEquals( "figureGraphics", ( (SinkEventElement) it.next() ).getName() );
-        assertEquals( "figureCaption", ( (SinkEventElement) it.next() ).getName() );
-        assertEquals( "text", ( (SinkEventElement) it.next() ).getName() );
-        assertEquals( "figureCaption_", ( (SinkEventElement) it.next() ).getName() );
-        assertEquals( "figure_", ( (SinkEventElement) it.next() ).getName() );
         assertFalse( it.hasNext() );
     }
 

Modified: maven/doxia/doxia/trunk/doxia-core/src/test/java/org/apache/maven/doxia/sink/SinkTestDocument.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-core/src/test/java/org/apache/maven/doxia/sink/SinkTestDocument.java?rev=635336&r1=635335&r2=635336&view=diff
==============================================================================
--- maven/doxia/doxia/trunk/doxia-core/src/test/java/org/apache/maven/doxia/sink/SinkTestDocument.java (original)
+++ maven/doxia/doxia/trunk/doxia-core/src/test/java/org/apache/maven/doxia/sink/SinkTestDocument.java Sun Mar  9 12:39:26 2008
@@ -286,12 +286,12 @@
      */
     public static void generateFigure( Sink sink )
     {
-        sink.figure();
+        sink.figure( null );
 
-        sink.figureGraphics( "figure.png" );
+        sink.figureGraphics( "figure.png", null );
 
-        sink.figureCaption();
-        sink.text( "Figure caption" );
+        sink.figureCaption( null );
+        sink.text( "Figure caption", null );
         sink.figureCaption_();
 
         sink.figure_();

Modified: maven/doxia/doxia/trunk/doxia-modules/doxia-module-xhtml/src/main/java/org/apache/maven/doxia/module/xhtml/XhtmlParser.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-modules/doxia-module-xhtml/src/main/java/org/apache/maven/doxia/module/xhtml/XhtmlParser.java?rev=635336&r1=635335&r2=635336&view=diff
==============================================================================
--- maven/doxia/doxia/trunk/doxia-modules/doxia-module-xhtml/src/main/java/org/apache/maven/doxia/module/xhtml/XhtmlParser.java (original)
+++ maven/doxia/doxia/trunk/doxia-modules/doxia-module-xhtml/src/main/java/org/apache/maven/doxia/module/xhtml/XhtmlParser.java Sun Mar  9 12:39:26 2008
@@ -111,6 +111,7 @@
             {
                 this.boxed = true;
             }
+            super.baseStartTag( parser, sink ); // pick up other divs
         }
         /*
          * The PRE element tells visual user agents that the enclosed text is
@@ -171,6 +172,7 @@
         else if ( parser.getName().equals( Tag.DIV.toString() ) )
         {
             this.boxed = false;
+            super.baseEndTag( parser, sink );
         }
         else if ( !baseEndTag( parser, sink ) )
         {