View Javadoc
1   package org.apache.maven.model.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  import org.apache.maven.model.Model;
23  
24  /**
25   * Describes a problem that was encountered during model building. A problem can either be an exception that was thrown
26   * or a simple string message. In addition, a problem carries a hint about its source, e.g. the POM file that exhibits
27   * the problem.
28   * 
29   * @author Benjamin Bentmann
30   */
31  public class DefaultModelProblem
32      implements ModelProblem
33  {
34  
35      private final String source;
36  
37      private final int lineNumber;
38  
39      private final int columnNumber;
40  
41      private final String modelId;
42  
43      private final String message;
44  
45      private final Exception exception;
46  
47      private final Severity severity;
48  
49      private final Version version;
50  
51  
52      /**
53       * Creates a new problem with the specified message and exception.
54       * 
55       * @param message The message describing the problem, may be {@code null}.
56       * @param severity The severity level of the problem, may be {@code null} to default to
57       *            {@link ModelProblem.Severity#ERROR}.
58       * @param source The source of the problem, may be {@code null}.
59       * @param lineNumber The one-based index of the line containing the error or {@code -1} if unknown.
60       * @param columnNumber The one-based index of the column containing the error or {@code -1} if unknown.
61       * @param exception The exception that caused this problem, may be {@code null}.
62       */
63      //mkleint: does this need to be public?
64      public DefaultModelProblem( String message, Severity severity, Version version, Model source, int lineNumber, int columnNumber,
65                                  Exception exception )
66      {
67          this( message, severity, version, ModelProblemUtils.toPath( source ), lineNumber, columnNumber,
68                ModelProblemUtils.toId( source ), exception );
69      }
70  
71      /**
72       * Creates a new problem with the specified message and exception.
73       * 
74       * @param message The message describing the problem, may be {@code null}.
75       * @param severity The severity level of the problem, may be {@code null} to default to
76       *            {@link ModelProblem.Severity#ERROR}.
77       * @param version The version since the problem is relevant
78       * @param source A hint about the source of the problem like a file path, may be {@code null}.
79       * @param lineNumber The one-based index of the line containing the problem or {@code -1} if unknown.
80       * @param columnNumber The one-based index of the column containing the problem or {@code -1} if unknown.
81       * @param modelId The identifier of the model that exhibits the problem, may be {@code null}.
82       * @param exception The exception that caused this problem, may be {@code null}.
83       */
84      //mkleint: does this need to be public?
85      public DefaultModelProblem( String message, Severity severity, Version version, String source, int lineNumber, int columnNumber,
86                                  String modelId, Exception exception )
87      {
88          this.message = message;
89          this.severity = ( severity != null ) ? severity : Severity.ERROR;
90          this.source = ( source != null ) ? source : "";
91          this.lineNumber = lineNumber;
92          this.columnNumber = columnNumber;
93          this.modelId = ( modelId != null ) ? modelId : "";
94          this.exception = exception;
95          this.version = version;
96      }
97  
98      public String getSource()
99      {
100         return source;
101     }
102 
103     public int getLineNumber()
104     {
105         return lineNumber;
106     }
107 
108     public int getColumnNumber()
109     {
110         return columnNumber;
111     }
112 
113     public String getModelId()
114     {
115         return modelId;
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     public Version getVersion()
150     {
151         return version;
152     }
153     
154 
155     @Override
156     public String toString()
157     {
158         StringBuilder buffer = new StringBuilder( 128 );
159 
160         buffer.append( "[" ).append( getSeverity() ).append( "] " );
161         buffer.append( getMessage() );
162         buffer.append( " @ " ).append( ModelProblemUtils.formatLocation( this, null ) );
163 
164         return buffer.toString();
165     }
166 
167 }