1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.maven.scm.provider;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 /**
25 * A utility class that validates and parses scm url:s. The code here is
26 * <strong>not</strong> scm provider specific.
27 * <p>
28 * If you need methods that work for a specific scm provider, please create a
29 * similar class for that provider. E.g. create the class SvnScmUrlUtils if
30 * you need Subversion-specific checking/parsing.
31 * </p>
32 *
33 * @author <a href="mailto:dennisl@apache.org">Dennis Lundberg</a>
34 *
35 */
36 public abstract class ScmUrlUtils {
37 private static final String ILLEGAL_SCM_URL =
38 "The scm url must be on the form 'scm:<scm provider><delimiter><provider specific part>' "
39 + "where <delimiter> can be either ':' or '|'.";
40
41 /**
42 * Get the delimiter used in the scm url.
43 *
44 * @param scmUrl A valid scm url to parse
45 * @return The delimiter used in the scm url
46 */
47 public static String getDelimiter(String scmUrl) {
48 scmUrl = scmUrl.substring(4);
49
50 int index = scmUrl.indexOf('|');
51
52 if (index == -1) {
53 index = scmUrl.indexOf(':');
54
55 if (index == -1) {
56 throw new IllegalArgumentException("The scm url does not contain a valid delimiter.");
57 }
58 }
59
60 return scmUrl.substring(index, index + 1);
61 }
62
63 /**
64 * Get the scm provider from the scm url.
65 *
66 * @param scmUrl A valid scm url to parse
67 * @return The scm provider from the scm url
68 */
69 public static String getProvider(String scmUrl) {
70 String delimiter = getDelimiter(scmUrl);
71
72 scmUrl = scmUrl.substring(4);
73
74 int firstDelimiterIndex = scmUrl.indexOf(delimiter);
75
76 return scmUrl.substring(0, firstDelimiterIndex);
77 }
78
79 /**
80 * Get the provider specific part of the scm url.
81 *
82 * @param scmUrl A valid scm url to parse
83 * @return The provider specific part of the scm url
84 */
85 public static String getProviderSpecificPart(String scmUrl) {
86 String delimiter = getDelimiter(scmUrl);
87
88 scmUrl = scmUrl.substring(4);
89
90 int firstDelimiterIndex = scmUrl.indexOf(delimiter);
91
92 return scmUrl.substring(firstDelimiterIndex + 1);
93 }
94
95 /**
96 * Validate that the scm url is in the correct format.
97 * <p>
98 * <strong>Note</strong>: does not validate scm provider specific format.
99 * </p>
100 *
101 * @param scmUrl The scm url to validate
102 * @return <code>true</code> if the scm url is in the correct format,
103 * otherwise <code>false</code>
104 */
105 public static boolean isValid(String scmUrl) {
106 List<String> messages = validate(scmUrl);
107
108 return messages.isEmpty();
109 }
110
111 /**
112 * Validate that the scm url is in the correct format.
113 * <p>
114 * <strong>Note</strong>: does not validate scm provider specific format.
115 * </p>
116 *
117 * @param scmUrl The scm url to validate
118 * @return A <code>List</code> that contains the errors that occured
119 */
120 public static List<String> validate(String scmUrl) {
121 List<String> messages = new ArrayList<String>();
122
123 if (scmUrl == null) {
124 messages.add("The scm url cannot be null.");
125
126 return messages;
127 }
128
129 if (!scmUrl.startsWith("scm:")) {
130 messages.add("The scm url must start with 'scm:'.");
131
132 return messages;
133 }
134
135 if (scmUrl.length() < 6) {
136 messages.add(ILLEGAL_SCM_URL);
137
138 return messages;
139 }
140
141 try {
142 getDelimiter(scmUrl);
143 } catch (IllegalArgumentException e) {
144 messages.add(e.getMessage());
145 }
146
147 return messages;
148 }
149 }