001package 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 021import org.apache.archiva.maven2.model.Artifact; 022import org.apache.archiva.maven2.model.TreeEntry; 023import org.modelmapper.ModelMapper; 024import org.modelmapper.convention.MatchingStrategies; 025import org.sonatype.aether.graph.DependencyNode; 026import org.sonatype.aether.graph.DependencyVisitor; 027 028import java.util.List; 029 030/** 031 * @author Olivier Lamy 032 * @since 1.4-M3 033 */ 034public class TreeDependencyNodeVisitor 035 implements DependencyVisitor 036{ 037 038 final List<TreeEntry> treeEntries; 039 040 private TreeEntry currentEntry; 041 042 private org.sonatype.aether.graph.DependencyNode firstDependencyNode; 043 044 public TreeDependencyNodeVisitor( List<TreeEntry> treeEntries ) 045 { 046 this.treeEntries = treeEntries; 047 } 048 049 050 @Override 051 public boolean visitEnter( DependencyNode dependencyNode ) 052 { 053 TreeEntry entry = 054 new TreeEntry( getModelMapper().map( dependencyNode.getDependency().getArtifact(), Artifact.class ) ); 055 entry.getArtifact().setFileExtension( dependencyNode.getDependency().getArtifact().getExtension() ); 056 entry.getArtifact().setScope( dependencyNode.getDependency().getScope() ); 057 entry.setParent( currentEntry ); 058 currentEntry = entry; 059 060 if ( firstDependencyNode == null ) 061 { 062 firstDependencyNode = dependencyNode; 063 treeEntries.add( currentEntry ); 064 } 065 else 066 { 067 currentEntry.getParent().getChilds().add( currentEntry ); 068 } 069 return true; 070 } 071 072 @Override 073 public boolean visitLeave( DependencyNode dependencyNode ) 074 { 075 currentEntry = currentEntry.getParent(); 076 return true; 077 } 078 079 private static class ModelMapperHolder 080 { 081 private static ModelMapper MODEL_MAPPER = new ModelMapper(); 082 083 static 084 { 085 MODEL_MAPPER.getConfiguration().setMatchingStrategy( MatchingStrategies.STRICT ); 086 } 087 } 088 089 protected ModelMapper getModelMapper() 090 { 091 return ModelMapperHolder.MODEL_MAPPER; 092 } 093}