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   */
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 }