1 package org.apache.maven.building;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 /**
23 * Describes a problem that was encountered during settings building. A problem can either be an exception that was
24 * thrown or a simple string message. In addition, a problem carries a hint about its source, e.g. the settings file
25 * that exhibits the problem.
26 *
27 * @author Benjamin Bentmann
28 * @author Robert Scholte
29 */
30 class DefaultProblem
31 implements Problem
32 {
33
34 private final String source;
35
36 private final int lineNumber;
37
38 private final int columnNumber;
39
40 private final String message;
41
42 private final Exception exception;
43
44 private final Severity severity;
45
46 /**
47 * Creates a new problem with the specified message and exception.
48 * Either {@code message} or {@code exception} is required
49 *
50 * @param message The message describing the problem, may be {@code null}.
51 * @param severity The severity level of the problem, may be {@code null} to default to
52 * {@link SettingsProblem.Severity#ERROR}.
53 * @param source A hint about the source of the problem like a file path, may be {@code null}.
54 * @param lineNumber The one-based index of the line containing the problem or {@code -1} if unknown.
55 * @param columnNumber The one-based index of the column containing the problem or {@code -1} if unknown.
56 * @param exception The exception that caused this problem, may be {@code null}.
57 */
58 DefaultProblem( String message, Severity severity, String source, int lineNumber, int columnNumber,
59 Exception exception )
60 {
61 this.message = message;
62 this.severity = ( severity != null ) ? severity : Severity.ERROR;
63 this.source = ( source != null ) ? source : "";
64 this.lineNumber = lineNumber;
65 this.columnNumber = columnNumber;
66 this.exception = exception;
67 }
68
69 public String getSource()
70 {
71 return source;
72 }
73
74 public int getLineNumber()
75 {
76 return lineNumber;
77 }
78
79 public int getColumnNumber()
80 {
81 return columnNumber;
82 }
83
84 public String getLocation()
85 {
86 StringBuilder buffer = new StringBuilder( 256 );
87
88 if ( getSource().length() > 0 )
89 {
90 if ( buffer.length() > 0 )
91 {
92 buffer.append( ", " );
93 }
94 buffer.append( getSource() );
95 }
96
97 if ( getLineNumber() > 0 )
98 {
99 if ( buffer.length() > 0 )
100 {
101 buffer.append( ", " );
102 }
103 buffer.append( "line " ).append( getLineNumber() );
104 }
105
106 if ( getColumnNumber() > 0 )
107 {
108 if ( buffer.length() > 0 )
109 {
110 buffer.append( ", " );
111 }
112 buffer.append( "column " ).append( getColumnNumber() );
113 }
114
115 return buffer.toString();
116 }
117
118 public Exception getException()
119 {
120 return exception;
121 }
122
123 public String getMessage()
124 {
125 String msg;
126
127 if ( message != null && message.length() > 0 )
128 {
129 msg = message;
130 }
131 else
132 {
133 msg = exception.getMessage();
134
135 if ( msg == null )
136 {
137 msg = "";
138 }
139 }
140
141 return msg;
142 }
143
144 public Severity getSeverity()
145 {
146 return severity;
147 }
148
149 @Override
150 public String toString()
151 {
152 StringBuilder buffer = new StringBuilder( 128 );
153
154 buffer.append( '[' ).append( getSeverity() ).append( "] " );
155 buffer.append( getMessage() );
156 buffer.append( " @ " ).append( getLocation() );
157
158 return buffer.toString();
159 }
160
161 }