Adding ability to calculate a ProjectReference from a path.

git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/branches/archiva-jpox-database-refactor@529108 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Joakim Erdfelt 2007-04-16 00:51:13 +00:00
parent 46fefb7f3e
commit 94ca53dece
4 changed files with 148 additions and 36 deletions

View File

@ -71,4 +71,13 @@ public interface BidirectionalRepositoryLayout
* @throws LayoutException if there was a problem converting the path to an artifact.
*/
public ArchivaArtifact toArtifact( String path ) throws LayoutException;
/**
* Given a repository relateive path to a filename, return the ProjectReference object suitable for the path.
*
* @param path the path relative to the repository base dir for the artifact.
* @return the ProjectReference representing the path. (or null if path cannot be converted to a ProjectReference)
* @throws LayoutException if there was a problem converting the path to an artifact.
*/
public ProjectReference toProjectReference( String path ) throws LayoutException;
}

View File

@ -100,9 +100,35 @@ public class DefaultBidirectionalRepositoryLayout
return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
}
public ArchivaArtifact toArtifact( String path )
class PathReferences
{
public String groupId;
public String artifactId;
public String baseVersion;
public String type;
public FilenameParts fileParts;
public void appendGroupId( String part )
{
if ( groupId == null )
{
groupId = part;
return;
}
groupId += "." + part;
}
}
private PathReferences toPathReferences( String path, boolean parseFilename )
throws LayoutException
{
PathReferences prefs = new PathReferences();
String normalizedPath = StringUtils.replace( path, "\\", "/" );
String pathParts[] = StringUtils.split( normalizedPath, '/' );
@ -126,42 +152,60 @@ public class DefaultBidirectionalRepositoryLayout
// Maven 2.x path.
int partCount = pathParts.length;
// Last part is the filename
String filename = pathParts[partCount - 1];
// Second to last is the baseVersion (the directory version)
String baseVersion = pathParts[partCount - 2];
prefs.baseVersion = pathParts[partCount - 2];
// Third to last is the artifact Id.
String artifactId = pathParts[partCount - 3];
prefs.artifactId = pathParts[partCount - 3];
// Remaining pieces are the groupId.
String groupId = "";
for ( int i = 0; i <= partCount - 4; i++ )
{
if ( groupId.length() > 0 )
{
groupId += ".";
}
groupId += pathParts[i];
prefs.appendGroupId( pathParts[i] );
}
// Now we need to parse the filename to get the artifact version Id.
FilenameParts fileParts = RepositoryLayoutUtils.splitFilename( filename, artifactId );
if ( parseFilename )
{
// Last part is the filename
String filename = pathParts[partCount - 1];
String type = extensionMapper.getType( filename );
// Now we need to parse the filename to get the artifact version Id.
prefs.fileParts = RepositoryLayoutUtils.splitFilename( filename, prefs.artifactId );
ArchivaArtifact artifact = new ArchivaArtifact( groupId, artifactId, fileParts.version, fileParts.classifier,
type );
prefs.type = extensionMapper.getType( filename );
}
return prefs;
}
public ProjectReference toProjectReference( String path )
throws LayoutException
{
PathReferences pathrefs = toPathReferences( path, false );
ProjectReference reference = new ProjectReference();
reference.setGroupId( pathrefs.groupId );
reference.setArtifactId( pathrefs.artifactId );
return reference;
}
public ArchivaArtifact toArtifact( String path )
throws LayoutException
{
PathReferences pathrefs = toPathReferences( path, true );
ArchivaArtifact artifact = new ArchivaArtifact( pathrefs.groupId, pathrefs.artifactId,
pathrefs.fileParts.version, pathrefs.fileParts.classifier,
pathrefs.type );
// Sanity Checks.
String artifactBaseVersion = VersionUtil.getBaseVersion( fileParts.version );
if ( !artifactBaseVersion.equals( baseVersion ) )
String artifactBaseVersion = VersionUtil.getBaseVersion( pathrefs.fileParts.version );
if ( !artifactBaseVersion.equals( pathrefs.baseVersion ) )
{
throw new LayoutException( "Invalid artifact location, version directory and filename mismatch." );
}
if ( !artifactId.equals( fileParts.artifactId ) )
if ( !pathrefs.artifactId.equals( pathrefs.fileParts.artifactId ) )
{
throw new LayoutException( "Invalid artifact Id" );
}

View File

@ -35,6 +35,33 @@ class FilenameParts
public String extension;
public String toFilename()
{
StringBuffer sb = new StringBuffer();
if ( artifactId != null )
{
sb.append( artifactId );
}
if ( classifier != null )
{
sb.append( "-" ).append( classifier );
}
if ( version != null )
{
sb.append( "-" ).append( version );
}
if ( extension != null )
{
sb.append( "." ).append( extension );
}
return sb.toString();
}
public void appendArtifactId( String piece )
{
if ( artifactId == null )

View File

@ -61,8 +61,8 @@ public class LegacyBidirectionalRepositoryLayout
public String toPath( ArchivaArtifact reference )
{
return toPath( reference.getGroupId(), reference.getArtifactId(), reference
.getVersion(), reference.getClassifier(), reference.getType() );
return toPath( reference.getGroupId(), reference.getArtifactId(), reference.getVersion(), reference
.getClassifier(), reference.getType() );
}
public String toPath( ProjectReference reference )
@ -73,8 +73,8 @@ public class LegacyBidirectionalRepositoryLayout
public String toPath( ArtifactReference artifact )
{
return toPath( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getClassifier(),
artifact.getType() );
return toPath( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(),
artifact.getClassifier(), artifact.getType() );
}
private String toPath( String groupId, String artifactId, String version, String classifier, String type )
@ -119,9 +119,22 @@ public class LegacyBidirectionalRepositoryLayout
return type + "s";
}
public ArchivaArtifact toArtifact( String path )
class PathReferences
{
public String groupId;
public String pathType;
public String type;
public FilenameParts fileParts;
}
private PathReferences toPathReferences( String path, boolean parseFilename )
throws LayoutException
{
PathReferences prefs = new PathReferences();
String normalizedPath = StringUtils.replace( path, "\\", "/" );
String pathParts[] = StringUtils.split( normalizedPath, '/' );
@ -142,30 +155,49 @@ public class LegacyBidirectionalRepositoryLayout
}
// The Group ID.
String groupId = pathParts[0];
prefs.groupId = pathParts[0];
// The Expected Type.
String expectedType = pathParts[1];
prefs.pathType = pathParts[1];
// The Filename.
String filename = pathParts[2];
if ( parseFilename )
{
// The Filename.
String filename = pathParts[2];
FilenameParts fileParts = RepositoryLayoutUtils.splitFilename( filename, null );
prefs.fileParts = RepositoryLayoutUtils.splitFilename( filename, null );
String type = extensionMapper.getType( filename );
prefs.type = extensionMapper.getType( filename );
}
ArchivaArtifact artifact = new ArchivaArtifact( groupId, fileParts.artifactId, fileParts.version,
fileParts.classifier, type );
return prefs;
}
public ProjectReference toProjectReference( String path )
throws LayoutException
{
throw new LayoutException( "Cannot parse legacy paths to a Project Reference." );
}
public ArchivaArtifact toArtifact( String path )
throws LayoutException
{
PathReferences pathrefs = toPathReferences( path, true );
ArchivaArtifact artifact = new ArchivaArtifact( pathrefs.groupId, pathrefs.fileParts.artifactId,
pathrefs.fileParts.version, pathrefs.fileParts.classifier,
pathrefs.type );
// Sanity Checks.
if ( StringUtils.isEmpty( fileParts.extension ) )
if ( StringUtils.isEmpty( pathrefs.fileParts.extension ) )
{
throw new LayoutException( "Invalid artifact, no extension." );
}
if ( !expectedType.equals( fileParts.extension + "s" ) )
if ( !pathrefs.pathType.equals( pathrefs.fileParts.extension + "s" ) )
{
throw new LayoutException( "Invalid artifact, extension and layout specified type mismatch." );
throw new LayoutException( "Invalid artifact, mismatch on extension <" + pathrefs.fileParts.extension
+ "> and layout specified type<" + pathrefs.pathType + ">." );
}
return artifact;