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