Author: vsiveton
Date: Tue Jan 20 04:31:12 2009
New Revision: 736001
URL: http://svn.apache.org/viewvc?rev=736001&view=rev
Log:
o deprecated XmlWriterXdocSink
o added a test case
Added:
maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XmlWriterXdocSinkTest.java
(with props)
Modified:
maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/main/java/org/apache/maven/doxia/module/xdoc/XmlWriterXdocSink.java
Modified:
maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/main/java/org/apache/maven/doxia/module/xdoc/XmlWriterXdocSink.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/XmlWriterXdocSink.java?rev=736001&r1=736000&r2=736001&view=diff
==============================================================================
---
maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/main/java/org/apache/maven/doxia/module/xdoc/XmlWriterXdocSink.java
(original)
+++
maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/main/java/org/apache/maven/doxia/module/xdoc/XmlWriterXdocSink.java
Tue Jan 20 04:31:12 2009
@@ -19,710 +19,73 @@
* under the License.
*/
-import org.apache.maven.doxia.sink.Sink;
-import org.apache.maven.doxia.sink.SinkAdapter;
-import org.apache.maven.doxia.util.HtmlTools;
-import org.codehaus.plexus.util.StringUtils;
+import java.io.IOException;
+import java.io.StringReader;
+import java.io.StringWriter;
+
+import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
import org.codehaus.plexus.util.xml.XMLWriter;
+import org.codehaus.plexus.util.xml.XmlUtil;
/**
- * A doxia Sink which produces an xdoc document.
+ * A Doxia Sink which produces an xdoc document.
*
* @author juan <a href="mailto:[email protected]">James Taylor</a>
* @author Juan F. Codagnone (replaced println with XmlWriterXdocSink)
* @version $Id$
- * @component
+ * @deprecated Since 1.1, this sink is not more supported. If you are looking
for a <code>Sink</code> which produces
+ * pretty formatted XML, you could use the {...@link
XdocSink#XdocSink(java.io.Writer)} as usual and reformat the
+ * <code>Sink</code> content produced with {...@link
XmlUtil#prettyFormat(java.io.Reader, java.io.Writer)}.
*/
public class XmlWriterXdocSink
- extends SinkAdapter
+ extends XdocSink
{
- /** The XMLWriter to write the result. */
- private final XMLWriter writer;
-
- /** Used to collect text events. */
- private StringBuffer buffer = new StringBuffer();
-
- /** An indication on if we're inside a head. */
- private boolean headFlag;
-
- /** An indication on if we're in verbatim mode. */
- private boolean verbatimFlag;
-
- /** Justification of table cells. */
- private int[] cellJustif;
-
- /** Number of cells in a table row. */
- private int cellCount;
-
- /** An indication on if we're inside a item. */
- private int itemFlag;
-
- /** An indication on if we're inside a section title. */
- private boolean sectionTitleFlag;
-
- /**
- * Constructor, initialize the XMLWriter.
- *
- * @param out The XMLWriter to write the result.
- */
- public XmlWriterXdocSink( XMLWriter out )
- {
- if ( out == null )
- {
- throw new IllegalArgumentException( "Argument can't be null!" );
- }
- this.writer = out;
- }
-
- /**
- * Reset all variables.
- */
- protected void resetState()
- {
- headFlag = false;
- buffer = new StringBuffer();
- itemFlag = 0;
- verbatimFlag = false;
- cellJustif = null;
- cellCount = 0;
- sectionTitleFlag = false;
- }
-
- /** {...@inheritdoc} */
- public void head()
- {
- resetState();
-
- headFlag = true;
-
- writer.startElement( "document" );
- writer.startElement( "properties" );
- }
-
- /** {...@inheritdoc} */
- public void head_()
- {
- headFlag = false;
-
- writer.endElement(); // properties
- }
-
- /** {...@inheritdoc} */
- public void title_()
- {
- if ( buffer.length() > 0 )
- {
- writer.startElement( "title" );
- content( buffer.toString() );
- writer.endElement(); // title
- buffer = new StringBuffer();
- }
- }
-
- /** {...@inheritdoc} */
- public void author_()
- {
- if ( buffer.length() > 0 )
- {
- writer.startElement( "author" );
- content( buffer.toString() );
- writer.endElement(); // author
- buffer = new StringBuffer();
- }
- }
-
- /** {...@inheritdoc} */
- public void date_()
- {
- if ( buffer.length() > 0 )
- {
- writer.startElement( "date" );
- content( buffer.toString() );
- writer.endElement();
- buffer = new StringBuffer();
- }
- }
-
- /** {...@inheritdoc} */
- public void body()
- {
- writer.startElement( "body" );
- }
-
- /** {...@inheritdoc} */
- public void body_()
- {
- writer.endElement(); // body
-
- writer.endElement(); // document
-
- resetState();
- }
-
- /** {...@inheritdoc} */
- public void section1()
- {
- writer.startElement( "section" );
- }
-
- /** {...@inheritdoc} */
- public void section2()
- {
- writer.startElement( "subsection" );
- }
-
- /** {...@inheritdoc} */
- public void section3()
- {
- writer.startElement( "subsection" );
- }
-
- /** {...@inheritdoc} */
- public void section4()
- {
- writer.startElement( "subsection" );
- }
-
- /** {...@inheritdoc} */
- public void section5()
- {
- writer.startElement( "subsection" );
- }
-
- /** {...@inheritdoc} */
- public void sectionTitle()
- {
- sectionTitleFlag = true;
- buffer = new StringBuffer();
- }
-
- /** {...@inheritdoc} */
- public void sectionTitle_()
- {
- sectionTitleFlag = false;
- writer.addAttribute( "name", buffer.toString() );
- }
-
- /** {...@inheritdoc} */
- public void section1_()
- {
- writer.endElement();
- }
-
- /** {...@inheritdoc} */
- public void section2_()
- {
- writer.endElement();
- }
-
- /** {...@inheritdoc} */
- public void section3_()
- {
- writer.endElement();
- }
-
- /** {...@inheritdoc} */
- public void section4_()
- {
- writer.endElement();
- }
-
- /** {...@inheritdoc} */
- public void section5_()
- {
- writer.endElement();
- }
-
- /** {...@inheritdoc} */
- public void list()
- {
- writer.startElement( "ul" );
- }
-
- /** {...@inheritdoc} */
- public void list_()
- {
- writer.endElement();
- }
-
- /** {...@inheritdoc} */
- public void listItem()
- {
- writer.startElement( "li" );
- itemFlag++;
- // What follows is at least a paragraph.
- }
-
- /** {...@inheritdoc} */
- public void listItem_()
- {
- writer.endElement();
- }
-
- /** {...@inheritdoc} */
- 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 );
- }
-
- /** {...@inheritdoc} */
- public void numberedList_()
- {
- writer.endElement();
- }
-
- /** {...@inheritdoc} */
- public void numberedListItem()
- {
- writer.startElement( "li" );
- itemFlag++;
- // What follows is at least a paragraph.
- }
-
- /** {...@inheritdoc} */
- public void numberedListItem_()
- {
- writer.endElement();
- }
-
- /** {...@inheritdoc} */
- public void definitionList()
- {
- writer.startElement( "dl" );
- writer.addAttribute( "compact", "compact" );
- }
-
- /** {...@inheritdoc} */
- public void definitionList_()
- {
- writer.endElement();
- }
-
- /** {...@inheritdoc} */
- public void definedTerm()
- {
- writer.startElement( "dt" );
- writer.startElement( "b" );
- }
-
- /** {...@inheritdoc} */
- public void definedTerm_()
- {
- writer.endElement();
- writer.endElement();
- }
-
- /** {...@inheritdoc} */
- public void definition()
- {
- writer.startElement( "dd" );
- itemFlag++;
- // What follows is at least a paragraph.
- }
-
- /** {...@inheritdoc} */
- public void definition_()
- {
- writer.endElement();
- }
-
- /** {...@inheritdoc} */
- public void paragraph()
- {
- if ( itemFlag == 0 )
- {
- writer.startElement( "p" );
- }
- }
-
- /** {...@inheritdoc} */
- public void paragraph_()
- {
- if ( itemFlag == 0 )
- {
- writer.endElement();
- }
- else
- {
- itemFlag--;
- }
- }
-
- /** {...@inheritdoc} */
- public void verbatim( boolean boxed )
- {
- verbatimFlag = true;
- if ( boxed )
- {
- writer.startElement( "source" );
- }
- else
- {
- writer.startElement( "pre" );
- }
- }
-
- /** {...@inheritdoc} */
- public void verbatim_()
- {
- writer.endElement();
-
- verbatimFlag = false;
- }
-
- /** {...@inheritdoc} */
- public void horizontalRule()
- {
- writer.startElement( "hr" );
- writer.endElement();
- }
-
- /** {...@inheritdoc} */
- public void table()
- {
- writer.startElement( "table" );
- writer.addAttribute( "align", "center" );
- }
-
- /** {...@inheritdoc} */
- public void table_()
- {
- writer.endElement();
- }
+ /** Writer used by Xdoc */
+ private StringWriter xdocWriter;
- /** {...@inheritdoc} */
- 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;
- }
-
- /** {...@inheritdoc} */
- public void tableRows_()
- {
- writer.endElement();
- }
-
- /** {...@inheritdoc} */
- public void tableRow()
- {
- writer.startElement( "tr" );
- writer.addAttribute( "valign", "top" );
- cellCount = 0;
- }
+ private XMLWriter xmlWriter;
- /** {...@inheritdoc} */
- public void tableRow_()
+ private XmlWriterXdocSink( StringWriter out, String encoding )
{
- writer.endElement();
- cellCount = 0;
- }
-
- /** {...@inheritdoc} */
- public void tableCell()
- {
- tableCell( false );
- }
-
- /** {...@inheritdoc} */
- public void tableHeaderCell()
- {
- tableCell( true );
+ super( out, encoding );
+ this.xdocWriter = out;
+ this.xmlWriter = new PrettyPrintXMLWriter( out );
}
/**
- * Starts a table cell.
- *
- * @param headerRow If this cell is part of a header row.
+ * @param out the wanted XML Writer.
+ * @deprecated since 1.1
*/
- public void tableCell( boolean headerRow )
- {
- String justif = null;
-
- if ( cellJustif != null )
- {
- switch ( cellJustif[cellCount] )
- {
- case Sink.JUSTIFY_LEFT:
- justif = "left";
- break;
- case Sink.JUSTIFY_RIGHT:
- justif = "right";
- break;
- case Sink.JUSTIFY_CENTER:
- default:
- justif = "center";
- break;
- }
- }
-
- writer.startElement( "t" + ( headerRow ? 'h' : 'd' ) );
- if ( justif != null )
- {
- writer.addAttribute( "align", justif );
- }
- }
-
- /** {...@inheritdoc} */
- public void tableCell_()
- {
- tableCell_( false );
- }
-
- /** {...@inheritdoc} */
- public void tableHeaderCell_()
- {
- tableCell_( true );
- }
-
- /**
- * Ends a table cell.
- *
- * @param headerRow If this cell is part of a header row.
- */
- public void tableCell_( boolean headerRow )
- {
- writer.endElement();
- ++cellCount;
- }
-
- /** {...@inheritdoc} */
- public void tableCaption()
- {
- writer.startElement( "p" );
- writer.startElement( "i" );
- }
-
- /** {...@inheritdoc} */
- public void tableCaption_()
- {
- writer.endElement();
- writer.endElement();
- }
-
- /** {...@inheritdoc} */
- public void anchor( String name )
- {
- if ( !headFlag )
- {
- String id = HtmlTools.encodeId( name );
- writer.startElement( "a" );
- writer.addAttribute( "id", id );
- writer.addAttribute( "name", id );
- }
- }
-
- /** {...@inheritdoc} */
- public void anchor_()
- {
- if ( !headFlag )
- {
- writer.endElement();
- }
- }
-
- /** {...@inheritdoc} */
- public void link( String name )
- {
- if ( !headFlag )
- {
- writer.startElement( "a" );
- writer.addAttribute( "href", name );
- }
- }
-
- /** {...@inheritdoc} */
- public void link_()
- {
- if ( !headFlag )
- {
- writer.endElement();
- }
- }
-
- /** {...@inheritdoc} */
- public void italic()
- {
- if ( !headFlag )
- {
- writer.startElement( "i" );
- }
- }
-
- /** {...@inheritdoc} */
- public void italic_()
- {
- if ( !headFlag )
- {
- writer.endElement();
- }
- }
-
- /** {...@inheritdoc} */
- public void bold()
- {
- if ( !headFlag )
- {
- writer.startElement( "b" );
- }
- }
-
- /** {...@inheritdoc} */
- public void bold_()
- {
- if ( !headFlag )
- {
- writer.endElement();
- }
- }
-
- /** {...@inheritdoc} */
- public void monospaced()
- {
- if ( !headFlag )
- {
- writer.startElement( "tt" );
- }
- }
-
- /** {...@inheritdoc} */
- public void monospaced_()
+ public XmlWriterXdocSink( XMLWriter out )
{
- if ( !headFlag )
- {
- writer.endElement();
- }
+ this( new StringWriter(), "UTF-8" );
+ this.xmlWriter = out;
}
/** {...@inheritdoc} */
- public void lineBreak()
- {
- if ( headFlag )
- {
- buffer.append( '\n' );
- }
- else
- {
- writer.startElement( "br" );
- writer.endElement();
- }
- }
-
- /** {...@inheritdoc} */
- public void nonBreakingSpace()
+ public void close()
{
- if ( headFlag )
- {
- buffer.append( ' ' );
- }
- else
- {
- writer.writeText( " " );
- }
- }
+ super.close();
- /** {...@inheritdoc} */
- public void text( String text )
- {
- if ( headFlag )
+ String xdocContent = xdocWriter.toString();
+ if ( getLog().isDebugEnabled() )
{
- buffer.append( text );
+ getLog().error( "Xdoc content: " + xdocContent );
}
- else if ( sectionTitleFlag )
+ StringWriter formattedContent = new StringWriter();
+ try
{
- buffer.append( text );
+ XmlUtil.prettyFormat( new StringReader( xdocContent ),
formattedContent );
}
- else
+ catch ( IOException e )
{
- if ( verbatimFlag )
+ if ( getLog().isDebugEnabled() )
{
- verbatimContent( text );
- }
- else
- {
- content( text );
+ getLog().error( "IOException: " + e.getMessage(), e );
}
+ formattedContent = new StringWriter();
+ formattedContent.write( xdocContent );
}
- }
-
- // ----------------------------------------------------------------------
- //
- // ----------------------------------------------------------------------
-
-
- /**
- * Write HTML escaped text to output.
- *
- * @param text The text to write.
- */
- protected void content( String text )
- {
- writer.writeText( escapeHTML( text ) );
- }
-
- /**
- * Write text to output, preserving white space.
- *
- * @param text The text to write.
- */
- protected void verbatimContent( String text )
- {
- writer.writeText( StringUtils.replace( text, " ", " " ) );
- }
-
- /**
- * Forward to HtmlTools.escapeHTML( text ).
- *
- * @param text the String to escape, may be null
- * @return the text escaped, "" if null String input
- * @see org.apache.maven.doxia.util.HtmlTools#escapeHTML(String).
- */
- public static String escapeHTML( String text )
- {
- return HtmlTools.escapeHTML( text );
- }
-
- /**
- * Forward to HtmlTools.encodeURL( text ).
- *
- * @param text the String to encode, may be null.
- * @return the text encoded, null if null String input.
- * @see org.apache.maven.doxia.util.HtmlTools#encodeURL(String).
- */
- public static String encodeURL( String text )
- {
- return HtmlTools.encodeURL( text );
- }
-
- /** {...@inheritdoc} */
- public void flush()
- {
- }
-
- /** {...@inheritdoc} */
- public void close()
- {
+ xmlWriter.writeMarkup( formattedContent.toString() );
}
}
Added:
maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XmlWriterXdocSinkTest.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/XmlWriterXdocSinkTest.java?rev=736001&view=auto
==============================================================================
---
maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XmlWriterXdocSinkTest.java
(added)
+++
maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XmlWriterXdocSinkTest.java
Tue Jan 20 04:31:12 2009
@@ -0,0 +1,106 @@
+package org.apache.maven.doxia.module.xdoc;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.Writer;
+
+import org.apache.maven.doxia.sink.Sink;
+import org.codehaus.plexus.util.StringUtils;
+import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
+import org.codehaus.plexus.util.xml.XmlUtil;
+
+/**
+ * @author <a href="mailto:[email protected]">Vincent Siveton</a>
+ * @version $Id$
+ * @since 1.1
+ */
+public class XmlWriterXdocSinkTest
+ extends XdocSinkTest
+{
+ private static final String DEFAULT_INDENT = StringUtils.repeat( " ",
XmlUtil.DEFAULT_INDENTATION_SIZE );
+
+ /** {...@inheritdoc} */
+ protected Sink createSink( Writer writer )
+ {
+ return new XmlWriterXdocSink( new PrettyPrintXMLWriter( writer ) );
+ }
+
+ /** {...@inheritdoc} */
+ protected String getSection1Block( String title )
+ {
+ return "<section name=\"" + title + "\"/>";
+ }
+
+ /** {...@inheritdoc} */
+ protected String getSection2Block( String title )
+ {
+ return "<subsection name=\"" + title + "\"/>";
+ }
+
+ /** {...@inheritdoc} */
+ protected String getListBlock( String item )
+ {
+ return "<ul>" + XmlUtil.DEFAULT_LINE_SEPARATOR + DEFAULT_INDENT +
"<li>" + item + "</li>"
+ + XmlUtil.DEFAULT_LINE_SEPARATOR + "</ul>";
+ }
+
+ /** {...@inheritdoc} */
+ protected String getNumberedListBlock( String item )
+ {
+ return "<ol style=\"list-style-type: lower-roman\">" +
XmlUtil.DEFAULT_LINE_SEPARATOR + DEFAULT_INDENT
+ + "<li>" + item + "</li>" + XmlUtil.DEFAULT_LINE_SEPARATOR +
"</ol>";
+ }
+
+ /** {...@inheritdoc} */
+ protected String getDefinitionListBlock( String definum, String definition
)
+ {
+ return "<dl>" + XmlUtil.DEFAULT_LINE_SEPARATOR + DEFAULT_INDENT +
"<dt>" + definum + "</dt>"
+ + XmlUtil.DEFAULT_LINE_SEPARATOR + DEFAULT_INDENT + "<dd>" +
definition + "</dd>"
+ + XmlUtil.DEFAULT_LINE_SEPARATOR + "</dl>";
+ }
+
+ /** {...@inheritdoc} */
+ protected String getFigureBlock( String source, String caption )
+ {
+ return "<img src=\"" + source + "\" alt=\"" + caption + "\"/>";
+ }
+
+ /** {...@inheritdoc} */
+ protected String getTableBlock( String cell, String caption )
+ {
+ return "<table align=\"center\" border=\"0\">" +
XmlUtil.DEFAULT_LINE_SEPARATOR + DEFAULT_INDENT
+ + "<caption>" + caption + "</caption>" +
XmlUtil.DEFAULT_LINE_SEPARATOR + DEFAULT_INDENT
+ + "<tr valign=\"top\">" + XmlUtil.DEFAULT_LINE_SEPARATOR +
DEFAULT_INDENT + DEFAULT_INDENT
+ + "<td align=\"center\">" + cell + "</td>" +
XmlUtil.DEFAULT_LINE_SEPARATOR + DEFAULT_INDENT + "</tr>"
+ + XmlUtil.DEFAULT_LINE_SEPARATOR + "</table>";
+ }
+
+ /** {...@inheritdoc} */
+ protected String getHorizontalRuleBlock()
+ {
+ return "<hr/>";
+ }
+
+ /** {...@inheritdoc} */
+ protected String getLineBreakBlock()
+ {
+ return "<br/>";
+ }
+}
Propchange:
maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XmlWriterXdocSinkTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
maven/doxia/doxia/trunk/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XmlWriterXdocSinkTest.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision