View Javadoc
1   package org.apache.maven.resolver.internal.ant.types;
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 org.apache.maven.model.Model;
23  import org.apache.tools.ant.Project;
24  import org.codehaus.plexus.interpolation.reflection.ReflectionValueExtractor;
25  
26  /**
27   */
28  class ModelValueExtractor
29  {
30  
31      private static final String PREFIX_PROPERTIES = "properties.";
32  
33      private final String prefix;
34  
35      private final Project project;
36  
37      private final Model model;
38  
39      ModelValueExtractor( String prefix, Model model, Project project )
40      {
41          if ( model == null )
42          {
43              throw new IllegalArgumentException( "reference to Maven POM has not been specified" );
44          }
45          if ( project == null )
46          {
47              throw new IllegalArgumentException( "reference to Ant project has not been specified" );
48          }
49          if ( prefix == null || prefix.length() <= 0 )
50          {
51              prefix = "pom.";
52          }
53          else if ( !prefix.endsWith( "." ) )
54          {
55              prefix += '.';
56          }
57          this.prefix = prefix;
58          this.model = model;
59          this.project = project;
60      }
61  
62      public Project getProject()
63      {
64          return project;
65      }
66  
67      public boolean isApplicable( String expression )
68      {
69          return expression.startsWith( prefix );
70      }
71  
72      public Object getValue( String expression )
73      {
74          if ( expression.startsWith( prefix ) )
75          {
76              String expr = expression.substring( prefix.length() );
77              try
78              {
79                  if ( expr.startsWith( PREFIX_PROPERTIES ) )
80                  {
81                      String key = expr.substring( PREFIX_PROPERTIES.length() );
82                      return model.getProperties().getProperty( key );
83                  }
84  
85                  return ReflectionValueExtractor.evaluate( expr, model, false );
86              }
87              catch ( Exception e )
88              {
89                  project.log( "Could not retrieve '" + expression + "' from POM: " + e.getMessage(), e, Project.MSG_WARN );
90                  return null;
91              }
92          }
93          else
94          {
95              return null;
96          }
97      }
98  
99  }