This is an automated email from the ASF dual-hosted git repository. michaelo pushed a commit to branch DOXIA-590 in repository https://gitbox.apache.org/repos/asf/maven-doxia.git
commit 1fe1baf782cbe86ca9df484e0712b050663669b7 Author: Fred Eckertson <[email protected]> AuthorDate: Wed Jun 1 22:07:08 2022 -0500 implement hidden rows --- .../maven/doxia/sink/impl/Xhtml5BaseSink.java | 37 +++++-- .../maven/doxia/sink/impl/XhtmlBaseSink.java | 38 ++++--- .../maven/doxia/sink/impl/Xhtml5BaseSinkTest.java | 120 +++++++++++++++++++- .../maven/doxia/sink/impl/XhtmlBaseSinkTest.java | 122 ++++++++++++++++++++- 4 files changed, 290 insertions(+), 27 deletions(-) diff --git a/doxia-core/src/main/java/org/apache/maven/doxia/sink/impl/Xhtml5BaseSink.java b/doxia-core/src/main/java/org/apache/maven/doxia/sink/impl/Xhtml5BaseSink.java index 57c14779..ace3220e 100644 --- a/doxia-core/src/main/java/org/apache/maven/doxia/sink/impl/Xhtml5BaseSink.java +++ b/doxia-core/src/main/java/org/apache/maven/doxia/sink/impl/Xhtml5BaseSink.java @@ -31,6 +31,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Stack; +import java.util.regex.Pattern; import javax.swing.text.MutableAttributeSet; import javax.swing.text.html.HTML.Attribute; @@ -64,6 +65,9 @@ public class Xhtml5BaseSink /** The PrintWriter to write the result. */ private final PrintWriter writer; + /** Used to identify if a class string contains `hidden` */ + private static final Pattern HIDDEN_CLASS_PATTERN = Pattern.compile( "(?:.*\\s|^)hidden(?:\\s.*|$)" ); + /** Used to collect text events mainly for the head events. */ private StringBuffer textBuffer = new StringBuffer(); @@ -1507,9 +1511,8 @@ public class Xhtml5BaseSink } /** - * The default class style is <code>a</code> or <code>b</code> depending the row id. + * Rows are striped with two colors by adding the class <code>a</code> or <code>b</code>. {@inheritDoc} * - * {@inheritDoc} * @see javax.swing.text.html.HTML.Tag#TR */ @Override @@ -1524,34 +1527,44 @@ public class Xhtml5BaseSink } /** - * The default class style is <code>a</code> or <code>b</code> depending the row id. + * Rows are striped with two colors by adding the class <code>a</code> or <code>b</code>. If the provided attributes + * specify the <code>hidden</code> class, the next call to tableRow will set the same striping class as this one. A + * style for <code>hidden</code> or <code>table.bodyTable hidden</code> may need to be provided to actually hide + * such a row. {@inheritDoc} * - * {@inheritDoc} * @see javax.swing.text.html.HTML.Tag#TR */ @Override public void tableRow( SinkEventAttributes attributes ) { - MutableAttributeSet atts = SinkUtils.filterAttributes( + MutableAttributeSet attrs = SinkUtils.filterAttributes( attributes, SinkUtils.SINK_TR_ATTRIBUTES ); - if ( atts == null ) + if ( attrs == null ) { - atts = new SinkEventAttributeSet(); + attrs = new SinkEventAttributeSet(); } String rowClass = evenTableRow ? "a" : "b"; - if ( atts.isDefined( Attribute.CLASS.toString() ) ) + boolean hidden = false; + if ( attrs.isDefined( Attribute.CLASS.toString() ) ) { - String givenRowClass = (String) atts.getAttribute( Attribute.CLASS.toString() ); + String givenRowClass = (String) attrs.getAttribute( Attribute.CLASS.toString() ); + if ( HIDDEN_CLASS_PATTERN.matcher( givenRowClass ).matches() ) + { + hidden = true; + } rowClass = givenRowClass + " " + rowClass; } - atts.addAttribute( Attribute.CLASS, rowClass ); + attrs.addAttribute( Attribute.CLASS, rowClass ); - writeStartTag( HtmlMarkup.TR, atts ); + writeStartTag( HtmlMarkup.TR, attrs ); - evenTableRow = !evenTableRow; + if ( !hidden ) + { + evenTableRow = !evenTableRow; + } if ( !this.cellCountStack.isEmpty() ) { diff --git a/doxia-core/src/main/java/org/apache/maven/doxia/sink/impl/XhtmlBaseSink.java b/doxia-core/src/main/java/org/apache/maven/doxia/sink/impl/XhtmlBaseSink.java index 5c879f30..7701484d 100644 --- a/doxia-core/src/main/java/org/apache/maven/doxia/sink/impl/XhtmlBaseSink.java +++ b/doxia-core/src/main/java/org/apache/maven/doxia/sink/impl/XhtmlBaseSink.java @@ -30,6 +30,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Stack; +import java.util.regex.Pattern; import javax.swing.text.MutableAttributeSet; import javax.swing.text.html.HTML.Attribute; @@ -68,6 +69,9 @@ public class XhtmlBaseSink /** The PrintWriter to write the result. */ private final PrintWriter writer; + /** Used to identify if a class string contains `hidden` */ + private static final Pattern HIDDEN_CLASS_PATTERN = Pattern.compile( "(?:.*\\s|^)hidden(?:\\s.*|$)" ); + /** Used to collect text events mainly for the head events. */ private StringBuffer textBuffer = new StringBuffer(); @@ -1385,9 +1389,8 @@ public class XhtmlBaseSink } /** - * The default class style is <code>a</code> or <code>b</code> depending the row id. + * Rows are striped with two colors by adding the class <code>a</code> or <code>b</code>. {@inheritDoc} * - * {@inheritDoc} * @see javax.swing.text.html.HTML.Tag#TR */ @Override @@ -1402,34 +1405,43 @@ public class XhtmlBaseSink } /** - * The default class style is <code>a</code> or <code>b</code> depending the row id. + * Rows are striped with two colors by adding the class <code>a</code> or <code>b</code>. If the provided attributes + * specify the <code>hidden</code> class, the next call to tableRow will set the same striping class as this one. A + * style for <code>hidden</code> or <code>table.bodyTable hidden</code> may need to be provided to actually hide + * such a row. {@inheritDoc} * - * {@inheritDoc} * @see javax.swing.text.html.HTML.Tag#TR */ @Override public void tableRow( SinkEventAttributes attributes ) { - MutableAttributeSet atts = SinkUtils.filterAttributes( - attributes, SinkUtils.SINK_TR_ATTRIBUTES ); + MutableAttributeSet attrs = SinkUtils.filterAttributes( attributes, SinkUtils.SINK_TR_ATTRIBUTES ); - if ( atts == null ) + if ( attrs == null ) { - atts = new SinkEventAttributeSet(); + attrs = new SinkEventAttributeSet(); } String rowClass = evenTableRow ? "a" : "b"; - if ( atts.isDefined( Attribute.CLASS.toString() ) ) + boolean hidden = false; + if ( attrs.isDefined( Attribute.CLASS.toString() ) ) { - String givenRowClass = (String) atts.getAttribute( Attribute.CLASS.toString() ); + String givenRowClass = (String) attrs.getAttribute( Attribute.CLASS.toString() ); + if ( HIDDEN_CLASS_PATTERN.matcher( givenRowClass ).matches() ) + { + hidden = true; + } rowClass = givenRowClass + " " + rowClass; } - atts.addAttribute( Attribute.CLASS, rowClass ); + attrs.addAttribute( Attribute.CLASS, rowClass ); - writeStartTag( HtmlMarkup.TR, atts ); + writeStartTag( HtmlMarkup.TR, attrs ); - evenTableRow = !evenTableRow; + if ( !hidden ) + { + evenTableRow = !evenTableRow; + } if ( !this.cellCountStack.isEmpty() ) { diff --git a/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/Xhtml5BaseSinkTest.java b/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/Xhtml5BaseSinkTest.java index 1e3ef4ec..6bc76fbc 100644 --- a/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/Xhtml5BaseSinkTest.java +++ b/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/Xhtml5BaseSinkTest.java @@ -1085,15 +1085,84 @@ public class Xhtml5BaseSinkTest { sink = new Xhtml5BaseSink( writer ); + sink.tableRows( null, false ); + sink.tableRow( attributes ); + sink.tableRow_(); + sink.tableRow(); + sink.tableRow_(); + sink.tableRows_(); + sink.table_(); + } + finally + { + sink.close(); + } + + String xmlExpected = "<table border=\"0\" class=\"bodyTable\">" + EOL + "<tr style=\"bold\" class=\"a\"></tr>" + + EOL + "<tr class=\"b\"></tr></table>"; + + assertEquals( xmlExpected, writer.toString() ); + } + + /** + * Test striping for hidden rows in tableRow method. + */ + @Test + public void testHiddenTableRowStriping() + { + try + { + SinkEventAttributeSet attributes2 = new SinkEventAttributeSet(); + SinkEventAttributeSet attributes3 = new SinkEventAttributeSet(); + attributes3.addAttributes( attributes ); + sink = new Xhtml5BaseSink( writer ); + + sink.tableRow(); + sink.tableRow_(); sink.tableRow( attributes ); sink.tableRow_(); + attributes2.addAttribute( SinkEventAttributes.CLASS, "hidden xyz abc" ); + sink.tableRow( attributes2 ); + sink.tableRow_(); + attributes2.addAttribute( SinkEventAttributes.CLASS, "abc hidden xyz" ); + sink.tableRow( attributes2 ); + sink.tableRow_(); + sink.tableRow(); + sink.tableRow_(); + attributes2.addAttribute( SinkEventAttributes.CLASS, "not-hidden xyz" ); + sink.tableRow( attributes2 ); + sink.tableRow_(); + attributes2.addAttribute( SinkEventAttributes.CLASS, "xyz not-hidden" ); + sink.tableRow( attributes2 ); + sink.tableRow_(); + attributes3.addAttribute( SinkEventAttributes.CLASS, "xyz abc hidden" ); + sink.tableRow( attributes3 ); + sink.tableRow_(); + attributes2.addAttribute( SinkEventAttributes.CLASS, "xyz hidden-not" ); + sink.tableRow( attributes2 ); + sink.tableRow_(); + sink.tableRow(); + sink.tableRow_(); } finally { sink.close(); } - assertEquals( "<tr style=\"bold\" class=\"a\"></tr>", writer.toString() ); + StringBuilder sbExpeted = new StringBuilder( "<table border=\"0\" class=\"bodyTable\">" ); + sbExpeted.append( EOL ).append( "<tr class=\"a\"></tr>" ).append( EOL ); + sbExpeted.append( "<tr style=\"bold\" class=\"b\"></tr>" ).append( EOL ); + sbExpeted.append( "<tr class=\"hidden xyz abc a\"></tr>" ).append( EOL ); + sbExpeted.append( "<tr class=\"abc hidden xyz a\"></tr>" ).append( EOL ); + sbExpeted.append( "<tr class=\"a\"></tr>" ).append( EOL ); + sbExpeted.append( "<tr class=\"not-hidden xyz b\"></tr>" ).append( EOL ); + sbExpeted.append( "<tr class=\"xyz not-hidden a\"></tr>" ).append( EOL ); + sbExpeted.append( "<tr style=\"bold\" class=\"xyz abc hidden b\"></tr>" ).append( EOL ); + sbExpeted.append( "<tr class=\"xyz hidden-not b\"></tr>" ).append( EOL ); + sbExpeted.append( "<tr class=\"a\"></tr>" ); + + String xmlExpected = sbExpeted.toString(); + assertEquals( xmlExpected, writer.toString() ); } /** @@ -1210,6 +1279,55 @@ public class Xhtml5BaseSinkTest assertEquals( "<a style=\"bold\" href=\"link.html\"></a>", writer.toString() ); } + /** + * Test of link method for an external link. + */ + @Test + public void testLinkExternal() + { + final String name = "https://www.apache.org"; + + try + { + sink = new Xhtml5BaseSink( writer ); + sink.link( name, attributes ); + sink.link_(); + } + finally + { + sink.close(); + } + + assertEquals( "<a style=\"bold\" class=\"externalLink\" href=\"https://www.apache.org\"></a>", + writer.toString() ); + } + + /** + * Test of link method for an external link when a css class is provided. + */ + @Test + public void testLinkExternalClassExtend() + { + final String name = "https://www.apache.org"; + SinkEventAttributeSet attributes2 = new SinkEventAttributeSet(); + attributes2.addAttributes( attributes ); + attributes2.addAttribute( SinkEventAttributes.CLASS, "cs1 cs2" ); + + try + { + sink = new Xhtml5BaseSink( writer ); + sink.link( name, attributes2 ); + sink.link_(); + } + finally + { + sink.close(); + } + + assertEquals( "<a style=\"bold\" class=\"cs1 cs2 externalLink\" href=\"https://www.apache.org\"></a>", + writer.toString() ); + } + /** * Test of inline method, of class Xhtml5BaseSink. */ diff --git a/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/XhtmlBaseSinkTest.java b/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/XhtmlBaseSinkTest.java index be4bd18c..ece60a56 100644 --- a/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/XhtmlBaseSinkTest.java +++ b/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/XhtmlBaseSinkTest.java @@ -47,6 +47,8 @@ public class XhtmlBaseSinkTest private XhtmlBaseSink sink; private Writer writer; + String EOL = System.lineSeparator(); + @BeforeEach public void setUp() { @@ -705,15 +707,84 @@ public class XhtmlBaseSinkTest { sink = new XhtmlBaseSink( writer ); + sink.tableRows( null, false ); + sink.tableRow( attributes ); + sink.tableRow_(); + sink.tableRow(); + sink.tableRow_(); + sink.tableRows_(); + sink.table_(); + } + finally + { + sink.close(); + } + + String xmlExpected = "<table border=\"0\" class=\"bodyTable\">" + EOL + "<tr style=\"bold\" class=\"a\"></tr>" + + EOL + "<tr class=\"b\"></tr></table>"; + + assertEquals( xmlExpected, writer.toString() ); + } + + /** + * Test striping for hidden rows in tableRow method. + */ + @Test + public void testHiddenTableRowStriping() + { + try + { + SinkEventAttributeSet attributes2 = new SinkEventAttributeSet(); + SinkEventAttributeSet attributes3 = new SinkEventAttributeSet(); + attributes3.addAttributes( attributes ); + sink = new XhtmlBaseSink( writer ); + + sink.tableRow(); + sink.tableRow_(); sink.tableRow( attributes ); sink.tableRow_(); + attributes2.addAttribute( SinkEventAttributes.CLASS, "hidden xyz abc" ); + sink.tableRow( attributes2 ); + sink.tableRow_(); + attributes2.addAttribute( SinkEventAttributes.CLASS, "abc hidden xyz" ); + sink.tableRow( attributes2 ); + sink.tableRow_(); + sink.tableRow(); + sink.tableRow_(); + attributes2.addAttribute( SinkEventAttributes.CLASS, "not-hidden xyz" ); + sink.tableRow( attributes2 ); + sink.tableRow_(); + attributes2.addAttribute( SinkEventAttributes.CLASS, "xyz not-hidden" ); + sink.tableRow( attributes2 ); + sink.tableRow_(); + attributes3.addAttribute( SinkEventAttributes.CLASS, "xyz abc hidden" ); + sink.tableRow( attributes3 ); + sink.tableRow_(); + attributes2.addAttribute( SinkEventAttributes.CLASS, "xyz hidden-not" ); + sink.tableRow( attributes2 ); + sink.tableRow_(); + sink.tableRow(); + sink.tableRow_(); } finally { sink.close(); } - assertEquals( "<tr style=\"bold\" class=\"a\"></tr>", writer.toString() ); + StringBuilder sbExpeted = new StringBuilder( "<table border=\"0\" class=\"bodyTable\">" ); + sbExpeted.append( EOL ).append( "<tr class=\"a\"></tr>" ).append( EOL ); + sbExpeted.append( "<tr style=\"bold\" class=\"b\"></tr>" ).append( EOL ); + sbExpeted.append( "<tr class=\"hidden xyz abc a\"></tr>" ).append( EOL ); + sbExpeted.append( "<tr class=\"abc hidden xyz a\"></tr>" ).append( EOL ); + sbExpeted.append( "<tr class=\"a\"></tr>" ).append( EOL ); + sbExpeted.append( "<tr class=\"not-hidden xyz b\"></tr>" ).append( EOL ); + sbExpeted.append( "<tr class=\"xyz not-hidden a\"></tr>" ).append( EOL ); + sbExpeted.append( "<tr style=\"bold\" class=\"xyz abc hidden b\"></tr>" ).append( EOL ); + sbExpeted.append( "<tr class=\"xyz hidden-not b\"></tr>" ).append( EOL ); + sbExpeted.append( "<tr class=\"a\"></tr>" ); + + String xmlExpected = sbExpeted.toString(); + assertEquals( xmlExpected, writer.toString() ); } /** @@ -830,6 +901,55 @@ public class XhtmlBaseSinkTest assertEquals( "<a style=\"bold\" href=\"link.html\"></a>", writer.toString() ); } + /** + * Test of link method for an external link. + */ + @Test + public void testLinkExternal() + { + final String name = "https://www.apache.org"; + + try + { + sink = new XhtmlBaseSink( writer ); + sink.link( name, attributes ); + sink.link_(); + } + finally + { + sink.close(); + } + + assertEquals( "<a style=\"bold\" class=\"externalLink\" href=\"https://www.apache.org\"></a>", + writer.toString() ); + } + + /** + * Test of link method for an external link when a css class is provided. + */ + @Test + public void testLinkExternalClassExtend() + { + final String name = "https://www.apache.org"; + SinkEventAttributeSet attributes2 = new SinkEventAttributeSet(); + attributes2.addAttributes( attributes ); + attributes2.addAttribute( SinkEventAttributes.CLASS, "cs1 cs2" ); + + try + { + sink = new XhtmlBaseSink( writer ); + sink.link( name, attributes2 ); + sink.link_(); + } + finally + { + sink.close(); + } + + assertEquals( "<a style=\"bold\" class=\"cs1 cs2 externalLink\" href=\"https://www.apache.org\"></a>", + writer.toString() ); + } + /** * Test of italic/bold/monospaced method, of class XhtmlBaseSink. */
