001    package org.apache.maven.project.harness;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *  http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.io.File;
023    import java.util.Iterator;
024    
025    import org.apache.commons.jxpath.JXPathContext;
026    import org.apache.commons.jxpath.JXPathNotFoundException;
027    import org.apache.commons.jxpath.ri.JXPathContextReferenceImpl;
028    import org.apache.maven.project.MavenProject;
029    
030    public class PomTestWrapper
031    {
032    
033        private File pomFile;
034    
035        private JXPathContext context;
036    
037        private MavenProject mavenProject;
038    
039        static
040        {
041            JXPathContextReferenceImpl.addNodePointerFactory( new Xpp3DomPointerFactory() );
042        }
043    
044        public PomTestWrapper( File pomFile, MavenProject mavenProject )
045        {
046            if ( mavenProject == null )
047            {
048                throw new IllegalArgumentException( "mavenProject: null" );
049            }
050            this.mavenProject = mavenProject;
051            this.pomFile = pomFile;
052            context = JXPathContext.newContext( mavenProject.getModel() );
053        }
054    
055        public PomTestWrapper( MavenProject mavenProject )
056        {
057            if ( mavenProject == null )
058            {
059                throw new IllegalArgumentException( "mavenProject: null" );
060            }
061            this.mavenProject = mavenProject;
062            context = JXPathContext.newContext( mavenProject.getModel() );
063        }
064    
065        public MavenProject getMavenProject()
066        {
067            return mavenProject;
068        }
069    
070        public File getBasedir()
071        {
072            return ( pomFile != null ) ? pomFile.getParentFile() : null;
073        }
074    
075        public void setValueOnModel( String expression, Object value )
076        {
077            context.setValue( expression, value );
078        }
079    
080        /*
081        public int containerCountForUri( String uri )
082            throws IOException
083        {
084            if ( uri == null || uri.trim().equals( "" ) )
085            {
086                throw new IllegalArgumentException( "uri: null or empty" );
087            }
088            ModelDataSource source = new DefaultModelDataSource();
089            source.init( domainModel.getModelProperties(), null );
090            return source.queryFor( uri ).size();
091        }
092            */
093    
094            public Iterator<?> getIteratorForXPathExpression( String expression )
095        {
096            return context.iterate( expression );
097        }
098    
099        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    }