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.internal.impl.model;
20  
21  import java.nio.file.Path;
22  
23  import org.apache.maven.api.model.Model;
24  import org.apache.maven.api.services.ModelProblem;
25  
26  /**
27   * Assists in the handling of model problems.
28   *
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          StringBuilder buffer = new StringBuilder(128);
43          buffer.append(toId(model));
44          Path pomPath = model.getPomFile();
45          if (pomPath != null) {
46              buffer.append(" (").append(pomPath).append(')');
47          }
48          return buffer.toString();
49      }
50  
51      static String toPath(Model model) {
52          String path = "";
53          if (model != null) {
54              Path pomPath = model.getPomFile();
55              if (pomPath != null) {
56                  path = pomPath.toAbsolutePath().toString();
57              }
58          }
59          return path;
60      }
61  
62      static String toId(Model model) {
63          if (model == null) {
64              return "";
65          }
66          String groupId = model.getGroupId();
67          if (groupId == null && model.getParent() != null) {
68              groupId = model.getParent().getGroupId();
69          }
70          String artifactId = model.getArtifactId();
71          String version = model.getVersion();
72          if (version == null && model.getParent() != null) {
73              version = model.getParent().getVersion();
74          }
75          return toId(groupId, artifactId, version);
76      }
77  
78      /**
79       * Creates a user-friendly artifact id from the specified coordinates.
80       *
81       * @param groupId The group id, may be {@code null}.
82       * @param artifactId The artifact id, may be {@code null}.
83       * @param version The version, may be {@code null}.
84       * @return The user-friendly artifact id, never {@code null}.
85       */
86      static String toId(String groupId, String artifactId, String version) {
87          return ((groupId != null && !groupId.isEmpty()) ? groupId : "[unknown-group-id]")
88                  + ':'
89                  + ((artifactId != null && !artifactId.isEmpty()) ? artifactId : "[unknown-artifact-id]")
90                  + ':'
91                  + ((version != null && !version.isEmpty()) ? version : "[unknown-version]");
92      }
93  
94      /**
95       * Creates a string with all location details for the specified model problem. If the project identifier is
96       * provided, the generated location will omit the model id and source information and only give line/column
97       * information for problems originating directly from this POM.
98       *
99       * @param problem The problem whose location should be formatted, must not be {@code null}.
100      * @param projectId The {@code <groupId>:<artifactId>:<version>} of the corresponding project, may be {@code null}
101      *            to force output of model id and source.
102      * @return The formatted problem location or an empty string if unknown, never {@code null}.
103      */
104     public static String formatLocation(ModelProblem problem, String projectId) {
105         StringBuilder buffer = new StringBuilder(256);
106 
107         if (!problem.getModelId().equals(projectId)) {
108             buffer.append(problem.getModelId());
109             if (!problem.getSource().isEmpty()) {
110                 if (!buffer.isEmpty()) {
111                     buffer.append(", ");
112                 }
113                 buffer.append(problem.getSource());
114             }
115         }
116 
117         if (problem.getLineNumber() > 0) {
118             if (!buffer.isEmpty()) {
119                 buffer.append(", ");
120             }
121             buffer.append("line ").append(problem.getLineNumber());
122         }
123 
124         if (problem.getColumnNumber() > 0) {
125             if (!buffer.isEmpty()) {
126                 buffer.append(", ");
127             }
128             buffer.append("column ").append(problem.getColumnNumber());
129         }
130 
131         return buffer.toString();
132     }
133 }