001    /*
002     *  Licensed under the Apache License, Version 2.0 (the "License");
003     *  you may not use this file except in compliance with the License.
004     *  You may obtain a copy of the License at
005     *
006     *      http://www.apache.org/licenses/LICENSE-2.0
007     *
008     *  Unless required by applicable law or agreed to in writing, software
009     *  distributed under the License is distributed on an "AS IS" BASIS,
010     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011     *  See the License for the specific language governing permissions and
012     *  limitations under the License.
013     */
014    /*
015     * This file has been modified by Chris Pheby in accordance with Section 4.2
016     * of the Apache Software License
017     */
018    package org.jadira.dependencynavigator.model;
019    
020    import org.apache.maven.model.Dependency;
021    import org.jadira.dependencynavigator.assembler.AssembledModel;
022    import org.jadira.dependencynavigator.controller.ArtifactResolver;
023    import org.jadira.dependencynavigator.implementations.IProgressMeter;
024    
025    import java.util.ArrayList;
026    import java.util.List;
027    import java.util.Stack;
028    
029    public class PomFile extends Artifact {
030    
031        protected List<Artifact> dependencies;
032        protected AssembledModel model;
033    
034        public PomFile(AssembledModel model, Artifact parent, String scope) throws ArtifactInitialisationException {
035            
036            super(parent, 
037                    model.getAssembledModel().getGroupId() == null ? model.getAssembledModel().getParent().getGroupId() : model.getAssembledModel().getGroupId(), 
038                    model.getAssembledModel().getArtifactId(), 
039                    model.getAssembledModel().getVersion() == null ? model.getAssembledModel().getParent().getVersion() : model.getAssembledModel().getVersion(), 
040                    scope);
041            
042            this.model = model;
043        }
044    
045        public void resolveDependencies(ArtifactResolver resolver, IProgressMeter progress, Stack<LeafDependency> path) {
046            progress.resolving(this);
047            dependencies = new ArrayList<Artifact>();
048            // add the dependencies
049            for (Object o : model.getAssembledModel().getDependencies()) {
050                Dependency dependency = (Dependency) o;
051                /*
052                 * Check to see if dependency contains in path
053                 */
054                try {
055                    LeafDependency leafDependency = new LeafDependency(this, dependency, dependency.getScope());
056                    /**
057                     * Prevent circular dependencies
058                     */
059                    if (!path.contains(leafDependency)) {
060                        path.push(leafDependency);
061                        Artifact artifact = resolver.resolveArtifact(this, dependency, progress, path);
062                        if (artifact != null) {
063                            dependencies.add(artifact);
064                        }
065                    }
066                } catch (ArtifactInitialisationException e) {
067                    e.printStackTrace();
068                }
069            }
070        }
071    
072        public AssembledModel getModel() {
073            return model;
074        }
075    
076        @Override
077        public boolean isLeaf() {
078            return dependencies.size() == 0;
079        }
080    
081        @Override
082        public List<Artifact> getDependencies() {
083            return dependencies;
084        }
085    
086        @Override
087        public int dependencyCount() {
088            if (isLeaf()) {
089                return 1;
090            }
091            int dependencyCount = 0;
092            for (Artifact artifact : dependencies) {
093                dependencyCount += artifact.dependencyCount();
094            }
095            return dependencyCount;
096        }
097    
098    }