001package org.eclipse.aether.util.graph.transformer;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.eclipse.aether.RepositoryException;
023import org.eclipse.aether.collection.DependencyGraphTransformationContext;
024import org.eclipse.aether.collection.DependencyGraphTransformer;
025import org.eclipse.aether.graph.Dependency;
026import org.eclipse.aether.graph.DependencyNode;
027import org.eclipse.aether.util.artifact.JavaScopes;
028
029/**
030 * A dependency graph transformer that refines the request context for nodes that belong to the "project" context by
031 * appending the classpath type to which the node belongs. For instance, a compile-time project dependency will be
032 * assigned the request context "project/compile".
033 * 
034 * @see DependencyNode#getRequestContext()
035 */
036public final class JavaDependencyContextRefiner
037    implements DependencyGraphTransformer
038{
039
040    public DependencyNode transformGraph( DependencyNode node, DependencyGraphTransformationContext context )
041        throws RepositoryException
042    {
043        String ctx = node.getRequestContext();
044
045        if ( "project".equals( ctx ) )
046        {
047            String scope = getClasspathScope( node );
048            if ( scope != null )
049            {
050                ctx += '/' + scope;
051                node.setRequestContext( ctx );
052            }
053        }
054
055        for ( DependencyNode child : node.getChildren() )
056        {
057            transformGraph( child, context );
058        }
059
060        return node;
061    }
062
063    private String getClasspathScope( DependencyNode node )
064    {
065        Dependency dependency = node.getDependency();
066        if ( dependency == null )
067        {
068            return null;
069        }
070
071        String scope = dependency.getScope();
072
073        if ( JavaScopes.COMPILE.equals( scope ) || JavaScopes.SYSTEM.equals( scope )
074            || JavaScopes.PROVIDED.equals( scope ) )
075        {
076            return JavaScopes.COMPILE;
077        }
078        else if ( JavaScopes.RUNTIME.equals( scope ) )
079        {
080            return JavaScopes.RUNTIME;
081        }
082        else if ( JavaScopes.TEST.equals( scope ) )
083        {
084            return JavaScopes.TEST;
085        }
086
087        return null;
088    }
089
090}