View Javadoc
1   package org.apache.maven.index.artifact;
2   
3   /*
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
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  import java.util.regex.Matcher;
25  import java.util.regex.Pattern;
26  
27  /**
28   * An M1 <code>GavCalculator</code>. Heavily under-maintained.
29   * 
30   * @author Jason van Zyl
31   * @author Tamas Cservenak
32   * @deprecated Maven1 support to be dropped.
33   */
34  @Singleton
35  @Named( "maven1" )
36  @Deprecated
37  public class M1GavCalculator
38      implements GavCalculator
39  {
40  
41      private static final Pattern PAT1 = Pattern.compile( "^([^0-9]+)-([0-9].+)\\.([^0-9]+)(\\.md5|\\.sha1){0,1}$" );
42  
43      private static final Pattern PAT2 =
44          Pattern.compile( "^([a-z0-9-_]+)-([0-9-].+)\\.([^0-9]+)(\\.md5|\\.sha1){0,1}$" );
45  
46      public Gav pathToGav( String str )
47      {
48          try
49          {
50              String s = str.startsWith( "/" ) ? str.substring( 1 ) : str;
51  
52              int n1 = s.lastIndexOf( '/' );
53  
54              if ( n1 == -1 )
55              {
56                  return null;
57              }
58  
59              int n2 = s.lastIndexOf( '/', n1 - 1 );
60  
61              if ( n2 == -1 )
62              {
63                  return null;
64              }
65  
66              String g = s.substring( 0, n2 ).replace( '/', '.' );
67              String middle = s.substring( n2 + 1, n1 );
68              String n = s.substring( n1 + 1 );
69  
70              String classifier = null;
71              if ( "java-sources".equals( middle ) )
72              {
73                  classifier = "sources";
74              }
75              else if ( "javadocs".equals( middle ) )
76              {
77                  classifier = "javadoc";
78              }
79              else if ( "ejbs".equals( middle )
80                  && ( n.endsWith( "client.jar" ) || n.endsWith( "client.jar.sha1" ) || n.endsWith( "client.jar.md5" ) ) )
81              {
82                  classifier = "client";
83              }
84  
85              boolean checksum = false;
86              Gav.HashType checksumType = null;
87              if ( s.endsWith( ".md5" ) )
88              {
89                  checksum = true;
90                  checksumType = Gav.HashType.md5;
91                  s = s.substring( 0, s.length() - 4 );
92              }
93              else if ( s.endsWith( ".sha1" ) )
94              {
95                  checksum = true;
96                  checksumType = Gav.HashType.sha1;
97                  s = s.substring( 0, s.length() - 5 );
98              }
99  
100             if ( s.endsWith( "maven-metadata.xml" ) )
101             {
102                 return null;
103             }
104 
105             String ext = s.substring( s.lastIndexOf( '.' ) + 1 );
106 
107             Matcher m = PAT1.matcher( n );
108             if ( m.matches() )
109             {
110                 String a = m.group( 1 );
111                 String version = m.group( 2 );
112                 if ( classifier != null )
113                 {
114                     version = version.substring( 0, version.length() - ( classifier.length() + 1 ) );
115                 }
116 
117                 return new Gav( g, a, version, classifier, ext, null, null, n, checksum, checksumType, false,
118                     null );
119             }
120             else
121             {
122                 m = PAT2.matcher( n );
123                 if ( m.matches() )
124                 {
125                     String a = m.group( 1 );
126                     String version = m.group( 2 );
127                     if ( classifier != null )
128                     {
129                         version = version.substring( 0, version.length() - ( classifier.length() + 1 ) );
130                     }
131 
132                     return new Gav( g, a, version, classifier, ext, null, null, n, checksum, checksumType,
133                         false, null );
134                 }
135                 else
136                 {
137                     return null;
138                 }
139             }
140         }
141         catch ( IndexOutOfBoundsException e )
142         {
143             return null;
144         }
145     }
146 
147     /**
148      * // XXX this is not accurate, m1 is using packaging as an artifact folder name.
149      */
150     public String gavToPath( Gav gav )
151     {
152         StringBuilder path = new StringBuilder( "/" );
153 
154         path.append( gav.getGroupId() );
155 
156         path.append( "/" );
157 
158         if ( gav.getClassifier() == null )
159         {
160             path.append( gav.getExtension() );
161         }
162         else
163         {
164             if ( gav.getClassifier().startsWith( "source" ) )
165             {
166                 path.append( "java-source" );
167             }
168             else if ( gav.getClassifier().startsWith( "client" ) )
169             {
170                 path.append( "ejb" );
171             }
172             else
173             {
174                 path.append( gav.getClassifier() );
175             }
176         }
177         path.append( "s" );
178 
179         path.append( "/" );
180 
181         path.append( gav.getArtifactId() );
182 
183         path.append( "-" );
184 
185         path.append( gav.getVersion() );
186 
187         if ( gav.getClassifier() != null )
188         {
189             path.append( "-" );
190 
191             path.append( gav.getClassifier() );
192         }
193 
194         path.append( "." );
195 
196         path.append( gav.getExtension() );
197 
198         if ( gav.isHash() )
199         {
200             path.append( "." );
201 
202             path.append( gav.getHashType().toString() );
203         }
204 
205         return path.toString();
206     }
207 
208 }