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