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    @Override
043    public DependencyNode transformGraph(DependencyNode node, DependencyGraphTransformationContext context)
044            throws RepositoryException {
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            String scope = getBuildpathScope(node);
051            if (scope != null) {
052                ctx += '/' + scope;
053                node.setRequestContext(ctx);
054            }
055        }
056
057        for (DependencyNode child : node.getChildren()) {
058            transformGraph(child, context);
059        }
060
061        return node;
062    }
063
064    private String getBuildpathScope(DependencyNode node) {
065        Dependency dependency = node.getDependency();
066        if (dependency == null) {
067            return null;
068        }
069
070        String scope = dependency.getScope();
071
072        if (JavaScopes.COMPILE.equals(scope) || JavaScopes.SYSTEM.equals(scope) || JavaScopes.PROVIDED.equals(scope)) {
073            return JavaScopes.COMPILE;
074        } else if (JavaScopes.RUNTIME.equals(scope)) {
075            return JavaScopes.RUNTIME;
076        } else if (JavaScopes.TEST.equals(scope)) {
077            return JavaScopes.TEST;
078        }
079
080        return null;
081    }
082}