KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > util > URLUtil


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
18 /* $Id: URLUtil.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.util;
21
22
23 /**
24  * DOCUMENT ME!
25  */

26 public class URLUtil {
27     /**
28      * DOCUMENT ME!
29      *
30      * @param args DOCUMENT ME!
31      */

32     public static void main(String JavaDoc[] args) {
33         System.out.println(URLUtil.complete("http://www.apache.org/download/index.html",
34                 "../images/lenya.jpeg"));
35         System.out.println(URLUtil.complete("http://www.apache.org/download/index.html",
36                 "/images/lenya.jpeg"));
37     }
38
39     /**
40      * DOCUMENT ME!
41      *
42      * @param parent DOCUMENT ME!
43      * @param child DOCUMENT ME!
44      *
45      * @return DOCUMENT ME!
46      */

47     public static String JavaDoc complete(String JavaDoc parent, String JavaDoc child) {
48         String JavaDoc url = child;
49         String JavaDoc urlLowCase = child.toLowerCase();
50         String JavaDoc currentURLPath = parent.substring(0, parent.lastIndexOf("/"));
51         String JavaDoc rootURL = parent.substring(0, parent.indexOf("/", 8));
52
53         if (urlLowCase.startsWith("/")) {
54             url = rootURL + url;
55         } else if (urlLowCase.startsWith("./")) {
56             url = currentURLPath + url.substring(1, url.length());
57         } else if (urlLowCase.startsWith("../")) {
58             int back = 1;
59
60             while (urlLowCase.indexOf("../", back * 3) != -1)
61                 back++;
62
63             int pos = currentURLPath.length();
64             int count = back;
65
66             while (count-- > 0) {
67                 pos = currentURLPath.lastIndexOf("/", pos) - 1;
68             }
69
70             url = currentURLPath.substring(0, pos + 2) + url.substring(3 * back, url.length());
71         } else if (urlLowCase.startsWith("javascript:")) {
72             // handle javascript:...
73
System.err.println(".parseHREF(): parseJavaScript is not implemented yet");
74         } else if (urlLowCase.startsWith("#")) {
75             // internal anchor... ignore.
76
url = null;
77         } else if (urlLowCase.startsWith("mailto:")) {
78             // handle mailto:...
79
url = null;
80         } else {
81             url = currentURLPath + "/" + url;
82         }
83
84         // strip anchor if exists otherwise crawler may index content multiple times
85
// links to the same url but with unique anchors would be considered unique
86
// by the crawler when they should not be
87
if (url != null) {
88             int i;
89
90             if ((i = url.indexOf("#")) != -1) {
91                 url = url.substring(0, i);
92             }
93         }
94
95         return url;
96     }
97 }
98
Popular Tags