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