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