View Javadoc

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