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