View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Collects problems that are encountered during model building. The primary purpose of this component is to account for
30   * the fact that the problem reporter has/should not have information about the calling context and hence cannot provide
31   * an expressive source hint for the model problem. Instead, the source hint is configured by the model builder before
32   * it delegates to other components that potentially encounter problems. Then, the problem reporter can focus on
33   * providing a simple error message, leaving the donkey work of creating a nice model problem to this component.
34   *
35   * @author Benjamin Bentmann
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 }