This is an automated email from the ASF dual-hosted git repository. slachiewicz pushed a commit to branch code-coverage in repository https://gitbox.apache.org/repos/asf/maven-doxia-converter.git
commit ea2afd691e7bfc14b14d7a5633a5034f69268e37 Author: Sylwester Lachiewicz <slachiew...@apache.org> AuthorDate: Sat Nov 2 21:13:25 2019 +0100 Minor code update, Java 7 and fix ide warning, formating --- .../org/apache/maven/doxia/DefaultConverter.java | 49 ++++++-------- .../org/apache/maven/doxia/cli/CLIManager.java | 51 ++++++--------- .../org/apache/maven/doxia/cli/ConverterCli.java | 15 ----- .../maven/doxia/wrapper/AbstractFileWrapper.java | 76 ++++++---------------- .../maven/doxia/wrapper/AbstractWrapper.java | 25 +++---- .../maven/doxia/wrapper/OutputFileWrapper.java | 7 +- .../maven/doxia/wrapper/OutputStreamWrapper.java | 4 +- .../java/org/apache/maven/doxia/ConverterTest.java | 13 ++-- 8 files changed, 77 insertions(+), 163 deletions(-) diff --git a/src/main/java/org/apache/maven/doxia/DefaultConverter.java b/src/main/java/org/apache/maven/doxia/DefaultConverter.java index 0de3513..86ddad4 100644 --- a/src/main/java/org/apache/maven/doxia/DefaultConverter.java +++ b/src/main/java/org/apache/maven/doxia/DefaultConverter.java @@ -55,7 +55,6 @@ import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.PlexusContainerException; import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import org.codehaus.plexus.util.FileUtils; -import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.ReaderFactory; import org.codehaus.plexus.util.SelectorUtils; import org.codehaus.plexus.util.StringUtils; @@ -69,6 +68,8 @@ import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import com.ibm.icu.text.CharsetDetector; import com.ibm.icu.text.CharsetMatch; +import static java.lang.String.format; + /** * Default implementation of <code>Converter</code> * @@ -194,8 +195,7 @@ public class DefaultConverter List<File> files; try { - files = - FileUtils.getFiles( input.getFile(), "**/*." + input.getFormat(), + files = FileUtils.getFiles( input.getFile(), "**/*." + input.getFormat(), StringUtils.join( FileUtils.getDefaultExcludes(), ", " ) ); } catch ( IOException e ) @@ -466,17 +466,16 @@ public class DefaultConverter private void parse( Parser parser, Reader reader, Sink sink ) throws ConverterException { - try + try ( Reader r = reader ) { - parser.parse( reader, sink ); + parser.parse( r, sink ); } - catch ( ParseException e ) + catch ( ParseException | IOException e ) { throw new ConverterException( "ParseException: " + e.getMessage(), e ); } finally { - IOUtil.close( reader ); sink.flush(); sink.close(); } @@ -534,39 +533,31 @@ public class DefaultConverter throw new IllegalArgumentException( "The file '" + f.getAbsolutePath() + "' is not a file, could not detect encoding." ); } - - Reader reader = null; - InputStream is = null; try { if ( XmlUtil.isXml( f ) ) { - reader = new XmlStreamReader( f ); - return ( (XmlStreamReader) reader ).getEncoding(); + try ( XmlStreamReader reader = new XmlStreamReader( f ) ) + { + return reader.getEncoding(); + } } - is = new BufferedInputStream( new FileInputStream( f ) ); - CharsetDetector detector = new CharsetDetector(); - detector.setText( is ); - CharsetMatch match = detector.detect(); + try ( InputStream is = new BufferedInputStream( new FileInputStream( f ) ) ) + { + CharsetDetector detector = new CharsetDetector(); + detector.setText( is ); + CharsetMatch match = detector.detect(); - return match.getName().toUpperCase( Locale.ENGLISH ); + return match.getName().toUpperCase( Locale.ENGLISH ); + } } catch ( IOException e ) { // nop } - finally - { - IOUtil.close( reader ); - IOUtil.close( is ); - } - - StringBuilder msg = new StringBuilder(); - msg.append( "Could not detect the encoding for file: " ); - msg.append( f.getAbsolutePath() ); - msg.append( "\n Specify explicitly the encoding." ); - throw new UnsupportedOperationException( msg.toString() ); + throw new UnsupportedOperationException( format( "Could not detect the encoding for file: %s\n" + + "Specify explicitly the encoding.", f.getAbsolutePath() ) ); } /** @@ -633,7 +624,7 @@ public class DefaultConverter } throw new UnsupportedOperationException( - String.format( "Could not detect the Doxia format for file: %s\n Specify explicitly the Doxia format.", + format( "Could not detect the Doxia format for file: %s\n Specify explicitly the Doxia format.", f.getAbsolutePath() ) ); } diff --git a/src/main/java/org/apache/maven/doxia/cli/CLIManager.java b/src/main/java/org/apache/maven/doxia/cli/CLIManager.java index 5e513bc..a363003 100644 --- a/src/main/java/org/apache/maven/doxia/cli/CLIManager.java +++ b/src/main/java/org/apache/maven/doxia/cli/CLIManager.java @@ -30,10 +30,11 @@ import org.apache.commons.cli.OptionBuilder; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.apache.maven.doxia.DefaultConverter; -import org.codehaus.plexus.util.StringUtils; import com.ibm.icu.text.CharsetDetector; +import static org.codehaus.plexus.util.StringUtils.join; + /** * Manager for Doxia converter CLI options. * @@ -82,58 +83,44 @@ class CLIManager OptionBuilder.withLongOpt( "help" ); OptionBuilder.withDescription( "Display help information." ); - OPTIONS.addOption( OptionBuilder - .create( HELP ) ); + OPTIONS.addOption( OptionBuilder.create( HELP ) ); OptionBuilder.withLongOpt( "version" ); OptionBuilder.withDescription( "Display version information." ); - OPTIONS.addOption( OptionBuilder - .create( VERSION ) ); + OPTIONS.addOption( OptionBuilder.create( VERSION ) ); OptionBuilder.withLongOpt( "input" ); OptionBuilder.withDescription( "Input file or directory." ); - OptionBuilder - .hasArg(); + OptionBuilder.hasArg(); OPTIONS.addOption( OptionBuilder.create( IN ) ); OptionBuilder.withLongOpt( "output" ); OptionBuilder.withDescription( "Output file or directory." ); - OptionBuilder - .hasArg(); + OptionBuilder.hasArg(); OPTIONS.addOption( OptionBuilder.create( OUT ) ); OptionBuilder.withDescription( "From format. If not specified, try to autodetect it." ); - OptionBuilder - .hasArg(); + OptionBuilder.hasArg(); OPTIONS.addOption( OptionBuilder.create( FROM ) ); OptionBuilder.withDescription( "To format." ); OptionBuilder.hasArg(); OPTIONS.addOption( OptionBuilder.create( TO ) ); OptionBuilder.withLongOpt( "inputEncoding" ); - OptionBuilder - .withDescription( "Input file encoding. " - + "If not specified, try to autodetect it." ); - OptionBuilder - .hasArg(); + OptionBuilder.withDescription( "Input file encoding. If not specified, try to autodetect it." ); + OptionBuilder.hasArg(); OPTIONS.addOption( OptionBuilder.create( INENCODING ) ); OptionBuilder.withLongOpt( "format" ); - OptionBuilder - .withDescription( "Format the output (actually only xml based outputs) " + OptionBuilder.withDescription( "Format the output (actually only xml based outputs) " + " to be human readable." ); - OPTIONS.addOption( OptionBuilder - .create( FORMAT ) ); + OPTIONS.addOption( OptionBuilder.create( FORMAT ) ); OptionBuilder.withLongOpt( "outputEncoding" ); - OptionBuilder - .withDescription( "Output file encoding. If not specified, use the " + OptionBuilder.withDescription( "Output file encoding. If not specified, use the " + "input encoding (or autodetected)." ); OptionBuilder.hasArg(); - OPTIONS.addOption( OptionBuilder - .create( OUTENCODING ) ); + OPTIONS.addOption( OptionBuilder.create( OUTENCODING ) ); OptionBuilder.withLongOpt( "debug" ); - OptionBuilder - .withDescription( "Produce execution debug output." ); + OptionBuilder.withDescription( "Produce execution debug output." ); OPTIONS.addOption( OptionBuilder.create( DEBUG ) ); OptionBuilder.withLongOpt( "errors" ); - OptionBuilder - .withDescription( "Produce execution error messages." ); + OptionBuilder.withDescription( "Produce execution error messages." ); OPTIONS.addOption( OptionBuilder.create( ERRORS ) ); } @@ -174,14 +161,14 @@ class CLIManager private static String getSupportedFormat() { - return "\nSupported Formats:\n from: " + StringUtils.join( DefaultConverter.SUPPORTED_FROM_FORMAT, ", " ) - + " or autodetect" + "\n out: " + StringUtils.join( DefaultConverter.SUPPORTED_TO_FORMAT, ", " ) + return "\nSupported Formats:\n from: " + join( DefaultConverter.SUPPORTED_FROM_FORMAT, ", " ) + + " or autodetect" + "\n out: " + join( DefaultConverter.SUPPORTED_TO_FORMAT, ", " ) + "\n"; } private static String getSupportedEncoding() { - return "\nSupported Encoding:\n " + StringUtils.join( CharsetDetector.getAllDetectableCharsets(), ", " ); + return "\nSupported Encoding:\n " + join( CharsetDetector.getAllDetectableCharsets(), ", " ); } private String[] cleanArgs( String[] args ) @@ -268,7 +255,7 @@ class CLIManager } int cleanedSz = cleaned.size(); - String[] cleanArgs = null; + String[] cleanArgs; if ( cleanedSz == 0 ) { diff --git a/src/main/java/org/apache/maven/doxia/cli/ConverterCli.java b/src/main/java/org/apache/maven/doxia/cli/ConverterCli.java index 239ba7c..01e9fed 100644 --- a/src/main/java/org/apache/maven/doxia/cli/ConverterCli.java +++ b/src/main/java/org/apache/maven/doxia/cli/ConverterCli.java @@ -84,21 +84,6 @@ public class ConverterCli return 1; } - if ( "1.4".compareTo( System.getProperty( "java.specification.version" ) ) > 0 ) - { - System.err.println( "Sorry, but JDK 1.4 or above is required to execute Doxia. You appear to be using " - + "Java:" ); - System.err.println( "java version \"" + System.getProperty( "java.version", "<unknown java version>" ) - + "\"" ); - System.err.println( System.getProperty( "java.runtime.name", "<unknown runtime name>" ) + " (build " - + System.getProperty( "java.runtime.version", "<unknown runtime version>" ) + ")" ); - System.err.println( System.getProperty( "java.vm.name", "<unknown vm name>" ) + " (build " - + System.getProperty( "java.vm.version", "<unknown vm version>" ) + ", " - + System.getProperty( "java.vm.info", "<unknown vm info>" ) + ")" ); - - return 1; - } - if ( commandLine.hasOption( CLIManager.HELP ) ) { CLIManager.displayHelp(); diff --git a/src/main/java/org/apache/maven/doxia/wrapper/AbstractFileWrapper.java b/src/main/java/org/apache/maven/doxia/wrapper/AbstractFileWrapper.java index b84ce1f..814037a 100644 --- a/src/main/java/org/apache/maven/doxia/wrapper/AbstractFileWrapper.java +++ b/src/main/java/org/apache/maven/doxia/wrapper/AbstractFileWrapper.java @@ -19,17 +19,18 @@ package org.apache.maven.doxia.wrapper; * under the License. */ -import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.OutputStream; -import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; +import java.util.Objects; -import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.StringUtils; import com.ibm.icu.text.CharsetDetector; +import static org.codehaus.plexus.util.StringUtils.isEmpty; +import static org.codehaus.plexus.util.StringUtils.isNotEmpty; + /** * Abstract File wrapper for Doxia converter. * @@ -58,7 +59,7 @@ abstract class AbstractFileWrapper { super( format, supportedFormat ); - if ( StringUtils.isEmpty( absolutePath ) ) + if ( isEmpty( absolutePath ) ) { throw new IllegalArgumentException( "absolutePath is required" ); } @@ -70,15 +71,15 @@ abstract class AbstractFileWrapper } this.file = filetoset; - if ( StringUtils.isNotEmpty( encoding ) && !encoding.equalsIgnoreCase( encoding ) - && !validateEncoding( encoding ) ) + if ( isNotEmpty( encoding ) && !encoding.equalsIgnoreCase( encoding ) + && !Charset.isSupported( encoding ) ) { StringBuilder msg = new StringBuilder(); msg.append( "The encoding '" + encoding + "' is not a valid one. The supported charsets are: " ); msg.append( StringUtils.join( CharsetDetector.getAllDetectableCharsets(), ", " ) ); throw new UnsupportedEncodingException( msg.toString() ); } - this.encoding = ( StringUtils.isNotEmpty( encoding ) ? encoding : AUTO_ENCODING ); + this.encoding = ( isNotEmpty( encoding ) ? encoding : AUTO_ENCODING ); } /** @@ -113,74 +114,35 @@ abstract class AbstractFileWrapper this.encoding = encoding; } - /** - * Validate if a charset is supported on this platform. - * - * @param charsetName the charsetName to be checked. - * @return <code>true</code> if the charset is supported by the JVM, <code>false</code> otherwise. - */ - static boolean validateEncoding( String charsetName ) + @Override + public boolean equals( Object o ) { - if ( StringUtils.isEmpty( charsetName ) ) - { - return false; - } - - OutputStream ost = new ByteArrayOutputStream(); - OutputStreamWriter osw = null; - try + if ( this == o ) { - osw = new OutputStreamWriter( ost, charsetName ); + return true; } - catch ( UnsupportedEncodingException exc ) + if ( o == null || getClass() != o.getClass() ) { return false; } - finally - { - IOUtil.close( osw ); - } - return true; - } - - /** {@inheritDoc} */ - @Override - public boolean equals( Object other ) - { - if ( this == other ) - { - return true; - } - - if ( !( other instanceof AbstractFileWrapper ) ) + if ( !super.equals( o ) ) { return false; } - - AbstractFileWrapper that = (AbstractFileWrapper) other; - boolean result = true; - result = result && super.equals( other ); - result = result && ( getFile() == null ? that.getFile() == null : getFile().equals( that.getFile() ) ); - return result; + AbstractFileWrapper that = (AbstractFileWrapper) o; + return Objects.equals( getFile(), that.getFile() ); } - /** {@inheritDoc} */ @Override public int hashCode() { - final int result = super.hashCode(); - final int hash = 37; - - return hash * result + ( getFile() != null ? getFile().hashCode() : 0 ); + return Objects.hash( super.hashCode(), getFile() ); } /** {@inheritDoc} */ @Override public java.lang.String toString() { - StringBuilder buf = new StringBuilder( super.toString() + "\n" ); - buf.append( "file= '" ); - buf.append( getFile() + "'" ); - return buf.toString(); + return super.toString() + "\n" + "file= '" + getFile() + "'"; } } diff --git a/src/main/java/org/apache/maven/doxia/wrapper/AbstractWrapper.java b/src/main/java/org/apache/maven/doxia/wrapper/AbstractWrapper.java index 807325a..f6e759a 100644 --- a/src/main/java/org/apache/maven/doxia/wrapper/AbstractWrapper.java +++ b/src/main/java/org/apache/maven/doxia/wrapper/AbstractWrapper.java @@ -20,8 +20,9 @@ package org.apache.maven.doxia.wrapper; */ import java.io.Serializable; +import java.util.Objects; -import org.codehaus.plexus.util.StringUtils; +import static org.codehaus.plexus.util.StringUtils.isNotEmpty; /** * Abstract wrapper for Doxia converter. @@ -44,7 +45,7 @@ abstract class AbstractWrapper */ AbstractWrapper( String format, String[] supportedFormat ) { - this.format = ( StringUtils.isNotEmpty( format ) ? format : AUTO_FORMAT ); + this.format = ( isNotEmpty( format ) ? format : AUTO_FORMAT ); if ( supportedFormat == null ) { throw new IllegalArgumentException( "supportedFormat is required" ); @@ -92,36 +93,26 @@ abstract class AbstractWrapper { return true; } - - if ( !( other instanceof AbstractWrapper ) ) + if ( other == null || getClass() != other.getClass() ) { return false; } AbstractWrapper that = (AbstractWrapper) other; - boolean result = true; - result = - result && ( getFormat() == null ? that.getFormat() == null : getFormat().equals( that.getFormat() ) ); - return result; + return Objects.equals( getFormat(), that.getFormat() ); } /** {@inheritDoc} */ @Override public int hashCode() { - final int result = 17; - final int hash = 37; - - return hash * result + ( format != null ? format.hashCode() : 0 ); + return Objects.hash( getFormat() ); } /** {@inheritDoc} */ @Override - public java.lang.String toString() + public String toString() { - StringBuilder buf = new StringBuilder(); - buf.append( "format = '" ); - buf.append( getFormat() + "'" ); - return buf.toString(); + return "format = '" + getFormat() + "'"; } } diff --git a/src/main/java/org/apache/maven/doxia/wrapper/OutputFileWrapper.java b/src/main/java/org/apache/maven/doxia/wrapper/OutputFileWrapper.java index 929ea8b..a90a1e1 100644 --- a/src/main/java/org/apache/maven/doxia/wrapper/OutputFileWrapper.java +++ b/src/main/java/org/apache/maven/doxia/wrapper/OutputFileWrapper.java @@ -21,9 +21,10 @@ package org.apache.maven.doxia.wrapper; import java.io.UnsupportedEncodingException; -import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.WriterFactory; +import static org.codehaus.plexus.util.StringUtils.isEmpty; + /** * Wrapper for an output file. * @@ -38,7 +39,7 @@ public class OutputFileWrapper /** * Private constructor. * - * @param file not null + * @param absolutePath not null * @param format not null * @param charsetName could be null * @param supportedFormat not null. @@ -81,7 +82,7 @@ public class OutputFileWrapper String[] supportedFormat ) throws UnsupportedEncodingException { - if ( StringUtils.isEmpty( format ) ) + if ( isEmpty( format ) ) { throw new IllegalArgumentException( "output format is required" ); } diff --git a/src/main/java/org/apache/maven/doxia/wrapper/OutputStreamWrapper.java b/src/main/java/org/apache/maven/doxia/wrapper/OutputStreamWrapper.java index 77cfbea..0ae064c 100644 --- a/src/main/java/org/apache/maven/doxia/wrapper/OutputStreamWrapper.java +++ b/src/main/java/org/apache/maven/doxia/wrapper/OutputStreamWrapper.java @@ -21,7 +21,7 @@ package org.apache.maven.doxia.wrapper; import java.io.OutputStream; -import org.codehaus.plexus.util.StringUtils; +import static org.codehaus.plexus.util.StringUtils.isEmpty; /** * Wrapper for an output stream. @@ -88,7 +88,7 @@ public class OutputStreamWrapper { throw new IllegalArgumentException( "output writer is required" ); } - if ( StringUtils.isEmpty( format ) ) + if ( isEmpty( format ) ) { throw new IllegalArgumentException( "output format is required" ); } diff --git a/src/test/java/org/apache/maven/doxia/ConverterTest.java b/src/test/java/org/apache/maven/doxia/ConverterTest.java index 1cf073d..08f6830 100644 --- a/src/test/java/org/apache/maven/doxia/ConverterTest.java +++ b/src/test/java/org/apache/maven/doxia/ConverterTest.java @@ -292,7 +292,7 @@ public class ConverterTest converter.setFormatOutput( formatOutput ); converter.convert( input, output ); - assertFalse( true ); + fail(); } catch ( UnsupportedFormatException e ) { @@ -507,7 +507,6 @@ public class ConverterTest throws Exception { String in = getBasedir() + "/src/test/resources/unit/xdoc/test.xml"; - String from = null; String out = getBasedir() + "/target/unit/writer/apt/test.xdoc.apt"; String to = "xhtml"; @@ -520,7 +519,7 @@ public class ConverterTest StringWriter writer = new StringWriter(); InputFileWrapper input = - InputFileWrapper.valueOf( inFile.getAbsolutePath(), from, converter.getInputFormats() ); + InputFileWrapper.valueOf( inFile.getAbsolutePath(), null, converter.getInputFormats() ); OutputFileWrapper output = OutputFileWrapper.valueOf( outFile.getAbsolutePath(), to, converter.getOutputFormats() ); @@ -534,7 +533,6 @@ public class ConverterTest } in = getBasedir() + "/src/test/resources/unit/apt/test.apt"; - from = null; out = getBasedir() + "/target/unit/writer/apt/test.apt.xhtml"; to = "xhtml"; @@ -547,7 +545,7 @@ public class ConverterTest StringWriter writer = new StringWriter(); InputFileWrapper input = - InputFileWrapper.valueOf( inFile.getAbsolutePath(), from, converter.getInputFormats() ); + InputFileWrapper.valueOf( inFile.getAbsolutePath(), null, converter.getInputFormats() ); OutputFileWrapper output = OutputFileWrapper.valueOf( outFile.getAbsolutePath(), to, converter.getOutputFormats() ); @@ -561,7 +559,6 @@ public class ConverterTest } in = getBasedir() + "/src/test/resources/unit/apt/test.unknown"; - from = null; out = getBasedir() + "/target/unit/writer/apt/test.apt.xhtml"; to = "xhtml"; @@ -572,14 +569,14 @@ public class ConverterTest try (FileWriter fw = new FileWriter( outFile )) { InputFileWrapper input = - InputFileWrapper.valueOf( inFile.getAbsolutePath(), from, converter.getInputFormats() ); + InputFileWrapper.valueOf( inFile.getAbsolutePath(), null, converter.getInputFormats() ); OutputFileWrapper output = OutputFileWrapper.valueOf( outFile.getAbsolutePath(), to, converter.getOutputFormats() ); converter.setFormatOutput( formatOutput ); converter.convert( input, output ); - assertFalse( true ); + fail(); } catch ( UnsupportedOperationException e ) {