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.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  class DefaultProblem implements Problem {
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       * Either {@code message} or {@code exception} is required
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 org.apache.maven.building.Problem.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      DefaultProblem(
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      public String getSource() {
64          return source;
65      }
66  
67      public int getLineNumber() {
68          return lineNumber;
69      }
70  
71      public int getColumnNumber() {
72          return columnNumber;
73      }
74  
75      public String getLocation() {
76          StringBuilder buffer = new StringBuilder(256);
77  
78          if (getSource().length() > 0) {
79              if (buffer.length() > 0) {
80                  buffer.append(", ");
81              }
82              buffer.append(getSource());
83          }
84  
85          if (getLineNumber() > 0) {
86              if (buffer.length() > 0) {
87                  buffer.append(", ");
88              }
89              buffer.append("line ").append(getLineNumber());
90          }
91  
92          if (getColumnNumber() > 0) {
93              if (buffer.length() > 0) {
94                  buffer.append(", ");
95              }
96              buffer.append("column ").append(getColumnNumber());
97          }
98  
99          return buffer.toString();
100     }
101 
102     public Exception getException() {
103         return exception;
104     }
105 
106     public String getMessage() {
107         String msg;
108 
109         if (message != null && message.length() > 0) {
110             msg = message;
111         } else {
112             msg = exception.getMessage();
113 
114             if (msg == null) {
115                 msg = "";
116             }
117         }
118 
119         return msg;
120     }
121 
122     public Severity getSeverity() {
123         return severity;
124     }
125 
126     @Override
127     public String toString() {
128         StringBuilder buffer = new StringBuilder(128);
129 
130         buffer.append('[').append(getSeverity()).append("] ");
131         buffer.append(getMessage());
132         String location = getLocation();
133         if (!location.isEmpty()) {
134             buffer.append(" @ ");
135             buffer.append(location);
136         }
137 
138         return buffer.toString();
139     }
140 }