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