View Javadoc

1   package org.apache.maven.model.building;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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.building.ModelProblem.Version;
30  import org.apache.maven.model.io.ModelParseException;
31  
32  /**
33   * Collects problems that are encountered during model building. The primary purpose of this component is to account for
34   * the fact that the problem reporter has/should not have information about the calling context and hence cannot provide
35   * an expressive source hint for the model problem. Instead, the source hint is configured by the model builder before
36   * it delegates to other components that potentially encounter problems. Then, the problem reporter can focus on
37   * providing a simple error message, leaving the donkey work of creating a nice model problem to this component.
38   * 
39   * @author Benjamin Bentmann
40   */
41  class DefaultModelProblemCollector
42      implements ModelProblemCollectorExt
43  {
44  
45      private final ModelBuildingResult result;
46  
47      private List<ModelProblem> problems;
48  
49      private String source;
50  
51      private Model sourceModel;
52  
53      private Model rootModel;
54  
55      private Set<ModelProblem.Severity> severities = EnumSet.noneOf( ModelProblem.Severity.class );
56  
57      public DefaultModelProblemCollector( ModelBuildingResult result )
58      {
59          this.result = result;
60          this.problems = result.getProblems();
61  
62          for ( ModelProblem problem : this.problems )
63          {
64              severities.add( problem.getSeverity() );
65          }
66      }
67  
68      public boolean hasFatalErrors()
69      {
70          return severities.contains( ModelProblem.Severity.FATAL );
71      }
72  
73      public boolean hasErrors()
74      {
75          return severities.contains( ModelProblem.Severity.ERROR ) || severities.contains( ModelProblem.Severity.FATAL );
76      }
77  
78      public List<ModelProblem> getProblems()
79      {
80          return problems;
81      }
82  
83      public void setSource( String source )
84      {
85          this.source = source;
86          this.sourceModel = null;
87      }
88  
89      public void setSource( Model source )
90      {
91          this.sourceModel = source;
92          this.source = null;
93  
94          if ( rootModel == null )
95          {
96              rootModel = source;
97          }
98      }
99  
100     private String getSource()
101     {
102         if ( source == null && sourceModel != null )
103         {
104             source = ModelProblemUtils.toPath( sourceModel );
105         }
106         return source;
107     }
108 
109     private String getModelId()
110     {
111         return ModelProblemUtils.toId( sourceModel );
112     }
113 
114     public void setRootModel( Model rootModel )
115     {
116         this.rootModel = rootModel;
117     }
118 
119     public Model getRootModel()
120     {
121         return rootModel;
122     }
123 
124     public String getRootModelId()
125     {
126         return ModelProblemUtils.toId( rootModel );
127     }
128 
129     public void add( ModelProblem problem )
130     {
131         problems.add( problem );
132 
133         severities.add( problem.getSeverity() );
134     }
135 
136     public void addAll( List<ModelProblem> problems )
137     {
138         this.problems.addAll( problems );
139 
140         for ( ModelProblem problem : problems )
141         {
142             severities.add( problem.getSeverity() );
143         }
144     }
145 
146     public void add( ModelProblemCollectorRequest req )
147     {
148         int line = -1;
149         int column = -1;
150         String source = null;
151         String modelId = null;
152 
153         if ( req.getLocation() != null )
154         {
155             line = req.getLocation().getLineNumber();
156             column = req.getLocation().getColumnNumber();
157             if ( req.getLocation().getSource() != null )
158             {
159                 modelId = req.getLocation().getSource().getModelId();
160                 source = req.getLocation().getSource().getLocation();
161             }
162         }
163 
164         if ( modelId == null )
165         {
166             modelId = getModelId();
167             source = getSource();
168         }
169 
170         if ( line <= 0 && column <= 0 && req.getException() instanceof ModelParseException )
171         {
172             ModelParseException e = (ModelParseException) req.getException();
173             line = e.getLineNumber();
174             column = e.getColumnNumber();
175         }
176 
177         ModelProblem problem = new DefaultModelProblem( req.getMessage(), req.getSeverity(), req.getVersion(), source, line, column, modelId, req.getException() );
178 
179         add( problem );
180     }
181 
182     public ModelBuildingException newModelBuildingException()
183     {
184         ModelBuildingResult result = this.result;
185         if ( result.getModelIds().isEmpty() )
186         {
187             DefaultModelBuildingResult tmp = new DefaultModelBuildingResult();
188             tmp.setEffectiveModel( result.getEffectiveModel() );
189             tmp.setProblems( getProblems() );
190             tmp.setActiveExternalProfiles( result.getActiveExternalProfiles() );
191             String id = getRootModelId();
192             tmp.addModelId( id );
193             tmp.setRawModel( id, getRootModel() );
194             result = tmp;
195         }
196         return new ModelBuildingException( result );
197     }
198 
199 }