]> source.dussan.org Git - archiva.git/commitdiff
Adding ability to calculate a ProjectReference from a path.
authorJoakim Erdfelt <joakime@apache.org>
Mon, 16 Apr 2007 00:51:13 +0000 (00:51 +0000)
committerJoakim Erdfelt <joakime@apache.org>
Mon, 16 Apr 2007 00:51:13 +0000 (00:51 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/branches/archiva-jpox-database-refactor@529108 13f79535-47bb-0310-9956-ffa450edef68

archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/layout/BidirectionalRepositoryLayout.java
archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/layout/DefaultBidirectionalRepositoryLayout.java
archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/layout/FilenameParts.java
archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/layout/LegacyBidirectionalRepositoryLayout.java

index bb108c4c9291326755bc62e28f2a6cca19d36723..68a8977536546e8d9b31c1ca95a35e3cf5e1f8bf 100644 (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;
 }
index 3affe5e140dfa1e78c92d86de9e24c6941d86839..c2ad432467958e99e7d8466412daabebfba62c69 100644 (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] );
+        }
+
+        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" );
         }
index 88aa34316b09f66c786e29660ece28d2f83b89c1..9ef6c98fbcc191474117373616ce7bcfc05c2eb6 100644 (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 )
index 9432c2939a7ab243a19afe82ff2170f7613483ea..9536673a67255ebb79a4081dc24263d7b343997e 100644 (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];
+
+        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;