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