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