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