1 package org.apache.maven.model.building;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.apache.maven.model.Model;
23
24 /**
25 * Describes a problem that was encountered during model building. A problem can either be an exception that was thrown
26 * or a simple string message. In addition, a problem carries a hint about its source, e.g. the POM file that exhibits
27 * the problem.
28 *
29 * @author Benjamin Bentmann
30 */
31 public class DefaultModelProblem
32 implements ModelProblem
33 {
34
35 private final String source;
36
37 private final int lineNumber;
38
39 private final int columnNumber;
40
41 private final String modelId;
42
43 private final String message;
44
45 private final Exception exception;
46
47 private final Severity severity;
48
49 /**
50 * Creates a new problem with the specified message and exception.
51 *
52 * @param message The message describing the problem, may be {@code null}.
53 * @param severity The severity level of the problem, may be {@code null} to default to
54 * {@link ModelProblem.Severity#ERROR}.
55 * @param source The source of the problem, may be {@code null}.
56 * @param lineNumber The one-based index of the line containing the error or {@code -1} if unknown.
57 * @param columnNumber The one-based index of the column containing the error or {@code -1} if unknown.
58 * @param exception The exception that caused this problem, may be {@code null}.
59 */
60 public DefaultModelProblem( String message, Severity severity, Model source, int lineNumber, int columnNumber,
61 Exception exception )
62 {
63 this( message, severity, ModelProblemUtils.toPath( source ), lineNumber, columnNumber,
64 ModelProblemUtils.toId( source ), exception );
65 }
66
67 /**
68 * Creates a new problem with the specified message and exception.
69 *
70 * @param message The message describing the problem, may be {@code null}.
71 * @param severity The severity level of the problem, may be {@code null} to default to
72 * {@link ModelProblem.Severity#ERROR}.
73 * @param source A hint about the source of the problem like a file path, may be {@code null}.
74 * @param lineNumber The one-based index of the line containing the problem or {@code -1} if unknown.
75 * @param columnNumber The one-based index of the column containing the problem or {@code -1} if unknown.
76 * @param modelId The identifier of the model that exhibits the problem, may be {@code null}.
77 * @param exception The exception that caused this problem, may be {@code null}.
78 */
79 public DefaultModelProblem( String message, Severity severity, String source, int lineNumber, int columnNumber,
80 String modelId, Exception exception )
81 {
82 this.message = message;
83 this.severity = ( severity != null ) ? severity : Severity.ERROR;
84 this.source = ( source != null ) ? source : "";
85 this.lineNumber = lineNumber;
86 this.columnNumber = columnNumber;
87 this.modelId = ( modelId != null ) ? modelId : "";
88 this.exception = exception;
89 }
90
91 public String getSource()
92 {
93 return source;
94 }
95
96 public int getLineNumber()
97 {
98 return lineNumber;
99 }
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 }