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.settings.building;
20  
21  /**
22   * Describes a problem that was encountered during settings building. A problem can either be an exception that was
23   * thrown or a simple string message. In addition, a problem carries a hint about its source, e.g. the settings file
24   * that exhibits the problem.
25   *
26   * @deprecated since 4.0.0, use {@link org.apache.maven.api.services.SettingsBuilder} instead
27   */
28  @Deprecated(since = "4.0.0")
29  public class DefaultSettingsProblem implements SettingsProblem {
30  
31      private final String source;
32  
33      private final int lineNumber;
34  
35      private final int columnNumber;
36  
37      private final String message;
38  
39      private final Exception exception;
40  
41      private final Severity severity;
42  
43      /**
44       * Creates a new problem with the specified message and exception.
45       *
46       * @param message The message describing the problem, may be {@code null}.
47       * @param severity The severity level of the problem, may be {@code null} to default to
48       *            {@link SettingsProblem.Severity#ERROR}.
49       * @param source A hint about the source of the problem like a file path, may be {@code null}.
50       * @param lineNumber The one-based index of the line containing the problem or {@code -1} if unknown.
51       * @param columnNumber The one-based index of the column containing the problem or {@code -1} if unknown.
52       * @param exception The exception that caused this problem, may be {@code null}.
53       */
54      public DefaultSettingsProblem(
55              String message, Severity severity, String source, int lineNumber, int columnNumber, Exception exception) {
56          this.message = message;
57          this.severity = (severity != null) ? severity : Severity.ERROR;
58          this.source = (source != null) ? source : "";
59          this.lineNumber = lineNumber;
60          this.columnNumber = columnNumber;
61          this.exception = exception;
62      }
63  
64      @Override
65      public String getSource() {
66          return source;
67      }
68  
69      @Override
70      public int getLineNumber() {
71          return lineNumber;
72      }
73  
74      @Override
75      public int getColumnNumber() {
76          return columnNumber;
77      }
78  
79      @Override
80      public String getLocation() {
81          StringBuilder buffer = new StringBuilder(256);
82  
83          if (!getSource().isEmpty()) {
84              if (buffer.length() > 0) {
85                  buffer.append(", ");
86              }
87              buffer.append(getSource());
88          }
89  
90          if (getLineNumber() > 0) {
91              if (buffer.length() > 0) {
92                  buffer.append(", ");
93              }
94              buffer.append("line ").append(getLineNumber());
95          }
96  
97          if (getColumnNumber() > 0) {
98              if (buffer.length() > 0) {
99                  buffer.append(", ");
100             }
101             buffer.append("column ").append(getColumnNumber());
102         }
103 
104         return buffer.toString();
105     }
106 
107     @Override
108     public Exception getException() {
109         return exception;
110     }
111 
112     @Override
113     public String getMessage() {
114         String msg;
115 
116         if (message != null && !message.isEmpty()) {
117             msg = message;
118         } else {
119             msg = exception.getMessage();
120 
121             if (msg == null) {
122                 msg = "";
123             }
124         }
125 
126         return msg;
127     }
128 
129     @Override
130     public Severity getSeverity() {
131         return severity;
132     }
133 
134     @Override
135     public String toString() {
136         StringBuilder buffer = new StringBuilder(128);
137 
138         buffer.append('[').append(getSeverity()).append("] ");
139         buffer.append(getMessage());
140         String location = getLocation();
141         if (!location.isEmpty()) {
142             buffer.append(" @ ");
143             buffer.append(location);
144         }
145 
146         return buffer.toString();
147     }
148 }