View Javadoc

1   package org.apache.maven.model.interpolation;
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.List;
23  import org.apache.maven.model.building.ModelProblem;
24  
25  import org.apache.maven.model.building.ModelProblemCollector;
26  import org.apache.maven.model.building.ModelProblem.Severity;
27  import org.apache.maven.model.building.ModelProblem.Version;
28  import org.apache.maven.model.building.ModelProblemCollectorRequest;
29  import org.codehaus.plexus.interpolation.ValueSource;
30  
31  /**
32   * Wraps another value source and intercepts interpolated expressions, checking for problems.
33   * 
34   * @author Benjamin Bentmann
35   */
36  class ProblemDetectingValueSource
37      implements ValueSource
38  {
39  
40      private final ValueSource valueSource;
41  
42      private final String bannedPrefix;
43  
44      private final String newPrefix;
45  
46      private final ModelProblemCollector problems;
47  
48      public ProblemDetectingValueSource( ValueSource valueSource, String bannedPrefix, String newPrefix,
49                                          ModelProblemCollector problems )
50      {
51          this.valueSource = valueSource;
52          this.bannedPrefix = bannedPrefix;
53          this.newPrefix = newPrefix;
54          this.problems = problems;
55      }
56  
57      public Object getValue( String expression )
58      {
59          Object value = valueSource.getValue( expression );
60  
61          if ( value != null && expression.startsWith( bannedPrefix ) )
62          {
63              String msg = "The expression ${" + expression + "} is deprecated.";
64              if ( newPrefix != null && newPrefix.length() > 0 )
65              {
66                  msg += " Please use ${" + newPrefix + expression.substring( bannedPrefix.length() ) + "} instead.";
67              }
68              problems.add( new ModelProblemCollectorRequest( Severity.WARNING, Version.V20 ).setMessage( msg ));
69          }
70  
71          return value;
72      }
73  
74      @SuppressWarnings( "unchecked" )
75      public List getFeedback()
76      {
77          return valueSource.getFeedback();
78      }
79  
80      public void clearFeedback()
81      {
82          valueSource.clearFeedback();
83      }
84  
85  }