Coverage Report - org.apache.maven.scm.provider.ScmUrlUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ScmUrlUtils
96 %
32/33
100 %
10/10
3
 
 1  
 package org.apache.maven.scm.provider;
 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.ArrayList;
 23  
 import java.util.List;
 24  
 
 25  
 /**
 26  
  * A utility class that validates and parses scm url:s. The code here is
 27  
  * <strong>not</strong> scm provider specific.
 28  
  * <p/>
 29  
  * If you need methods that work for a specific scm provider, please create a
 30  
  * similar class for that provider. E.g. create the class CvsScmUrlUtils if
 31  
  * you need cvs specific checking/parsing.
 32  
  * </p>
 33  
  *
 34  
  * @author <a href="mailto:dennisl@apache.org">Dennis Lundberg</a>
 35  
  * @version $Id: ScmUrlUtils.java 1054130 2010-12-31 16:01:45Z olamy $
 36  
  */
 37  0
 public abstract class ScmUrlUtils
 38  
 {
 39  
     private static final String ILLEGAL_SCM_URL =
 40  
         "The scm url must be on the form 'scm:<scm provider><delimiter><provider specific part>' "
 41  
             + "where <delimiter> can be either ':' or '|'.";
 42  
 
 43  
     /**
 44  
      * Get the delimiter used in the scm url.
 45  
      *
 46  
      * @param scmUrl A valid scm url to parse
 47  
      * @return The delimiter used in the scm url
 48  
      */
 49  
     public static String getDelimiter( String scmUrl )
 50  
     {
 51  23
         scmUrl = scmUrl.substring( 4 );
 52  
 
 53  23
         int index = scmUrl.indexOf( '|' );
 54  
 
 55  23
         if ( index == -1 )
 56  
         {
 57  11
             index = scmUrl.indexOf( ':' );
 58  
 
 59  11
             if ( index == -1 )
 60  
             {
 61  1
                 throw new IllegalArgumentException( "The scm url does not contain a valid delimiter." );
 62  
             }
 63  
         }
 64  
 
 65  22
         return scmUrl.substring( index, index + 1 );
 66  
     }
 67  
 
 68  
     /**
 69  
      * Get the scm provider from the scm url.
 70  
      *
 71  
      * @param scmUrl A valid scm url to parse
 72  
      * @return The scm provider from the scm url
 73  
      */
 74  
     public static String getProvider( String scmUrl )
 75  
     {
 76  7
         String delimiter = getDelimiter( scmUrl );
 77  
 
 78  7
         scmUrl = scmUrl.substring( 4 );
 79  
 
 80  7
         int firstDelimiterIndex = scmUrl.indexOf( delimiter );
 81  
 
 82  7
         return scmUrl.substring( 0, firstDelimiterIndex );
 83  
     }
 84  
 
 85  
     /**
 86  
      * Get the provider specific part of the scm url.
 87  
      *
 88  
      * @param scmUrl A valid scm url to parse
 89  
      * @return The provider specific part of the scm url
 90  
      */
 91  
     public static String getProviderSpecificPart( String scmUrl )
 92  
     {
 93  7
         String delimiter = getDelimiter( scmUrl );
 94  
 
 95  7
         scmUrl = scmUrl.substring( 4 );
 96  
 
 97  7
         int firstDelimiterIndex = scmUrl.indexOf( delimiter );
 98  
 
 99  7
         return scmUrl.substring( firstDelimiterIndex + 1 );
 100  
     }
 101  
 
 102  
     /**
 103  
      * Validate that the scm url is in the correct format.
 104  
      * <p/>
 105  
      * <strong>Note</strong>: does not validate scm provider specific format.
 106  
      * </p>
 107  
      *
 108  
      * @param scmUrl The scm url to validate
 109  
      * @return <code>true</code> if the scm url is in the correct format,
 110  
      *         otherwise <code>false</code>
 111  
      */
 112  
     public static boolean isValid( String scmUrl )
 113  
     {
 114  10
         List<String> messages = validate( scmUrl );
 115  
 
 116  10
         return messages.isEmpty();
 117  
     }
 118  
 
 119  
     /**
 120  
      * Validate that the scm url is in the correct format.
 121  
      * <p/>
 122  
      * <strong>Note</strong>: does not validate scm provider specific format.
 123  
      * </p>
 124  
      *
 125  
      * @param scmUrl The scm url to validate
 126  
      * @return A <code>List</code> that contains the errors that occured
 127  
      */
 128  
     public static List<String> validate( String scmUrl )
 129  
     {
 130  10
         List<String> messages = new ArrayList<String>();
 131  
 
 132  10
         if ( scmUrl == null )
 133  
         {
 134  1
             messages.add( "The scm url cannot be null." );
 135  
 
 136  1
             return messages;
 137  
         }
 138  
 
 139  9
         if ( !scmUrl.startsWith( "scm:" ) )
 140  
         {
 141  2
             messages.add( "The scm url must start with 'scm:'." );
 142  
 
 143  2
             return messages;
 144  
         }
 145  
 
 146  7
         if ( scmUrl.length() < 6 )
 147  
         {
 148  1
             messages.add( ILLEGAL_SCM_URL );
 149  
 
 150  1
             return messages;
 151  
         }
 152  
 
 153  
         try
 154  
         {
 155  6
             getDelimiter( scmUrl );
 156  
         }
 157  1
         catch ( IllegalArgumentException e )
 158  
         {
 159  1
             messages.add( e.getMessage() );
 160  5
         }
 161  
 
 162  6
         return messages;
 163  
     }
 164  
 }