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