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