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 public abstract class ScmUrlUtils {
36 private static final String ILLEGAL_SCM_URL =
37 "The scm url must be on the form 'scm:<scm provider><delimiter><provider specific part>' "
38 + "where <delimiter> can be either ':' or '|'.";
39
40 /**
41 * Get the delimiter used in the scm url.
42 *
43 * @param scmUrl a valid scm url to parse
44 * @return the delimiter used in the scm url
45 */
46 public static String getDelimiter(String scmUrl) {
47 scmUrl = scmUrl.substring(4);
48
49 int index = scmUrl.indexOf('|');
50
51 if (index == -1) {
52 index = scmUrl.indexOf(':');
53
54 if (index == -1) {
55 throw new IllegalArgumentException("The scm url does not contain a valid delimiter.");
56 }
57 }
58
59 return scmUrl.substring(index, index + 1);
60 }
61
62 /**
63 * Get the scm provider from the scm url.
64 *
65 * @param scmUrl a valid scm url to parse
66 * @return the scm provider from the scm url
67 */
68 public static String getProvider(String scmUrl) {
69 String delimiter = getDelimiter(scmUrl);
70
71 scmUrl = scmUrl.substring(4);
72
73 int firstDelimiterIndex = scmUrl.indexOf(delimiter);
74
75 return scmUrl.substring(0, firstDelimiterIndex);
76 }
77
78 /**
79 * Get the provider specific part of the scm url.
80 *
81 * @param scmUrl a valid scm url to parse
82 * @return the provider specific part of the scm url
83 */
84 public static String getProviderSpecificPart(String scmUrl) {
85 String delimiter = getDelimiter(scmUrl);
86
87 scmUrl = scmUrl.substring(4);
88
89 int firstDelimiterIndex = scmUrl.indexOf(delimiter);
90
91 return scmUrl.substring(firstDelimiterIndex + 1);
92 }
93
94 /**
95 * Validate that the scm url is in the correct format.
96 * <p>
97 * <strong>Note</strong>: does not validate scm provider specific format.
98 * </p>
99 *
100 * @param scmUrl the scm url to validate
101 * @return <code>true</code> if the scm url is in the correct format,
102 * otherwise <code>false</code>
103 */
104 public static boolean isValid(String scmUrl) {
105 List<String> messages = validate(scmUrl);
106
107 return messages.isEmpty();
108 }
109
110 /**
111 * Validate that the scm url is in the correct format.
112 * <p>
113 * <strong>Note</strong>: does not validate scm provider specific format.
114 * </p>
115 *
116 * @param scmUrl the scm url to validate
117 * @return a <code>List</code> that contains the errors that occured
118 */
119 public static List<String> validate(String scmUrl) {
120 List<String> messages = new ArrayList<>();
121
122 if (scmUrl == null) {
123 messages.add("The scm url cannot be null.");
124
125 return messages;
126 }
127
128 if (!scmUrl.startsWith("scm:")) {
129 messages.add("The scm url must start with 'scm:'.");
130
131 return messages;
132 }
133
134 if (scmUrl.length() < 6) {
135 messages.add(ILLEGAL_SCM_URL);
136
137 return messages;
138 }
139
140 try {
141 getDelimiter(scmUrl);
142 } catch (IllegalArgumentException e) {
143 messages.add(e.getMessage());
144 }
145
146 return messages;
147 }
148 }