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.ArtifactReference;
25 import org.apache.maven.archiva.repository.layout.LayoutException;
28 * DefaultPathParser is a parser for maven 2 (default layout) paths to ArtifactReference.
30 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
33 * @plexus.component role="org.apache.maven.archiva.repository.content.DefaultPathParser"
35 public class DefaultPathParser
37 private static final String INVALID_ARTIFACT_PATH = "Invalid path to Artifact: ";
40 * Convert a path to an ArtifactReference.
44 * @throws LayoutException
46 protected static ArtifactReference toArtifactReference( String path )
47 throws LayoutException
49 if ( StringUtils.isBlank( path ) )
51 throw new LayoutException( "Unable to convert blank path." );
54 ArtifactReference artifact = new ArtifactReference();
56 String normalizedPath = StringUtils.replace( path, "\\", "/" );
57 String pathParts[] = StringUtils.split( normalizedPath, '/' );
61 * path = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar"
62 * path[0] = "commons-lang"; // The Group ID
63 * path[1] = "commons-lang"; // The Artifact ID
64 * path[2] = "2.1"; // The Version
65 * path[3] = "commons-lang-2.1.jar" // The filename.
68 if ( pathParts.length < 4 )
70 // Illegal Path Parts Length.
71 throw new LayoutException( "Not enough parts to the path [" + path
72 + "] to construct an ArchivaArtifact from. (Requires at least 4 parts)" );
76 int partCount = pathParts.length;
77 int filenamePos = partCount - 1;
78 int baseVersionPos = partCount - 2;
79 int artifactIdPos = partCount - 3;
80 int groupIdPos = partCount - 4;
82 // Second to last is the baseVersion (the directory version)
83 String baseVersion = pathParts[baseVersionPos];
85 // Third to last is the artifact Id.
86 artifact.setArtifactId( pathParts[artifactIdPos] );
88 // Remaining pieces are the groupId.
89 for ( int i = 0; i <= groupIdPos; i++ )
93 artifact.setGroupId( pathParts[i] );
97 artifact.setGroupId( artifact.getGroupId() + "." + pathParts[i] );
103 // Last part is the filename
104 String filename = pathParts[filenamePos];
106 // Now we need to parse the filename to get the artifact version Id.
107 if ( StringUtils.isBlank( filename ) )
109 throw new IllegalArgumentException( INVALID_ARTIFACT_PATH + "Unable to split blank filename." );
112 FilenameParser parser = new FilenameParser( filename );
114 // Expect the filename to start with the artifactId.
115 artifact.setArtifactId( parser.expect( artifact.getArtifactId() ) );
117 if ( artifact.getArtifactId() == null )
119 throw new LayoutException( INVALID_ARTIFACT_PATH + "filename format is invalid, "
120 + "should start with artifactId as stated in path." );
123 // Process the version.
124 artifact.setVersion( parser.expect( baseVersion ) );
126 if ( artifact.getVersion() == null )
128 // We working with a snapshot?
129 if ( VersionUtil.isSnapshot( baseVersion ) )
131 artifact.setVersion( parser.nextVersion() );
132 if ( !VersionUtil.isUniqueSnapshot( artifact.getVersion() ) )
134 throw new LayoutException( INVALID_ARTIFACT_PATH + "filename format is invalid,"
135 + "expected timestamp format in filename." );
140 throw new LayoutException( INVALID_ARTIFACT_PATH + "filename format is invalid, "
141 + "expected version as stated in path." );
145 // Do we have a classifier?
146 artifact.setClassifier( parser.remaining() );
149 artifact.setType( ArtifactExtensionMapping.guessTypeFromFilename( filename ) );
151 artifact.setType( ArtifactExtensionMapping.guessTypeFromFilename( filename ) );
153 catch ( LayoutException e )
160 // Do we have a snapshot version?
161 if ( VersionUtil.isSnapshot( artifact.getVersion() ) )
163 // Rules are different for SNAPSHOTS
164 if ( !VersionUtil.isGenericSnapshot( baseVersion ) )
166 String filenameBaseVersion = VersionUtil.getBaseVersion( artifact.getVersion() );
167 throw new LayoutException( "Invalid snapshot artifact location, version directory should be "
168 + filenameBaseVersion );
173 // Non SNAPSHOT rules.
174 // Do we pass the simple test?
175 if ( !StringUtils.equals( baseVersion, artifact.getVersion() ) )
177 throw new LayoutException( "Invalid artifact: version declared in directory path does"
178 + " not match what was found in the artifact filename." );