View Javadoc

1   package org.apache.maven.util;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  
25  /**
26   * NOTE: This is very CVS specific, but I would like to try additional SCM
27   * package like subversion ASAP.
28   *
29   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
30   *
31   * @version $Id: RepositoryUtils.java 532339 2007-04-25 12:28:56Z ltheussl $
32   */
33  public final class RepositoryUtils
34  {
35      /**
36       * Get the separator used in an SCM string
37       * @param connection
38       * @return String that can be either ":" or "|"
39       */
40      public static String getSCMConnectionSeparator( String connection )
41      {
42          if ( connection == null )
43          {
44              throw new NullPointerException( "repository connection is null" );
45          }
46  
47          if( connection.indexOf( "|" ) != -1 ) {
48              return "|";
49          }
50          else
51          {
52              return ":";
53          }
54      }
55  
56      /**
57       * Splits an SCM string into parts
58       * @param connection
59       * @return String[]
60       */
61      public static String[] splitSCMConnection( String connection )
62      {
63          if ( connection == null )
64          {
65              throw new NullPointerException( "repository connection is null" );
66          }
67  
68          if ( connection.length() < 5 )
69          {
70              throw new IllegalArgumentException( 
71                  "repository connection is too short" );
72          }
73  
74          if ( !connection.startsWith( "scm:" ) )
75          {
76              throw new IllegalArgumentException( 
77                  "repository connection must start with scm:" );
78          }
79  
80          String delimiter = getSCMConnectionSeparator( connection );
81  
82          // If the tokenizer is going to work correctly then the character
83          // following "scm" must be the same as the delimiter, which is not
84          // always the case. Therefor we give it a modified connection.
85          String modifiedConnection = "scm" + delimiter + connection.substring( 4 );
86  
87          EnhancedStringTokenizer tok =
88              new EnhancedStringTokenizer( modifiedConnection, delimiter );
89  
90          String[] tokens = tokenizerToArray( tok );
91  
92          // for a valid repository, it should be scm:<provider> at least
93          if ( ( tokens.length >= 1 ) && tokens[1].equals( "cvs" ) )
94          {
95              if ( ( tokens.length >= 2 ) && tokens[2].equals( "local" ) )
96              {
97                  if ( tokens.length == 6 )
98                  {
99                      if ( ( tokens[3].length() > 0 )
100                         && !tokens[3].equals( "local" ) )
101                     {
102                         throw new IllegalArgumentException( 
103                             "cvs local repository connection string must specify 5 tokens, or an empty 3rd token if 6" );
104                     }
105                 }
106                 else if ( tokens.length == 5 )
107                 {
108                     String[] newTokens = new String[6];
109 
110                     newTokens[0] = tokens[0];
111                     newTokens[1] = tokens[1];
112                     newTokens[2] = tokens[2];
113                     newTokens[3] = "";
114                     newTokens[4] = tokens[3];
115                     newTokens[5] = tokens[4];
116                     tokens = newTokens;
117                 }
118                 else
119                 {
120                     throw new IllegalArgumentException( 
121                         "cvs local repository connection string doesn't contain five tokens" );
122                 }
123             }
124             else if ( ( tokens.length == 7 ) && tokens[2].equals( "pserver" ) && tokens[4].startsWith("@"))
125             {
126                 String[] newTokens = new String[6];
127 
128                 newTokens[0] = tokens[0];
129                 newTokens[1] = tokens[1];
130                 newTokens[2] = tokens[2];
131                 newTokens[3] = tokens[3] + tokens[4];
132                 newTokens[4] = tokens[5];
133                 newTokens[5] = tokens[6];
134                 tokens = newTokens;
135             }
136             else if ( tokens.length != 6 )
137             {
138                 throw new IllegalArgumentException( 
139                     "cvs repository connection string doesn't contain six tokens" );
140             }
141         }
142 
143         return tokens;
144     }
145 
146     /**
147      * Converts a tokenizer to an array of strings
148      * FIXME: This should be in a string util class
149      * @param tok
150      * @return String[]
151      */
152     public static String[] tokenizerToArray( EnhancedStringTokenizer tok )
153     {
154         List l = new ArrayList();
155 
156         while ( tok.hasMoreTokens() )
157         {
158             l.add( tok.nextToken() );
159         }
160 
161         return (String[]) l.toArray( new String[l.size()] );
162     }
163 }