John Reck | fb3017f | 2010-10-26 19:01:24 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.browser; |
| 17 | |
| 18 | import java.util.regex.Matcher; |
| 19 | import java.util.regex.Pattern; |
| 20 | |
| 21 | public class UrlUtils { |
| 22 | |
| 23 | // Regular expression which matches http://, followed by some stuff, followed by |
| 24 | // optionally a trailing slash, all matched as separate groups. |
| 25 | private static final Pattern STRIP_URL_PATTERN = Pattern.compile("^(http://)(.*?)(/$)?"); |
| 26 | |
| 27 | private UrlUtils() { /* cannot be instantiated */ } |
| 28 | |
| 29 | /** |
| 30 | * Strips the provided url of preceding "http://" and any trailing "/". Does not |
| 31 | * strip "https://". If the provided string cannot be stripped, the original string |
| 32 | * is returned. |
| 33 | * |
| 34 | * TODO: Put this in TextUtils to be used by other packages doing something similar. |
| 35 | * |
| 36 | * @param url a url to strip, like "http://www.google.com/" |
| 37 | * @return a stripped url like "www.google.com", or the original string if it could |
| 38 | * not be stripped |
| 39 | */ |
| 40 | /* package */ static String stripUrl(String url) { |
| 41 | if (url == null) return null; |
| 42 | Matcher m = STRIP_URL_PATTERN.matcher(url); |
| 43 | if (m.matches() && m.groupCount() == 3) { |
| 44 | return m.group(2); |
| 45 | } else { |
| 46 | return url; |
| 47 | } |
| 48 | } |
| 49 | } |