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.DependencyScopes;
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 buildpath 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 *
038 * @deprecated This class belongs to consumer project. Resolver have no notion of scopes other than those defined
039 * in {@link DependencyScopes} class, moreover it has no knowledge about scope transformation
040 * of dependencies to build path scopes.
041 */
042@Deprecated
043public final class JavaDependencyContextRefiner implements DependencyGraphTransformer {
044
045    public DependencyNode transformGraph(DependencyNode node, DependencyGraphTransformationContext context)
046            throws RepositoryException {
047        requireNonNull(node, "node cannot be null");
048        requireNonNull(context, "context cannot be null");
049        String ctx = node.getRequestContext();
050
051        if ("project".equals(ctx)) {
052            String scope = getBuildpathScope(node);
053            if (scope != null) {
054                ctx += '/' + scope;
055                node.setRequestContext(ctx);
056            }
057        }
058
059        for (DependencyNode child : node.getChildren()) {
060            transformGraph(child, context);
061        }
062
063        return node;
064    }
065
066    private String getBuildpathScope(DependencyNode node) {
067        Dependency dependency = node.getDependency();
068        if (dependency == null) {
069            return null;
070        }
071
072        String scope = dependency.getScope();
073
074        if (JavaScopes.COMPILE.equals(scope) || JavaScopes.SYSTEM.equals(scope) || JavaScopes.PROVIDED.equals(scope)) {
075            return JavaScopes.COMPILE;
076        } else if (JavaScopes.RUNTIME.equals(scope)) {
077            return JavaScopes.RUNTIME;
078        } else if (JavaScopes.TEST.equals(scope)) {
079            return JavaScopes.TEST;
080        }
081
082        return null;
083    }
084}