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 java.io.File;
23  
24  import org.apache.maven.model.Model;
25  
26  /**
27   * Assists in the handling of model problems.
28   *
29   * @author Benjamin Bentmann
30   */
31  public class ModelProblemUtils
32  {
33  
34      /**
35       * Creates a user-friendly source hint for the specified model.
36       *
37       * @param model The model to create a source hint for, may be {@code null}.
38       * @return The user-friendly source hint, never {@code null}.
39       */
40      static String toSourceHint( Model model )
41      {
42          if ( model == null )
43          {
44              return "";
45          }
46  
47          StringBuilder buffer = new StringBuilder( 128 );
48  
49          buffer.append( toId( model ) );
50  
51          File pomFile = model.getPomFile();
52          if ( pomFile != null )
53          {
54              buffer.append( " (" ).append( pomFile ).append( ')' );
55          }
56  
57          return buffer.toString();
58      }
59  
60      static String toPath( Model model )
61      {
62          String path = "";
63  
64          if ( model != null )
65          {
66              File pomFile = model.getPomFile();
67  
68              if ( pomFile != null )
69              {
70                  path = pomFile.getAbsolutePath();
71              }
72          }
73  
74          return path;
75      }
76  
77      static String toId( Model model )
78      {
79          if ( model == null )
80          {
81              return "";
82          }
83          return toId( model.getDelegate() );
84      }
85  
86      static String toId( org.apache.maven.api.model.Model model )
87      {
88          String groupId = model.getGroupId();
89          if ( groupId == null && model.getParent() != null )
90          {
91              groupId = model.getParent().getGroupId();
92          }
93  
94          String artifactId = model.getArtifactId();
95  
96          String version = model.getVersion();
97          if ( version == null && model.getParent() != null )
98          {
99              version = model.getParent().getVersion();
100         }
101         if ( version == null )
102         {
103             version = "[unknown-version]";
104         }
105 
106         return toId( groupId, artifactId, version );
107     }
108 
109     /**
110      * Creates a user-friendly artifact id from the specified coordinates.
111      *
112      * @param groupId The group id, may be {@code null}.
113      * @param artifactId The artifact id, may be {@code null}.
114      * @param version The version, may be {@code null}.
115      * @return The user-friendly artifact id, never {@code null}.
116      */
117     static String toId( String groupId, String artifactId, String version )
118     {
119         StringBuilder buffer = new StringBuilder( 128 );
120 
121         buffer.append( ( groupId != null && groupId.length() > 0 ) ? groupId : "[unknown-group-id]" );
122         buffer.append( ':' );
123         buffer.append( ( artifactId != null && artifactId.length() > 0 ) ? artifactId : "[unknown-artifact-id]" );
124         buffer.append( ':' );
125         buffer.append( ( version != null && version.length() > 0 ) ? version : "[unknown-version]" );
126 
127         return buffer.toString();
128     }
129 
130     /**
131      * Creates a string with all location details for the specified model problem. If the project identifier is
132      * provided, the generated location will omit the model id and source information and only give line/column
133      * information for problems originating directly from this POM.
134      *
135      * @param problem The problem whose location should be formatted, must not be {@code null}.
136      * @param projectId The {@code <groupId>:<artifactId>:<version>} of the corresponding project, may be {@code null}
137      *            to force output of model id and source.
138      * @return The formatted problem location or an empty string if unknown, never {@code null}.
139      */
140     public static String formatLocation( ModelProblem problem, String projectId )
141     {
142         StringBuilder buffer = new StringBuilder( 256 );
143 
144         if ( !problem.getModelId().equals( projectId ) )
145         {
146             buffer.append( problem.getModelId() );
147 
148             if ( problem.getSource().length() > 0 )
149             {
150                 if ( buffer.length() > 0 )
151                 {
152                     buffer.append( ", " );
153                 }
154                 buffer.append( problem.getSource() );
155             }
156         }
157 
158         if ( problem.getLineNumber() > 0 )
159         {
160             if ( buffer.length() > 0 )
161             {
162                 buffer.append( ", " );
163             }
164             buffer.append( "line " ).append( problem.getLineNumber() );
165         }
166 
167         if ( problem.getColumnNumber() > 0 )
168         {
169             if ( buffer.length() > 0 )
170             {
171                 buffer.append( ", " );
172             }
173             buffer.append( "column " ).append( problem.getColumnNumber() );
174         }
175 
176         return buffer.toString();
177     }
178 
179 }