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.io.ModelParseException;
30  
31  /**
32   * Collects problems that are encountered during model building. The primary purpose of this component is to account for
33   * the fact that the problem reporter has/should not have information about the calling context and hence cannot provide
34   * an expressive source hint for the model problem. Instead, the source hint is configured by the model builder before
35   * it delegates to other components that potentially encounter problems. Then, the problem reporter can focus on
36   * providing a simple error message, leaving the donkey work of creating a nice model problem to this component.
37   * 
38   * @author Benjamin Bentmann
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 }