From 419a1d4627ae59080b767d403e72e02ed2b2be7c Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Fri, 16 Apr 2010 13:18:48 +0000 Subject: [PATCH] o Refactored code git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@934861 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/maven/DefaultMaven.java | 4 +- .../exception/DefaultExceptionHandler.java | 13 +--- .../project/ProjectBuildingException.java | 3 +- .../model/building/DefaultModelProblem.java | 38 +----------- .../building/ModelBuildingException.java | 2 +- .../maven/model/building/ModelProblem.java | 9 --- .../model/building/ModelProblemUtils.java | 61 +++++++++++++++++-- 7 files changed, 64 insertions(+), 66 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java index 758c1bdf47..c19af9e2e6 100644 --- a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java +++ b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java @@ -40,6 +40,7 @@ import org.apache.maven.execution.ProjectDependencyGraph; import org.apache.maven.lifecycle.LifecycleExecutor; import org.apache.maven.lifecycle.internal.ExecutionEventCatapult; import org.apache.maven.model.building.ModelProblem; +import org.apache.maven.model.building.ModelProblemUtils; import org.apache.maven.model.building.ModelSource; import org.apache.maven.model.building.UrlModelSource; import org.apache.maven.project.DuplicateProjectException; @@ -414,7 +415,8 @@ public class DefaultMaven for ( ModelProblem problem : result.getProblems() ) { - logger.warn( problem.getMessage() + " @ " + problem.getLocation() ); + logger.warn( problem.getMessage() + " @ " + + ModelProblemUtils.formatLocation( problem, result.getProjectId() ) ); } problems = true; diff --git a/maven-core/src/main/java/org/apache/maven/exception/DefaultExceptionHandler.java b/maven-core/src/main/java/org/apache/maven/exception/DefaultExceptionHandler.java index f7138fc213..df7ca25f88 100644 --- a/maven-core/src/main/java/org/apache/maven/exception/DefaultExceptionHandler.java +++ b/maven-core/src/main/java/org/apache/maven/exception/DefaultExceptionHandler.java @@ -25,6 +25,7 @@ import java.util.List; import org.apache.maven.lifecycle.LifecycleExecutionException; import org.apache.maven.model.building.ModelProblem; +import org.apache.maven.model.building.ModelProblemUtils; import org.apache.maven.plugin.AbstractMojoExecutionException; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; @@ -149,17 +150,7 @@ public class DefaultExceptionHandler { String message = problem.getMessage(); - String location = ""; - - if ( !problem.getModelId().equals( projectId ) ) - { - location += problem.getModelId(); - - if ( StringUtils.isNotEmpty( problem.getSource() ) ) - { - location += " (" + problem.getSource() + ")"; - } - } + String location = ModelProblemUtils.formatLocation( problem, projectId ); if ( StringUtils.isNotEmpty( location ) ) { diff --git a/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingException.java b/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingException.java index 164c7397b5..46bbb18220 100644 --- a/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingException.java +++ b/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingException.java @@ -25,6 +25,7 @@ import java.io.StringWriter; import java.util.List; import org.apache.maven.model.building.ModelProblem; +import org.apache.maven.model.building.ModelProblemUtils; /** * @author Jason van Zyl @@ -134,7 +135,7 @@ public class ProjectBuildingException writer.print( "] " ); writer.print( problem.getMessage() ); writer.print( " @ " ); - writer.println( problem.getLocation() ); + writer.println( ModelProblemUtils.formatLocation( problem, result.getProjectId() ) ); } } writer.close(); diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelProblem.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelProblem.java index 5092e787c2..86e27082eb 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelProblem.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelProblem.java @@ -106,42 +106,6 @@ public class DefaultModelProblem return modelId; } - public String getLocation() - { - StringBuilder buffer = new StringBuilder( 256 ); - - buffer.append( getModelId() ); - - if ( getSource().length() > 0 ) - { - if ( buffer.length() > 0 ) - { - buffer.append( ", " ); - } - buffer.append( getSource() ); - } - - if ( getLineNumber() > 0 ) - { - if ( buffer.length() > 0 ) - { - buffer.append( ", " ); - } - buffer.append( "line " ).append( getLineNumber() ); - } - - if ( getColumnNumber() > 0 ) - { - if ( buffer.length() > 0 ) - { - buffer.append( ", " ); - } - buffer.append( "column " ).append( getColumnNumber() ); - } - - return buffer.toString(); - } - public Exception getException() { return exception; @@ -180,7 +144,7 @@ public class DefaultModelProblem buffer.append( "[" ).append( getSeverity() ).append( "] " ); buffer.append( getMessage() ); - buffer.append( " @ " ).append( getLocation() ); + buffer.append( " @ " ).append( ModelProblemUtils.formatLocation( this, null ) ); return buffer.toString(); } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelBuildingException.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelBuildingException.java index 9a198e1393..c216c92d69 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelBuildingException.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelBuildingException.java @@ -119,7 +119,7 @@ public class ModelBuildingException writer.print( "] " ); writer.print( problem.getMessage() ); writer.print( " @ " ); - writer.println( problem.getLocation() ); + writer.println( ModelProblemUtils.formatLocation( problem, modelId ) ); } return buffer.toString(); diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelProblem.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelProblem.java index 0384d100b8..d1044028a3 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelProblem.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelProblem.java @@ -78,15 +78,6 @@ public interface ModelProblem */ String getModelId(); - /** - * Gets the location of the problem. The location is a user-friendly combination of the values from - * {@link #getModelId()}, {@link #getSource()}, {@link #getLineNumber()} and {@link #getColumnNumber()}. The exact - * syntax of the returned value is undefined. - * - * @return The location of the problem, never {@code null}. - */ - String getLocation(); - /** * Gets the exception that caused this problem (if any). * diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelProblemUtils.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelProblemUtils.java index ea63e76b62..330f4f1432 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelProblemUtils.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelProblemUtils.java @@ -24,11 +24,11 @@ import java.io.File; import org.apache.maven.model.Model; /** - * Assists in the creation of model problems. + * Assists in the handling of model problems. * * @author Benjamin Bentmann */ -class ModelProblemUtils +public class ModelProblemUtils { /** @@ -37,7 +37,7 @@ class ModelProblemUtils * @param model The model to create a source hint for, may be {@code null}. * @return The user-friendly source hint, never {@code null}. */ - public static String toSourceHint( Model model ) + static String toSourceHint( Model model ) { if ( model == null ) { @@ -57,7 +57,7 @@ class ModelProblemUtils return buffer.toString(); } - public static String toPath( Model model ) + static String toPath( Model model ) { String path = ""; @@ -74,7 +74,7 @@ class ModelProblemUtils return path; } - public static String toId( Model model ) + static String toId( Model model ) { if ( model == null ) { @@ -106,7 +106,7 @@ class ModelProblemUtils * @param version The version, may be {@code null}. * @return The user-friendly artifact id, never {@code null}. */ - public static String toId( String groupId, String artifactId, String version ) + static String toId( String groupId, String artifactId, String version ) { StringBuilder buffer = new StringBuilder( 96 ); @@ -119,4 +119,53 @@ class ModelProblemUtils return buffer.toString(); } + /** + * Creates a string with all location details for the specified model problem. If the project identifier is + * provided, the generated location will omit the model id and source information and only give line/column + * information for problems originating directly from this POM. + * + * @param problem The problem whose location should be formatted, must not be {@code null}. + * @param projectId The {@code ::} of the corresponding project, may be {@code null} + * to force output of model id and source. + * @return The formatted problem location or an empty string if unknown, never {@code null}. + */ + public static String formatLocation( ModelProblem problem, String projectId ) + { + StringBuilder buffer = new StringBuilder( 256 ); + + if ( !problem.getModelId().equals( projectId ) ) + { + buffer.append( problem.getModelId() ); + + if ( problem.getSource().length() > 0 ) + { + if ( buffer.length() > 0 ) + { + buffer.append( ", " ); + } + buffer.append( problem.getSource() ); + } + } + + if ( problem.getLineNumber() > 0 ) + { + if ( buffer.length() > 0 ) + { + buffer.append( ", " ); + } + buffer.append( "line " ).append( problem.getLineNumber() ); + } + + if ( problem.getColumnNumber() > 0 ) + { + if ( buffer.length() > 0 ) + { + buffer.append( ", " ); + } + buffer.append( "column " ).append( problem.getColumnNumber() ); + } + + return buffer.toString(); + } + }