Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
URLResolver |
|
| 2.6666666666666665;2.667 |
1 | /* | |
2 | * Copyright 1999,2004 The Apache Software Foundation. | |
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 | ||
17 | package org.apache.commons.feedparser.network; | |
18 | ||
19 | ||
20 | /** | |
21 | * This is a portable and thin URL resolver. The goal is to quickly resolve and | |
22 | * normalize URLs. This includes potentially saving redirects and having them | |
23 | * fully qualified. | |
24 | * | |
25 | * @author <a href="mailto:burton@openprivacy.org">Kevin A. Burton</a> | |
26 | * @version $Id: URLResolver.java 373622 2006-01-30 22:53:00Z mvdb $ | |
27 | */ | |
28 | 0 | public class URLResolver { |
29 | ||
30 | public static String resolve( String resource ) { | |
31 | ||
32 | //include "www" in hostnames like xmlhack.com | |
33 | 0 | resource = resolveTrailingSlash( resource ); |
34 | 0 | resource = resolveNoHostname( resource ); |
35 | ||
36 | 0 | return resource; |
37 | ||
38 | } | |
39 | ||
40 | private static String resolveNoHostname( String resource ) { | |
41 | ||
42 | 0 | if ( resource.startsWith( "http://" ) ) { |
43 | ||
44 | 0 | int first = resource.indexOf( "." ); |
45 | 0 | int second = resource.indexOf( ".", first + 1 ); |
46 | ||
47 | 0 | if ( second == -1 ) { |
48 | //then we don't have a hostname on this domain. | |
49 | 0 | return resource.substring( 0, "http://".length() ) + |
50 | "www." + | |
51 | resource.substring( "http://".length(), resource.length() ); | |
52 | } | |
53 | ||
54 | } | |
55 | ||
56 | 0 | return resource; |
57 | ||
58 | } | |
59 | ||
60 | private static String resolveTrailingSlash( String resource ) { | |
61 | ||
62 | 0 | if ( resource.startsWith( "http://" ) && ( resource.endsWith( "org" ) || |
63 | resource.endsWith( "com" ) || | |
64 | resource.endsWith( "net" ) ) ) { | |
65 | ||
66 | 0 | return resource + "/"; |
67 | ||
68 | } | |
69 | ||
70 | 0 | return resource; |
71 | ||
72 | } | |
73 | ||
74 | } |