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.internal.impl.model;
20  
21  import org.apache.maven.api.model.Model;
22  import org.apache.maven.api.services.ModelProblem;
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   */
30  public class DefaultModelProblem implements ModelProblem {
31  
32      private final String source;
33  
34      private final int lineNumber;
35  
36      private final int columnNumber;
37  
38      private final String modelId;
39  
40      private final String message;
41  
42      private final Exception exception;
43  
44      private final Severity severity;
45  
46      private final Version version;
47  
48      /**
49       * Creates a new problem with the specified message and exception.
50       *
51       * @param message The message describing the problem, may be {@code null}.
52       * @param severity The severity level of the problem, may be {@code null} to default to
53       *            {@link Severity#ERROR}.
54       * @param source The source of the problem, may be {@code null}.
55       * @param lineNumber The one-based index of the line containing the error or {@code -1} if unknown.
56       * @param columnNumber The one-based index of the column containing the error or {@code -1} if unknown.
57       * @param exception The exception that caused this problem, may be {@code null}.
58       */
59      // mkleint: does this need to be public?
60      public DefaultModelProblem(
61              String message,
62              Severity severity,
63              Version version,
64              Model source,
65              int lineNumber,
66              int columnNumber,
67              Exception exception) {
68          this(
69                  message,
70                  severity,
71                  version,
72                  ModelProblemUtils.toPath(source),
73                  lineNumber,
74                  columnNumber,
75                  ModelProblemUtils.toId(source),
76                  exception);
77      }
78  
79      /**
80       * Creates a new problem with the specified message and exception.
81       *
82       * @param message The message describing the problem, may be {@code null}.
83       * @param severity The severity level of the problem, may be {@code null} to default to
84       *            {@link Severity#ERROR}.
85       * @param version The version since the problem is relevant
86       * @param source A hint about the source of the problem like a file path, may be {@code null}.
87       * @param lineNumber The one-based index of the line containing the problem or {@code -1} if unknown.
88       * @param columnNumber The one-based index of the column containing the problem or {@code -1} if unknown.
89       * @param modelId The identifier of the model that exhibits the problem, may be {@code null}.
90       * @param exception The exception that caused this problem, may be {@code null}.
91       */
92      // mkleint: does this need to be public?
93      @SuppressWarnings("checkstyle:parameternumber")
94      public DefaultModelProblem(
95              String message,
96              Severity severity,
97              Version version,
98              String source,
99              int lineNumber,
100             int columnNumber,
101             String modelId,
102             Exception exception) {
103         this.message = message;
104         this.severity = (severity != null) ? severity : Severity.ERROR;
105         this.source = (source != null) ? source : "";
106         this.lineNumber = lineNumber;
107         this.columnNumber = columnNumber;
108         this.modelId = (modelId != null) ? modelId : "";
109         this.exception = exception;
110         this.version = version != null ? version : Version.BASE;
111     }
112 
113     @Override
114     public String getSource() {
115         return source;
116     }
117 
118     @Override
119     public int getLineNumber() {
120         return lineNumber;
121     }
122 
123     @Override
124     public int getColumnNumber() {
125         return columnNumber;
126     }
127 
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 getLocation() {
139         return "";
140     }
141 
142     @Override
143     public String getMessage() {
144         String msg = null;
145 
146         if (message != null && !message.isEmpty()) {
147             msg = message;
148         } else if (exception != null) {
149             msg = exception.getMessage();
150         }
151 
152         if (msg == null) {
153             msg = "";
154         }
155 
156         return msg;
157     }
158 
159     @Override
160     public Severity getSeverity() {
161         return severity;
162     }
163 
164     public Version getVersion() {
165         return version;
166     }
167 
168     @Override
169     public String toString() {
170         StringBuilder buffer = new StringBuilder(128);
171 
172         buffer.append('[').append(getSeverity()).append("] ");
173         buffer.append(getMessage());
174         String location = ModelProblemUtils.formatLocation(this, null);
175         if (!location.isEmpty()) {
176             buffer.append(" @ ");
177             buffer.append(location);
178         }
179 
180         return buffer.toString();
181     }
182 }