01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.harmony.jndi.internal;
19:
20: /**
21: * Identifies the simplest URL syntax:
22: * <code>{scheme}:{scheme specific part}</code>.
23: */
24: public class UrlParser {
25:
26: /*
27: * Prevent instantiate.
28: */
29: private UrlParser() {
30: super ();
31: }
32:
33: /**
34: * Returns an URL's scheme part, in lower case. If the url is not a valid
35: * URL, null is returned.
36: *
37: * @param url
38: * a url string
39: * @return the URL's scheme part, in lower case. If the url is not a valid
40: * URL, null is returned.
41: */
42: public static String getScheme(String url) {
43: if (null == url) {
44: return null;
45: }
46: int colPos = url.indexOf(':');
47: if (colPos < 0) {
48: return null;
49: }
50: String scheme = url.substring(0, colPos);
51: char c;
52: boolean inCharSet;
53: for (int i = 0; i < scheme.length(); i++) {
54: c = scheme.charAt(i);
55: inCharSet = (c >= 'A' && c <= 'Z')
56: || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')
57: || c == '+' || c == '.' || c == '-' || c == '_';
58: if (!inCharSet) {
59: return null;
60: }
61: }
62: return scheme;
63: }
64:
65: }
|