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