]> source.dussan.org Git - archiva.git/blob
a5092c1bd279412b0d06e2b12b9bd4f12c4434a8
[archiva.git] /
1 package org.apache.maven.repository.discovery;
2
3 /*
4  * Copyright 2005-2006 The Apache Software Foundation.
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 import org.apache.maven.artifact.Artifact;
20
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.StringTokenizer;
25
26 /**
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.
32  *
33  * @author John Casey
34  * @author Brett Porter
35  * @plexus.component role="org.apache.maven.repository.discovery.ArtifactDiscoverer" role-hint="legacy"
36  */
37 public class LegacyArtifactDiscoverer
38     extends AbstractArtifactDiscoverer
39 {
40     public Artifact buildArtifact( String path )
41     {
42         StringTokenizer tokens = new StringTokenizer( path, "/\\" );
43
44         Artifact result = null;
45
46         int numberOfTokens = tokens.countTokens();
47
48         if ( numberOfTokens == 3 )
49         {
50             String groupId = tokens.nextToken();
51
52             String type = tokens.nextToken();
53
54             if ( type.endsWith( "s" ) )
55             {
56                 type = type.substring( 0, type.length() - 1 );
57
58                 // contains artifactId, version, classifier, and extension.
59                 String avceGlob = tokens.nextToken();
60
61                 //noinspection CollectionDeclaredAsConcreteClass
62                 LinkedList avceTokenList = new LinkedList();
63
64                 StringTokenizer avceTokenizer = new StringTokenizer( avceGlob, "-" );
65                 while ( avceTokenizer.hasMoreTokens() )
66                 {
67                     avceTokenList.addLast( avceTokenizer.nextToken() );
68                 }
69
70                 String lastAvceToken = (String) avceTokenList.removeLast();
71
72                 boolean valid = true;
73
74                 // TODO: share with other discoverer, use artifact handlers instead
75                 if ( lastAvceToken.endsWith( ".tar.gz" ) )
76                 {
77                     type = "distribution-tgz";
78
79                     lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".tar.gz".length() );
80
81                     avceTokenList.addLast( lastAvceToken );
82                 }
83                 else if ( lastAvceToken.endsWith( "sources.jar" ) )
84                 {
85                     type = "java-source";
86
87                     lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".jar".length() );
88
89                     avceTokenList.addLast( lastAvceToken );
90                 }
91                 else if ( lastAvceToken.endsWith( ".zip" ) )
92                 {
93                     type = "distribution-zip";
94
95                     lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".zip".length() );
96
97                     avceTokenList.addLast( lastAvceToken );
98                 }
99                 else
100                 {
101                     int extPos = lastAvceToken.lastIndexOf( '.' );
102
103                     if ( extPos > 0 )
104                     {
105                         String ext = lastAvceToken.substring( extPos + 1 );
106                         if ( type.equals( ext ) )
107                         {
108                             lastAvceToken = lastAvceToken.substring( 0, extPos );
109
110                             avceTokenList.addLast( lastAvceToken );
111                         }
112                         else
113                         {
114                             //type does not match extension
115                             valid = false;
116                         }
117                     }
118                     else
119                     {
120                         // no extension
121                         valid = false;
122                     }
123                 }
124
125                 if ( valid )
126                 {
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 );
131
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]*)";
140
141                     StringBuffer classifierBuffer = new StringBuffer();
142                     StringBuffer versionBuffer = new StringBuffer();
143
144                     boolean firstVersionTokenEncountered = false;
145                     boolean firstToken = true;
146
147                     int tokensIterated = 0;
148                     for ( Iterator it = avceTokenList.iterator(); it.hasNext(); )
149                     {
150                         String token = (String) it.next();
151
152                         boolean tokenIsVersionPart = token.matches( validVersionParts );
153
154                         StringBuffer bufferToUpdate;
155
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 )
160                         {
161                             if ( firstVersionTokenEncountered )
162                             {
163                                 //noinspection BreakStatement
164                                 break;
165                             }
166                             else
167                             {
168                                 bufferToUpdate = classifierBuffer;
169                             }
170                         }
171                         else
172                         {
173                             firstVersionTokenEncountered = true;
174
175                             bufferToUpdate = versionBuffer;
176                         }
177
178                         if ( firstToken )
179                         {
180                             firstToken = false;
181                         }
182                         else
183                         {
184                             bufferToUpdate.insert( 0, '-' );
185                         }
186
187                         bufferToUpdate.insert( 0, token );
188
189                         tokensIterated++;
190                     }
191
192                     // Now, restore the proper ordering so we can build the artifactId.
193                     Collections.reverse( avceTokenList );
194
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 )
198                     {
199                         if ( avceTokenList.size() > 1 )
200                         {
201                             int lastIdx = avceTokenList.size() - 1;
202
203                             versionBuffer.append( avceTokenList.get( lastIdx ) );
204                             avceTokenList.remove( lastIdx );
205                         }
206
207                         classifierBuffer.setLength( 0 );
208                     }
209                     else
210                     {
211                         // if everything is kosher, then pop off all the classifier and
212                         // version tokens, leaving the naked artifact id in the list.
213                         avceTokenList =
214                             new LinkedList( avceTokenList.subList( 0, avceTokenList.size() - tokensIterated ) );
215                     }
216
217                     StringBuffer artifactIdBuffer = new StringBuffer();
218
219                     firstToken = true;
220                     for ( Iterator it = avceTokenList.iterator(); it.hasNext(); )
221                     {
222                         String token = (String) it.next();
223
224                         if ( firstToken )
225                         {
226                             firstToken = false;
227                         }
228                         else
229                         {
230                             artifactIdBuffer.append( '-' );
231                         }
232
233                         artifactIdBuffer.append( token );
234                     }
235
236                     String artifactId = artifactIdBuffer.toString();
237
238                     if ( artifactId.length() > 0 )
239                     {
240                         int lastVersionCharIdx = versionBuffer.length() - 1;
241                         if ( lastVersionCharIdx > -1 && versionBuffer.charAt( lastVersionCharIdx ) == '-' )
242                         {
243                             versionBuffer.setLength( lastVersionCharIdx );
244                         }
245
246                         String version = versionBuffer.toString();
247
248                         if ( version.length() >= 1 )
249                         {
250                             if ( classifierBuffer.length() > 0 )
251                             {
252                                 result = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version,
253                                                                                        type,
254                                                                                        classifierBuffer.toString() );
255                             }
256                             else
257                             {
258                                 result = artifactFactory.createArtifact( groupId, artifactId, version,
259                                                                          Artifact.SCOPE_RUNTIME, type );
260                             }
261                         }
262                     }
263                 }
264             }
265         }
266
267         return result;
268     }
269 }