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.lifecycle;
20  
21  import java.io.IOException;
22  import java.util.Iterator;
23  
24  import org.apache.commons.jxpath.JXPathContext;
25  import org.apache.commons.jxpath.JXPathNotFoundException;
26  import org.apache.commons.jxpath.ri.JXPathContextReferenceImpl;
27  import org.apache.maven.plugin.MojoExecution;
28  import org.apache.maven.project.harness.Xpp3DomPointerFactory;
29  
30  public class MojoExecutionXPathContainer {
31      private JXPathContext context;
32  
33      static {
34          JXPathContextReferenceImpl.addNodePointerFactory(new Xpp3DomPointerFactory());
35      }
36  
37      public MojoExecutionXPathContainer(MojoExecution mojoExecution) throws IOException {
38          context = JXPathContext.newContext(mojoExecution);
39      }
40  
41      public Iterator<?> getIteratorForXPathExpression(String expression) {
42          return context.iterate(expression);
43      }
44  
45      public boolean containsXPathExpression(String expression) {
46          return context.getValue(expression) != null;
47      }
48  
49      public Object getValue(String expression) {
50          try {
51              return context.getValue(expression);
52          } catch (JXPathNotFoundException e) {
53              return null;
54          }
55      }
56  
57      public boolean xPathExpressionEqualsValue(String expression, String value) {
58          return context.getValue(expression) != null
59                  && context.getValue(expression).equals(value);
60      }
61  }