diff --git a/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/RepositoryCleaner.java b/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/RepositoryCleaner.java index 02562ea895..5feee908e2 100644 --- a/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/RepositoryCleaner.java +++ b/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/RepositoryCleaner.java @@ -23,6 +23,7 @@ import org.apache.maven.tools.repoclean.index.ArtifactIndexer; import org.apache.maven.tools.repoclean.phase.DiscoveryPhase; import org.apache.maven.tools.repoclean.phase.RewritePhase; import org.apache.maven.tools.repoclean.report.FileReporter; +import org.apache.maven.tools.repoclean.report.Reporter; import org.codehaus.plexus.PlexusConstants; import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.context.Context; @@ -78,7 +79,7 @@ public class RepositoryCleaner { Logger logger = getLogger(); - FileReporter repoReporter = null; + Reporter repoReporter = null; try { repoReporter = new FileReporter( reportsBase, "repository.report.txt", diff --git a/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/report/AbstractReporter.java b/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/report/AbstractReporter.java new file mode 100644 index 0000000000..353a34bb05 --- /dev/null +++ b/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/report/AbstractReporter.java @@ -0,0 +1,125 @@ +package org.apache.maven.tools.repoclean.report; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed 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.PrintWriter; +import java.io.StringWriter; + +/** + * Base implementation of reporter. + * + * @author Brett Porter + * @version $Id$ + */ +public abstract class AbstractReporter + implements Reporter +{ + protected static final String WARN_LEVEL = "[WARNING]"; + + protected static final String ERROR_LEVEL = "[ERROR]"; + + protected boolean hasError; + + protected boolean hasWarning; + + protected final boolean warningsEnabled; + + protected AbstractReporter( boolean warningsEnabled ) + { + this.warningsEnabled = warningsEnabled; + } + + public boolean hasWarning() + { + return hasWarning; + } + + public boolean hasError() + { + return hasError; + } + + protected String getSourceLine() + { + NullPointerException npe = new NullPointerException(); + + StackTraceElement element = npe.getStackTrace()[2]; + + return "Reported from: (" + element.getClassName() + "." + element.getMethodName() + "(..):" + + element.getLineNumber() + ")\n"; + } + + protected String format( String level, String source, String message ) + { + return format( level, source, message, null ); + } + + protected String format( String level, String source, String message, Throwable error ) + { + StringBuffer buffer = new StringBuffer(); + buffer.append( level ); + buffer.append( " " ); + buffer.append( source ); + buffer.append( " " ); + buffer.append( message ); + if ( error != null ) + { + buffer.append( formatThrowable( error ) ); + } + return buffer.toString(); + } + + private String formatThrowable( Throwable throwable ) + { + StringWriter sWriter = new StringWriter(); + PrintWriter pWriter = new PrintWriter( sWriter ); + + throwable.printStackTrace( pWriter ); + + return sWriter.toString(); + } + + protected abstract void write( String message ) + throws ReportWriteException; + + public void warn( String message ) + throws ReportWriteException + { + if ( warningsEnabled ) + { + hasWarning = true; + String source = getSourceLine(); + write( format( WARN_LEVEL, source, message ) ); + } + } + + public void error( String message, Throwable error ) + throws ReportWriteException + { + hasError = true; + String source = getSourceLine(); + write( format( ERROR_LEVEL, source, message, error ) ); + } + + public void error( String message ) + throws ReportWriteException + { + hasError = true; + String source = getSourceLine(); + write( format( ERROR_LEVEL, source, message ) ); + } +} diff --git a/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/report/FileReporter.java b/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/report/FileReporter.java index d42b2b523c..f21903d9a0 100644 --- a/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/report/FileReporter.java +++ b/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/report/FileReporter.java @@ -21,39 +21,21 @@ import org.codehaus.plexus.util.IOUtil; import java.io.File; import java.io.FileWriter; import java.io.IOException; -import java.io.PrintWriter; -import java.io.StringWriter; import java.io.Writer; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; /** * @author jdcasey */ public class FileReporter - implements Reporter + extends AbstractReporter { - - private static final String WARN_LEVEL = "[WARNING] "; - - private static final String ERROR_LEVEL = "[ERROR] "; - private File reportsFile; - private List messages = new ArrayList(); - - private boolean hasError = false; - - private boolean hasWarning = false; - private Writer writer; - private final boolean warningsEnabled; - public FileReporter( File reportsBase, String reportPath, boolean warningsEnabled ) { - this.warningsEnabled = warningsEnabled; + super( warningsEnabled ); this.reportsFile = new File( reportsBase, reportPath ); @@ -87,7 +69,7 @@ public class FileReporter IOUtil.close( writer ); } - private void write( Object message ) + protected void write( String message ) throws ReportWriteException { try @@ -97,14 +79,7 @@ public class FileReporter open(); } - if ( message instanceof List ) - { - writer.write( format( (List) message ).toString() ); - } - else - { - writer.write( String.valueOf( message ) ); - } + writer.write( message ); writer.write( '\n' ); @@ -116,97 +91,4 @@ public class FileReporter } } - public boolean hasWarning() - { - return hasWarning; - } - - public boolean hasError() - { - return hasError; - } - - public void warn( String message ) - throws ReportWriteException - { - if ( warningsEnabled ) - { - hasWarning = true; - String source = getSourceLine(); - write( new AppendingList( 3 ).append( WARN_LEVEL ).append( source ).append( message ) ); - } - } - - public void error( String message, Throwable error ) - throws ReportWriteException - { - hasError = true; - String source = getSourceLine(); - write( new AppendingList( 4 ).append( ERROR_LEVEL ).append( source ).append( message ).append( error ) ); - } - - public void error( String message ) - throws ReportWriteException - { - hasError = true; - String source = getSourceLine(); - write( new AppendingList( 3 ).append( ERROR_LEVEL ).append( source ).append( message ) ); - } - - private String getSourceLine() - { - NullPointerException npe = new NullPointerException(); - - StackTraceElement element = npe.getStackTrace()[2]; - - return " Reported from: (" + element.getClassName() + "." + element.getMethodName() + "(..):" + - element.getLineNumber() + ")\n"; - } - - private CharSequence format( List messageParts ) - { - StringBuffer buffer = new StringBuffer(); - for ( Iterator it = messageParts.iterator(); it.hasNext(); ) - { - Object part = it.next(); - if ( part instanceof Throwable ) - { - part = formatThrowable( (Throwable) part ); - } - - buffer.append( part ); - } - - return buffer; - } - - private String formatThrowable( Throwable throwable ) - { - StringWriter sWriter = new StringWriter(); - PrintWriter pWriter = new PrintWriter( sWriter ); - - throwable.printStackTrace( pWriter ); - - return sWriter.toString(); - } - - private static class AppendingList - extends ArrayList - { - public AppendingList() - { - } - - public AppendingList( int size ) - { - super( size ); - } - - public AppendingList append( Object item ) - { - super.add( item ); - return this; - } - } - } \ No newline at end of file