001package org.eclipse.aether.internal.impl;
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 javax.inject.Named;
023import javax.inject.Singleton;
024
025import org.eclipse.aether.artifact.Artifact;
026import org.eclipse.aether.metadata.Metadata;
027
028import static java.util.Objects.requireNonNull;
029
030/**
031 * Default implementation of {@link LocalPathComposer}.
032 *
033 * @since 1.8.1
034 */
035@Singleton
036@Named
037public final class DefaultLocalPathComposer implements LocalPathComposer
038{
039    @Override
040    public String getPathForArtifact( Artifact artifact, boolean local )
041    {
042        requireNonNull( artifact );
043
044        StringBuilder path = new StringBuilder( 128 );
045
046        path.append( artifact.getGroupId().replace( '.', '/' ) ).append( '/' );
047
048        path.append( artifact.getArtifactId() ).append( '/' );
049
050        path.append( artifact.getBaseVersion() ).append( '/' );
051
052        path.append( artifact.getArtifactId() ).append( '-' );
053        if ( local )
054        {
055            path.append( artifact.getBaseVersion() );
056        }
057        else
058        {
059            path.append( artifact.getVersion() );
060        }
061
062        if ( artifact.getClassifier().length() > 0 )
063        {
064            path.append( '-' ).append( artifact.getClassifier() );
065        }
066
067        if ( artifact.getExtension().length() > 0 )
068        {
069            path.append( '.' ).append( artifact.getExtension() );
070        }
071
072        return path.toString();
073    }
074
075    @Override
076    public String getPathForMetadata( Metadata metadata, String repositoryKey )
077    {
078        requireNonNull( metadata );
079        requireNonNull( repositoryKey );
080
081        StringBuilder path = new StringBuilder( 128 );
082
083        if ( metadata.getGroupId().length() > 0 )
084        {
085            path.append( metadata.getGroupId().replace( '.', '/' ) ).append( '/' );
086
087            if ( metadata.getArtifactId().length() > 0 )
088            {
089                path.append( metadata.getArtifactId() ).append( '/' );
090
091                if ( metadata.getVersion().length() > 0 )
092                {
093                    path.append( metadata.getVersion() ).append( '/' );
094                }
095            }
096        }
097
098        path.append( insertRepositoryKey( metadata.getType(), repositoryKey ) );
099
100        return path.toString();
101    }
102
103    private String insertRepositoryKey( String metadataType, String repositoryKey )
104    {
105        String result;
106        int idx = metadataType.indexOf( '.' );
107        if ( idx < 0 )
108        {
109            result = metadataType + '-' + repositoryKey;
110        }
111        else
112        {
113            result = metadataType.substring( 0, idx ) + '-' + repositoryKey + metadataType.substring( idx );
114        }
115        return result;
116    }
117}