Author: ltheussl
Date: Thu Jul 19 02:42:05 2007
New Revision: 557536
URL: http://svn.apache.org/viewvc?view=rev&rev=557536
Log:
Clean up Exception handling. Split up the long parseXdoc method into parts.
Javadocs.
Modified:
maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/main/java/org/apache/maven/doxia/module/xdoc/XdocParser.java
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=557536&r1=557535&r2=557536
==============================================================================
---
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
Thu Jul 19 02:42:05 2007
@@ -19,12 +19,15 @@
* under the License.
*/
+import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
+import org.apache.maven.doxia.macro.MacroExecutionException;
+import org.apache.maven.doxia.macro.manager.MacroNotFoundException;
import org.apache.maven.doxia.macro.MacroRequest;
import org.apache.maven.doxia.parser.AbstractParser;
import org.apache.maven.doxia.parser.ParseException;
@@ -33,26 +36,34 @@
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.xml.pull.MXParser;
import org.codehaus.plexus.util.xml.pull.XmlPullParser;
+import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
/**
- * Parse an xdoc model and emit events into the specified doxia
- * Sink.
+ * Parse an xdoc model and emit events into the specified doxia Sink.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
* @version $Id:XdocParser.java 348605 2005-11-24 12:02:44 +1100 (Thu, 24 Nov
2005) brett $
- * @plexus.component role="org.apache.maven.doxia.parser.Parser"
- * role-hint="xdoc"
+ * @plexus.component role="org.apache.maven.doxia.parser.Parser"
role-hint="xdoc"
*/
public class XdocParser
extends AbstractParser
{
+ /** The source content of the input reader. Used to pass into macros. */
private String sourceContent;
+ /** Used to distinguish <a href=""> from <a name="">. */
private boolean isLink;
+
+ /** Used to distinguish <a href=""> from <a name="">. */
private boolean isAnchor;
+
+ /** Empty elements don't write a closing tag. */
private boolean isEmptyElement;
+
+ /** Used for nested lists. */
private int orderedListDepth = 0;
+ /** [EMAIL PROTECTED] */
public void parse( Reader reader, Sink sink )
throws ParseException
{
@@ -68,14 +79,30 @@
parseXdoc( parser, sink );
}
- catch ( Exception ex )
+ catch ( XmlPullParserException ex )
+ {
+ throw new ParseException( "Error parsing the model!", ex );
+ }
+ catch ( IOException ex )
{
- throw new ParseException( "Error parsing the model.", ex );
+ throw new ParseException( "Error reading the input model!", ex );
+ }
+ catch ( MacroExecutionException ex )
+ {
+ throw new ParseException( "Macro execution failed!", ex );
}
}
+ /**
+ * Parse the model from the XmlPullParser into the given sink.
+ *
+ * @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
+ */
public void parseXdoc( XmlPullParser parser, Sink sink )
- throws Exception
+ throws XmlPullParserException, MacroExecutionException
{
int eventType = parser.getEventType();
@@ -83,400 +110,450 @@
{
if ( eventType == XmlPullParser.START_TAG )
{
- if ( parser.getName().equals( "document" ) )
- {
- //Do nothing
- }
- else if ( parser.getName().equals( "title" ) )
- {
- sink.title();
- }
- else if ( parser.getName().equals( "author" ) )
- {
- sink.author();
- }
- else if ( parser.getName().equals( "body" ) )
- {
- sink.body();
- }
- else if ( parser.getName().equals( "section" ) )
- {
- sink.section1();
-
- sink.sectionTitle1();
-
- sink.text( parser.getAttributeValue( null, "name" ) );
-
- sink.sectionTitle1_();
- }
- else if ( parser.getName().equals( "subsection" ) )
- {
- sink.section2();
-
- sink.sectionTitle2();
-
- sink.text( parser.getAttributeValue( null, "name" ) );
-
- sink.sectionTitle2_();
- }
- // TODO section3 section4 section5
- else if ( parser.getName().equals( "h4" ) )
- {
- sink.sectionTitle3();
- }
- else if ( parser.getName().equals( "h5" ) )
- {
- sink.sectionTitle4();
- }
- else if ( parser.getName().equals( "h6" ) )
- {
- sink.sectionTitle5();
- }
- else if ( parser.getName().equals( "p" ) )
- {
- sink.paragraph();
- }
- else if ( parser.getName().equals( "source" ) )
- {
- sink.verbatim( true );
- }
- else if ( parser.getName().equals( "ul" ) )
- {
- sink.list();
- }
- else if ( parser.getName().equals( "ol" ) )
- {
- sink.numberedList( Sink.NUMBERING_DECIMAL );
- orderedListDepth++;
- }
- else if ( parser.getName().equals( "li" ) )
- {
- if ( orderedListDepth == 0 )
- {
- sink.listItem();
- }
- else
- {
- sink.numberedListItem();
- }
- }
- else if ( parser.getName().equals( "dl" ) )
- {
- sink.definitionList();
- }
- else if ( parser.getName().equals( "dt" ) )
- {
- sink.definitionListItem();
- sink.definedTerm();
- }
- else if ( parser.getName().equals( "dd" ) )
- {
- sink.definition();
- }
- else if ( parser.getName().equals( "properties" ) )
- {
- sink.head();
- }
- else if ( parser.getName().equals( "b" ) )
- {
- sink.bold();
- }
- else if ( parser.getName().equals( "i" ) )
- {
- sink.italic();
- }
- else if ( parser.getName().equals( "tt" ) )
- {
- sink.monospaced();
- }
- else if ( parser.getName().equals( "a" ) )
- {
- String href = parser.getAttributeValue( null, "href" );
- if ( href != null )
- {
- sink.link( href );
- isLink = true;
- }
- else
- {
- String name = parser.getAttributeValue( null, "name" );
- if ( name != null )
- {
- sink.anchor( name );
- isAnchor = true;
- }
- else
- {
- handleRawText( sink, parser );
- }
- }
- }
- else if ( parser.getName().equals( "macro" ) )
- {
- if ( !secondParsing )
- {
- String macroName = parser.getAttributeValue( null,
"name" );
+ handleStartTag( parser, sink );
+ }
+ else if ( eventType == XmlPullParser.END_TAG )
+ {
+ handleEndTag( parser, sink );
+ }
+ else if ( eventType == XmlPullParser.TEXT )
+ {
+ handleText( parser, sink );
+ }
- int count = parser.getAttributeCount();
+ try
+ {
+ eventType = parser.next();
+ }
+ catch ( IOException io )
+ {
+ throw new XmlPullParserException(
+ "Error parsing the model!", parser, io );
+ }
+ }
+ }
- Map parameters = new HashMap();
+ /**
+ * Goes through the possible start tags.
+ *
+ * @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
+ {
+ isEmptyElement = parser.isEmptyElementTag();
- for ( int i = 1; i < count; i++ )
- {
- parameters.put( parser.getAttributeName( i ),
parser.getAttributeValue( i ) );
- }
+ if ( parser.getName().equals( "document" ) )
+ {
+ //Do nothing
+ return;
+ }
+ else if ( parser.getName().equals( "title" ) )
+ {
+ sink.title();
+ }
+ else if ( parser.getName().equals( "author" ) )
+ {
+ sink.author();
+ }
+ else if ( parser.getName().equals( "body" ) )
+ {
+ sink.body();
+ }
+ else if ( parser.getName().equals( "section" ) )
+ {
+ sink.section1();
- parameters.put( "sourceContent", sourceContent );
+ sink.sectionTitle1();
- XdocParser xdocParser = new XdocParser();
- xdocParser.setSecondParsing( true );
- parameters.put( "parser", xdocParser );
+ sink.text( parser.getAttributeValue( null, "name" ) );
- MacroRequest request = new MacroRequest( parameters,
getBasedir() );
+ sink.sectionTitle1_();
+ }
+ else if ( parser.getName().equals( "subsection" ) )
+ {
+ sink.section2();
- executeMacro( macroName, request, sink );
- }
- }
+ sink.sectionTitle2();
- //
----------------------------------------------------------------------
- // Tables
- //
----------------------------------------------------------------------
+ sink.text( parser.getAttributeValue( null, "name" ) );
- else if ( parser.getName().equals( "table" ) )
- {
- sink.table();
- }
- else if ( parser.getName().equals( "tr" ) )
- {
- sink.tableRow();
- }
- else if ( parser.getName().equals( "th" ) )
+ sink.sectionTitle2_();
+ }
+ // TODO section3 section4 section5
+ else if ( parser.getName().equals( "h4" ) )
+ {
+ sink.sectionTitle3();
+ }
+ else if ( parser.getName().equals( "h5" ) )
+ {
+ sink.sectionTitle4();
+ }
+ else if ( parser.getName().equals( "h6" ) )
+ {
+ sink.sectionTitle5();
+ }
+ else if ( parser.getName().equals( "p" ) )
+ {
+ sink.paragraph();
+ }
+ else if ( parser.getName().equals( "source" ) )
+ {
+ sink.verbatim( true );
+ }
+ else if ( parser.getName().equals( "ul" ) )
+ {
+ sink.list();
+ }
+ else if ( parser.getName().equals( "ol" ) )
+ {
+ sink.numberedList( Sink.NUMBERING_DECIMAL );
+ orderedListDepth++;
+ }
+ else if ( parser.getName().equals( "li" ) )
+ {
+ if ( orderedListDepth == 0 )
+ {
+ sink.listItem();
+ }
+ else
+ {
+ sink.numberedListItem();
+ }
+ }
+ else if ( parser.getName().equals( "dl" ) )
+ {
+ sink.definitionList();
+ }
+ else if ( parser.getName().equals( "dt" ) )
+ {
+ sink.definitionListItem();
+ sink.definedTerm();
+ }
+ else if ( parser.getName().equals( "dd" ) )
+ {
+ sink.definition();
+ }
+ else if ( parser.getName().equals( "properties" ) )
+ {
+ sink.head();
+ }
+ else if ( parser.getName().equals( "b" ) )
+ {
+ sink.bold();
+ }
+ else if ( parser.getName().equals( "i" ) )
+ {
+ sink.italic();
+ }
+ else if ( parser.getName().equals( "tt" ) )
+ {
+ sink.monospaced();
+ }
+ else if ( parser.getName().equals( "a" ) )
+ {
+ String href = parser.getAttributeValue( null, "href" );
+ if ( href != null )
+ {
+ sink.link( href );
+ isLink = true;
+ }
+ else
+ {
+ String name = parser.getAttributeValue( null, "name" );
+ if ( name != null )
{
- String colspan = parser.getAttributeValue( null, "colspan"
);
- if ( colspan == null)
- {
- sink.tableHeaderCell();
- }
- else
- {
- sink.tableHeaderCell( colspan );
- }
+ sink.anchor( name );
+ isAnchor = true;
}
- else if ( parser.getName().equals( "td" ) )
+ else
{
- String colspan = parser.getAttributeValue( null, "colspan"
);
- if ( colspan == null)
- {
- sink.tableCell();
- }
- else
- {
- sink.tableCell( colspan );
- }
+ handleRawText( sink, parser );
}
+ }
+ }
+ else if ( parser.getName().equals( "macro" ) )
+ {
+ if ( !secondParsing )
+ {
+ String macroName = parser.getAttributeValue( null, "name" );
- //
----------------------------------------------------------------------
- // Empty elements: <br/>, <hr/> and <img />
- //
----------------------------------------------------------------------
+ int count = parser.getAttributeCount();
- else if ( parser.getName().equals( "br" ) )
- {
- sink.lineBreak();
- }
- else if ( parser.getName().equals( "hr" ) )
+ Map parameters = new HashMap();
+
+ for ( int i = 1; i < count; i++ )
{
- sink.horizontalRule();
+ parameters.put( parser.getAttributeName( i ),
parser.getAttributeValue( i ) );
}
- else if ( parser.getName().equals( "img" ) )
- {
- String src = parser.getAttributeValue( null, "src" );
- String alt = parser.getAttributeValue( null, "alt" );
- sink.figure();
- sink.figureGraphics( src );
+ parameters.put( "sourceContent", sourceContent );
+
+ XdocParser xdocParser = new XdocParser();
+ xdocParser.setSecondParsing( true );
+ parameters.put( "parser", xdocParser );
- if ( alt != null )
- {
- sink.figureCaption();
- sink.text( alt );
- sink.figureCaption_();
- }
+ MacroRequest request = new MacroRequest( parameters,
getBasedir() );
- sink.figure_();
+ try
+ {
+ executeMacro( macroName, request, sink );
}
- else
+ catch ( MacroNotFoundException me )
{
- handleRawText( sink, parser );
+ throw new MacroExecutionException( "Macro not found: " +
macroName, me );
}
+ }
+ }
- isEmptyElement = parser.isEmptyElementTag();
+ //
----------------------------------------------------------------------
+ // Tables
+ //
----------------------------------------------------------------------
+ else if ( parser.getName().equals( "table" ) )
+ {
+ sink.table();
+ }
+ else if ( parser.getName().equals( "tr" ) )
+ {
+ sink.tableRow();
+ }
+ else if ( parser.getName().equals( "th" ) )
+ {
+ String colspan = parser.getAttributeValue( null, "colspan" );
+ if ( colspan == null )
+ {
+ sink.tableHeaderCell();
}
- else if ( eventType == XmlPullParser.END_TAG )
+ else
{
- if ( parser.getName().equals( "document" ) )
- {
- //Do nothing
- }
- else if ( parser.getName().equals( "title" ) )
- {
- sink.title_();
- }
- else if ( parser.getName().equals( "author" ) )
- {
- sink.author_();
- }
- else if ( parser.getName().equals( "body" ) )
- {
- sink.body_();
- }
- else if ( parser.getName().equals( "p" ) )
- {
- sink.paragraph_();
- }
- else if ( parser.getName().equals( "source" ) )
- {
- sink.verbatim_();
- }
- else if ( parser.getName().equals( "ul" ) )
- {
- sink.list_();
- }
- else if ( parser.getName().equals( "ol" ) )
- {
- sink.numberedList_();
- orderedListDepth--;
- }
- else if ( parser.getName().equals( "li" ) )
- {
- if ( orderedListDepth == 0 )
- {
- sink.listItem_();
- }
- else
- {
- sink.numberedListItem_();
- }
- }
- else if ( parser.getName().equals( "dl" ) )
- {
- sink.definitionList_();
- }
- else if ( parser.getName().equals( "dt" ) )
- {
- sink.definedTerm_();
- }
- else if ( parser.getName().equals( "dd" ) )
- {
- sink.definition_();
- sink.definitionListItem_();
- }
- else if ( parser.getName().equals( "properties" ) )
- {
- sink.head_();
- }
- else if ( parser.getName().equals( "b" ) )
- {
- sink.bold_();
- }
- else if ( parser.getName().equals( "i" ) )
- {
- sink.italic_();
- }
- else if ( parser.getName().equals( "tt" ) )
- {
- sink.monospaced_();
- }
- else if ( parser.getName().equals( "a" ) )
- {
- if ( isLink )
- {
- sink.link_();
- isLink = false;
- }
- else if ( isAnchor )
- {
- sink.anchor_();
- isAnchor = false;
- }
- }
- else if ( parser.getName().equals( "macro" ) )
- {
- //Do nothing
- }
+ sink.tableHeaderCell( colspan );
+ }
+ }
+ else if ( parser.getName().equals( "td" ) )
+ {
+ String colspan = parser.getAttributeValue( null, "colspan" );
+ if ( colspan == null )
+ {
+ sink.tableCell();
+ }
+ else
+ {
+ sink.tableCell( colspan );
+ }
+ }
- //
----------------------------------------------------------------------
- // Tables
- //
----------------------------------------------------------------------
+ //
----------------------------------------------------------------------
+ // Empty elements: <br/>, <hr/> and <img />
+ //
----------------------------------------------------------------------
- else if ( parser.getName().equals( "table" ) )
- {
- sink.table_();
- }
- else if ( parser.getName().equals( "tr" ) )
- {
- sink.tableRow_();
- }
- else if ( parser.getName().equals( "th" ) )
- {
- sink.tableHeaderCell_();
- }
- else if ( parser.getName().equals( "td" ) )
- {
- sink.tableCell_();
- }
+ else if ( parser.getName().equals( "br" ) )
+ {
+ sink.lineBreak();
+ }
+ else if ( parser.getName().equals( "hr" ) )
+ {
+ sink.horizontalRule();
+ }
+ else if ( parser.getName().equals( "img" ) )
+ {
+ String src = parser.getAttributeValue( null, "src" );
+ String alt = parser.getAttributeValue( null, "alt" );
- //
----------------------------------------------------------------------
- // Sections
- //
----------------------------------------------------------------------
+ sink.figure();
+ sink.figureGraphics( src );
- else if ( parser.getName().equals( "section" ) )
- {
- sink.section1_();
- }
- else if ( parser.getName().equals( "subsection" ) )
- {
- sink.section2_();
- }
- else if ( parser.getName().equals( "h4" ) )
- {
- sink.sectionTitle3_();
- }
- else if ( parser.getName().equals( "h5" ) )
- {
- sink.sectionTitle4_();
- }
- else if ( parser.getName().equals( "h6" ) )
- {
- sink.sectionTitle5_();
- }
- else if ( !isEmptyElement )
- {
- sink.rawText( "</" );
+ if ( alt != null )
+ {
+ sink.figureCaption();
+ sink.text( alt );
+ sink.figureCaption_();
+ }
- sink.rawText( parser.getName() );
+ sink.figure_();
+ }
+ else
+ {
+ handleRawText( sink, parser );
+ }
- sink.rawText( ">" );
- }
- else
- {
- isEmptyElement = false;
- }
+ }
+ /**
+ * Goes through the possible end tags.
+ *
+ * @param parser A parser.
+ * @param sink the sink to receive the events.
+ */
+ private void handleEndTag( XmlPullParser parser, Sink sink )
+ {
+ if ( parser.getName().equals( "document" ) )
+ {
+ //Do nothing
+ return;
+ }
+ else if ( parser.getName().equals( "title" ) )
+ {
+ sink.title_();
+ }
+ else if ( parser.getName().equals( "author" ) )
+ {
+ sink.author_();
+ }
+ else if ( parser.getName().equals( "body" ) )
+ {
+ sink.body_();
+ }
+ else if ( parser.getName().equals( "p" ) )
+ {
+ sink.paragraph_();
+ }
+ else if ( parser.getName().equals( "source" ) )
+ {
+ sink.verbatim_();
+ }
+ else if ( parser.getName().equals( "ul" ) )
+ {
+ sink.list_();
+ }
+ else if ( parser.getName().equals( "ol" ) )
+ {
+ sink.numberedList_();
+ orderedListDepth--;
+ }
+ else if ( parser.getName().equals( "li" ) )
+ {
+ if ( orderedListDepth == 0 )
+ {
+ sink.listItem_();
}
- else if ( eventType == XmlPullParser.TEXT )
+ else
{
- String text = parser.getText();
- if ( !"".equals( text.trim() ) )
- {
- sink.text( text );
- }
+ sink.numberedListItem_();
}
+ }
+ else if ( parser.getName().equals( "dl" ) )
+ {
+ sink.definitionList_();
+ }
+ else if ( parser.getName().equals( "dt" ) )
+ {
+ sink.definedTerm_();
+ }
+ else if ( parser.getName().equals( "dd" ) )
+ {
+ sink.definition_();
+ sink.definitionListItem_();
+ }
+ else if ( parser.getName().equals( "properties" ) )
+ {
+ sink.head_();
+ }
+ else if ( parser.getName().equals( "b" ) )
+ {
+ sink.bold_();
+ }
+ else if ( parser.getName().equals( "i" ) )
+ {
+ sink.italic_();
+ }
+ else if ( parser.getName().equals( "tt" ) )
+ {
+ sink.monospaced_();
+ }
+ else if ( parser.getName().equals( "a" ) )
+ {
+ if ( isLink )
+ {
+ sink.link_();
+ isLink = false;
+ }
+ else if ( isAnchor )
+ {
+ sink.anchor_();
+ isAnchor = false;
+ }
+ }
+ else if ( parser.getName().equals( "macro" ) )
+ {
+ //Do nothing
+ return;
+ }
+ else if ( parser.getName().equals( "table" ) )
+ {
+ sink.table_();
+ }
+ else if ( parser.getName().equals( "tr" ) )
+ {
+ sink.tableRow_();
+ }
+ else if ( parser.getName().equals( "th" ) )
+ {
+ sink.tableHeaderCell_();
+ }
+ else if ( parser.getName().equals( "td" ) )
+ {
+ sink.tableCell_();
+ }
+ else if ( parser.getName().equals( "section" ) )
+ {
+ sink.section1_();
+ }
+ else if ( parser.getName().equals( "subsection" ) )
+ {
+ sink.section2_();
+ }
+ else if ( parser.getName().equals( "h4" ) )
+ {
+ sink.sectionTitle3_();
+ }
+ else if ( parser.getName().equals( "h5" ) )
+ {
+ sink.sectionTitle4_();
+ }
+ else if ( parser.getName().equals( "h6" ) )
+ {
+ sink.sectionTitle5_();
+ }
+ else if ( !isEmptyElement )
+ {
+ sink.rawText( "</" );
- eventType = parser.next();
+ sink.rawText( parser.getName() );
+
+ sink.rawText( ">" );
+ }
+ else
+ {
+ isEmptyElement = false;
+ }
+ }
+
+ /**
+ * Handles text events.
+ *
+ * @param parser A parser.
+ * @param sink the sink to receive the events.
+ */
+ private void handleText( XmlPullParser parser, Sink sink )
+ {
+ String text = parser.getText();
+
+ if ( !"".equals( text.trim() ) )
+ {
+ sink.text( text );
}
}
+ /**
+ * Handles raw text events.
+ *
+ * @param sink the sink to receive the events.
+ * @param parser A parser.
+ */
private void handleRawText( Sink sink, XmlPullParser parser )
{
sink.rawText( "<" );