View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.resolver.internal.ant.types;
20  
21  import org.apache.maven.model.Model;
22  import org.apache.tools.ant.Project;
23  import org.codehaus.plexus.interpolation.reflection.ReflectionValueExtractor;
24  
25  /**
26   */
27  class ModelValueExtractor {
28  
29      private static final String PREFIX_PROPERTIES = "properties.";
30  
31      private final String prefix;
32  
33      private final Project project;
34  
35      private final Model model;
36  
37      ModelValueExtractor(String prefix, Model model, Project project) {
38          if (model == null) {
39              throw new IllegalArgumentException("reference to Maven POM has not been specified");
40          }
41          if (project == null) {
42              throw new IllegalArgumentException("reference to Ant project has not been specified");
43          }
44          if (prefix == null || prefix.length() <= 0) {
45              prefix = "pom.";
46          } else if (!prefix.endsWith(".")) {
47              prefix += '.';
48          }
49          this.prefix = prefix;
50          this.model = model;
51          this.project = project;
52      }
53  
54      public Project getProject() {
55          return project;
56      }
57  
58      public boolean isApplicable(String expression) {
59          return expression.startsWith(prefix);
60      }
61  
62      public Object getValue(String expression) {
63          if (expression.startsWith(prefix)) {
64              String expr = expression.substring(prefix.length());
65              try {
66                  if (expr.startsWith(PREFIX_PROPERTIES)) {
67                      String key = expr.substring(PREFIX_PROPERTIES.length());
68                      return model.getProperties().getProperty(key);
69                  }
70  
71                  return ReflectionValueExtractor.evaluate(expr, model, false);
72              } catch (Exception e) {
73                  project.log("Could not retrieve '" + expression + "' from POM: " + e.getMessage(), e, Project.MSG_WARN);
74                  return null;
75              }
76          } else {
77              return null;
78          }
79      }
80  }