View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.model.building;
20  
21  import org.apache.maven.model.Model;
22  
23  /**
24   * Describes a problem that was encountered during model building. A problem can either be an exception that was thrown
25   * or a simple string message. In addition, a problem carries a hint about its source, e.g. the POM file that exhibits
26   * the problem.
27   *
28   */
29  public class DefaultModelProblem implements ModelProblem {
30  
31      private final String source;
32  
33      private final int lineNumber;
34  
35      private final int columnNumber;
36  
37      private final String modelId;
38  
39      private final String message;
40  
41      private final Exception exception;
42  
43      private final Severity severity;
44  
45      private final Version version;
46  
47      /**
48       * Creates a new problem with the specified message and exception.
49       *
50       * @param message The message describing the problem, may be {@code null}.
51       * @param severity The severity level of the problem, may be {@code null} to default to
52       *            {@link ModelProblem.Severity#ERROR}.
53       * @param source The source of the problem, may be {@code null}.
54       * @param lineNumber The one-based index of the line containing the error or {@code -1} if unknown.
55       * @param columnNumber The one-based index of the column containing the error or {@code -1} if unknown.
56       * @param exception The exception that caused this problem, may be {@code null}.
57       */
58      // mkleint: does this need to be public?
59      public DefaultModelProblem(
60              String message,
61              Severity severity,
62              Version version,
63              Model source,
64              int lineNumber,
65              int columnNumber,
66              Exception exception) {
67          this(
68                  message,
69                  severity,
70                  version,
71                  ModelProblemUtils.toPath(source),
72                  lineNumber,
73                  columnNumber,
74                  ModelProblemUtils.toId(source),
75                  exception);
76      }
77  
78      /**
79       * Creates a new problem with the specified message and exception.
80       *
81       * @param message The message describing the problem, may be {@code null}.
82       * @param severity The severity level of the problem, may be {@code null} to default to
83       *            {@link ModelProblem.Severity#ERROR}.
84       * @param version The version since the problem is relevant
85       * @param source A hint about the source of the problem like a file path, may be {@code null}.
86       * @param lineNumber The one-based index of the line containing the problem or {@code -1} if unknown.
87       * @param columnNumber The one-based index of the column containing the problem or {@code -1} if unknown.
88       * @param modelId The identifier of the model that exhibits the problem, may be {@code null}.
89       * @param exception The exception that caused this problem, may be {@code null}.
90       */
91      // mkleint: does this need to be public?
92      @SuppressWarnings("checkstyle:parameternumber")
93      public DefaultModelProblem(
94              String message,
95              Severity severity,
96              Version version,
97              String source,
98              int lineNumber,
99              int columnNumber,
100             String modelId,
101             Exception exception) {
102         this.message = message;
103         this.severity = (severity != null) ? severity : Severity.ERROR;
104         this.source = (source != null) ? source : "";
105         this.lineNumber = lineNumber;
106         this.columnNumber = columnNumber;
107         this.modelId = (modelId != null) ? modelId : "";
108         this.exception = exception;
109         this.version = version;
110     }
111 
112     @Override
113     public String getSource() {
114         return source;
115     }
116 
117     @Override
118     public int getLineNumber() {
119         return lineNumber;
120     }
121 
122     @Override
123     public int getColumnNumber() {
124         return columnNumber;
125     }
126 
127     @Override
128     public String getModelId() {
129         return modelId;
130     }
131 
132     @Override
133     public Exception getException() {
134         return exception;
135     }
136 
137     @Override
138     public String getMessage() {
139         String msg = null;
140 
141         if (message != null && !message.isEmpty()) {
142             msg = message;
143         } else if (exception != null) {
144             msg = exception.getMessage();
145         }
146 
147         if (msg == null) {
148             msg = "";
149         }
150 
151         return msg;
152     }
153 
154     @Override
155     public Severity getSeverity() {
156         return severity;
157     }
158 
159     @Override
160     public Version getVersion() {
161         return version;
162     }
163 
164     @Override
165     public String toString() {
166         StringBuilder buffer = new StringBuilder(128);
167 
168         buffer.append('[').append(getSeverity()).append("] ");
169         buffer.append(getMessage());
170         String location = ModelProblemUtils.formatLocation(this, null);
171         if (!location.isEmpty()) {
172             buffer.append(" @ ");
173             buffer.append(location);
174         }
175 
176         return buffer.toString();
177     }
178 }