001package org.apache.maven.settings.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
022/**
023 * Describes a problem that was encountered during settings building. A problem can either be an exception that was
024 * thrown or a simple string message. In addition, a problem carries a hint about its source, e.g. the settings file
025 * that exhibits the problem.
026 * 
027 * @author Benjamin Bentmann
028 */
029public class DefaultSettingsProblem
030    implements SettingsProblem
031{
032
033    private final String source;
034
035    private final int lineNumber;
036
037    private final int columnNumber;
038
039    private final String message;
040
041    private final Exception exception;
042
043    private final Severity severity;
044
045    /**
046     * Creates a new problem with the specified message and exception.
047     * 
048     * @param message The message describing the problem, may be {@code null}.
049     * @param severity The severity level of the problem, may be {@code null} to default to
050     *            {@link SettingsProblem.Severity#ERROR}.
051     * @param source A hint about the source of the problem like a file path, may be {@code null}.
052     * @param lineNumber The one-based index of the line containing the problem or {@code -1} if unknown.
053     * @param columnNumber The one-based index of the column containing the problem or {@code -1} if unknown.
054     * @param exception The exception that caused this problem, may be {@code null}.
055     */
056    public DefaultSettingsProblem( String message, Severity severity, String source, int lineNumber, int columnNumber,
057                                   Exception exception )
058    {
059        this.message = message;
060        this.severity = ( severity != null ) ? severity : Severity.ERROR;
061        this.source = ( source != null ) ? source : "";
062        this.lineNumber = lineNumber;
063        this.columnNumber = columnNumber;
064        this.exception = exception;
065    }
066
067    public String getSource()
068    {
069        return source;
070    }
071
072    public int getLineNumber()
073    {
074        return lineNumber;
075    }
076
077    public int getColumnNumber()
078    {
079        return columnNumber;
080    }
081
082    public String getLocation()
083    {
084        StringBuilder buffer = new StringBuilder( 256 );
085
086        if ( getSource().length() > 0 )
087        {
088            if ( buffer.length() > 0 )
089            {
090                buffer.append( ", " );
091            }
092            buffer.append( getSource() );
093        }
094
095        if ( getLineNumber() > 0 )
096        {
097            if ( buffer.length() > 0 )
098            {
099                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}