This is an automated email from the ASF dual-hosted git repository. elharo pushed a commit to branch xml3 in repository https://gitbox.apache.org/repos/asf/maven-shared-utils.git
commit fee97f9574d7db4b961d98e4f15eaf275ada69b1 Author: Elliotte Rusty Harold <elh...@ibiblio.org> AuthorDate: Sun Feb 23 11:37:33 2020 -0500 fail fast on empty element names --- .../shared/utils/xml/PrettyPrintXMLWriter.java | 15 ++++---- .../apache/maven/shared/utils/xml/XMLWriter.java | 26 ++++++++------ .../shared/utils/xml/PrettyPrintXmlWriterTest.java | 41 ++++++++++------------ 3 files changed, 42 insertions(+), 40 deletions(-) diff --git a/src/main/java/org/apache/maven/shared/utils/xml/PrettyPrintXMLWriter.java b/src/main/java/org/apache/maven/shared/utils/xml/PrettyPrintXMLWriter.java index deb07d2..d948d8e 100644 --- a/src/main/java/org/apache/maven/shared/utils/xml/PrettyPrintXMLWriter.java +++ b/src/main/java/org/apache/maven/shared/utils/xml/PrettyPrintXMLWriter.java @@ -27,10 +27,8 @@ import org.apache.maven.shared.utils.Os; /** * XMLWriter with nice indentation - */ -/** + * * @author kama - * */ public class PrettyPrintXMLWriter implements XMLWriter @@ -214,7 +212,7 @@ public class PrettyPrintXMLWriter } /** - * @param lineSeparator The line separator to be used. + * @param lineSeparator the line separator to be output */ public void setLineSeparator( String lineSeparator ) { @@ -227,7 +225,7 @@ public class PrettyPrintXMLWriter } /** - * @param lineIndentParameter The line indent parameter. + * @param lineIndentParameter the line indent parameter */ public void setLineIndenter( String lineIndentParameter ) { @@ -242,6 +240,11 @@ public class PrettyPrintXMLWriter /** {@inheritDoc} */ public void startElement( String elementName ) throws IOException { + + if (elementName.isEmpty()) { + throw new IllegalArgumentException("Element name cannot be empty"); + } + boolean firstLine = ensureDocumentStarted(); completePreviouslyOpenedElement(); @@ -328,7 +331,7 @@ public class PrettyPrintXMLWriter } /** - * Write the documents if not already done. + * Write the document if not already done. * * @return <code>true</code> if the document headers have freshly been written. */ diff --git a/src/main/java/org/apache/maven/shared/utils/xml/XMLWriter.java b/src/main/java/org/apache/maven/shared/utils/xml/XMLWriter.java index 0daad8b..32a56d2 100644 --- a/src/main/java/org/apache/maven/shared/utils/xml/XMLWriter.java +++ b/src/main/java/org/apache/maven/shared/utils/xml/XMLWriter.java @@ -30,7 +30,7 @@ public interface XMLWriter /** * Sets the encoding of the document. - * If not set, UTF-8 is being used + * If not set, UTF-8 is used. * * @param encoding the encoding * @throws IllegalStateException if the generation of the document has already started @@ -38,7 +38,7 @@ public interface XMLWriter void setEncoding( String encoding ); /** - * Sets the docType of the document. + * Sets the DOCTYPE of the document. * * @param docType the docType * @throws IllegalStateException if the generation of the document has already started @@ -48,15 +48,17 @@ public interface XMLWriter /** * Start an XML Element tag. - * @param name The name of the tag. - * @throws IOException if starting the element fails. + * + * @param name the name of the tag + * @throws IOException if starting the element fails */ void startElement( String name ) throws IOException; /** * Add a XML attribute to the current XML Element. - * This method must get called immediately after {@link #startElement(String)} + * This method must get called immediately after {@link #startElement(String)}. + * * @param key The key of the attribute. * @param value The value of the attribute. * @throws IllegalStateException if no element tag is currently in process @@ -65,8 +67,9 @@ public interface XMLWriter void addAttribute( String key, String value ) throws IOException; /** - * Add a value text to the current element tag - * This will perform XML escaping to guarantee valid content + * Add text to the current element tag. + * This performs XML escaping to guarantee well-formed content. + * * @param text The text which should be written. * @throws IllegalStateException if no element tag got started yet * @throws IOException if writing the text fails. @@ -74,10 +77,11 @@ public interface XMLWriter void writeText( String text ) throws IOException; /** - * Add a preformatted markup to the current element tag - * @param text The text which should be written. - * @throws IllegalStateException if no element tag got started yet - * @throws IOException if writing the markup fails. + * Add preformatted markup to the current element tag. + * + * @param text the text which should be written + * @throws IllegalStateException if no element tag is started yet + * @throws IOException if writing the markup fails */ void writeMarkup( String text ) throws IOException; diff --git a/src/test/java/org/apache/maven/shared/utils/xml/PrettyPrintXmlWriterTest.java b/src/test/java/org/apache/maven/shared/utils/xml/PrettyPrintXmlWriterTest.java index f272332..28ba583 100644 --- a/src/test/java/org/apache/maven/shared/utils/xml/PrettyPrintXmlWriterTest.java +++ b/src/test/java/org/apache/maven/shared/utils/xml/PrettyPrintXmlWriterTest.java @@ -1,8 +1,3 @@ -package org.apache.maven.shared.utils.xml; - -import java.io.IOException; -import javax.swing.text.html.HTML; -import java.io.StringWriter; /* * Licensed to the Apache Software Foundation (ASF) under one @@ -23,13 +18,15 @@ import java.io.StringWriter; * under the License. */ +package org.apache.maven.shared.utils.xml; +import java.io.IOException; +import javax.swing.text.html.HTML; +import java.io.StringWriter; import org.apache.maven.shared.utils.Os; import org.apache.maven.shared.utils.StringUtils; -import org.junit.After; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; /** @@ -40,26 +37,24 @@ import org.junit.Test; */ public class PrettyPrintXmlWriterTest { - StringWriter w; + private StringWriter w = new StringWriter();; - PrettyPrintXMLWriter writer; + private PrettyPrintXMLWriter writer = new PrettyPrintXMLWriter( w ); - @Before - public void before() - throws Exception - { - w = new StringWriter(); - writer = new PrettyPrintXMLWriter( w ); - } - - @After - public void after() - throws Exception + + @Test + public void testNoStartTag() throws IOException { - writer = null; - w = null; + + try { + writer.startElement( "" ); + Assert.fail( "allowed empty name" ); + } catch ( IllegalArgumentException ex ) { + Assert.assertEquals( "Element name cannot be empty", ex.getMessage() ); + } + } - + @Test public void testDefaultPrettyPrintXMLWriter() throws IOException {