1 package org.apache.maven.archiva.repository.content;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.maven.archiva.common.utils.VersionUtil;
24 import org.apache.maven.archiva.model.ArchivaArtifact;
25 import org.apache.maven.archiva.model.ArtifactReference;
26 import org.apache.maven.archiva.model.ProjectReference;
27 import org.apache.maven.archiva.model.VersionedReference;
28 import org.apache.maven.archiva.repository.layout.LayoutException;
31 * AbstractDefaultRepositoryContent - common methods for working with default (maven 2) layout.
33 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
36 public abstract class AbstractDefaultRepositoryContent
38 public static final String MAVEN_METADATA = "maven-metadata.xml";
40 protected static final char PATH_SEPARATOR = '/';
42 protected static final char GROUP_SEPARATOR = '.';
44 protected static final char ARTIFACT_SEPARATOR = '-';
47 * @plexus.requirement role-hint="default"
49 private PathParser defaultPathParser;
51 public ArtifactReference toArtifactReference( String path )
52 throws LayoutException
54 return defaultPathParser.toArtifactReference( path );
57 public String toMetadataPath( ProjectReference reference )
59 StringBuffer path = new StringBuffer();
61 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
62 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
63 path.append( MAVEN_METADATA );
65 return path.toString();
68 public String toMetadataPath( VersionedReference reference )
70 StringBuffer path = new StringBuffer();
72 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
73 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
74 if ( reference.getVersion() != null )
76 // add the version only if it is present
77 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
79 path.append( MAVEN_METADATA );
81 return path.toString();
84 public String toPath( ArchivaArtifact reference )
86 if ( reference == null )
88 throw new IllegalArgumentException( "ArchivaArtifact cannot be null" );
91 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
92 return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
93 reference.getClassifier(), reference.getType() );
96 public String toPath( ArtifactReference reference )
98 if ( reference == null )
100 throw new IllegalArgumentException( "Artifact reference cannot be null" );
103 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
104 return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
105 reference.getClassifier(), reference.getType() );
108 private String formatAsDirectory( String directory )
110 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
113 private String toPath( String groupId, String artifactId, String baseVersion, String version, String classifier,
116 StringBuffer path = new StringBuffer();
118 path.append( formatAsDirectory( groupId ) ).append( PATH_SEPARATOR );
119 path.append( artifactId ).append( PATH_SEPARATOR );
121 if ( baseVersion != null )
123 path.append( baseVersion ).append( PATH_SEPARATOR );
124 if ( ( version != null ) && ( type != null ) )
126 path.append( artifactId ).append( ARTIFACT_SEPARATOR ).append( version );
128 if ( StringUtils.isNotBlank( classifier ) )
130 path.append( ARTIFACT_SEPARATOR ).append( classifier );
133 path.append( GROUP_SEPARATOR ).append( ArtifactExtensionMapping.getExtension( type ) );
137 return path.toString();