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   * @author Benjamin Bentmann
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 ModelProblem.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 ModelProblem.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;
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     @Override
129     public String getModelId() {
130         return modelId;
131     }
132 
133     @Override
134     public Exception getException() {
135         return exception;
136     }
137 
138     @Override
139     public String getMessage() {
140         String msg;
141 
142         if (message != null && message.length() > 0) {
143             msg = message;
144         } else {
145             msg = exception.getMessage();
146 
147             if (msg == null) {
148                 msg = "";
149             }
150         }
151 
152         return msg;
153     }
154 
155     @Override
156     public Severity getSeverity() {
157         return severity;
158     }
159 
160     @Override
161     public Version getVersion() {
162         return version;
163     }
164 
165     @Override
166     public String toString() {
167         StringBuilder buffer = new StringBuilder(128);
168 
169         buffer.append('[').append(getSeverity()).append("] ");
170         buffer.append(getMessage());
171         String location = ModelProblemUtils.formatLocation(this, null);
172         if (!location.isEmpty()) {
173             buffer.append(" @ ");
174             buffer.append(location);
175         }
176 
177         return buffer.toString();
178     }
179 }