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