1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.model.building;
20
21 import java.util.EnumSet;
22 import java.util.List;
23 import java.util.Set;
24
25 import org.apache.maven.model.Model;
26 import org.apache.maven.model.io.ModelParseException;
27
28
29
30
31
32
33
34
35
36
37 @Deprecated(since = "4.0.0")
38 class DefaultModelProblemCollector implements ModelProblemCollectorExt {
39
40 private final ModelBuildingResult result;
41
42 private List<ModelProblem> problems;
43
44 private String source;
45
46 private Model sourceModel;
47
48 private Model rootModel;
49
50 private Set<ModelProblem.Severity> severities = EnumSet.noneOf(ModelProblem.Severity.class);
51
52 DefaultModelProblemCollector(ModelBuildingResult result) {
53 this.result = result;
54 this.problems = result.getProblems();
55
56 for (ModelProblem problem : this.problems) {
57 severities.add(problem.getSeverity());
58 }
59 }
60
61 public boolean hasFatalErrors() {
62 return severities.contains(ModelProblem.Severity.FATAL);
63 }
64
65 public boolean hasErrors() {
66 return severities.contains(ModelProblem.Severity.ERROR) || severities.contains(ModelProblem.Severity.FATAL);
67 }
68
69 @Override
70 public List<ModelProblem> getProblems() {
71 return problems;
72 }
73
74 public void setSource(String source) {
75 this.source = source;
76 this.sourceModel = null;
77 }
78
79 public void setSource(Model source) {
80 this.sourceModel = source;
81 this.source = null;
82
83 if (rootModel == null) {
84 rootModel = source;
85 }
86 }
87
88 private String getSource() {
89 if (source == null && sourceModel != null) {
90 source = ModelProblemUtils.toPath(sourceModel);
91 }
92 return source;
93 }
94
95 private String getModelId() {
96 return ModelProblemUtils.toId(sourceModel);
97 }
98
99 public void setRootModel(Model rootModel) {
100 this.rootModel = rootModel;
101 }
102
103 public Model getRootModel() {
104 return rootModel;
105 }
106
107 public String getRootModelId() {
108 return ModelProblemUtils.toId(rootModel);
109 }
110
111 public void add(ModelProblem problem) {
112 problems.add(problem);
113
114 severities.add(problem.getSeverity());
115 }
116
117 public void addAll(List<ModelProblem> problems) {
118 this.problems.addAll(problems);
119
120 for (ModelProblem problem : problems) {
121 severities.add(problem.getSeverity());
122 }
123 }
124
125 @Override
126 public void add(ModelProblemCollectorRequest req) {
127 int line = -1;
128 int column = -1;
129 String source = null;
130 String modelId = null;
131
132 if (req.getLocation() != null) {
133 line = req.getLocation().getLineNumber();
134 column = req.getLocation().getColumnNumber();
135 if (req.getLocation().getSource() != null) {
136 modelId = req.getLocation().getSource().getModelId();
137 source = req.getLocation().getSource().getLocation();
138 }
139 }
140
141 if (modelId == null) {
142 modelId = getModelId();
143 source = getSource();
144 }
145
146 if (line <= 0 && column <= 0 && req.getException() instanceof ModelParseException) {
147 ModelParseException e = (ModelParseException) req.getException();
148 line = e.getLineNumber();
149 column = e.getColumnNumber();
150 }
151
152 ModelProblem problem = new DefaultModelProblem(
153 req.getMessage(),
154 req.getSeverity(),
155 req.getVersion(),
156 source,
157 line,
158 column,
159 modelId,
160 req.getException());
161
162 add(problem);
163 }
164
165 public ModelBuildingException newModelBuildingException() {
166 ModelBuildingResult result = this.result;
167 if (result.getModelIds().isEmpty()) {
168 DefaultModelBuildingResult tmp = new DefaultModelBuildingResult();
169 tmp.setEffectiveModel(result.getEffectiveModel());
170 tmp.setProblems(getProblems());
171 tmp.setActiveExternalProfiles(result.getActiveExternalProfiles());
172 String id = getRootModelId();
173 tmp.addModelId(id);
174 tmp.setRawModel(id, getRootModel());
175 result = tmp;
176 }
177 return new ModelBuildingException(result);
178 }
179 }