001package org.apache.maven.scm.provider;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.ArrayList;
023import java.util.List;
024
025/**
026 * A utility class that validates and parses scm url:s. The code here is
027 * <strong>not</strong> scm provider specific.
028 * <p/>
029 * If you need methods that work for a specific scm provider, please create a
030 * similar class for that provider. E.g. create the class CvsScmUrlUtils if
031 * you need cvs specific checking/parsing.
032 * </p>
033 *
034 * @author <a href="mailto:dennisl@apache.org">Dennis Lundberg</a>
035 *
036 */
037public abstract class ScmUrlUtils
038{
039    private static final String ILLEGAL_SCM_URL =
040        "The scm url must be on the form 'scm:<scm provider><delimiter><provider specific part>' "
041            + "where <delimiter> can be either ':' or '|'.";
042
043    /**
044     * Get the delimiter used in the scm url.
045     *
046     * @param scmUrl A valid scm url to parse
047     * @return The delimiter used in the scm url
048     */
049    public static String getDelimiter( String scmUrl )
050    {
051        scmUrl = scmUrl.substring( 4 );
052
053        int index = scmUrl.indexOf( '|' );
054
055        if ( index == -1 )
056        {
057            index = scmUrl.indexOf( ':' );
058
059            if ( index == -1 )
060            {
061                throw new IllegalArgumentException( "The scm url does not contain a valid delimiter." );
062            }
063        }
064
065        return scmUrl.substring( index, index + 1 );
066    }
067
068    /**
069     * Get the scm provider from the scm url.
070     *
071     * @param scmUrl A valid scm url to parse
072     * @return The scm provider from the scm url
073     */
074    public static String getProvider( String scmUrl )
075    {
076        String delimiter = getDelimiter( scmUrl );
077
078        scmUrl = scmUrl.substring( 4 );
079
080        int firstDelimiterIndex = scmUrl.indexOf( delimiter );
081
082        return scmUrl.substring( 0, firstDelimiterIndex );
083    }
084
085    /**
086     * Get the provider specific part of the scm url.
087     *
088     * @param scmUrl A valid scm url to parse
089     * @return The provider specific part of the scm url
090     */
091    public static String getProviderSpecificPart( String scmUrl )
092    {
093        String delimiter = getDelimiter( scmUrl );
094
095        scmUrl = scmUrl.substring( 4 );
096
097        int firstDelimiterIndex = scmUrl.indexOf( delimiter );
098
099        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        List<String> messages = validate( scmUrl );
115
116        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        List<String> messages = new ArrayList<String>();
131
132        if ( scmUrl == null )
133        {
134            messages.add( "The scm url cannot be null." );
135
136            return messages;
137        }
138
139        if ( !scmUrl.startsWith( "scm:" ) )
140        {
141            messages.add( "The scm url must start with 'scm:'." );
142
143            return messages;
144        }
145
146        if ( scmUrl.length() < 6 )
147        {
148            messages.add( ILLEGAL_SCM_URL );
149
150            return messages;
151        }
152
153        try
154        {
155            getDelimiter( scmUrl );
156        }
157        catch ( IllegalArgumentException e )
158        {
159            messages.add( e.getMessage() );
160        }
161
162        return messages;
163    }
164}