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.eclipse.aether.graph.DependencyVisitor;
24  import org.modelmapper.ModelMapper;
25  import org.modelmapper.convention.MatchingStrategies;
26  import org.eclipse.aether.graph.DependencyNode;
27  import org.eclipse.aether.graph.DependencyVisitor;
28  
29  import java.util.List;
30  
31  /**
32   * @author Olivier Lamy
33   * @since 1.4-M3
34   */
35  public class TreeDependencyNodeVisitor
36      implements DependencyVisitor
37  {
38  
39      final List<TreeEntry> treeEntries;
40  
41      private TreeEntry currentEntry;
42  
43      private org.eclipse.aether.graph.DependencyNode firstDependencyNode;
44  
45      public TreeDependencyNodeVisitor( List<TreeEntry> treeEntries )
46      {
47          this.treeEntries = treeEntries;
48      }
49  
50  
51      @Override
52      public boolean visitEnter( DependencyNode dependencyNode )
53      {
54          TreeEntry entry =
55              new TreeEntry( getModelMapper().map( dependencyNode.getDependency().getArtifact(), Artifact.class ) );
56          entry.getArtifact().setFileExtension( dependencyNode.getDependency().getArtifact().getExtension() );
57          entry.getArtifact().setScope( dependencyNode.getDependency().getScope() );
58          entry.setParent( currentEntry );
59          currentEntry = entry;
60  
61          if ( firstDependencyNode == null )
62          {
63              firstDependencyNode = dependencyNode;
64              treeEntries.add( currentEntry );
65          }
66          else
67          {
68              currentEntry.getParent().getChilds().add( currentEntry );
69          }
70          return true;
71      }
72  
73      @Override
74      public boolean visitLeave( DependencyNode dependencyNode )
75      {
76          currentEntry = currentEntry.getParent();
77          return true;
78      }
79  
80      private static class ModelMapperHolder
81      {
82          private static ModelMapper MODEL_MAPPER = new ModelMapper();
83  
84          static
85          {
86              MODEL_MAPPER.getConfiguration().setMatchingStrategy( MatchingStrategies.STRICT );
87          }
88      }
89  
90      protected ModelMapper getModelMapper()
91      {
92          return ModelMapperHolder.MODEL_MAPPER;
93      }
94  }