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;
20  
21  import org.apache.maven.api.services.BuilderProblem;
22  
23  /**
24   * Describes a problem that was encountered during settings building. A problem can either be an exception that was
25   * thrown or a simple string message. In addition, a problem carries a hint about its source, e.g. the settings file
26   * that exhibits the problem.
27   */
28  class DefaultBuilderProblem implements BuilderProblem {
29      final String source;
30      final int lineNumber;
31      final int columnNumber;
32      final Exception exception;
33      final String message;
34      final Severity severity;
35  
36      DefaultBuilderProblem(
37              String source, int lineNumber, int columnNumber, Exception exception, String message, Severity severity) {
38          this.source = source;
39          this.lineNumber = lineNumber;
40          this.columnNumber = columnNumber;
41          this.exception = exception;
42          this.message = message;
43          this.severity = severity;
44      }
45  
46      @Override
47      public String getSource() {
48          return source;
49      }
50  
51      @Override
52      public int getLineNumber() {
53          return lineNumber;
54      }
55  
56      @Override
57      public int getColumnNumber() {
58          return columnNumber;
59      }
60  
61      @Override
62      public Exception getException() {
63          return exception;
64      }
65  
66      @Override
67      public String getMessage() {
68          return message;
69      }
70  
71      @Override
72      public Severity getSeverity() {
73          return severity;
74      }
75  
76      @Override
77      public String getLocation() {
78          StringBuilder buffer = new StringBuilder(256);
79          if (getSource() != null && !getSource().isEmpty()) {
80              buffer.append(getSource());
81          }
82          if (getLineNumber() > 0) {
83              if (!buffer.isEmpty()) {
84                  buffer.append(", ");
85              }
86              buffer.append("line ").append(getLineNumber());
87          }
88          if (getColumnNumber() > 0) {
89              if (!buffer.isEmpty()) {
90                  buffer.append(", ");
91              }
92              buffer.append("column ").append(getColumnNumber());
93          }
94          return buffer.toString();
95      }
96  
97      @Override
98      public String toString() {
99          StringBuilder buffer = new StringBuilder(128);
100         buffer.append('[').append(severity).append("]");
101         String msg = message != null ? message : exception != null ? exception.getMessage() : null;
102         if (msg != null && !msg.isEmpty()) {
103             buffer.append(" ").append(msg);
104         }
105         String location = getLocation();
106         if (!location.isEmpty()) {
107             buffer.append(" @ ").append(location);
108         }
109         return buffer.toString();
110     }
111 }