Coverage Report - org.apache.maven.archiva.repository.content.FilenameParser
 
Classes in this File Line Coverage Branch Coverage Complexity
FilenameParser
0%
0/87
0%
0/50
3.9
 
 1  
 package org.apache.maven.archiva.repository.content;
 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 org.apache.maven.archiva.common.utils.VersionUtil;
 23  
 
 24  
 import java.util.regex.Matcher;
 25  
 import java.util.regex.Pattern;
 26  
 
 27  
 /**
 28  
  * Generic Filename Parser for use with layout routines.
 29  
  *
 30  
  * @version $Id: FilenameParser.java 718864 2008-11-19 06:33:35Z brett $
 31  
  */
 32  
 public class FilenameParser
 33  
 {
 34  
     private String name;
 35  
 
 36  
     private String extension;
 37  
 
 38  
     private int offset;
 39  
 
 40  0
     private static final Pattern mavenPluginPattern = Pattern.compile( "(maven-.*-plugin)|(.*-maven-plugin)" );
 41  
 
 42  0
     private static final Pattern extensionPattern =
 43  
         Pattern.compile( "(\\.tar\\.gz$)|(\\.tar\\.bz2$)|(\\.[\\-a-z0-9]*$)", Pattern.CASE_INSENSITIVE );
 44  
 
 45  0
     private static final Pattern SNAPSHOT_PATTERN = Pattern.compile( "^([0-9]{8}\\.[0-9]{6}-[0-9]+)(.*)$" );
 46  
 
 47  0
     private static final Pattern section = Pattern.compile( "([^-]*)" );
 48  
 
 49  
     private Matcher matcher;
 50  
 
 51  
     protected FilenameParser( String filename )
 52  0
     {
 53  0
         this.name = filename;
 54  
 
 55  0
         Matcher mat = extensionPattern.matcher( name );
 56  0
         if ( mat.find() )
 57  
         {
 58  0
             extension = filename.substring( mat.start() + 1 );
 59  0
             name = name.substring( 0, name.length() - extension.length() - 1 );
 60  
         }
 61  
 
 62  0
         matcher = section.matcher( name );
 63  
 
 64  0
         reset();
 65  0
     }
 66  
 
 67  
     protected void reset()
 68  
     {
 69  0
         offset = 0;
 70  0
     }
 71  
 
 72  
     protected String next()
 73  
     {
 74  
         // Past the end of the string.
 75  0
         if ( offset > name.length() )
 76  
         {
 77  0
             return null;
 78  
         }
 79  
 
 80  
         // Return the next section.
 81  0
         if ( matcher.find( offset ) )
 82  
         {
 83  
             // Return found section.
 84  0
             offset = matcher.end() + 1;
 85  0
             return matcher.group();
 86  
         }
 87  
 
 88  
         // Nothing to return.
 89  0
         return null;
 90  
     }
 91  
 
 92  
     protected String expect( String expected )
 93  
     {
 94  0
         String value = null;
 95  
 
 96  0
         if ( name.startsWith( expected, offset ) )
 97  
         {
 98  0
             value = expected;
 99  
         }
 100  0
         else if ( VersionUtil.isGenericSnapshot( expected ) )
 101  
         {
 102  0
             String version = name.substring( offset );
 103  
 
 104  
             // check it starts with the same version up to the snapshot part
 105  0
             int leadingLength = expected.length() - 9;
 106  0
             if ( leadingLength > 0 && version.startsWith( expected.substring( 0, leadingLength ) ) &&
 107  
                 version.length() > leadingLength )
 108  
             {
 109  
                 // If we expect a non-generic snapshot - look for the timestamp
 110  0
                 Matcher m = SNAPSHOT_PATTERN.matcher( version.substring( leadingLength + 1 ) );
 111  0
                 if ( m.matches() )
 112  
                 {
 113  0
                     value = version.substring( 0, leadingLength + 1 ) + m.group( 1 );
 114  
                 }
 115  
             }
 116  
         }
 117  
 
 118  0
         if ( value != null )
 119  
         {
 120  
             // Potential hit. check for '.' or '-' at end of expected.
 121  0
             int seperatorOffset = offset + value.length();
 122  
 
 123  
             // Test for "out of bounds" first. 
 124  0
             if ( seperatorOffset >= name.length() )
 125  
             {
 126  0
                 offset = name.length();
 127  0
                 return value;
 128  
             }
 129  
 
 130  
             // Test for seperator char.
 131  0
             char seperatorChar = name.charAt( seperatorOffset );
 132  0
             if ( ( seperatorChar == '-' ) || ( seperatorChar == '.' ) )
 133  
             {
 134  0
                 offset = seperatorOffset + 1;
 135  0
                 return value;
 136  
             }
 137  
         }
 138  
 
 139  0
         return null;
 140  
     }
 141  
 
 142  
     /**
 143  
      * Get the current seperator character.
 144  
      *
 145  
      * @return the seperator character (either '.' or '-'), or 0 if no seperator character available.
 146  
      */
 147  
     protected char seperator()
 148  
     {
 149  
         // Past the end of the string?
 150  0
         if ( offset >= name.length() )
 151  
         {
 152  0
             return 0;
 153  
         }
 154  
 
 155  
         // Before the start of the string?
 156  0
         if ( offset <= 0 )
 157  
         {
 158  0
             return 0;
 159  
         }
 160  
 
 161  0
         return name.charAt( offset - 1 );
 162  
     }
 163  
 
 164  
     protected String getName()
 165  
     {
 166  0
         return name;
 167  
     }
 168  
 
 169  
     protected String getExtension()
 170  
     {
 171  0
         return extension;
 172  
     }
 173  
 
 174  
     protected String remaining()
 175  
     {
 176  0
         if ( offset >= name.length() )
 177  
         {
 178  0
             return null;
 179  
         }
 180  
 
 181  0
         String end = name.substring( offset );
 182  0
         offset = name.length();
 183  0
         return end;
 184  
     }
 185  
 
 186  
     protected String nextNonVersion()
 187  
     {
 188  0
         boolean done = false;
 189  
 
 190  0
         StringBuffer ver = new StringBuffer();
 191  
 
 192  
         // Any text upto the end of a special case is considered non-version. 
 193  0
         Matcher specialMat = mavenPluginPattern.matcher( name );
 194  0
         if ( specialMat.find() )
 195  
         {
 196  0
             ver.append( name.substring( offset, specialMat.end() ) );
 197  0
             offset = specialMat.end() + 1;
 198  
         }
 199  
 
 200  0
         while ( !done )
 201  
         {
 202  0
             int initialOffset = offset;
 203  0
             String section = next();
 204  0
             if ( section == null )
 205  
             {
 206  0
                 done = true;
 207  
             }
 208  0
             else if ( !VersionUtil.isVersion( section ) )
 209  
             {
 210  0
                 if ( ver.length() > 0 )
 211  
                 {
 212  0
                     ver.append( '-' );
 213  
                 }
 214  0
                 ver.append( section );
 215  
             }
 216  
             else
 217  
             {
 218  0
                 offset = initialOffset;
 219  0
                 done = true;
 220  
             }
 221  0
         }
 222  
 
 223  0
         return ver.toString();
 224  
     }
 225  
 
 226  
     protected String nextVersion()
 227  
     {
 228  0
         boolean done = false;
 229  
 
 230  0
         StringBuffer ver = new StringBuffer();
 231  
 
 232  0
         while ( !done )
 233  
         {
 234  0
             int initialOffset = offset;
 235  0
             String section = next();
 236  0
             if ( section == null )
 237  
             {
 238  0
                 done = true;
 239  
             }
 240  0
             else if ( VersionUtil.isVersion( section ) )
 241  
             {
 242  0
                 if ( ver.length() > 0 )
 243  
                 {
 244  0
                     ver.append( '-' );
 245  
                 }
 246  0
                 ver.append( section );
 247  
             }
 248  
             else
 249  
             {
 250  0
                 offset = initialOffset;
 251  0
                 done = true;
 252  
             }
 253  0
         }
 254  
 
 255  0
         return ver.toString();
 256  
     }
 257  
 
 258  
 
 259  
 }