Juan F. Codagnone a ?crit :
> Hi,
> i implemented an XmlWriterXdocSink (an xdoc sink implementation that use
> org.codehaus.plexus.util.xml.XMLWriter to output things). The cool thing
> about using a XMLWriter is that you can use a PrettyPrintXMLWriter and make
> the output easily readable. The implementation is based on XdocSink.java. I
> attach the source file and the differences.
> You can use it as: new XmlWriterXdocSink(new PrettyPrintXMLWriter(writer,
> "ISO-8859-1", null);
>
> Juan.
>
>
>
> ------------------------------------------------------------------------
>
> package org.codehaus.doxia.module.xdoc;
>
> import org.codehaus.doxia.module.HtmlTools;
> import org.codehaus.doxia.module.apt.AptParser;
> import org.codehaus.doxia.sink.SinkAdapter;
> import org.codehaus.doxia.sink.StructureSink;
> import org.codehaus.plexus.util.xml.XMLWriter;
>
> /**
> * A doxia Sink which produces an xdoc document.
> *
> * @author juan <a href="mailto:
[hidden email]">James Taylor</a>
> * @author Juan F. Codagnone (replaced println with XmlWriterXdocSink)
> * @version $Id: XdocSink.java 94 2005-07-08 12:43:37Z evenisse $
> * @componentx
> */
> public class XmlWriterXdocSink
> extends SinkAdapter
> {
> private final XMLWriter writer;
>
> private StringBuffer buffer = new StringBuffer();
>
> private boolean headFlag;
> private boolean itemFlag;
> private boolean verbatimFlag;
> private int[] cellJustif;
> private int cellCount;
>
> private boolean sectionTitleFlag;
>
> public XmlWriterXdocSink( XMLWriter writer)
> {
> if(writer == null) {
> throw new IllegalArgumentException("argument can't be null");
> }
> this.writer = writer;
>
> }
>
> protected void resetState()
> {
> headFlag = false;
> buffer = new StringBuffer();
> itemFlag = false;
> verbatimFlag = false;
> cellJustif = null;
> cellCount = 0;
> sectionTitleFlag = false;
> }
>
> public void head()
> {
> resetState();
>
> headFlag = true;
>
> writer.startElement("document");
> writer.startElement("properties");
> }
>
> public void head_()
> {
> headFlag = false;
>
> writer.endElement(); // properties
> }
>
> public void title_()
> {
> if ( buffer.length() > 0 )
> {
> writer.startElement("title");
> content( buffer.toString() );
> writer.endElement(); // title
> buffer = new StringBuffer();
> }
> }
>
> public void author_()
> {
> if ( buffer.length() > 0 )
> {
> writer.startElement("author");
> content( buffer.toString() );
> writer.endElement(); // author
> buffer = new StringBuffer();
> }
> }
>
> public void date_()
> {
> if ( buffer.length() > 0 )
> {
> writer.startElement("date");
> content( buffer.toString() );
> writer.endElement();
> buffer = new StringBuffer();
> }
> }
>
> public void body()
> {
> writer.startElement("body");
> }
>
> public void body_()
> {
> writer.endElement(); // body
>
> writer.endElement(); // document
>
> resetState();
> }
>
> public void section1()
> {
> writer.startElement("section");
> }
>
> public void section2()
> {
> writer.startElement("subsection");
> }
>
> public void section3()
> {
> writer.startElement("subsection");
> }
>
> public void section4()
> {
> writer.startElement("subsection");
> }
>
> public void section5()
> {
> writer.startElement("subsection");
> }
>
> public void sectionTitle()
> {
> sectionTitleFlag = true;
> buffer = new StringBuffer();
> }
>
> public void sectionTitle_()
> {
> sectionTitleFlag = false;
> writer.addAttribute("name", buffer.toString());
> }
>
> public void section1_()
> {
> writer.endElement();
> }
>
> public void section2_()
> {
> writer.endElement();
> }
>
> public void section3_()
> {
> writer.endElement();
> }
>
> public void section4_()
> {
> writer.endElement();
> }
>
> public void section5_()
> {
> writer.endElement();
> }
>
> public void list()
> {
> writer.startElement("ul");
> }
>
> public void list_()
> {
> writer.endElement();
> }
>
> public void listItem()
> {
> writer.startElement("li");
> itemFlag = true;
> // What follows is at least a paragraph.
> }
>
> public void listItem_()
> {
> writer.endElement();
> }
>
> public void numberedList( int numbering )
> {
> String style;
> switch ( numbering )
> {
> case NUMBERING_UPPER_ALPHA:
> style = "upper-alpha";
> break;
> case NUMBERING_LOWER_ALPHA:
> style = "lower-alpha";
> break;
> case NUMBERING_UPPER_ROMAN:
> style = "upper-roman";
> break;
> case NUMBERING_LOWER_ROMAN:
> style = "lower-roman";
> break;
> case NUMBERING_DECIMAL:
> default:
> style = "decimal";
> }
> writer.startElement("ol");
> writer.addAttribute("style", "list-style-type: " + style);
> }
>
> public void numberedList_()
> {
> writer.endElement();
> }
>
> public void numberedListItem()
> {
> writer.startElement("li");
> itemFlag = true;
> // What follows is at least a paragraph.
> }
>
> public void numberedListItem_()
> {
> writer.endElement();
> }
>
> public void definitionList()
> {
> writer.startElement("dl");
> writer.addAttribute("compact", "compact");
> }
>
> public void definitionList_()
> {
> writer.endElement();
> }
>
> public void definedTerm()
> {
> writer.startElement("dt");
> writer.startElement("b");
> }
>
> public void definedTerm_()
> {
> writer.endElement();
> writer.endElement();
> }
>
> public void definition()
> {
> writer.startElement("dd");
> itemFlag = true;
> // What follows is at least a paragraph.
> }
>
> public void definition_()
> {
> writer.endElement();
> }
>
> public void paragraph()
> {
> if ( !itemFlag )
> writer.startElement("p");
> }
>
> public void paragraph_()
> {
> if ( itemFlag )
> itemFlag = false;
> else
> writer.endElement();
> }
>
> public void verbatim( boolean boxed )
> {
> verbatimFlag = true;
> if ( boxed )
> {
> writer.startElement("source" );
> }
> else
> {
> writer.startElement("pre");
> }
> }
>
> public void verbatim_()
> {
> writer.endElement();
>
> verbatimFlag = false;
> }
>
> public void horizontalRule()
> {
> writer.startElement("hr");
> writer.endElement();
> }
>
> public void table()
> {
> writer.startElement("table");
> writer.addAttribute("align", "center");
> }
>
> public void table_()
> {
> writer.endElement();
> }
>
> public void tableRows( int[] justification, boolean grid )
>
> {
> writer.startElement("table");
> writer.addAttribute("align", "center");
> writer.addAttribute("border", String.valueOf(grid ? 1 : 0) );
> this.cellJustif = justification;
> }
>
> public void tableRows_()
> {
> writer.endElement();
> }
>
> public void tableRow()
> {
> writer.startElement("tr");
> writer.addAttribute("valign", "top");
> cellCount = 0;
> }
>
> public void tableRow_()
> {
> writer.endElement();
> cellCount = 0;
> }
>
> public void tableCell()
> {
> tableCell( false );
> }
>
> public void tableHeaderCell()
> {
> tableCell( true );
> }
>
> public void tableCell( boolean headerRow )
> {
> String justif = null;
>
> if ( cellJustif != null )
> {
> switch ( cellJustif[cellCount] )
> {
> case AptParser.JUSTIFY_LEFT:
> justif = "left";
> break;
> case AptParser.JUSTIFY_RIGHT:
> justif = "right";
> break;
> case AptParser.JUSTIFY_CENTER:
> default:
> justif = "center";
> break;
> }
> }
>
>
> writer.startElement("t" + ( headerRow ? 'h' : 'd' ));
> if ( justif != null )
> writer.addAttribute("align", justif);
> }
>
> public void tableCell_()
> {
> tableCell_( false );
> }
>
> public void tableHeaderCell_()
> {
> tableCell_( true );
> }
>
> public void tableCell_( boolean headerRow )
> {
> writer.endElement();
> ++cellCount;
> }
>
> public void tableCaption()
> {
> writer.startElement("p");
> writer.startElement("i");
> }
>
> public void tableCaption_()
> {
> writer.endElement();
> writer.endElement();
> }
>
> public void anchor( String name )
> {
> if ( !headFlag )
> {
> String id = StructureSink.linkToKey( name );
> writer.startElement("a");
> writer.addAttribute("id", id);
> writer.addAttribute("name", id);
> }
> }
>
> public void anchor_()
> {
> if ( !headFlag )
> writer.endElement();
> }
>
> public void link( String name )
> {
> if ( !headFlag )
> {
> writer.startElement("a");
> writer.addAttribute("href", name);
> }
> }
>
> public void link_()
> {
> if ( !headFlag )
> writer.endElement();
> }
>
> public void italic()
> {
> if ( !headFlag )
> writer.startElement("i");
> }
>
> public void italic_()
> {
> if ( !headFlag )
> writer.endElement();
> }
>
> public void bold()
> {
> if ( !headFlag )
> writer.startElement("b");
> }
>
> public void bold_()
> {
> if ( !headFlag )
> writer.endElement();
> }
>
> public void monospaced()
> {
> if ( !headFlag )
> writer.startElement("tt");
> }
>
> public void monospaced_()
> {
> if ( !headFlag )
> writer.endElement();
> }
>
> public void lineBreak()
> {
> if ( headFlag )
> {
> buffer.append( '\n' );
> }
> else
> {
> writer.startElement("br");
> writer.endElement();
> }
> }
>
> public void nonBreakingSpace()
> {
> if ( headFlag )
> {
> buffer.append( ' ' );
> }
> else
> {
> writer.writeText( " " );
> }
> }
>
> public void text( String text )
> {
> if ( headFlag )
> {
> buffer.append( text );
> }
> else if ( sectionTitleFlag )
> {
> buffer.append( text );
> }
> else
> {
> if ( verbatimFlag )
> {
> verbatimContent( text );
> }
> else
> {
> content( text );
> }
> }
> }
>
> // ----------------------------------------------------------------------
> //
> // ----------------------------------------------------------------------
>
>
> protected void content( String text )
> {
> writer.writeText(escapeHTML( text ));
> }
>
> protected void verbatimContent( String text )
> {
> writer.writeText( text.replace(" ", " "));
> }
>
> public static String escapeHTML( String text )
> {
> return HtmlTools.escapeHTML( text );
> }
>
> public static String encodeURL( String text )
> {
> return HtmlTools.encodeURL( text );
> }
>
> public void flush()
> {
> }
>
> public void close()
> {
> }
> }
>
>
> ------------------------------------------------------------------------
>
> --- src/main/java/org/codehaus/doxia/module/xdoc/XdocSink.java 2005-11-05 20:06:21.000000000 -0300
> +++ src/main/java/org/codehaus/doxia/module/xdoc/XmlWriterXdocSink.java 2005-11-07 00:33:53.000000000 -0300
> @@ -4,35 +4,38 @@
> import org.codehaus.doxia.module.apt.AptParser;
> import org.codehaus.doxia.sink.SinkAdapter;
> import org.codehaus.doxia.sink.StructureSink;
> -import org.codehaus.doxia.util.LineBreaker;
> -
> -import java.io.Writer;
> +import org.codehaus.plexus.util.xml.XMLWriter;
>
> /**
> * A doxia Sink which produces an xdoc document.
> *
> - * @author <a href="mailto:
[hidden email]">James Taylor</a>
> + * @author juan <a href="mailto:
[hidden email]">James Taylor</a>
> + * @author Juan F. Codagnone (replaced println with XmlWriterXdocSink)
> * @version $Id: XdocSink.java 94 2005-07-08 12:43:37Z evenisse $
> * @componentx
> */
> -public class XdocSink
> +public class XmlWriterXdocSink
> extends SinkAdapter
> {
> - private LineBreaker out;
> + private final XMLWriter writer;
>
> private StringBuffer buffer = new StringBuffer();
>
> private boolean headFlag;
> private boolean itemFlag;
> - private boolean boxedFlag;
> private boolean verbatimFlag;
> private int[] cellJustif;
> private int cellCount;
> - private String section;
>
> - public XdocSink( Writer out )
> + private boolean sectionTitleFlag;
> +
> + public XmlWriterXdocSink( XMLWriter writer)
> {
> - this.out = new LineBreaker( out );
> + if(writer == null) {
> + throw new IllegalArgumentException("argument can't be null");
> + }
> + this.writer = writer;
> +
> }
>
> protected void resetState()
> @@ -40,10 +43,10 @@
> headFlag = false;
> buffer = new StringBuffer();
> itemFlag = false;
> - boxedFlag = false;
> verbatimFlag = false;
> cellJustif = null;
> cellCount = 0;
> + sectionTitleFlag = false;
> }
>
> public void head()
> @@ -52,27 +55,24 @@
>
> headFlag = true;
>
> - markup( "<?xml version=\"1.0\" ?>\n" );
> -
> - markup( "<document>\n" );
> -
> - markup( "<properties>\n" );
> + writer.startElement("document");
> + writer.startElement("properties");
> }
>
> public void head_()
> {
> headFlag = false;
> -
> - markup( "</properties>\n" );
> +
> + writer.endElement(); // properties
> }
>
> public void title_()
> {
> if ( buffer.length() > 0 )
> {
> - markup( "<title>" );
> + writer.startElement("title");
> content( buffer.toString() );
> - markup( "</title>\n" );
> + writer.endElement(); // title
> buffer = new StringBuffer();
> }
> }
> @@ -81,9 +81,9 @@
> {
> if ( buffer.length() > 0 )
> {
> - markup( "<author>" );
> + writer.startElement("author");
> content( buffer.toString() );
> - markup( "</author>\n" );
> + writer.endElement(); // author
> buffer = new StringBuffer();
> }
> }
> @@ -92,109 +92,109 @@
> {
> if ( buffer.length() > 0 )
> {
> - markup( "<date>" );
> + writer.startElement("date");
> content( buffer.toString() );
> - markup( "</date>" );
> + writer.endElement();
> buffer = new StringBuffer();
> }
> }
>
> public void body()
> {
> - markup( "<body>\n" );
> + writer.startElement("body");
> }
>
> public void body_()
> {
> - markup( "</body>\n" );
> + writer.endElement(); // body
>
> - markup( "</document>\n" );
> -
> - out.flush();
> + writer.endElement(); // document
>
> resetState();
> }
>
> public void section1()
> {
> - section = "section";
> + writer.startElement("section");
> }
>
> public void section2()
> {
> - section = "subsection";
> + writer.startElement("subsection");
> }
>
> public void section3()
> {
> - section = "subsection";
> + writer.startElement("subsection");
> }
>
> public void section4()
> {
> - section = "subsection";
> + writer.startElement("subsection");
> }
>
> public void section5()
> {
> - section = "subsection";
> + writer.startElement("subsection");
> }
>
> public void sectionTitle()
> {
> - markup( "<" + section + " name=\"" );
> + sectionTitleFlag = true;
> + buffer = new StringBuffer();
> }
>
> public void sectionTitle_()
> {
> - markup( "\">" );
> + sectionTitleFlag = false;
> + writer.addAttribute("name", buffer.toString());
> }
>
> public void section1_()
> {
> - markup( "</section>" );
> + writer.endElement();
> }
>
> public void section2_()
> {
> - markup( "</subsection>" );
> + writer.endElement();
> }
>
> public void section3_()
> {
> - markup( "</subsection>" );
> + writer.endElement();
> }
>
> public void section4_()
> {
> - markup( "</subsection>" );
> + writer.endElement();
> }
>
> public void section5_()
> {
> - markup( "</subsection>" );
> + writer.endElement();
> }
>
> public void list()
> {
> - markup( "<ul>\n" );
> + writer.startElement("ul");
> }
>
> public void list_()
> {
> - markup( "</ul>" );
> + writer.endElement();
> }
>
> public void listItem()
> {
> - markup( "<li>" );
> + writer.startElement("li");
> itemFlag = true;
> // What follows is at least a paragraph.
> }
>
> public void listItem_()
> {
> - markup( "</li>\n" );
> + writer.endElement();
> }
>
> public void numberedList( int numbering )
> @@ -218,62 +218,66 @@
> default:
> style = "decimal";
> }
> - markup( "<ol style=\"list-style-type: " + style + "\">\n" );
> + writer.startElement("ol");
> + writer.addAttribute("style", "list-style-type: " + style);
> }
>
> public void numberedList_()
> {
> - markup( "</ol>" );
> + writer.endElement();
> }
>
> public void numberedListItem()
> {
> - markup( "<li>" );
> + writer.startElement("li");
> itemFlag = true;
> // What follows is at least a paragraph.
> }
>
> public void numberedListItem_()
> {
> - markup( "</li>\n" );
> + writer.endElement();
> }
>
> public void definitionList()
> {
> - markup( "<dl compact=\"compact\">\n" );
> + writer.startElement("dl");
> + writer.addAttribute("compact", "compact");
> }
>
> public void definitionList_()
> {
> - markup( "</dl>" );
> + writer.endElement();
> }
>
> public void definedTerm()
> {
> - markup( "<dt><b>" );
> + writer.startElement("dt");
> + writer.startElement("b");
> }
>
> public void definedTerm_()
> {
> - markup( "</b></dt>\n" );
> + writer.endElement();
> + writer.endElement();
> }
>
> public void definition()
> {
> - markup( "<dd>" );
> + writer.startElement("dd");
> itemFlag = true;
> // What follows is at least a paragraph.
> }
>
> public void definition_()
> {
> - markup( "</dd>\n" );
> + writer.endElement();
> }
>
> public void paragraph()
> {
> if ( !itemFlag )
> - markup( "<p>" );
> + writer.startElement("p");
> }
>
> public void paragraph_()
> @@ -281,75 +285,70 @@
> if ( itemFlag )
> itemFlag = false;
> else
> - markup( "</p>" );
> + writer.endElement();
> }
>
> public void verbatim( boolean boxed )
> {
> verbatimFlag = true;
> - boxedFlag = boxed;
> if ( boxed )
> {
> - markup( "<source>" );
> + writer.startElement("source" );
> }
> else
> {
> - markup( "<pre>" );
> + writer.startElement("pre");
> }
> }
>
> public void verbatim_()
> {
> - if ( boxedFlag )
> - {
> - markup( "</source>" );
> - }
> - else
> - {
> - markup( "</pre>" );
> - }
> + writer.endElement();
>
> verbatimFlag = false;
> -
> - boxedFlag = false;
> }
>
> public void horizontalRule()
> {
> - markup( "<hr />" );
> + writer.startElement("hr");
> + writer.endElement();
> }
>
> public void table()
> {
> - markup( "<table align=\"center\">\n" );
> + writer.startElement("table");
> + writer.addAttribute("align", "center");
> }
>
> public void table_()
> {
> - markup( "</table>" );
> + writer.endElement();
> }
>
> public void tableRows( int[] justification, boolean grid )
>
> {
> - markup( "<table align=\"center\" border=\"" + ( grid ? 1 : 0 ) + "\">\n" );
> + writer.startElement("table");
> + writer.addAttribute("align", "center");
> + writer.addAttribute("border", String.valueOf(grid ? 1 : 0) );
> this.cellJustif = justification;
> }
>
> public void tableRows_()
> {
> - markup( "</table>" );
> + writer.endElement();
> }
>
> public void tableRow()
> {
> - markup( "<tr valign=\"top\">\n" );
> + writer.startElement("tr");
> + writer.addAttribute("valign", "top");
> cellCount = 0;
> }
>
> public void tableRow_()
> {
> - markup( "</tr>\n" );
> + writer.endElement();
> cellCount = 0;
> }
>
> @@ -384,14 +383,10 @@
> }
> }
>
> +
> + writer.startElement("t" + ( headerRow ? 'h' : 'd' ));
> if ( justif != null )
> - {
> - markup( "<t" + ( headerRow ? 'h' : 'd' ) + " align=\"" + justif + "\">" );
> - }
> - else
> - {
> - markup( "<t" + ( headerRow ? 'h' : 'd' ) + ">" );
> - }
> + writer.addAttribute("align", justif);
> }
>
> public void tableCell_()
> @@ -406,18 +401,20 @@
>
> public void tableCell_( boolean headerRow )
> {
> - markup( "</t" + ( headerRow ? 'h' : 'd' ) + ">\n" );
> + writer.endElement();
> ++cellCount;
> }
>
> public void tableCaption()
> {
> - markup( "<p><i>" );
> + writer.startElement("p");
> + writer.startElement("i");
> }
>
> public void tableCaption_()
> {
> - markup( "</i></p>" );
> + writer.endElement();
> + writer.endElement();
> }
>
> public void anchor( String name )
> @@ -425,64 +422,67 @@
> if ( !headFlag )
> {
> String id = StructureSink.linkToKey( name );
> - markup( "<a id=\"" + id + "\" name=\"" + id + "\">" );
> + writer.startElement("a");
> + writer.addAttribute("id", id);
> + writer.addAttribute("name", id);
> }
> }
>
> public void anchor_()
> {
> if ( !headFlag )
> - markup( "</a>" );
> + writer.endElement();
> }
>
> public void link( String name )
> {
> if ( !headFlag )
> {
> - markup( "<a href=\"" + name + "\">" );
> + writer.startElement("a");
> + writer.addAttribute("href", name);
> }
> }
>
> public void link_()
> {
> if ( !headFlag )
> - markup( "</a>" );
> + writer.endElement();
> }
>
> public void italic()
> {
> if ( !headFlag )
> - markup( "<i>" );
> + writer.startElement("i");
> }
>
> public void italic_()
> {
> if ( !headFlag )
> - markup( "</i>" );
> + writer.endElement();
> }
>
> public void bold()
> {
> if ( !headFlag )
> - markup( "<b>" );
> + writer.startElement("b");
> }
>
> public void bold_()
> {
> if ( !headFlag )
> - markup( "</b>" );
> + writer.endElement();
> }
>
> public void monospaced()
> {
> if ( !headFlag )
> - markup( "<tt>" );
> + writer.startElement("tt");
> }
>
> public void monospaced_()
> {
> if ( !headFlag )
> - markup( "</tt>" );
> + writer.endElement();
> }
>
> public void lineBreak()
> @@ -493,7 +493,8 @@
> }
> else
> {
> - markup( "<br />" );
> + writer.startElement("br");
> + writer.endElement();
> }
> }
>
> @@ -505,7 +506,7 @@
> }
> else
> {
> - markup( " " );
> + writer.writeText( " " );
> }
> }
>
> @@ -515,6 +516,10 @@
> {
> buffer.append( text );
> }
> + else if ( sectionTitleFlag )
> + {
> + buffer.append( text );
> + }
> else
> {
> if ( verbatimFlag )
> @@ -532,19 +537,15 @@
> //
> // ----------------------------------------------------------------------
>
> - protected void markup( String text )
> - {
> - out.write( text, true );
> - }
>
> protected void content( String text )
> {
> - out.write( escapeHTML( text ), false );
> + writer.writeText(escapeHTML( text ));
> }
>
> protected void verbatimContent( String text )
> {
> - out.write( escapeHTML( text ), true );
> + writer.writeText( text.replace(" ", " "));
> }
>
> public static String escapeHTML( String text )
> @@ -559,11 +560,9 @@
>
> public void flush()
> {
> - out.flush();
> }
>
> public void close()
> {
> - out.close();
> }
> }