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
029import static java.util.Objects.requireNonNull;
030
031/**
032 * A dependency graph transformer that refines the request context for nodes that belong to the "project" context by
033 * appending the classpath type to which the node belongs. For instance, a compile-time project dependency will be
034 * assigned the request context "project/compile".
035 * 
036 * @see DependencyNode#getRequestContext()
037 */
038public final class JavaDependencyContextRefiner
039    implements DependencyGraphTransformer
040{
041
042    public DependencyNode transformGraph( DependencyNode node, DependencyGraphTransformationContext context )
043        throws RepositoryException
044    {
045        requireNonNull( node, "node cannot be null" );
046        requireNonNull( context, "context cannot be null" );
047        String ctx = node.getRequestContext();
048
049        if ( "project".equals( ctx ) )
050        {
051            String scope = getClasspathScope( node );
052            if ( scope != null )
053            {
054                ctx += '/' + scope;
055                node.setRequestContext( ctx );
056            }
057        }
058
059        for ( DependencyNode child : node.getChildren() )
060        {
061            transformGraph( child, context );
062        }
063
064        return node;
065    }
066
067    private String getClasspathScope( DependencyNode node )
068    {
069        Dependency dependency = node.getDependency();
070        if ( dependency == null )
071        {
072            return null;
073        }
074
075        String scope = dependency.getScope();
076
077        if ( JavaScopes.COMPILE.equals( scope ) || JavaScopes.SYSTEM.equals( scope )
078            || JavaScopes.PROVIDED.equals( scope ) )
079        {
080            return JavaScopes.COMPILE;
081        }
082        else if ( JavaScopes.RUNTIME.equals( scope ) )
083        {
084            return JavaScopes.RUNTIME;
085        }
086        else if ( JavaScopes.TEST.equals( scope ) )
087        {
088            return JavaScopes.TEST;
089        }
090
091        return null;
092    }
093
094}