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