001package org.apache.maven.model.building;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.model.Model;
023
024/**
025 * Describes a problem that was encountered during model building. A problem can either be an exception that was thrown
026 * or a simple string message. In addition, a problem carries a hint about its source, e.g. the POM file that exhibits
027 * the problem.
028 *
029 * @author Benjamin Bentmann
030 */
031public class DefaultModelProblem
032    implements ModelProblem
033{
034
035    private final String source;
036
037    private final int lineNumber;
038
039    private final int columnNumber;
040
041    private final String modelId;
042
043    private final String message;
044
045    private final Exception exception;
046
047    private final Severity severity;
048
049    private final Version version;
050
051
052    /**
053     * Creates a new problem with the specified message and exception.
054     *
055     * @param message The message describing the problem, may be {@code null}.
056     * @param severity The severity level of the problem, may be {@code null} to default to
057     *            {@link ModelProblem.Severity#ERROR}.
058     * @param source The source of the problem, may be {@code null}.
059     * @param lineNumber The one-based index of the line containing the error or {@code -1} if unknown.
060     * @param columnNumber The one-based index of the column containing the error or {@code -1} if unknown.
061     * @param exception The exception that caused this problem, may be {@code null}.
062     */
063    //mkleint: does this need to be public?
064    public DefaultModelProblem( String message, Severity severity, Version version, Model source, int lineNumber,
065                                int columnNumber, Exception exception )
066    {
067        this( message, severity, version, ModelProblemUtils.toPath( source ), lineNumber, columnNumber,
068              ModelProblemUtils.toId( source ), exception );
069    }
070
071    /**
072     * Creates a new problem with the specified message and exception.
073     *
074     * @param message The message describing the problem, may be {@code null}.
075     * @param severity The severity level of the problem, may be {@code null} to default to
076     *            {@link ModelProblem.Severity#ERROR}.
077     * @param version The version since the problem is relevant
078     * @param source A hint about the source of the problem like a file path, may be {@code null}.
079     * @param lineNumber The one-based index of the line containing the problem or {@code -1} if unknown.
080     * @param columnNumber The one-based index of the column containing the problem or {@code -1} if unknown.
081     * @param modelId The identifier of the model that exhibits the problem, may be {@code null}.
082     * @param exception The exception that caused this problem, may be {@code null}.
083     */
084    //mkleint: does this need to be public?
085    public DefaultModelProblem( String message, Severity severity, Version version, String source, int lineNumber,
086                                int columnNumber, String modelId, Exception exception )
087    {
088        this.message = message;
089        this.severity = ( severity != null ) ? severity : Severity.ERROR;
090        this.source = ( source != null ) ? source : "";
091        this.lineNumber = lineNumber;
092        this.columnNumber = columnNumber;
093        this.modelId = ( modelId != null ) ? modelId : "";
094        this.exception = exception;
095        this.version = version;
096    }
097
098    @Override
099    public String getSource()
100    {
101        return source;
102    }
103
104    @Override
105    public int getLineNumber()
106    {
107        return lineNumber;
108    }
109
110    @Override
111    public int getColumnNumber()
112    {
113        return columnNumber;
114    }
115
116    @Override
117    public String getModelId()
118    {
119        return modelId;
120    }
121
122    @Override
123    public Exception getException()
124    {
125        return exception;
126    }
127
128    @Override
129    public String getMessage()
130    {
131        String msg;
132
133        if ( message != null && message.length() > 0 )
134        {
135            msg = message;
136        }
137        else
138        {
139            msg = exception.getMessage();
140
141            if ( msg == null )
142            {
143                msg = "";
144            }
145        }
146
147        return msg;
148    }
149
150    @Override
151    public Severity getSeverity()
152    {
153        return severity;
154    }
155
156    @Override
157    public Version getVersion()
158    {
159        return version;
160    }
161
162
163    @Override
164    public String toString()
165    {
166        StringBuilder buffer = new StringBuilder( 128 );
167
168        buffer.append( "[" ).append( getSeverity() ).append( "] " );
169        buffer.append( getMessage() );
170        buffer.append( " @ " ).append( ModelProblemUtils.formatLocation( this, null ) );
171
172        return buffer.toString();
173    }
174
175}