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.
35 public abstract class AbstractDefaultRepositoryContent
37 public static final String MAVEN_METADATA = "maven-metadata.xml";
39 protected static final char PATH_SEPARATOR = '/';
41 protected static final char GROUP_SEPARATOR = '.';
43 protected static final char ARTIFACT_SEPARATOR = '-';
45 private PathParser defaultPathParser = new DefaultPathParser();
47 public ArtifactReference toArtifactReference( String path )
48 throws LayoutException
50 return defaultPathParser.toArtifactReference( path );
53 public String toMetadataPath( ProjectReference reference )
55 StringBuffer path = new StringBuffer();
57 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
58 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
59 path.append( MAVEN_METADATA );
61 return path.toString();
64 public String toMetadataPath( VersionedReference reference )
66 StringBuffer path = new StringBuffer();
68 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
69 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
70 if ( reference.getVersion() != null )
72 // add the version only if it is present
73 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
75 path.append( MAVEN_METADATA );
77 return path.toString();
80 public String toPath( ArchivaArtifact reference )
82 if ( reference == null )
84 throw new IllegalArgumentException( "ArchivaArtifact cannot be null" );
87 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
88 return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
89 reference.getClassifier(), reference.getType() );
92 public String toPath( ArtifactReference reference )
94 if ( reference == null )
96 throw new IllegalArgumentException( "Artifact reference cannot be null" );
99 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
100 return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
101 reference.getClassifier(), reference.getType() );
104 private String formatAsDirectory( String directory )
106 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
109 private String toPath( String groupId, String artifactId, String baseVersion, String version, String classifier,
112 StringBuffer path = new StringBuffer();
114 path.append( formatAsDirectory( groupId ) ).append( PATH_SEPARATOR );
115 path.append( artifactId ).append( PATH_SEPARATOR );
117 if ( baseVersion != null )
119 path.append( baseVersion ).append( PATH_SEPARATOR );
120 if ( ( version != null ) && ( type != null ) )
122 path.append( artifactId ).append( ARTIFACT_SEPARATOR ).append( version );
124 if ( StringUtils.isNotBlank( classifier ) )
126 path.append( ARTIFACT_SEPARATOR ).append( classifier );
129 path.append( GROUP_SEPARATOR ).append( ArtifactExtensionMapping.getExtension( type ) );
133 return path.toString();