1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
28
29
30 public class ModelProblemUtils {
31
32
33
34
35
36
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
80
81
82
83
84
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
96
97
98
99
100
101
102
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 }