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