View Javadoc
1   package org.apache.maven.project.harness;
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.io.File;
23  import java.util.Iterator;
24  import java.util.Objects;
25  
26  import org.apache.commons.jxpath.JXPathContext;
27  import org.apache.commons.jxpath.JXPathNotFoundException;
28  import org.apache.commons.jxpath.ri.JXPathContextReferenceImpl;
29  import org.apache.maven.project.MavenProject;
30  
31  public class PomTestWrapper
32  {
33  
34      private File pomFile;
35  
36      private JXPathContext context;
37  
38      private MavenProject mavenProject;
39  
40      static
41      {
42          JXPathContextReferenceImpl.addNodePointerFactory( new Xpp3DomPointerFactory() );
43      }
44  
45      public PomTestWrapper( File pomFile, MavenProject mavenProject )
46      {
47          this.mavenProject = Objects.requireNonNull( mavenProject, "mavenProject cannot be null" );
48          this.pomFile = pomFile;
49          context = JXPathContext.newContext( mavenProject.getModel() );
50      }
51  
52      public PomTestWrapper( MavenProject mavenProject )
53      {
54          this.mavenProject = Objects.requireNonNull( mavenProject, "mavenProject cannot be null" );
55          context = JXPathContext.newContext( mavenProject.getModel() );
56      }
57  
58      public MavenProject getMavenProject()
59      {
60          return mavenProject;
61      }
62  
63      public File getBasedir()
64      {
65          return ( pomFile != null ) ? pomFile.getParentFile() : null;
66      }
67  
68      public void setValueOnModel( String expression, Object value )
69      {
70          context.setValue( expression, value );
71      }
72  
73      /*
74      public int containerCountForUri( String uri )
75          throws IOException
76      {
77          Validate.notEmpty( uri, "uri can neither be null nor empty " );
78          ModelDataSource source = new DefaultModelDataSource();
79          source.init( domainModel.getModelProperties(), null );
80          return source.queryFor( uri ).size();
81      }
82  	*/
83  
84  	public Iterator<?> getIteratorForXPathExpression( String expression )
85      {
86          return context.iterate( expression );
87      }
88  
89      public boolean containsXPathExpression( String expression )
90      {
91          return context.getValue( expression ) != null;
92      }
93  
94      public Object getValue( String expression )
95      {
96          try
97          {
98              return context.getValue( expression );
99          }
100         catch ( JXPathNotFoundException e )
101         {
102             return null;
103         }
104     }
105 
106     public boolean xPathExpressionEqualsValue( String expression, String value )
107     {
108         return context.getValue( expression ) != null && context.getValue( expression ).equals( value );
109     }
110 
111 }