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