maven-doxia-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vsive...@apache.org
Subject svn commit: r561304 - in /maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src: main/java/org/apache/maven/doxia/module/xdoc/ test/java/org/apache/maven/doxia/module/xdoc/ test/resources/
Date Tue, 31 Jul 2007 11:58:49 GMT
Author: vsiveton
Date: Tue Jul 31 04:58:48 2007
New Revision: 561304

URL: http://svn.apache.org/viewvc?view=rev&rev=561304
Log:
DOXIA-130: Macro renaming

o using 'param' tag to handle macro settings in xdoc
o updated the xdoc parser to handle this
o review tests to write output into files
o updated test resources with the new macro declaration

Modified:
    maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/main/java/org/apache/maven/doxia/module/xdoc/XdocParser.java
    maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XdocParserTest.java
    maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/test/resources/macro.xml
    maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/test/resources/toc.xml

Modified: maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/main/java/org/apache/maven/doxia/module/xdoc/XdocParser.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/main/java/org/apache/maven/doxia/module/xdoc/XdocParser.java?view=diff&rev=561304&r1=561303&r2=561304
==============================================================================
--- maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/main/java/org/apache/maven/doxia/module/xdoc/XdocParser.java
(original)
+++ maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/main/java/org/apache/maven/doxia/module/xdoc/XdocParser.java
Tue Jul 31 04:58:48 2007
@@ -37,6 +37,7 @@
 import org.apache.maven.doxia.sink.Sink;
 
 import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.xml.pull.MXParser;
 import org.codehaus.plexus.util.xml.pull.XmlPullParser;
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
@@ -68,6 +69,12 @@
     /** Used for nested lists. */
     private int orderedListDepth = 0;
 
+    /** A macro name. */
+    private String macroName;
+
+    /** The macro parameters. */
+    private Map macroParameters = new HashMap();
+
     /** {@inheritDoc} */
     public void parse( Reader reader, Sink sink )
         throws ParseException
@@ -144,10 +151,9 @@
      * @param parser A parser.
      * @param sink the sink to receive the events.
      * @throws XmlPullParserException if there's a problem parsing the model
-     * @throws MacroExecutionException if there's a problem executing a macro
      */
     private void handleStartTag( XmlPullParser parser, Sink sink )
-        throws XmlPullParserException, MacroExecutionException
+        throws XmlPullParserException
     {
         isEmptyElement = parser.isEmptyElementTag();
 
@@ -288,37 +294,47 @@
                 }
             }
         }
+
+        // ----------------------------------------------------------------------
+        // Macro
+        // ----------------------------------------------------------------------
+
         else if ( parser.getName().equals( MACRO_TAG.toString() ) )
         {
             if ( !secondParsing )
             {
-                String macroName = parser.getAttributeValue( null, Attribute.NAME.toString()
);
-
-                int count = parser.getAttributeCount();
-
-                Map parameters = new HashMap();
-
-                for ( int i = 1; i < count; i++ )
-                {
-                    parameters.put( parser.getAttributeName( i ), parser.getAttributeValue(
i ) );
-                }
-
-                // TODO handles specific attributes
-                parameters.put( "sourceContent", sourceContent );
+                macroName = parser.getAttributeValue( null, Attribute.NAME.toString() );
 
-                XdocParser xdocParser = new XdocParser();
-                xdocParser.setSecondParsing( true );
-                parameters.put( "parser", xdocParser );
-
-                MacroRequest request = new MacroRequest( parameters, getBasedir() );
-
-                try
+                if ( StringUtils.isEmpty( macroName ) )
                 {
-                    executeMacro( macroName, request, sink );
+                    // TODO use logging?
+                    throw new IllegalArgumentException( "The '" + Attribute.NAME.toString()
+ "' attribute for the '"
+                        + MACRO_TAG.toString() + "' tag is required." );
                 }
-                catch ( MacroNotFoundException me )
+            }
+        }
+        else if ( parser.getName().equals( Tag.PARAM.toString() ) )
+        {
+            if ( !secondParsing )
+            {
+                if ( StringUtils.isNotEmpty( macroName ) )
                 {
-                    throw new MacroExecutionException( "Macro not found: " + macroName, me
);
+                    if ( macroParameters == null )
+                    {
+                        macroParameters = new HashMap();
+                    }
+
+                    String paramName = parser.getAttributeValue( null, Attribute.NAME.toString()
);
+                    String paramValue = parser.getAttributeValue( null, Attribute.VALUE.toString()
);
+
+                    if ( StringUtils.isEmpty( paramName ) || StringUtils.isEmpty( paramValue
) )
+                    {
+                        throw new IllegalArgumentException( "'" + Attribute.NAME.toString()
+ "' and '"
+                            + Attribute.VALUE.toString() + "' attributes for the '" + Tag.PARAM.toString()
+                            + "' tag are required inside the '" + MACRO_TAG.toString() +
"' tag." );
+                    }
+
+                    macroParameters.put( paramName, paramValue );
                 }
             }
         }
@@ -401,8 +417,10 @@
      *
      * @param parser A parser.
      * @param sink the sink to receive the events.
+     * @throws MacroExecutionException if there's a problem executing a macro
      */
     private void handleEndTag( XmlPullParser parser, Sink sink )
+        throws MacroExecutionException
     {
         if ( parser.getName().equals( DOCUMENT_TAG.toString() ) )
         {
@@ -491,11 +509,50 @@
                 isAnchor = false;
             }
         }
+
+        // ----------------------------------------------------------------------
+        // Macro
+        // ----------------------------------------------------------------------
+
         else if ( parser.getName().equals( MACRO_TAG.toString() ) )
         {
-            //Do nothing
-            return;
+            if ( !secondParsing )
+            {
+                if ( StringUtils.isNotEmpty( macroName ) )
+                {
+                    // TODO handles specific macro attributes
+                    macroParameters.put( "sourceContent", sourceContent );
+
+                    XdocParser xdocParser = new XdocParser();
+                    xdocParser.setSecondParsing( true );
+                    macroParameters.put( "parser", xdocParser );
+
+                    MacroRequest request = new MacroRequest( macroParameters, getBasedir()
);
+
+                    try
+                    {
+                        executeMacro( macroName, request, sink );
+                    }
+                    catch ( MacroNotFoundException me )
+                    {
+                        throw new MacroExecutionException( "Macro not found: " + macroName,
me );
+                    }
+                }
+            }
+
+            // Reinit macro
+            macroName = null;
+            macroParameters = null;
+        }
+        else if ( parser.getName().equals( Tag.PARAM.toString() ) )
+        {
+            // do nothing
         }
+
+        // ----------------------------------------------------------------------
+        // Tables
+        // ----------------------------------------------------------------------
+
         else if ( parser.getName().equals( Tag.TABLE.toString() ) )
         {
             sink.table_();

Modified: maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XdocParserTest.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XdocParserTest.java?view=diff&rev=561304&r1=561303&r2=561304
==============================================================================
--- maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XdocParserTest.java
(original)
+++ maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XdocParserTest.java
Tue Jul 31 04:58:48 2007
@@ -19,13 +19,16 @@
  * under the License.
  */
 
+import java.io.File;
 import java.io.FileReader;
+import java.io.FileWriter;
 import java.io.Reader;
 import java.io.StringWriter;
 
 import org.apache.maven.doxia.parser.AbstractParserTestCase;
 import org.apache.maven.doxia.parser.Parser;
 import org.apache.maven.doxia.sink.Sink;
+import org.codehaus.plexus.util.IOUtil;
 
 /**
  * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
@@ -62,64 +65,78 @@
     public void testSnippetMacro()
         throws Exception
     {
-        StringWriter output = null;
+        FileWriter output = null;
         Reader reader = null;
+        File f = getTestFile( getBasedir(), "target/output/macro.xml" );
 
         try
         {
-            output = new StringWriter();
+            output = new FileWriter( f );
             reader = new FileReader( getTestFile( getBasedir(), "src/test/resources/macro.xml"
) );
 
             Sink sink = new XdocSink( output );
             getParser().parse( reader, sink );
+        }
+        finally
+        {
+            IOUtil.close( output );
+            IOUtil.close( reader );
+        }
 
-            assertTrue( output.toString().indexOf( "&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;"
) != -1 );
+        assertTrue( "The file " + f.getAbsolutePath() + " was not created", f.exists() );
+
+        String content;
+        try
+        {
+            reader = new FileReader( f );
+            content = IOUtil.toString( reader );
         }
         finally
         {
-            if ( output != null )
-            {
-                output.close();
-            }
-
-            if ( reader != null )
-            {
-                reader.close();
-            }
+            IOUtil.close( reader );
         }
+
+        assertTrue( content.indexOf( "&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;"
) != -1 );
     }
 
     /** @throws Exception  */
     public void testTocMacro()
         throws Exception
     {
-        StringWriter output = null;
+        FileWriter output = null;
         Reader reader = null;
+        File f = getTestFile( getBasedir(), "target/output/toc.xml" );
 
         try
         {
-            output = new StringWriter();
+            output = new FileWriter( getTestFile( getBasedir(), "target/output/toc.xml" )
);
             reader = new FileReader( getTestFile( getBasedir(), "src/test/resources/toc.xml"
) );
 
             Sink sink = new XdocSink( output );
             getParser().parse( reader, sink );
+        }
+        finally
+        {
+            IOUtil.close( output );
+            IOUtil.close( reader );
+        }
 
-            // No section, only subsection 1 and 2
-            assertTrue( noNewLine( output.toString() ).indexOf( "<li><a href=\"#section_11\">Section
11</a></li>" ) != -1 );
-            assertTrue( noNewLine( output.toString() ).indexOf( "<li><a href=\"#section_1211\">Section
1211</a></li>" ) == -1 );
+        assertTrue( "The file " + f.getAbsolutePath() + " was not created", f.exists() );
+
+        String content;
+        try
+        {
+            reader = new FileReader( f );
+            content = IOUtil.toString( reader );
         }
         finally
         {
-            if ( output != null )
-            {
-                output.close();
-            }
-
-            if ( reader != null )
-            {
-                reader.close();
-            }
+            IOUtil.close( reader );
         }
+
+        // No section, only subsection 1 and 2
+        assertTrue( noNewLine( content ).indexOf( "<li><a href=\"#section_11\">Section
11</a></li>" ) != -1 );
+        assertTrue( noNewLine( content ).indexOf( "<li><a href=\"#section_1211\">Section
1211</a></li>" ) == -1 );
     }
 
     /**

Modified: maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/test/resources/macro.xml
URL: http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/test/resources/macro.xml?view=diff&rev=561304&r1=561303&r2=561304
==============================================================================
--- maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/test/resources/macro.xml (original)
+++ maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/test/resources/macro.xml Tue
Jul 31 04:58:48 2007
@@ -27,6 +27,9 @@
       <p>
         Test DOXIA-77
       </p>
-      <macro name="snippet" id="superpom" url="http://svn.apache.org/repos/asf/maven/components/trunk/maven-project/src/main/resources/org/apache/maven/project/pom-4.0.0.xml"/>
+      <macro name="snippet">
+        <param name="id" value="superpom"/>
+        <param name="url" value="http://svn.apache.org/repos/asf/maven/components/trunk/maven-project/src/main/resources/org/apache/maven/project/pom-4.0.0.xml"/>
+      </macro>
   </body>
 </document>

Modified: maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/test/resources/toc.xml
URL: http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/test/resources/toc.xml?view=diff&rev=561304&r1=561303&r2=561304
==============================================================================
--- maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/test/resources/toc.xml (original)
+++ maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/test/resources/toc.xml Tue
Jul 31 04:58:48 2007
@@ -32,7 +32,11 @@
           Section 1
         </p>
         <p>
-          <macro name="toc" section="1" fromDepth="1" toDepth="2"/>
+          <macro name="toc">
+           <param name="section" value="1"/>
+           <param name="fromDepth" value="1"/>
+           <param name="toDepth" value="2"/>
+          </macro>
         </p>
         <subsection name="Section 11">
           <p>



Mime
View raw message