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 java.io.File;
023
024import org.apache.maven.model.Model;
025
026/**
027 * Assists in the handling of model problems.
028 * 
029 * @author Benjamin Bentmann
030 */
031public class ModelProblemUtils
032{
033
034    /**
035     * Creates a user-friendly source hint for the specified model.
036     * 
037     * @param model The model to create a source hint for, may be {@code null}.
038     * @return The user-friendly source hint, never {@code null}.
039     */
040    static String toSourceHint( Model model )
041    {
042        if ( model == null )
043        {
044            return "";
045        }
046
047        StringBuilder buffer = new StringBuilder( 192 );
048
049        buffer.append( toId( model ) );
050
051        File pomFile = model.getPomFile();
052        if ( pomFile != null )
053        {
054            buffer.append( " (" ).append( pomFile ).append( ")" );
055        }
056
057        return buffer.toString();
058    }
059
060    static String toPath( Model model )
061    {
062        String path = "";
063
064        if ( model != null )
065        {
066            File pomFile = model.getPomFile();
067
068            if ( pomFile != null )
069            {
070                path = pomFile.getAbsolutePath();
071            }
072        }
073
074        return path;
075    }
076
077    static String toId( Model model )
078    {
079        if ( model == null )
080        {
081            return "";
082        }
083
084        String groupId = model.getGroupId();
085        if ( groupId == null && model.getParent() != null )
086        {
087            groupId = model.getParent().getGroupId();
088        }
089
090        String artifactId = model.getArtifactId();
091
092        String version = model.getVersion();
093        if ( version == null && model.getParent() != null )
094        {
095            version = model.getParent().getVersion();
096        }
097
098        return toId( groupId, artifactId, version );
099    }
100
101    /**
102     * Creates a user-friendly artifact id from the specified coordinates.
103     * 
104     * @param groupId The group id, may be {@code null}.
105     * @param artifactId The artifact id, may be {@code null}.
106     * @param version The version, may be {@code null}.
107     * @return The user-friendly artifact id, never {@code null}.
108     */
109    static String toId( String groupId, String artifactId, String version )
110    {
111        StringBuilder buffer = new StringBuilder( 96 );
112
113        buffer.append( ( groupId != null && groupId.length() > 0 ) ? groupId : "[unknown-group-id]" );
114        buffer.append( ':' );
115        buffer.append( ( artifactId != null && artifactId.length() > 0 ) ? artifactId : "[unknown-artifact-id]" );
116        buffer.append( ':' );
117        buffer.append( ( version != null && version.length() > 0 ) ? version : "[unknown-version]" );
118
119        return buffer.toString();
120    }
121
122    /**
123     * Creates a string with all location details for the specified model problem. If the project identifier is
124     * provided, the generated location will omit the model id and source information and only give line/column
125     * information for problems originating directly from this POM.
126     * 
127     * @param problem The problem whose location should be formatted, must not be {@code null}.
128     * @param projectId The {@code <groupId>:<artifactId>:<version>} of the corresponding project, may be {@code null}
129     *            to force output of model id and source.
130     * @return The formatted problem location or an empty string if unknown, never {@code null}.
131     */
132    public static String formatLocation( ModelProblem problem, String projectId )
133    {
134        StringBuilder buffer = new StringBuilder( 256 );
135
136        if ( !problem.getModelId().equals( projectId ) )
137        {
138            buffer.append( problem.getModelId() );
139
140            if ( problem.getSource().length() > 0 )
141            {
142                if ( buffer.length() > 0 )
143                {
144                    buffer.append( ", " );
145                }
146                buffer.append( problem.getSource() );
147            }
148        }
149
150        if ( problem.getLineNumber() > 0 )
151        {
152            if ( buffer.length() > 0 )
153            {
154                buffer.append( ", " );
155            }
156            buffer.append( "line " ).append( problem.getLineNumber() );
157        }
158
159        if ( problem.getColumnNumber() > 0 )
160        {
161            if ( buffer.length() > 0 )
162            {
163                buffer.append( ", " );
164            }
165            buffer.append( "column " ).append( problem.getColumnNumber() );
166        }
167
168        return buffer.toString();
169    }
170
171}