1 package org.apache.maven.repository.discovery;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.maven.artifact.Artifact;
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.StringTokenizer;
27 * Artifact discoverer for the legacy repository layout (Maven 1.x).
28 * Method used to build an artifact object using a relative path from a repository base directory. An artifactId
29 * having the words "DEV", "PRE", "RC", "ALPHA", "BETA", "DEBUG", "UNOFFICIAL", "CURRENT", "LATEST", "FCS",
30 * "RELEASE", "NIGHTLY", "SNAPSHOT" and "TEST" (not case-sensitive) will most likely make this method fail as
31 * they are reserved for version usage.
34 * @author Brett Porter
35 * @plexus.component role="org.apache.maven.repository.discovery.ArtifactDiscoverer" role-hint="legacy"
37 public class LegacyArtifactDiscoverer
38 extends AbstractArtifactDiscoverer
40 public Artifact buildArtifact( String path )
42 StringTokenizer tokens = new StringTokenizer( path, "/\\" );
44 Artifact result = null;
46 int numberOfTokens = tokens.countTokens();
48 if ( numberOfTokens == 3 )
50 String groupId = tokens.nextToken();
52 String type = tokens.nextToken();
54 if ( type.endsWith( "s" ) )
56 type = type.substring( 0, type.length() - 1 );
58 // contains artifactId, version, classifier, and extension.
59 String avceGlob = tokens.nextToken();
61 //noinspection CollectionDeclaredAsConcreteClass
62 LinkedList avceTokenList = new LinkedList();
64 StringTokenizer avceTokenizer = new StringTokenizer( avceGlob, "-" );
65 while ( avceTokenizer.hasMoreTokens() )
67 avceTokenList.addLast( avceTokenizer.nextToken() );
70 String lastAvceToken = (String) avceTokenList.removeLast();
74 // TODO: share with other discoverer, use artifact handlers instead
75 if ( lastAvceToken.endsWith( ".tar.gz" ) )
77 type = "distribution-tgz";
79 lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".tar.gz".length() );
81 avceTokenList.addLast( lastAvceToken );
83 else if ( lastAvceToken.endsWith( "sources.jar" ) )
87 lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".jar".length() );
89 avceTokenList.addLast( lastAvceToken );
91 else if ( lastAvceToken.endsWith( ".zip" ) )
93 type = "distribution-zip";
95 lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".zip".length() );
97 avceTokenList.addLast( lastAvceToken );
101 int extPos = lastAvceToken.lastIndexOf( '.' );
105 String ext = lastAvceToken.substring( extPos + 1 );
106 if ( type.equals( ext ) )
108 lastAvceToken = lastAvceToken.substring( 0, extPos );
110 avceTokenList.addLast( lastAvceToken );
114 //type does not match extension
127 // let's discover the version, and whatever's leftover will be either
128 // a classifier, or part of the artifactId, depending on position.
129 // Since version is at the end, we have to move in from the back.
130 Collections.reverse( avceTokenList );
132 // TODO: this is obscene - surely a better way?
133 String validVersionParts = "([Dd][Ee][Vv][_.0-9]*)|" + "([Ss][Nn][Aa][Pp][Ss][Hh][Oo][Tt])|" +
134 "([0-9][_.0-9a-zA-Z]*)|" + "([Gg]?[_.0-9ab]*([Pp][Rr][Ee]|[Rr][Cc]|[Gg]|[Mm])[_.0-9]*)|" +
135 "([Aa][Ll][Pp][Hh][Aa][_.0-9]*)|" + "([Bb][Ee][Tt][Aa][_.0-9]*)|" + "([Rr][Cc][_.0-9]*)|" +
136 "([Tt][Ee][Ss][Tt][_.0-9]*)|" + "([Dd][Ee][Bb][Uu][Gg][_.0-9]*)|" +
137 "([Uu][Nn][Oo][Ff][Ff][Ii][Cc][Ii][Aa][Ll][_.0-9]*)|" + "([Cc][Uu][Rr][Rr][Ee][Nn][Tt])|" +
138 "([Ll][Aa][Tt][Ee][Ss][Tt])|" + "([Ff][Cc][Ss])|" + "([Rr][Ee][Ll][Ee][Aa][Ss][Ee][_.0-9]*)|" +
139 "([Nn][Ii][Gg][Hh][Tt][Ll][Yy])|" + "[Ff][Ii][Nn][Aa][Ll]|" + "([AaBb][_.0-9]*)";
141 StringBuffer classifierBuffer = new StringBuffer();
142 StringBuffer versionBuffer = new StringBuffer();
144 boolean firstVersionTokenEncountered = false;
145 boolean firstToken = true;
147 int tokensIterated = 0;
148 for ( Iterator it = avceTokenList.iterator(); it.hasNext(); )
150 String token = (String) it.next();
152 boolean tokenIsVersionPart = token.matches( validVersionParts );
154 StringBuffer bufferToUpdate;
156 // NOTE: logic in code is reversed, since we're peeling off the back
157 // Any token after the last versionPart will be in the classifier.
158 // Any token UP TO first non-versionPart is part of the version.
159 if ( !tokenIsVersionPart )
161 if ( firstVersionTokenEncountered )
163 //noinspection BreakStatement
168 bufferToUpdate = classifierBuffer;
173 firstVersionTokenEncountered = true;
175 bufferToUpdate = versionBuffer;
184 bufferToUpdate.insert( 0, '-' );
187 bufferToUpdate.insert( 0, token );
192 // Now, restore the proper ordering so we can build the artifactId.
193 Collections.reverse( avceTokenList );
195 // if we didn't find a version, then punt. Use the last token
196 // as the version, and set the classifier empty.
197 if ( versionBuffer.length() < 1 )
199 if ( avceTokenList.size() > 1 )
201 int lastIdx = avceTokenList.size() - 1;
203 versionBuffer.append( avceTokenList.get( lastIdx ) );
204 avceTokenList.remove( lastIdx );
207 classifierBuffer.setLength( 0 );
211 // if everything is kosher, then pop off all the classifier and
212 // version tokens, leaving the naked artifact id in the list.
214 new LinkedList( avceTokenList.subList( 0, avceTokenList.size() - tokensIterated ) );
217 StringBuffer artifactIdBuffer = new StringBuffer();
220 for ( Iterator it = avceTokenList.iterator(); it.hasNext(); )
222 String token = (String) it.next();
230 artifactIdBuffer.append( '-' );
233 artifactIdBuffer.append( token );
236 String artifactId = artifactIdBuffer.toString();
238 if ( artifactId.length() > 0 )
240 int lastVersionCharIdx = versionBuffer.length() - 1;
241 if ( lastVersionCharIdx > -1 && versionBuffer.charAt( lastVersionCharIdx ) == '-' )
243 versionBuffer.setLength( lastVersionCharIdx );
246 String version = versionBuffer.toString();
248 if ( version.length() >= 1 )
250 if ( classifierBuffer.length() > 0 )
252 result = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version,
254 classifierBuffer.toString() );
258 result = artifactFactory.createArtifact( groupId, artifactId, version,
259 Artifact.SCOPE_RUNTIME, type );