Coverage Report - org.apache.maven.index.artifact.M2GavCalculator
 
Classes in this File Line Coverage Branch Coverage Complexity
M2GavCalculator
81 %
94/116
71 %
40/56
9,6
 
 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 java.text.ParseException;
 23  
 import java.text.SimpleDateFormat;
 24  
 
 25  
 import org.codehaus.plexus.component.annotations.Component;
 26  
 
 27  
 /**
 28  
  * An M2 <code>GavCalculator</code>.
 29  
  * 
 30  
  * @author Jason van Zyl
 31  
  * @author Tamas Cservenak
 32  
  */
 33  
 @Component( role = GavCalculator.class, hint = "maven2" )
 34  7
 public class M2GavCalculator
 35  
     implements GavCalculator
 36  
 {
 37  
     public Gav pathToGav( String str )
 38  
     {
 39  
         try
 40  
         {
 41  55
             String s = str.startsWith( "/" ) ? str.substring( 1 ) : str;
 42  
 
 43  55
             int vEndPos = s.lastIndexOf( '/' );
 44  
 
 45  55
             if ( vEndPos == -1 )
 46  
             {
 47  1
                 return null;
 48  
             }
 49  
 
 50  54
             int aEndPos = s.lastIndexOf( '/', vEndPos - 1 );
 51  
 
 52  54
             if ( aEndPos == -1 )
 53  
             {
 54  0
                 return null;
 55  
             }
 56  
 
 57  54
             int gEndPos = s.lastIndexOf( '/', aEndPos - 1 );
 58  
 
 59  54
             if ( gEndPos == -1 )
 60  
             {
 61  1
                 return null;
 62  
             }
 63  
 
 64  53
             String groupId = s.substring( 0, gEndPos ).replace( '/', '.' );
 65  53
             String artifactId = s.substring( gEndPos + 1, aEndPos );
 66  53
             String version = s.substring( aEndPos + 1, vEndPos );
 67  53
             String fileName = s.substring( vEndPos + 1 );
 68  
 
 69  53
             boolean checksum = false;
 70  53
             boolean signature = false;
 71  53
             Gav.HashType checksumType = null;
 72  53
             Gav.SignatureType signatureType = null;
 73  53
             if ( s.endsWith( ".md5" ) )
 74  
             {
 75  2
                 checksum = true;
 76  2
                 checksumType = Gav.HashType.md5;
 77  2
                 s = s.substring( 0, s.length() - 4 );
 78  
             }
 79  51
             else if ( s.endsWith( ".sha1" ) )
 80  
             {
 81  5
                 checksum = true;
 82  5
                 checksumType = Gav.HashType.sha1;
 83  5
                 s = s.substring( 0, s.length() - 5 );
 84  
             }
 85  
 
 86  53
             if ( s.endsWith( ".asc" ) )
 87  
             {
 88  2
                 signature = true;
 89  2
                 signatureType = Gav.SignatureType.gpg;
 90  2
                 s = s.substring( 0, s.length() - 4 );
 91  
             }
 92  
 
 93  53
             if ( s.endsWith( "maven-metadata.xml" ) )
 94  
             {
 95  5
                 return null;
 96  
             }
 97  
 
 98  48
             boolean snapshot = version.endsWith( "SNAPSHOT" );
 99  
 
 100  48
             if ( snapshot )
 101  
             {
 102  30
                 return getSnapshotGav( s, vEndPos, groupId, artifactId, version, fileName, checksum, signature,
 103  
                     checksumType, signatureType );
 104  
             }
 105  
             else
 106  
             {
 107  18
                 return getReleaseGav( s, vEndPos, groupId, artifactId, version, fileName, checksum, signature,
 108  
                     checksumType, signatureType );
 109  
             }
 110  
         }
 111  2
         catch ( NumberFormatException e )
 112  
         {
 113  2
             return null;
 114  
         }
 115  0
         catch ( StringIndexOutOfBoundsException e )
 116  
         {
 117  0
             return null;
 118  
         }
 119  
     }
 120  
 
 121  
     private Gav getReleaseGav( String s, int vEndPos, String groupId, String artifactId, String version,
 122  
                                String fileName, boolean checksum, boolean signature, Gav.HashType checksumType,
 123  
                                Gav.SignatureType signatureType )
 124  
     {
 125  18
         if ( !fileName.startsWith( artifactId + "-" + version + "." )
 126  
             && !fileName.startsWith( artifactId + "-" + version + "-" ) )
 127  
         {
 128  
             // The path does not represents an artifact (filename does not match artifactId-version)!
 129  6
             return null;
 130  
         }
 131  
 
 132  12
         int nTailPos = vEndPos + artifactId.length() + version.length() + 2;
 133  
 
 134  12
         String tail = s.substring( nTailPos );
 135  
 
 136  12
         int nExtPos = tail.indexOf( '.' );
 137  
 
 138  12
         if ( nExtPos == -1 )
 139  
         {
 140  
             // NX-563: not allowing extensionless paths to be interpreted as artifact
 141  0
             return null;
 142  
         }
 143  
 
 144  12
         String ext = tail.substring( nExtPos + 1 );
 145  
 
 146  12
         String classifier = tail.charAt( 0 ) == '-' ? tail.substring( 1, nExtPos ) : null;
 147  
 
 148  12
         return new Gav( groupId, artifactId, version, classifier, ext, null, null, fileName, checksum, checksumType,
 149  
             signature, signatureType );
 150  
     }
 151  
 
 152  
     private Gav getSnapshotGav( String s, int vEndPos, String groupId, String artifactId, String version,
 153  
                                 String fileName, boolean checksum, boolean signature, Gav.HashType checksumType,
 154  
                                 Gav.SignatureType signatureType )
 155  
     {
 156  
 
 157  30
         Integer snapshotBuildNo = null;
 158  
 
 159  30
         Long snapshotTimestamp = null;
 160  
 
 161  30
         int vSnapshotStart = vEndPos + artifactId.length() + version.length() - 9 + 3;
 162  
 
 163  30
         String vSnapshot = s.substring( vSnapshotStart, vSnapshotStart + 8 );
 164  
 
 165  30
         String classifier = null;
 166  
 
 167  30
         String ext = null;
 168  
 
 169  30
         if ( "SNAPSHOT".equals( vSnapshot ) )
 170  
         {
 171  12
             int nTailPos = vEndPos + artifactId.length() + version.length() + 2;
 172  
 
 173  12
             String tail = s.substring( nTailPos );
 174  
 
 175  12
             int nExtPos = tail.indexOf( '.' );
 176  
 
 177  12
             if ( nExtPos == -1 )
 178  
             {
 179  
                 // NX-563: not allowing extensionless paths to be interpreted as artifact
 180  0
                 return null;
 181  
             }
 182  
 
 183  12
             ext = tail.substring( nExtPos + 1 );
 184  
 
 185  12
             classifier = tail.charAt( 0 ) == '-' ? tail.substring( 1, nExtPos ) : null;
 186  12
         }
 187  
         else
 188  
         {
 189  18
             StringBuffer sb = new StringBuffer( vSnapshot );
 190  18
             sb.append( s.substring( vSnapshotStart + sb.length(), vSnapshotStart + sb.length() + 8 ) );
 191  
 
 192  
             try
 193  
             {
 194  18
                 SimpleDateFormat df = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
 195  18
                 snapshotTimestamp = Long.valueOf( df.parse( sb.toString() ).getTime() );
 196  
             }
 197  1
             catch ( ParseException e )
 198  
             {
 199  17
             }
 200  
 
 201  18
             int buildNumberPos = vSnapshotStart + sb.length();
 202  18
             StringBuffer bnr = new StringBuffer();
 203  41
             while ( s.charAt( buildNumberPos ) >= '0' && s.charAt( buildNumberPos ) <= '9' )
 204  
             {
 205  23
                 sb.append( s.charAt( buildNumberPos ) );
 206  23
                 bnr.append( s.charAt( buildNumberPos ) );
 207  23
                 buildNumberPos++;
 208  
             }
 209  18
             String snapshotBuildNumber = sb.toString();
 210  18
             snapshotBuildNo = Integer.parseInt( bnr.toString() );
 211  
 
 212  16
             int n = version.length() > 9 ? version.length() - 9 + 1 : 0;
 213  
 
 214  16
             String tail = s.substring( vEndPos + artifactId.length() + n + snapshotBuildNumber.length() + 2 );
 215  
 
 216  16
             int nExtPos = tail.indexOf( '.' );
 217  
 
 218  16
             if ( nExtPos == -1 )
 219  
             {
 220  
                 // NX-563: not allowing extensionless paths to be interpreted as artifact
 221  0
                 return null;
 222  
             }
 223  
 
 224  16
             ext = tail.substring( nExtPos + 1 );
 225  
 
 226  16
             classifier = tail.charAt( 0 ) == '-' ? tail.substring( 1, nExtPos ) : null;
 227  
 
 228  16
             version = version.substring( 0, version.length() - 8 ) + snapshotBuildNumber;
 229  
         }
 230  
 
 231  28
         return new Gav( groupId, artifactId, version, classifier, ext, snapshotBuildNo, snapshotTimestamp, fileName,
 232  
             checksum, checksumType, signature, signatureType );
 233  
     }
 234  
 
 235  
     public String gavToPath( Gav gav )
 236  
     {
 237  26
         StringBuffer path = new StringBuffer( "/" );
 238  
 
 239  26
         path.append( gav.getGroupId().replaceAll( "(?m)(.)\\.", "$1/" ) ); // replace all '.' except the first char
 240  
 
 241  26
         path.append( "/" );
 242  
 
 243  26
         path.append( gav.getArtifactId() );
 244  
 
 245  26
         path.append( "/" );
 246  
 
 247  26
         path.append( gav.getBaseVersion() );
 248  
 
 249  26
         path.append( "/" );
 250  
 
 251  26
         path.append( calculateArtifactName( gav ) );
 252  
 
 253  26
         return path.toString();
 254  
     }
 255  
 
 256  
     public String calculateArtifactName( Gav gav )
 257  
     {
 258  26
         if ( gav.getName() != null && gav.getName().trim().length() > 0 )
 259  
         {
 260  26
             return gav.getName();
 261  
         }
 262  
         else
 263  
         {
 264  0
             StringBuffer path = new StringBuffer( gav.getArtifactId() );
 265  
 
 266  0
             path.append( "-" );
 267  
 
 268  0
             path.append( gav.getVersion() );
 269  
 
 270  0
             if ( gav.getClassifier() != null && gav.getClassifier().trim().length() > 0 )
 271  
             {
 272  0
                 path.append( "-" );
 273  
 
 274  0
                 path.append( gav.getClassifier() );
 275  
             }
 276  
 
 277  0
             if ( gav.getExtension() != null )
 278  
             {
 279  0
                 path.append( "." );
 280  
 
 281  0
                 path.append( gav.getExtension() );
 282  
             }
 283  
 
 284  0
             if ( gav.isSignature() )
 285  
             {
 286  0
                 path.append( "." );
 287  
 
 288  0
                 path.append( gav.getSignatureType().toString() );
 289  
             }
 290  
 
 291  0
             if ( gav.isHash() )
 292  
             {
 293  0
                 path.append( "." );
 294  
 
 295  0
                 path.append( gav.getHashType().toString() );
 296  
             }
 297  
 
 298  0
             return path.toString();
 299  
         }
 300  
     }
 301  
 
 302  
 }