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.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.concurrent.CopyOnWriteArrayList;
25 import java.util.stream.Collectors;
26 import java.util.stream.Stream;
27
28 import org.apache.maven.api.model.Model;
29 import org.apache.maven.api.model.Profile;
30 import org.apache.maven.api.services.ModelBuilderResult;
31 import org.apache.maven.api.services.ModelProblem;
32 import org.apache.maven.api.services.ModelSource;
33
34
35
36
37 class DefaultModelBuilderResult implements ModelBuilderResult {
38 private ModelSource source;
39 private Model fileModel;
40 private Model rawModel;
41 private Model parentModel;
42 private Model effectiveModel;
43 private List<Profile> activePomProfiles;
44 private List<Profile> activeExternalProfiles;
45 private final List<ModelProblem> problems = new CopyOnWriteArrayList<>();
46 private final DefaultModelBuilderResult problemHolder;
47
48 private final List<DefaultModelBuilderResult> children = new ArrayList<>();
49
50 DefaultModelBuilderResult() {
51 this(null);
52 }
53
54 DefaultModelBuilderResult(DefaultModelBuilderResult problemHolder) {
55 this.problemHolder = problemHolder;
56 }
57
58 public ModelSource getSource() {
59 return source;
60 }
61
62 public void setSource(ModelSource source) {
63 this.source = source;
64 }
65
66 @Override
67 public Model getFileModel() {
68 return fileModel;
69 }
70
71 public void setFileModel(Model fileModel) {
72 this.fileModel = fileModel;
73 }
74
75 @Override
76 public Model getRawModel() {
77 return rawModel;
78 }
79
80 public void setRawModel(Model rawModel) {
81 this.rawModel = rawModel;
82 }
83
84 @Override
85 public Model getParentModel() {
86 return parentModel;
87 }
88
89 public void setParentModel(Model parentModel) {
90 this.parentModel = parentModel;
91 }
92
93 @Override
94 public Model getEffectiveModel() {
95 return effectiveModel;
96 }
97
98 public void setEffectiveModel(Model model) {
99 this.effectiveModel = model;
100 }
101
102 @Override
103 public List<Profile> getActivePomProfiles() {
104 return activePomProfiles;
105 }
106
107 public void setActivePomProfiles(List<Profile> activeProfiles) {
108 this.activePomProfiles = activeProfiles;
109 }
110
111 @Override
112 public List<Profile> getActiveExternalProfiles() {
113 return activeExternalProfiles;
114 }
115
116 public void setActiveExternalProfiles(List<Profile> activeProfiles) {
117 this.activeExternalProfiles = activeProfiles;
118 }
119
120
121
122
123
124
125
126 @Override
127 public List<ModelProblem> getProblems() {
128 return Collections.unmodifiableList(problems);
129 }
130
131
132
133
134
135
136 public void addProblem(ModelProblem problem) {
137 problems.add(problem);
138 if (problemHolder != null) {
139 problemHolder.addProblem(problem);
140 }
141 }
142
143 @Override
144 public List<DefaultModelBuilderResult> getChildren() {
145 return children;
146 }
147
148 public String toString() {
149 String modelId;
150 if (effectiveModel != null) {
151 modelId = effectiveModel.getId();
152 } else if (rawModel != null) {
153 modelId = rawModel.getId();
154 } else if (fileModel != null) {
155 modelId = fileModel.getId();
156 } else {
157 modelId = null;
158 }
159 if (!problems.isEmpty()) {
160 StringBuilder sb = new StringBuilder();
161 sb.append(problems.size())
162 .append(
163 (problems.size() == 1)
164 ? " problem was "
165 : " problems were encountered while building the effective model");
166 if (modelId != null && !modelId.isEmpty()) {
167 sb.append(" for ");
168 sb.append(modelId);
169 }
170 for (ModelProblem problem : problems) {
171 sb.append(System.lineSeparator());
172 sb.append(" - [");
173 sb.append(problem.getSeverity());
174 sb.append("] ");
175 if (problem.getMessage() != null && !problem.getMessage().isEmpty()) {
176 sb.append(problem.getMessage());
177 } else if (problem.getException() != null) {
178 sb.append(problem.getException().toString());
179 }
180 String loc = Stream.of(
181 problem.getModelId().equals(modelId) ? problem.getModelId() : "",
182 problem.getModelId().equals(modelId) ? problem.getSource() : "",
183 problem.getLineNumber() > 0 ? "line " + problem.getLineNumber() : "",
184 problem.getColumnNumber() > 0 ? "column " + problem.getColumnNumber() : "")
185 .filter(s -> !s.isEmpty())
186 .collect(Collectors.joining(", "));
187 if (!loc.isEmpty()) {
188 sb.append(" @ ").append(loc);
189 }
190 }
191 return sb.toString();
192 }
193 return modelId;
194 }
195 }