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.ArrayList;
23  import java.util.EnumSet;
24  import java.util.List;
25  import java.util.Set;
26  
27  import org.apache.maven.model.InputLocation;
28  import org.apache.maven.model.Model;
29  import org.apache.maven.model.building.ModelProblem.Severity;
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 ModelProblemCollector
43  {
44  
45      private List<ModelProblem> problems;
46  
47      private String source;
48  
49      private Model sourceModel;
50  
51      private Model rootModel;
52  
53      private Set<ModelProblem.Severity> severities = EnumSet.noneOf( ModelProblem.Severity.class );
54  
55      public DefaultModelProblemCollector( List<ModelProblem> problems )
56      {
57          this.problems = ( problems != null ) ? problems : new ArrayList<ModelProblem>();
58  
59          for ( ModelProblem problem : this.problems )
60          {
61              severities.add( problem.getSeverity() );
62          }
63      }
64  
65      public boolean hasFatalErrors()
66      {
67          return severities.contains( ModelProblem.Severity.FATAL );
68      }
69  
70      public boolean hasErrors()
71      {
72          return severities.contains( ModelProblem.Severity.ERROR ) || severities.contains( ModelProblem.Severity.FATAL );
73      }
74  
75      public List<ModelProblem> getProblems()
76      {
77          return problems;
78      }
79  
80      public void setSource( String source )
81      {
82          this.source = source;
83          this.sourceModel = null;
84      }
85  
86      public void setSource( Model source )
87      {
88          this.sourceModel = source;
89          this.source = null;
90  
91          if ( rootModel == null )
92          {
93              rootModel = source;
94          }
95      }
96  
97      private String getSource()
98      {
99          if ( source == null && sourceModel != null )
100         {
101             source = ModelProblemUtils.toPath( sourceModel );
102         }
103         return source;
104     }
105 
106     private String getModelId()
107     {
108         return ModelProblemUtils.toId( sourceModel );
109     }
110 
111     public void setRootModel( Model rootModel )
112     {
113         this.rootModel = rootModel;
114     }
115 
116     public Model getRootModel()
117     {
118         return rootModel;
119     }
120 
121     public String getRootModelId()
122     {
123         return ModelProblemUtils.toId( rootModel );
124     }
125 
126     public void add( ModelProblem problem )
127     {
128         problems.add( problem );
129 
130         severities.add( problem.getSeverity() );
131     }
132 
133     public void addAll( List<ModelProblem> problems )
134     {
135         this.problems.addAll( problems );
136 
137         for ( ModelProblem problem : problems )
138         {
139             severities.add( problem.getSeverity() );
140         }
141     }
142 
143     public void add( Severity severity, String message, InputLocation location, Exception cause )
144     {
145         int line = -1;
146         int column = -1;
147         String source = null;
148         String modelId = null;
149 
150         if ( location != null )
151         {
152             line = location.getLineNumber();
153             column = location.getColumnNumber();
154             if ( location.getSource() != null )
155             {
156                 modelId = location.getSource().getModelId();
157                 source = location.getSource().getLocation();
158             }
159         }
160 
161         if ( modelId == null )
162         {
163             modelId = getModelId();
164             source = getSource();
165         }
166 
167         if ( line <= 0 && column <= 0 && cause instanceof ModelParseException )
168         {
169             ModelParseException e = (ModelParseException) cause;
170             line = e.getLineNumber();
171             column = e.getColumnNumber();
172         }
173 
174         ModelProblem problem = new DefaultModelProblem( message, severity, source, line, column, modelId, cause );
175 
176         add( problem );
177     }
178 
179 }