001package org.apache.maven.artifact.repository.layout;
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.apache.maven.artifact.Artifact;
023import org.apache.maven.artifact.handler.ArtifactHandler;
024import org.apache.maven.artifact.metadata.ArtifactMetadata;
025import org.apache.maven.artifact.repository.ArtifactRepository;
026import org.codehaus.plexus.component.annotations.Component;
027
028/**
029 * @author jdcasey
030 */
031@Component( role = ArtifactRepositoryLayout.class, hint = "default" )
032public class DefaultRepositoryLayout
033    implements ArtifactRepositoryLayout
034{
035    private static final char PATH_SEPARATOR = '/';
036
037    private static final char GROUP_SEPARATOR = '.';
038
039    private static final char ARTIFACT_SEPARATOR = '-';
040
041    public String getId()
042    {
043        return "default";
044    }
045
046    public String pathOf( Artifact artifact )
047    {
048        ArtifactHandler artifactHandler = artifact.getArtifactHandler();
049
050        StringBuilder path = new StringBuilder( 128 );
051
052        path.append( formatAsDirectory( artifact.getGroupId() ) ).append( PATH_SEPARATOR );
053        path.append( artifact.getArtifactId() ).append( PATH_SEPARATOR );
054        path.append( artifact.getBaseVersion() ).append( PATH_SEPARATOR );
055        path.append( artifact.getArtifactId() ).append( ARTIFACT_SEPARATOR ).append( artifact.getVersion() );
056
057        if ( artifact.hasClassifier() )
058        {
059            path.append( ARTIFACT_SEPARATOR ).append( artifact.getClassifier() );
060        }
061
062        if ( artifactHandler.getExtension() != null && artifactHandler.getExtension().length() > 0 )
063        {
064            path.append( GROUP_SEPARATOR ).append( artifactHandler.getExtension() );
065        }
066
067        return path.toString();
068    }
069
070    public String pathOfLocalRepositoryMetadata( ArtifactMetadata metadata, ArtifactRepository repository )
071    {
072        return pathOfRepositoryMetadata( metadata, metadata.getLocalFilename( repository ) );
073    }
074
075    private String pathOfRepositoryMetadata( ArtifactMetadata metadata,
076                                             String filename )
077    {
078        StringBuilder path = new StringBuilder( 128 );
079
080        path.append( formatAsDirectory( metadata.getGroupId() ) ).append( PATH_SEPARATOR );
081        if ( !metadata.storedInGroupDirectory() )
082        {
083            path.append( metadata.getArtifactId() ).append( PATH_SEPARATOR );
084
085            if ( metadata.storedInArtifactVersionDirectory() )
086            {
087                path.append( metadata.getBaseVersion() ).append( PATH_SEPARATOR );
088            }
089        }
090
091        path.append( filename );
092
093        return path.toString();
094    }
095
096    public String pathOfRemoteRepositoryMetadata( ArtifactMetadata metadata )
097    {
098        return pathOfRepositoryMetadata( metadata, metadata.getRemoteFilename() );
099    }
100
101    private String formatAsDirectory( String directory )
102    {
103        return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
104    }
105
106    @Override
107    public String toString()
108    {
109        return getId();
110    }
111
112}