From fbba19197a95d15fa63b041ea29dfbc33272fe55 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Fri, 29 May 2020 19:26:09 -0400 Subject: [PATCH 1/7] suppress deprecations and clean up test methods --- .../shared/utils/io/MatchPatternTest.java | 2 +- .../shared/utils/io/MatchPatternsTest.java | 2 +- .../shared/utils/io/SelectorUtilsTest.java | 3 +- .../utils/testhelpers/ExceptionHelper.java | 67 ------------------- .../utils/testhelpers/FileTestHelper.java | 33 +++++---- 5 files changed, 22 insertions(+), 85 deletions(-) delete mode 100644 src/test/java/org/apache/maven/shared/utils/testhelpers/ExceptionHelper.java diff --git a/src/test/java/org/apache/maven/shared/utils/io/MatchPatternTest.java b/src/test/java/org/apache/maven/shared/utils/io/MatchPatternTest.java index c7f4ff7b..6c7e11d3 100644 --- a/src/test/java/org/apache/maven/shared/utils/io/MatchPatternTest.java +++ b/src/test/java/org/apache/maven/shared/utils/io/MatchPatternTest.java @@ -25,11 +25,11 @@ /** * @author Kristian Rosenvold */ +@SuppressWarnings( "deprecation" ) public class MatchPatternTest { @Test public void matchPath() - throws Exception { MatchPattern mp = MatchPattern.fromString( "ABC*" ); assertTrue( mp.matchPath( "ABCD", true ) ); diff --git a/src/test/java/org/apache/maven/shared/utils/io/MatchPatternsTest.java b/src/test/java/org/apache/maven/shared/utils/io/MatchPatternsTest.java index 1a5d5a68..84b41f96 100644 --- a/src/test/java/org/apache/maven/shared/utils/io/MatchPatternsTest.java +++ b/src/test/java/org/apache/maven/shared/utils/io/MatchPatternsTest.java @@ -26,11 +26,11 @@ /** * @author Kristian Rosenvold */ +@SuppressWarnings( "deprecation" ) public class MatchPatternsTest { @Test public void matches() - throws Exception { MatchPatterns from = MatchPatterns.from( "ABC**", "CDE**" ); assertTrue( from.matches( "ABCDE", true ) ); diff --git a/src/test/java/org/apache/maven/shared/utils/io/SelectorUtilsTest.java b/src/test/java/org/apache/maven/shared/utils/io/SelectorUtilsTest.java index ad4283fd..750015a7 100644 --- a/src/test/java/org/apache/maven/shared/utils/io/SelectorUtilsTest.java +++ b/src/test/java/org/apache/maven/shared/utils/io/SelectorUtilsTest.java @@ -27,8 +27,8 @@ /** * Test the {@link SelectorUtils} class. - * */ +@SuppressWarnings( "deprecation" ) public class SelectorUtilsTest { @@ -67,7 +67,6 @@ public void testAntPatternStrings() assertAntDoesNotMatch( "/aaa/", "\\aaa\\bbb" ); } - private void assertAntDoesNotMatch( String pattern, String target ) { assertEquals( false, SelectorUtils.matchPatternStart( wrapWithAntHandler( pattern ), target ) ); diff --git a/src/test/java/org/apache/maven/shared/utils/testhelpers/ExceptionHelper.java b/src/test/java/org/apache/maven/shared/utils/testhelpers/ExceptionHelper.java deleted file mode 100644 index 8d153b4b..00000000 --- a/src/test/java/org/apache/maven/shared/utils/testhelpers/ExceptionHelper.java +++ /dev/null @@ -1,67 +0,0 @@ -package org.apache.maven.shared.utils.testhelpers; - -/* - * 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 org.hamcrest.BaseMatcher; -import org.hamcrest.Description; -import org.hamcrest.Matcher; - - -public class ExceptionHelper -{ - - /** - * A matcher that verifies that the a root cause of an exception is of the specified type. - * - * @param cause the type of exception that caused this. - * @return A matcher that verifies that the a root cause of an exception is of the specified type. - */ - public static Matcher hasCause( Class cause ) - { - return new HasCause( cause ); - } - - private static class HasCause - extends BaseMatcher - { - private final Class cause; - - public HasCause( Class cause ) - { - this.cause = cause; - } - - public boolean matches( Object item ) - { - Throwable throwable = (Throwable) item; - while ( throwable != null && !cause.isInstance( throwable ) ) - { - throwable = throwable.getCause(); - } - return cause.isInstance( throwable ); - } - - public void describeTo( Description description ) - { - description.appendText( "was caused by a " ).appendValue( cause ).appendText( " being thrown" ); - } - } -} diff --git a/src/test/java/org/apache/maven/shared/utils/testhelpers/FileTestHelper.java b/src/test/java/org/apache/maven/shared/utils/testhelpers/FileTestHelper.java index 81d4fad6..b7dfb68a 100644 --- a/src/test/java/org/apache/maven/shared/utils/testhelpers/FileTestHelper.java +++ b/src/test/java/org/apache/maven/shared/utils/testhelpers/FileTestHelper.java @@ -19,20 +19,26 @@ * under the License. */ -import org.apache.maven.shared.utils.io.FileUtils; -import org.junit.rules.TemporaryFolder; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; + +import org.apache.commons.io.FileUtils; -import java.io.*; +import org.junit.rules.TemporaryFolder; /** - * A few utility methods for file based tests + * A few utility methods for file based tests. */ public final class FileTestHelper { private FileTestHelper() { - // utility function doesn't need a public ct + // utility function doesn't need a public constructor } public static void generateTestData( OutputStream out, long size ) @@ -54,14 +60,12 @@ public static void generateTestFile( File testfile, int size ) testfile.delete(); } - OutputStream os = new FileOutputStream( testfile ); - generateTestData( os, size ); - os.flush(); - os.close(); + try ( OutputStream os = new FileOutputStream( testfile ) ) { + generateTestData( os, size ); + os.flush(); + } } - - public static void createLineBasedFile( File file, String[] data ) throws IOException { @@ -70,11 +74,12 @@ public static void createLineBasedFile( File file, String[] data ) throw new IOException( "Cannot create file " + file + " as the parent directory does not exist" ); } - try ( PrintWriter out = new PrintWriter( new OutputStreamWriter( new FileOutputStream( file ), "UTF-8" ) ) ) + try ( Writer out = new OutputStreamWriter( new FileOutputStream( file ), "UTF-8" ) ) { for ( String aData : data ) { - out.println( aData ); + out.write( aData ); + out.write( System.getProperty( "line.separator" ) ); } } } @@ -92,7 +97,7 @@ public static File newFile( TemporaryFolder folder, String filename ) if ( destination.exists() ) { - FileUtils.forceDelete( destination ); + FileUtils.deleteQuietly( destination ); } return destination; } From 627ee2bc010bca19ce80b4dc112f5e7925764142 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Sat, 30 May 2020 08:06:55 -0400 Subject: [PATCH 2/7] line separator --- .../apache/maven/shared/utils/testhelpers/FileTestHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/apache/maven/shared/utils/testhelpers/FileTestHelper.java b/src/test/java/org/apache/maven/shared/utils/testhelpers/FileTestHelper.java index b7dfb68a..aa7323cd 100644 --- a/src/test/java/org/apache/maven/shared/utils/testhelpers/FileTestHelper.java +++ b/src/test/java/org/apache/maven/shared/utils/testhelpers/FileTestHelper.java @@ -79,7 +79,7 @@ public static void createLineBasedFile( File file, String[] data ) for ( String aData : data ) { out.write( aData ); - out.write( System.getProperty( "line.separator" ) ); + out.write( System.lineSeparator() ); } } } From 159065931b909ac5e447e618d34fdd84ec9b5f98 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Sat, 30 May 2020 10:31:07 -0400 Subject: [PATCH 3/7] revert changes from wrong client --- .../utils/xml/PrettyPrintXMLWriter.java | 128 +++--------- .../maven/shared/utils/xml/XMLEncode.java | 194 ++++++++---------- .../utils/xml/PrettyPrintXmlWriterTest.java | 43 ++-- 3 files changed, 132 insertions(+), 233 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 deb07d21..796c8bbe 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 @@ -20,17 +20,15 @@ */ import java.io.IOException; -import java.io.PrintWriter; import java.io.Writer; import java.util.ArrayList; import org.apache.maven.shared.utils.Os; /** - * XMLWriter with nice indentation - */ -/** + * XMLWriter with nice indentation. This class does minimal checking of its input + * and can produce malformed XML. + * * @author kama - * */ public class PrettyPrintXMLWriter implements XMLWriter @@ -41,7 +39,7 @@ public class PrettyPrintXMLWriter private static final char[] DEFAULT_LINE_INDENT = new char[]{ ' ', ' ' }; - private PrintWriter writer; + private Writer writer; private ArrayList elementStack = new ArrayList(); @@ -63,28 +61,11 @@ public class PrettyPrintXMLWriter /** * @param writer not null - * @param lineIndent could be null, but the normal way is some spaces. - */ - public PrettyPrintXMLWriter( PrintWriter writer, String lineIndent ) - { - this( writer, lineIndent, null, null ); - } - - /** - * @param writer not null - * @param lineIndent could be null, but the normal way is some spaces. + * @param lineIndent can be null, but the normal way is some spaces */ public PrettyPrintXMLWriter( Writer writer, String lineIndent ) { - this( new PrintWriter( writer ), lineIndent ); - } - - /** - * @param writer not null - */ - public PrettyPrintXMLWriter( PrintWriter writer ) - { - this( writer, null, null ); + this( writer, lineIndent.toCharArray(), Os.LINE_SEP.toCharArray(), null, null ); } /** @@ -92,75 +73,40 @@ public PrettyPrintXMLWriter( PrintWriter writer ) */ public PrettyPrintXMLWriter( Writer writer ) { - this( new PrintWriter( writer ) ); - } - - /** - * @param writer not null - * @param lineIndent could be null, but the normal way is some spaces. - * @param encoding could be null or invalid. - * @param doctype could be null. - */ - public PrettyPrintXMLWriter( PrintWriter writer, String lineIndent, String encoding, String doctype ) - { - this( writer, lineIndent.toCharArray(), Os.LINE_SEP.toCharArray(), encoding, doctype ); + this( writer, DEFAULT_LINE_INDENT, Os.LINE_SEP.toCharArray(), null, null ); } /** * @param writer not null - * @param lineIndent could be null, but the normal way is some spaces. - * @param encoding could be null or invalid. - * @param doctype could be null. + * @param lineIndent can be null, but the normal way is some spaces + * @param encoding can be null or invalid + * @param doctype can be null */ public PrettyPrintXMLWriter( Writer writer, String lineIndent, String encoding, String doctype ) { - this( new PrintWriter( writer ), lineIndent, encoding, doctype ); - } - - /** - * @param writer not null - * @param encoding could be null or invalid. - * @param doctype could be null. - */ - public PrettyPrintXMLWriter( PrintWriter writer, String encoding, String doctype ) - { - this( writer, DEFAULT_LINE_INDENT, Os.LINE_SEP.toCharArray(), encoding, doctype ); + this( writer, lineIndent.toCharArray(), Os.LINE_SEP.toCharArray(), encoding, doctype ); } /** * @param writer not null - * @param encoding could be null or invalid. - * @param doctype could be null. + * @param encoding can be null or invalid + * @param doctype can be null */ public PrettyPrintXMLWriter( Writer writer, String encoding, String doctype ) { - this( new PrintWriter( writer ), encoding, doctype ); - } - - /** - * @param writer not null - * @param lineIndent could be null, but the normal way is some spaces. - * @param lineSeparator could be null, but the normal way is valid line separator - * @param encoding could be null or the encoding to use. - * @param doctype could be null. - */ - public PrettyPrintXMLWriter( PrintWriter writer, String lineIndent, String lineSeparator, String encoding, - String doctype ) - { - this( writer, lineIndent.toCharArray(), lineSeparator.toCharArray(), encoding, doctype ); + this( writer, DEFAULT_LINE_INDENT, Os.LINE_SEP.toCharArray(), encoding, doctype ); } /** * @param writer not null - * @param lineIndent could be null, but the normal way is some spaces. - * @param lineSeparator could be null, but the normal way is valid line separator - * @param encoding could be null or the encoding to use. - * @param doctype could be null. + * @param lineIndent can be null, but the normal way is some spaces + * @param lineSeparator can be null, but the normal way is valid line separator + * @param encoding can be null or the encoding to use + * @param doctype can be null */ - private PrettyPrintXMLWriter( PrintWriter writer, char[] lineIndent, char[] lineSeparator, String encoding, + private PrettyPrintXMLWriter( Writer writer, char[] lineIndent, char[] lineSeparator, String encoding, String doctype ) { - super(); this.writer = writer; this.lineIndent = lineIndent; this.lineSeparator = lineSeparator; @@ -168,9 +114,6 @@ private PrettyPrintXMLWriter( PrintWriter writer, char[] lineIndent, char[] line this.docType = doctype; depth = 0; - - // Fail early with assertions enabled. Issue is in the calling code not having checked for any errors. - assert !writer.checkError() : "Unexpected error state PrintWriter passed to PrettyPrintXMLWriter."; } /** {@inheritDoc} */ @@ -185,10 +128,6 @@ public void addAttribute( String key, String value ) throws IOException writer.write( key ); writer.write( '=' ); XMLEncode.xmlEncodeTextAsPCDATA( value, true, '"', writer ); - if ( writer.checkError() ) - { - throw new IOException( "Failure adding attribute '" + key + "' with value '" + value + "'" ); - } } /** {@inheritDoc} */ @@ -253,10 +192,6 @@ public void startElement( String elementName ) throws IOException writer.write( '<' ); writer.write( elementName ); - if ( writer.checkError() ) - { - throw new IOException( "Failure starting element '" + elementName + "'." ); - } processingElement = true; @@ -273,11 +208,6 @@ public void writeText( String text ) throws IOException XMLEncode.xmlEncodeText( text, writer ); endOnSameLine = true; - - if ( writer.checkError() ) - { - throw new IOException( "Failure writing text." ); - } } /** {@inheritDoc} */ @@ -288,11 +218,6 @@ public void writeMarkup( String markup ) throws IOException completePreviouslyOpenedElement(); writer.write( markup ); - - if ( writer.checkError() ) - { - throw new IOException( "Failure writing markup." ); - } } /** {@inheritDoc} */ @@ -320,19 +245,14 @@ public void endElement() throws IOException } endOnSameLine = false; - - if ( writer.checkError() ) - { - throw new IOException( "Failure ending element." ); - } } /** * Write the documents if not already done. * - * @return true if the document headers have freshly been written. + * @return true if the document headers have freshly been written */ - private boolean ensureDocumentStarted() + private boolean ensureDocumentStarted() throws IOException { if ( !documentStarted ) { @@ -349,7 +269,7 @@ private boolean ensureDocumentStarted() return false; } - private void writeDocumentHeader() + private void writeDocumentHeader() throws IOException { writer.write( " CDATA_BLOCK_THRESHOLD_LENGTH ) { - // only encode as cdata if is is longer than CDATA block overhead: - if ( text.length() > CDATA_BLOCK_THRESHOLD_LENGTH ) + String cdata = xmlEncodeTextAsCDATABlock( text ); + if ( cdata != null ) { - String cdata = xmlEncodeTextAsCDATABlock( text ); - if ( cdata != null ) - { - writer.write( cdata ); - return; - } + writer.write( cdata ); + return; } } } - catch ( IOException e ) - { - throw new RuntimeException( e ); - } // if every thing else fails, do it the save way... xmlEncodeTextAsPCDATA( text, false, DEFAULT_QUOTE_CHAR, writer ); } /** * Encodes any text as PCDATA. + * @throws IOException */ - public static String xmlEncodeTextAsPCDATA( String text ) + static String xmlEncodeTextAsPCDATA( String text ) throws IOException { if ( text == null ) { @@ -131,7 +125,7 @@ public static String xmlEncodeTextAsPCDATA( String text ) * @param forAttribute if you want * quotes and apostrophes specially treated for attributes */ - public static String xmlEncodeTextAsPCDATA( String text, boolean forAttribute ) + static String xmlEncodeTextAsPCDATA( String text, boolean forAttribute ) throws IOException { return xmlEncodeTextAsPCDATA( text, forAttribute, DEFAULT_QUOTE_CHAR ); } @@ -142,8 +136,10 @@ public static String xmlEncodeTextAsPCDATA( String text, boolean forAttribute ) * @param forAttribute if you want * quotes and apostrophes specially treated for attributes * @param quoteChar if this is for attributes this char is used to quote the attribute value + * @throws IOException */ - public static String xmlEncodeTextAsPCDATA( String text, boolean forAttribute, char quoteChar ) + static String xmlEncodeTextAsPCDATA( String text, boolean forAttribute, char quoteChar ) + throws IOException { if ( text == null ) { @@ -154,99 +150,91 @@ public static String xmlEncodeTextAsPCDATA( String text, boolean forAttribute, c return writer.toString(); } - public static void xmlEncodeTextAsPCDATA( String text, boolean forAttribute, char quoteChar, Writer n ) + static void xmlEncodeTextAsPCDATA( String text, boolean forAttribute, char quoteChar, Writer n ) + throws IOException { if ( text == null ) { return; } - try + int length = text.length(); + if ( forAttribute ) { - char c; - int length = text.length(); - if ( forAttribute ) - { - n.append( quoteChar ); - } + n.append( quoteChar ); + } - for ( int i = 0; i < length; i++ ) + for ( int i = 0; i < length; i++ ) + { + char c = text.charAt( i ); + switch ( c ) { - c = text.charAt( i ); - switch ( c ) - { - case '&': - n.append( "&" ); - break; - case '<': - n.append( "<" ); - break; - case '>': // FIX for sourceforge bug #802520 ("]]>" needs encoding) - n.append( ">" ); - break; - case '"': - if ( forAttribute ) - { - n.append( """ ); - } - else - { - n.append( c ); - } - break; - case '\'': - if ( forAttribute ) - { - n.append( "'" ); - } - else - { - n.append( c ); - } - break; - case '\r': - if ( forAttribute ) - { - if ( i == ( length - 1 ) || text.charAt( i + 1 ) != '\n' ) - { - n.append( " " ); - } - } - else - { - n.append( c ); - } - // but skip the \r in \r\n - - break; - case '\n': - if ( forAttribute ) + case '&': + n.append( "&" ); + break; + case '<': + n.append( "<" ); + break; + case '>': // FIX for sourceforge bug #802520 ("]]>" needs encoding) + n.append( ">" ); + break; + case '"': + if ( forAttribute ) + { + n.append( """ ); + } + else + { + n.append( c ); + } + break; + case '\'': + if ( forAttribute ) + { + n.append( "'" ); + } + else + { + n.append( c ); + } + break; + case '\r': + if ( forAttribute ) + { + if ( i == ( length - 1 ) || text.charAt( i + 1 ) != '\n' ) { - n.append( " " ); + n.append( " " ); } - break; - - default: + } + else + { n.append( c ); - break; - } - } + } + // but skip the \r in \r\n - if ( forAttribute ) - { - n.append( quoteChar ); + break; + case '\n': + if ( forAttribute ) + { + n.append( " " ); + } + break; + + default: + n.append( c ); + break; } } - catch ( IOException e ) + + if ( forAttribute ) { - throw new RuntimeException( e ); + n.append( quoteChar ); } - } /** * Returns string as CDATA block if possible, otherwise null. */ - public static String xmlEncodeTextAsCDATABlock( String text ) + static String xmlEncodeTextAsCDATABlock( String text ) { if ( text == null ) { @@ -265,7 +253,7 @@ public static String xmlEncodeTextAsCDATABlock( String text ) /** * Checks if this text needs encoding in order to be represented in XML. */ - public static boolean needsEncoding( String text ) + static boolean needsEncoding( String text ) { return needsEncoding( text, false ); } @@ -276,7 +264,7 @@ public static boolean needsEncoding( String text ) * Set checkForAttr if you want to check for storability in * an attribute. */ - public static boolean needsEncoding( String data, boolean checkForAttr ) + static boolean needsEncoding( String data, boolean checkForAttr ) { if ( data == null ) { @@ -295,9 +283,9 @@ public static boolean needsEncoding( String data, boolean checkForAttr ) } /** - * Can this text be stored into a CDATA block? + * Can this text be stored in a CDATA block? */ - public static boolean isCompatibleWithCDATABlock( String text ) + static boolean isCompatibleWithCDATABlock( String text ) { return text != null && ( !text.contains( "]]>" ) ); } @@ -306,7 +294,7 @@ public static boolean isCompatibleWithCDATABlock( String text ) * Make CDATA out of possibly encoded PCDATA.
* E.g. make '&' out of '&amp;' */ - public static String xmlDecodeTextToCDATA( String pcdata ) + static String xmlDecodeTextToCDATA( String pcdata ) { if ( pcdata == null ) { 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 5679512f..b0f470c9 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,9 @@ package org.apache.maven.shared.utils.xml; -import java.io.IOException; -import javax.swing.text.html.HTML; -import java.io.StringWriter; +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.FileOutputStream; /* * Licensed to the Apache Software Foundation (ASF) under one @@ -23,11 +24,18 @@ * under the License. */ +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import javax.swing.text.html.HTML; +import java.io.StringWriter; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; 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 +48,9 @@ */ public class PrettyPrintXmlWriterTest { - StringWriter w; - - PrettyPrintXMLWriter writer; - - @Before - public void before() - throws Exception - { - w = new StringWriter(); - writer = new PrettyPrintXMLWriter( w ); - } - - @After - public void after() - throws Exception - { - writer = null; - w = null; - } - + private StringWriter w = new StringWriter(); + private PrettyPrintXMLWriter writer = new PrettyPrintXMLWriter( w ); + @Test public void testDefaultPrettyPrintXMLWriter() throws IOException { @@ -178,12 +169,12 @@ private void writeXhtmlBody( XMLWriter writer ) throws IOException writer.endElement(); // Tag.BODY } - private String expectedResult( String lineSeparator ) + private static String expectedResult( String lineSeparator ) { return expectedResult( " ", lineSeparator ); } - private String expectedResult( String lineIndenter, String lineSeparator ) + private static String expectedResult( String lineIndenter, String lineSeparator ) { StringBuilder expected = new StringBuilder(); From 3f925b9b57d692e57ff2deebea907c8218726867 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Sat, 30 May 2020 10:31:43 -0400 Subject: [PATCH 4/7] unused imports --- .../maven/shared/utils/xml/PrettyPrintXmlWriterTest.java | 6 ------ 1 file changed, 6 deletions(-) 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 b0f470c9..4145cd04 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 @@ -25,19 +25,13 @@ */ import java.io.IOException; -import java.io.OutputStream; -import java.io.OutputStreamWriter; import javax.swing.text.html.HTML; import java.io.StringWriter; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; import org.apache.maven.shared.utils.Os; import org.apache.maven.shared.utils.StringUtils; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; /** From 2310c55d9bf842cac9cae2103fab7fb7f516b200 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Sat, 30 May 2020 10:34:39 -0400 Subject: [PATCH 5/7] remove deprecated methods --- .../apache/maven/shared/utils/XmlStreamReaderTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/apache/maven/shared/utils/XmlStreamReaderTest.java b/src/test/java/org/apache/maven/shared/utils/XmlStreamReaderTest.java index 3ffb98d3..2e305382 100644 --- a/src/test/java/org/apache/maven/shared/utils/XmlStreamReaderTest.java +++ b/src/test/java/org/apache/maven/shared/utils/XmlStreamReaderTest.java @@ -27,7 +27,8 @@ import junit.framework.ComparisonFailure; import junit.framework.TestCase; -import org.apache.maven.shared.utils.io.IOUtil; + +import org.apache.commons.io.IOUtils; import org.apache.maven.shared.utils.xml.XmlStreamReader; /** @@ -66,8 +67,7 @@ private static String createXmlContent( String text, String encoding ) return xmlDecl + "\n" + text + ""; } - private static void checkXmlContent( String xml, String encoding ) - throws IOException + private static void checkXmlContent( String xml, String encoding ) throws IOException { checkXmlContent( xml, encoding, null ); } @@ -76,7 +76,7 @@ private static void checkXmlContent( String xml, String encoding, byte[] bom ) t { byte[] xmlContent = xml.getBytes( encoding ); InputStream in = new ByteArrayInputStream( xmlContent ); - + if ( bom != null ) { in = new SequenceInputStream( new ByteArrayInputStream( bom ), in ); @@ -84,7 +84,7 @@ private static void checkXmlContent( String xml, String encoding, byte[] bom ) t XmlStreamReader reader = new XmlStreamReader( in ); assertEquals( encoding, reader.getEncoding() ); - String result = IOUtil.toString( reader ); + String result = IOUtils.toString( reader ); assertEquals( xml, result ); } From e395e2aa5321cb7478a56fdbd495956e166dda7c Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Sat, 30 May 2020 10:44:52 -0400 Subject: [PATCH 6/7] revert changes from wrong client --- .../utils/xml/PrettyPrintXMLWriter.java | 124 ++++++++++++++---- 1 file changed, 101 insertions(+), 23 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 796c8bbe..851cffbf 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 @@ -20,13 +20,13 @@ */ import java.io.IOException; +import java.io.PrintWriter; import java.io.Writer; import java.util.ArrayList; import org.apache.maven.shared.utils.Os; /** - * XMLWriter with nice indentation. This class does minimal checking of its input - * and can produce malformed XML. + * XMLWriter with nice indentation. * * @author kama */ @@ -39,7 +39,7 @@ public class PrettyPrintXMLWriter private static final char[] DEFAULT_LINE_INDENT = new char[]{ ' ', ' ' }; - private Writer writer; + private PrintWriter writer; private ArrayList elementStack = new ArrayList(); @@ -61,11 +61,28 @@ public class PrettyPrintXMLWriter /** * @param writer not null - * @param lineIndent can be null, but the normal way is some spaces + * @param lineIndent could be null, but the normal way is some spaces. + */ + public PrettyPrintXMLWriter( PrintWriter writer, String lineIndent ) + { + this( writer, lineIndent, null, null ); + } + + /** + * @param writer not null + * @param lineIndent could be null, but the normal way is some spaces. */ public PrettyPrintXMLWriter( Writer writer, String lineIndent ) { - this( writer, lineIndent.toCharArray(), Os.LINE_SEP.toCharArray(), null, null ); + this( new PrintWriter( writer ), lineIndent ); + } + + /** + * @param writer not null + */ + public PrettyPrintXMLWriter( PrintWriter writer ) + { + this( writer, null, null ); } /** @@ -73,40 +90,75 @@ public PrettyPrintXMLWriter( Writer writer, String lineIndent ) */ public PrettyPrintXMLWriter( Writer writer ) { - this( writer, DEFAULT_LINE_INDENT, Os.LINE_SEP.toCharArray(), null, null ); + this( new PrintWriter( writer ) ); } /** * @param writer not null - * @param lineIndent can be null, but the normal way is some spaces - * @param encoding can be null or invalid - * @param doctype can be null + * @param lineIndent could be null, but the normal way is some spaces. + * @param encoding could be null or invalid. + * @param doctype could be null. */ - public PrettyPrintXMLWriter( Writer writer, String lineIndent, String encoding, String doctype ) + public PrettyPrintXMLWriter( PrintWriter writer, String lineIndent, String encoding, String doctype ) { this( writer, lineIndent.toCharArray(), Os.LINE_SEP.toCharArray(), encoding, doctype ); } /** * @param writer not null - * @param encoding can be null or invalid - * @param doctype can be null + * @param lineIndent could be null, but the normal way is some spaces. + * @param encoding could be null or invalid. + * @param doctype could be null. */ - public PrettyPrintXMLWriter( Writer writer, String encoding, String doctype ) + public PrettyPrintXMLWriter( Writer writer, String lineIndent, String encoding, String doctype ) + { + this( new PrintWriter( writer ), lineIndent, encoding, doctype ); + } + + /** + * @param writer not null + * @param encoding could be null or invalid. + * @param doctype could be null. + */ + public PrettyPrintXMLWriter( PrintWriter writer, String encoding, String doctype ) { this( writer, DEFAULT_LINE_INDENT, Os.LINE_SEP.toCharArray(), encoding, doctype ); } + /** + * @param writer not null + * @param encoding could be null or invalid. + * @param doctype could be null. + */ + public PrettyPrintXMLWriter( Writer writer, String encoding, String doctype ) + { + this( new PrintWriter( writer ), encoding, doctype ); + } + + /** + * @param writer not null + * @param lineIndent could be null, but the normal way is some spaces. + * @param lineSeparator could be null, but the normal way is valid line separator + * @param encoding could be null or the encoding to use. + * @param doctype could be null. + */ + public PrettyPrintXMLWriter( PrintWriter writer, String lineIndent, String lineSeparator, String encoding, + String doctype ) + { + this( writer, lineIndent.toCharArray(), lineSeparator.toCharArray(), encoding, doctype ); + } + /** * @param writer not null - * @param lineIndent can be null, but the normal way is some spaces - * @param lineSeparator can be null, but the normal way is valid line separator - * @param encoding can be null or the encoding to use - * @param doctype can be null + * @param lineIndent could be null, but the normal way is some spaces. + * @param lineSeparator could be null, but the normal way is valid line separator + * @param encoding could be null or the encoding to use. + * @param doctype could be null. */ - private PrettyPrintXMLWriter( Writer writer, char[] lineIndent, char[] lineSeparator, String encoding, + private PrettyPrintXMLWriter( PrintWriter writer, char[] lineIndent, char[] lineSeparator, String encoding, String doctype ) { + super(); this.writer = writer; this.lineIndent = lineIndent; this.lineSeparator = lineSeparator; @@ -114,6 +166,9 @@ private PrettyPrintXMLWriter( Writer writer, char[] lineIndent, char[] lineSepar this.docType = doctype; depth = 0; + + // Fail early with assertions enabled. Issue is in the calling code not having checked for any errors. + assert !writer.checkError() : "Unexpected error state PrintWriter passed to PrettyPrintXMLWriter."; } /** {@inheritDoc} */ @@ -128,6 +183,10 @@ public void addAttribute( String key, String value ) throws IOException writer.write( key ); writer.write( '=' ); XMLEncode.xmlEncodeTextAsPCDATA( value, true, '"', writer ); + if ( writer.checkError() ) + { + throw new IOException( "Failure adding attribute '" + key + "' with value '" + value + "'" ); + } } /** {@inheritDoc} */ @@ -192,6 +251,10 @@ public void startElement( String elementName ) throws IOException writer.write( '<' ); writer.write( elementName ); + if ( writer.checkError() ) + { + throw new IOException( "Failure starting element '" + elementName + "'." ); + } processingElement = true; @@ -208,6 +271,11 @@ public void writeText( String text ) throws IOException XMLEncode.xmlEncodeText( text, writer ); endOnSameLine = true; + + if ( writer.checkError() ) + { + throw new IOException( "Failure writing text." ); + } } /** {@inheritDoc} */ @@ -218,6 +286,11 @@ public void writeMarkup( String markup ) throws IOException completePreviouslyOpenedElement(); writer.write( markup ); + + if ( writer.checkError() ) + { + throw new IOException( "Failure writing markup." ); + } } /** {@inheritDoc} */ @@ -245,14 +318,19 @@ public void endElement() throws IOException } endOnSameLine = false; + + if ( writer.checkError() ) + { + throw new IOException( "Failure ending element." ); + } } /** * Write the documents if not already done. * - * @return true if the document headers have freshly been written + * @return true if the document headers have freshly been written. */ - private boolean ensureDocumentStarted() throws IOException + private boolean ensureDocumentStarted() { if ( !documentStarted ) { @@ -269,7 +347,7 @@ private boolean ensureDocumentStarted() throws IOException return false; } - private void writeDocumentHeader() throws IOException + private void writeDocumentHeader() { writer.write( " Date: Sat, 30 May 2020 10:48:43 -0400 Subject: [PATCH 7/7] revert changes from wrong client --- .../maven/shared/utils/xml/XMLEncode.java | 194 ++++++++++-------- 1 file changed, 103 insertions(+), 91 deletions(-) diff --git a/src/main/java/org/apache/maven/shared/utils/xml/XMLEncode.java b/src/main/java/org/apache/maven/shared/utils/xml/XMLEncode.java index 694352b6..0d6ecd16 100644 --- a/src/main/java/org/apache/maven/shared/utils/xml/XMLEncode.java +++ b/src/main/java/org/apache/maven/shared/utils/xml/XMLEncode.java @@ -39,7 +39,7 @@ final class XMLEncode * Checks if this text purely consists of the white space characters * ' ', TAB, NEWLINE. */ - static boolean isWhiteSpace( String text ) + public static boolean isWhiteSpace( String text ) { for ( int i = 0; i < text.length(); i++ ) { @@ -55,7 +55,7 @@ static boolean isWhiteSpace( String text ) /** * Makes any text fit into XML attributes. */ - static String xmlEncodeTextForAttribute( String text, char quoteChar ) throws IOException + public static String xmlEncodeTextForAttribute( String text, char quoteChar ) { if ( text == null ) { @@ -67,7 +67,7 @@ static String xmlEncodeTextForAttribute( String text, char quoteChar ) throws IO /** * Encodes text as XML in the most suitable way, either CDATA block or PCDATA. */ - static String xmlEncodeText( String text ) throws IOException + public static String xmlEncodeText( String text ) { if ( text == null ) { @@ -78,39 +78,45 @@ static String xmlEncodeText( String text ) throws IOException return writer.toString(); } - static void xmlEncodeText( String text, Writer writer ) throws IOException + public static void xmlEncodeText( String text, Writer writer ) { if ( text == null ) { return; } - if ( !needsEncoding( text ) ) - { - writer.write( text ); - return; - } - else + try { - // only encode as cdata if is is longer than CDATA block overhead: - if ( text.length() > CDATA_BLOCK_THRESHOLD_LENGTH ) + if ( !needsEncoding( text ) ) + { + writer.write( text ); + return; + } + else { - String cdata = xmlEncodeTextAsCDATABlock( text ); - if ( cdata != null ) + // only encode as cdata if is is longer than CDATA block overhead: + if ( text.length() > CDATA_BLOCK_THRESHOLD_LENGTH ) { - writer.write( cdata ); - return; + String cdata = xmlEncodeTextAsCDATABlock( text ); + if ( cdata != null ) + { + writer.write( cdata ); + return; + } } } } + catch ( IOException e ) + { + throw new RuntimeException( e ); + } // if every thing else fails, do it the save way... xmlEncodeTextAsPCDATA( text, false, DEFAULT_QUOTE_CHAR, writer ); } /** * Encodes any text as PCDATA. - * @throws IOException */ - static String xmlEncodeTextAsPCDATA( String text ) throws IOException + public static String xmlEncodeTextAsPCDATA( String text ) { if ( text == null ) { @@ -125,7 +131,7 @@ static String xmlEncodeTextAsPCDATA( String text ) throws IOException * @param forAttribute if you want * quotes and apostrophes specially treated for attributes */ - static String xmlEncodeTextAsPCDATA( String text, boolean forAttribute ) throws IOException + public static String xmlEncodeTextAsPCDATA( String text, boolean forAttribute ) { return xmlEncodeTextAsPCDATA( text, forAttribute, DEFAULT_QUOTE_CHAR ); } @@ -136,10 +142,8 @@ static String xmlEncodeTextAsPCDATA( String text, boolean forAttribute ) throws * @param forAttribute if you want * quotes and apostrophes specially treated for attributes * @param quoteChar if this is for attributes this char is used to quote the attribute value - * @throws IOException */ - static String xmlEncodeTextAsPCDATA( String text, boolean forAttribute, char quoteChar ) - throws IOException + public static String xmlEncodeTextAsPCDATA( String text, boolean forAttribute, char quoteChar ) { if ( text == null ) { @@ -150,91 +154,99 @@ static String xmlEncodeTextAsPCDATA( String text, boolean forAttribute, char quo return writer.toString(); } - static void xmlEncodeTextAsPCDATA( String text, boolean forAttribute, char quoteChar, Writer n ) - throws IOException + public static void xmlEncodeTextAsPCDATA( String text, boolean forAttribute, char quoteChar, Writer n ) { if ( text == null ) { return; } - int length = text.length(); - if ( forAttribute ) + try { - n.append( quoteChar ); - } + char c; + int length = text.length(); + if ( forAttribute ) + { + n.append( quoteChar ); + } - for ( int i = 0; i < length; i++ ) - { - char c = text.charAt( i ); - switch ( c ) + for ( int i = 0; i < length; i++ ) { - case '&': - n.append( "&" ); - break; - case '<': - n.append( "<" ); - break; - case '>': // FIX for sourceforge bug #802520 ("]]>" needs encoding) - n.append( ">" ); - break; - case '"': - if ( forAttribute ) - { - n.append( """ ); - } - else - { - n.append( c ); - } - break; - case '\'': - if ( forAttribute ) - { - n.append( "'" ); - } - else - { - n.append( c ); - } - break; - case '\r': - if ( forAttribute ) - { - if ( i == ( length - 1 ) || text.charAt( i + 1 ) != '\n' ) + c = text.charAt( i ); + switch ( c ) + { + case '&': + n.append( "&" ); + break; + case '<': + n.append( "<" ); + break; + case '>': // FIX for sourceforge bug #802520 ("]]>" needs encoding) + n.append( ">" ); + break; + case '"': + if ( forAttribute ) { - n.append( " " ); + n.append( """ ); } - } - else - { - n.append( c ); - } - // but skip the \r in \r\n + else + { + n.append( c ); + } + break; + case '\'': + if ( forAttribute ) + { + n.append( "'" ); + } + else + { + n.append( c ); + } + break; + case '\r': + if ( forAttribute ) + { + if ( i == ( length - 1 ) || text.charAt( i + 1 ) != '\n' ) + { + n.append( " " ); + } + } + else + { + n.append( c ); + } + // but skip the \r in \r\n - break; - case '\n': - if ( forAttribute ) - { - n.append( " " ); - } - break; + break; + case '\n': + if ( forAttribute ) + { + n.append( " " ); + } + break; - default: - n.append( c ); - break; + default: + n.append( c ); + break; + } } - } - if ( forAttribute ) + if ( forAttribute ) + { + n.append( quoteChar ); + } + } + catch ( IOException e ) { - n.append( quoteChar ); + throw new RuntimeException( e ); } + } /** * Returns string as CDATA block if possible, otherwise null. */ - static String xmlEncodeTextAsCDATABlock( String text ) + public static String xmlEncodeTextAsCDATABlock( String text ) { if ( text == null ) { @@ -253,7 +265,7 @@ static String xmlEncodeTextAsCDATABlock( String text ) /** * Checks if this text needs encoding in order to be represented in XML. */ - static boolean needsEncoding( String text ) + public static boolean needsEncoding( String text ) { return needsEncoding( text, false ); } @@ -264,7 +276,7 @@ static boolean needsEncoding( String text ) * Set checkForAttr if you want to check for storability in * an attribute. */ - static boolean needsEncoding( String data, boolean checkForAttr ) + public static boolean needsEncoding( String data, boolean checkForAttr ) { if ( data == null ) { @@ -283,9 +295,9 @@ static boolean needsEncoding( String data, boolean checkForAttr ) } /** - * Can this text be stored in a CDATA block? + * Can this text be stored into a CDATA block? */ - static boolean isCompatibleWithCDATABlock( String text ) + public static boolean isCompatibleWithCDATABlock( String text ) { return text != null && ( !text.contains( "]]>" ) ); } @@ -294,7 +306,7 @@ static boolean isCompatibleWithCDATABlock( String text ) * Make CDATA out of possibly encoded PCDATA.
* E.g. make '&' out of '&amp;' */ - static String xmlDecodeTextToCDATA( String pcdata ) + public static String xmlDecodeTextToCDATA( String pcdata ) { if ( pcdata == null ) {