View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.model.building;
20  
21  import java.io.File;
22  
23  import org.apache.maven.model.Model;
24  
25  /**
26   * Assists in the handling of model problems.
27   *
28   * @author Benjamin Bentmann
29   */
30  public class ModelProblemUtils {
31  
32      /**
33       * Creates a user-friendly source hint for the specified model.
34       *
35       * @param model The model to create a source hint for, may be {@code null}.
36       * @return The user-friendly source hint, never {@code null}.
37       */
38      static String toSourceHint(Model model) {
39          if (model == null) {
40              return "";
41          }
42  
43          StringBuilder buffer = new StringBuilder(128);
44  
45          buffer.append(toId(model));
46  
47          File pomFile = model.getPomFile();
48          if (pomFile != null) {
49              buffer.append(" (").append(pomFile).append(')');
50          }
51  
52          return buffer.toString();
53      }
54  
55      static String toPath(Model model) {
56          String path = "";
57  
58          if (model != null) {
59              File pomFile = model.getPomFile();
60  
61              if (pomFile != null) {
62                  path = pomFile.getAbsolutePath();
63              }
64          }
65  
66          return path;
67      }
68  
69      static String toId(Model model) {
70          if (model == null) {
71              return "";
72          }
73          return toId(model.getDelegate());
74      }
75  
76      static String toId(org.apache.maven.api.model.Model model) {
77          String groupId = model.getGroupId();
78          if (groupId == null && model.getParent() != null) {
79              groupId = model.getParent().getGroupId();
80          }
81  
82          String artifactId = model.getArtifactId();
83  
84          String version = model.getVersion();
85          if (version == null && model.getParent() != null) {
86              version = model.getParent().getVersion();
87          }
88          if (version == null) {
89              version = "[unknown-version]";
90          }
91  
92          return toId(groupId, artifactId, version);
93      }
94  
95      /**
96       * Creates a user-friendly artifact id from the specified coordinates.
97       *
98       * @param groupId The group id, may be {@code null}.
99       * @param artifactId The artifact id, may be {@code null}.
100      * @param version The version, may be {@code null}.
101      * @return The user-friendly artifact id, never {@code null}.
102      */
103     static String toId(String groupId, String artifactId, String version) {
104         StringBuilder buffer = new StringBuilder(128);
105 
106         buffer.append((groupId != null && groupId.length() > 0) ? groupId : "[unknown-group-id]");
107         buffer.append(':');
108         buffer.append((artifactId != null && artifactId.length() > 0) ? artifactId : "[unknown-artifact-id]");
109         buffer.append(':');
110         buffer.append((version != null && version.length() > 0) ? version : "[unknown-version]");
111 
112         return buffer.toString();
113     }
114 
115     /**
116      * Creates a string with all location details for the specified model problem. If the project identifier is
117      * provided, the generated location will omit the model id and source information and only give line/column
118      * information for problems originating directly from this POM.
119      *
120      * @param problem The problem whose location should be formatted, must not be {@code null}.
121      * @param projectId The {@code <groupId>:<artifactId>:<version>} of the corresponding project, may be {@code null}
122      *            to force output of model id and source.
123      * @return The formatted problem location or an empty string if unknown, never {@code null}.
124      */
125     public static String formatLocation(ModelProblem problem, String projectId) {
126         StringBuilder buffer = new StringBuilder(256);
127 
128         if (!problem.getModelId().equals(projectId)) {
129             buffer.append(problem.getModelId());
130 
131             if (problem.getSource().length() > 0) {
132                 if (buffer.length() > 0) {
133                     buffer.append(", ");
134                 }
135                 buffer.append(problem.getSource());
136             }
137         }
138 
139         if (problem.getLineNumber() > 0) {
140             if (buffer.length() > 0) {
141                 buffer.append(", ");
142             }
143             buffer.append("line ").append(problem.getLineNumber());
144         }
145 
146         if (problem.getColumnNumber() > 0) {
147             if (buffer.length() > 0) {
148                 buffer.append(", ");
149             }
150             buffer.append("column ").append(problem.getColumnNumber());
151         }
152 
153         return buffer.toString();
154     }
155 }