001    package org.apache.archiva.dependency.tree.maven2;
002    /*
003     * Licensed to the Apache Software Foundation (ASF) under one
004     * or more contributor license agreements.  See the NOTICE file
005     * distributed with this work for additional information
006     * regarding copyright ownership.  The ASF licenses this file
007     * to you under the Apache License, Version 2.0 (the
008     * "License"); you may not use this file except in compliance
009     * with the License.  You may obtain a copy of the License at
010     *
011     *   http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing,
014     * software distributed under the License is distributed on an
015     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016     * KIND, either express or implied.  See the License for the
017     * specific language governing permissions and limitations
018     * under the License.
019     */
020    
021    import net.sf.beanlib.provider.replicator.BeanReplicator;
022    import org.apache.archiva.maven2.model.Artifact;
023    import org.apache.archiva.maven2.model.TreeEntry;
024    import org.sonatype.aether.graph.DependencyNode;
025    import org.sonatype.aether.graph.DependencyVisitor;
026    
027    import java.util.List;
028    
029    /**
030     * @author Olivier Lamy
031     * @since 1.4-M3
032     */
033    public class TreeDependencyNodeVisitor
034        implements DependencyVisitor
035    {
036    
037        final List<TreeEntry> treeEntries;
038    
039        private TreeEntry currentEntry;
040    
041        private org.sonatype.aether.graph.DependencyNode firstDependencyNode;
042    
043        public TreeDependencyNodeVisitor( List<TreeEntry> treeEntries )
044        {
045            this.treeEntries = treeEntries;
046        }
047    
048    
049        public boolean visitEnter( DependencyNode dependencyNode )
050        {
051            TreeEntry entry = new TreeEntry(
052                new BeanReplicator().replicateBean( dependencyNode.getDependency().getArtifact(), Artifact.class ) );
053            entry.getArtifact().setScope( dependencyNode.getDependency().getScope() );
054            entry.setParent( currentEntry );
055            currentEntry = entry;
056    
057            if ( firstDependencyNode == null )
058            {
059                firstDependencyNode = dependencyNode;
060                treeEntries.add( currentEntry );
061            }
062            else
063            {
064                currentEntry.getParent().getChilds().add( currentEntry );
065            }
066            return true;
067        }
068    
069        public boolean visitLeave( DependencyNode dependencyNode )
070        {
071            currentEntry = currentEntry.getParent();
072            return true;
073        }
074    }