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.util.artifact.JavaScopes;
024import org.eclipse.aether.util.graph.transformer.ConflictResolver.ScopeContext;
025import org.eclipse.aether.util.graph.transformer.ConflictResolver.ScopeDeriver;
026
027/**
028 * A scope deriver for use with {@link ConflictResolver} that supports the scopes from {@link JavaScopes}.
029 */
030public final class JavaScopeDeriver
031    extends ScopeDeriver
032{
033
034    /**
035     * Creates a new instance of this scope deriver.
036     */
037    public JavaScopeDeriver()
038    {
039    }
040
041    @Override
042    public void deriveScope( ScopeContext context )
043        throws RepositoryException
044    {
045        context.setDerivedScope( getDerivedScope( context.getParentScope(), context.getChildScope() ) );
046    }
047
048    private String getDerivedScope( String parentScope, String childScope )
049    {
050        String derivedScope;
051
052        if ( JavaScopes.SYSTEM.equals( childScope ) || JavaScopes.TEST.equals( childScope ) )
053        {
054            derivedScope = childScope;
055        }
056        else if ( parentScope == null || parentScope.isEmpty() || JavaScopes.COMPILE.equals( parentScope ) )
057        {
058            derivedScope = childScope;
059        }
060        else if ( JavaScopes.TEST.equals( parentScope ) || JavaScopes.RUNTIME.equals( parentScope ) )
061        {
062            derivedScope = parentScope;
063        }
064        else if ( JavaScopes.SYSTEM.equals( parentScope ) || JavaScopes.PROVIDED.equals( parentScope ) )
065        {
066            derivedScope = JavaScopes.PROVIDED;
067        }
068        else
069        {
070            derivedScope = JavaScopes.RUNTIME;
071        }
072
073        return derivedScope;
074    }
075
076}