From 1055e7aa884eac519f16d93f8d89c40724c7ced4 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Mon, 16 Apr 2007 00:51:13 +0000 Subject: [PATCH] Adding ability to calculate a ProjectReference from a path. git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/branches@529108 13f79535-47bb-0310-9956-ffa450edef68 --- .../layout/BidirectionalRepositoryLayout.java | 9 ++ .../DefaultBidirectionalRepositoryLayout.java | 84 ++++++++++++++----- .../repository/layout/FilenameParts.java | 27 ++++++ .../LegacyBidirectionalRepositoryLayout.java | 64 ++++++++++---- 4 files changed, 148 insertions(+), 36 deletions(-) diff --git a/archiva-jpox-database-refactor/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/layout/BidirectionalRepositoryLayout.java b/archiva-jpox-database-refactor/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/layout/BidirectionalRepositoryLayout.java index bb108c4c9..68a897753 100644 --- a/archiva-jpox-database-refactor/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/layout/BidirectionalRepositoryLayout.java +++ b/archiva-jpox-database-refactor/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/layout/BidirectionalRepositoryLayout.java @@ -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; } diff --git a/archiva-jpox-database-refactor/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/layout/DefaultBidirectionalRepositoryLayout.java b/archiva-jpox-database-refactor/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/layout/DefaultBidirectionalRepositoryLayout.java index 3affe5e14..c2ad43246 100644 --- a/archiva-jpox-database-refactor/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/layout/DefaultBidirectionalRepositoryLayout.java +++ b/archiva-jpox-database-refactor/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/layout/DefaultBidirectionalRepositoryLayout.java @@ -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] ); + } + + 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" ); } diff --git a/archiva-jpox-database-refactor/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/layout/FilenameParts.java b/archiva-jpox-database-refactor/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/layout/FilenameParts.java index 88aa34316..9ef6c98fb 100644 --- a/archiva-jpox-database-refactor/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/layout/FilenameParts.java +++ b/archiva-jpox-database-refactor/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/layout/FilenameParts.java @@ -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 ) diff --git a/archiva-jpox-database-refactor/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/layout/LegacyBidirectionalRepositoryLayout.java b/archiva-jpox-database-refactor/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/layout/LegacyBidirectionalRepositoryLayout.java index 9432c2939..9536673a6 100644 --- a/archiva-jpox-database-refactor/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/layout/LegacyBidirectionalRepositoryLayout.java +++ b/archiva-jpox-database-refactor/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/layout/LegacyBidirectionalRepositoryLayout.java @@ -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]; + + 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; -- 2.39.5