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