001 package org.apache.maven.model.building;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022 import org.apache.maven.model.Model;
023
024 /**
025 * Describes a problem that was encountered during model building. A problem can either be an exception that was thrown
026 * or a simple string message. In addition, a problem carries a hint about its source, e.g. the POM file that exhibits
027 * the problem.
028 *
029 * @author Benjamin Bentmann
030 */
031 public class DefaultModelProblem
032 implements ModelProblem
033 {
034
035 private final String source;
036
037 private final int lineNumber;
038
039 private final int columnNumber;
040
041 private final String modelId;
042
043 private final String message;
044
045 private final Exception exception;
046
047 private final Severity severity;
048
049 /**
050 * Creates a new problem with the specified message and exception.
051 *
052 * @param message The message describing the problem, may be {@code null}.
053 * @param severity The severity level of the problem, may be {@code null} to default to
054 * {@link ModelProblem.Severity#ERROR}.
055 * @param source The source of the problem, may be {@code null}.
056 * @param lineNumber The one-based index of the line containing the error or {@code -1} if unknown.
057 * @param columnNumber The one-based index of the column containing the error or {@code -1} if unknown.
058 * @param exception The exception that caused this problem, may be {@code null}.
059 */
060 public DefaultModelProblem( String message, Severity severity, Model source, int lineNumber, int columnNumber,
061 Exception exception )
062 {
063 this( message, severity, ModelProblemUtils.toPath( source ), lineNumber, columnNumber,
064 ModelProblemUtils.toId( source ), exception );
065 }
066
067 /**
068 * Creates a new problem with the specified message and exception.
069 *
070 * @param message The message describing the problem, may be {@code null}.
071 * @param severity The severity level of the problem, may be {@code null} to default to
072 * {@link ModelProblem.Severity#ERROR}.
073 * @param source A hint about the source of the problem like a file path, may be {@code null}.
074 * @param lineNumber The one-based index of the line containing the problem or {@code -1} if unknown.
075 * @param columnNumber The one-based index of the column containing the problem or {@code -1} if unknown.
076 * @param modelId The identifier of the model that exhibits the problem, may be {@code null}.
077 * @param exception The exception that caused this problem, may be {@code null}.
078 */
079 public DefaultModelProblem( String message, Severity severity, String source, int lineNumber, int columnNumber,
080 String modelId, Exception exception )
081 {
082 this.message = message;
083 this.severity = ( severity != null ) ? severity : Severity.ERROR;
084 this.source = ( source != null ) ? source : "";
085 this.lineNumber = lineNumber;
086 this.columnNumber = columnNumber;
087 this.modelId = ( modelId != null ) ? modelId : "";
088 this.exception = exception;
089 }
090
091 public String getSource()
092 {
093 return source;
094 }
095
096 public int getLineNumber()
097 {
098 return lineNumber;
099 }
100
101 public int getColumnNumber()
102 {
103 return columnNumber;
104 }
105
106 public String getModelId()
107 {
108 return modelId;
109 }
110
111 public Exception getException()
112 {
113 return exception;
114 }
115
116 public String getMessage()
117 {
118 String msg;
119
120 if ( message != null && message.length() > 0 )
121 {
122 msg = message;
123 }
124 else
125 {
126 msg = exception.getMessage();
127
128 if ( msg == null )
129 {
130 msg = "";
131 }
132 }
133
134 return msg;
135 }
136
137 public Severity getSeverity()
138 {
139 return severity;
140 }
141
142 @Override
143 public String toString()
144 {
145 StringBuilder buffer = new StringBuilder( 128 );
146
147 buffer.append( "[" ).append( getSeverity() ).append( "] " );
148 buffer.append( getMessage() );
149 buffer.append( " @ " ).append( ModelProblemUtils.formatLocation( this, null ) );
150
151 return buffer.toString();
152 }
153
154 }