This project has retired. For details please refer to its Attic page.
TreeDependencyNodeVisitor xref
View Javadoc
1   package org.apache.archiva.dependency.tree.maven2;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import org.apache.archiva.maven2.model.Artifact;
22  import org.apache.archiva.maven2.model.TreeEntry;
23  import org.modelmapper.ModelMapper;
24  import org.modelmapper.convention.MatchingStrategies;
25  import org.sonatype.aether.graph.DependencyNode;
26  import org.sonatype.aether.graph.DependencyVisitor;
27  
28  import java.util.List;
29  
30  /**
31   * @author Olivier Lamy
32   * @since 1.4-M3
33   */
34  public class TreeDependencyNodeVisitor
35      implements DependencyVisitor
36  {
37  
38      final List<TreeEntry> treeEntries;
39  
40      private TreeEntry currentEntry;
41  
42      private org.sonatype.aether.graph.DependencyNode firstDependencyNode;
43  
44      public TreeDependencyNodeVisitor( List<TreeEntry> treeEntries )
45      {
46          this.treeEntries = treeEntries;
47      }
48  
49  
50      @Override
51      public boolean visitEnter( DependencyNode dependencyNode )
52      {
53          TreeEntry entry =
54              new TreeEntry( getModelMapper().map( dependencyNode.getDependency().getArtifact(), Artifact.class ) );
55          entry.getArtifact().setFileExtension( dependencyNode.getDependency().getArtifact().getExtension() );
56          entry.getArtifact().setScope( dependencyNode.getDependency().getScope() );
57          entry.setParent( currentEntry );
58          currentEntry = entry;
59  
60          if ( firstDependencyNode == null )
61          {
62              firstDependencyNode = dependencyNode;
63              treeEntries.add( currentEntry );
64          }
65          else
66          {
67              currentEntry.getParent().getChilds().add( currentEntry );
68          }
69          return true;
70      }
71  
72      @Override
73      public boolean visitLeave( DependencyNode dependencyNode )
74      {
75          currentEntry = currentEntry.getParent();
76          return true;
77      }
78  
79      private static class ModelMapperHolder
80      {
81          private static ModelMapper MODEL_MAPPER = new ModelMapper();
82  
83          static
84          {
85              MODEL_MAPPER.getConfiguration().setMatchingStrategy( MatchingStrategies.STRICT );
86          }
87      }
88  
89      protected ModelMapper getModelMapper()
90      {
91          return ModelMapperHolder.MODEL_MAPPER;
92      }
93  }