* @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;
}
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, '/' );
// 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] );
+ }
+
+ if ( parseFilename )
+ {
+ // Last part is the filename
+ String filename = pathParts[partCount - 1];
+
+ // Now we need to parse the filename to get the artifact version Id.
+ prefs.fileParts = RepositoryLayoutUtils.splitFilename( filename, prefs.artifactId );
+
+ prefs.type = extensionMapper.getType( filename );
}
- // Now we need to parse the filename to get the artifact version Id.
- FilenameParts fileParts = RepositoryLayoutUtils.splitFilename( filename, artifactId );
+ 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;
+ }
- String type = extensionMapper.getType( filename );
+ public ArchivaArtifact toArtifact( String path )
+ throws LayoutException
+ {
+ PathReferences pathrefs = toPathReferences( path, true );
- ArchivaArtifact artifact = new ArchivaArtifact( groupId, artifactId, fileParts.version, fileParts.classifier,
- type );
+ 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" );
}
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 )
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 )
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 )
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, '/' );
}
// The Group ID.
- String groupId = pathParts[0];
+ prefs.groupId = pathParts[0];
// The Expected Type.
- String expectedType = pathParts[1];
+ prefs.pathType = pathParts[1];
+
+ if ( parseFilename )
+ {
+ // The Filename.
+ String filename = pathParts[2];
- // The Filename.
- String filename = pathParts[2];
+ prefs.fileParts = RepositoryLayoutUtils.splitFilename( filename, null );
- FilenameParts fileParts = RepositoryLayoutUtils.splitFilename( filename, null );
+ prefs.type = extensionMapper.getType( filename );
+ }
+
+ return prefs;
+ }
+
+ public ProjectReference toProjectReference( String path )
+ throws LayoutException
+ {
+ throw new LayoutException( "Cannot parse legacy paths to a Project Reference." );
+ }
- String type = extensionMapper.getType( filename );
+ public ArchivaArtifact toArtifact( String path )
+ throws LayoutException
+ {
+ PathReferences pathrefs = toPathReferences( path, true );
- ArchivaArtifact artifact = new ArchivaArtifact( groupId, fileParts.artifactId, fileParts.version,
- fileParts.classifier, type );
+ 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;