1 /*
2 * ========================================================================
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * ========================================================================
20 */
21 package org.apache.cactus.internal.util;
22
23 import java.net.URL;
24
25 /**
26 * Various utility methods for URL manipulation.
27 *
28 * @version $Id: UrlUtil.java 238991 2004-05-22 11:34:50Z vmassol $
29 */
30 public class UrlUtil
31 {
32 /**
33 * Returns the path part of the URL. This method is needed for
34 * JDK 1.2 support as <code>URL.getPath()</code> does not exist in
35 * JDK 1.2 (only for JDK 1.3+).
36 *
37 * @param theURL the URL from which to extract the path
38 * @return the path part of the URL
39 */
40 public static String getPath(URL theURL)
41 {
42 String file = theURL.getFile();
43 String path = null;
44
45 if (file != null)
46 {
47 int q = file.lastIndexOf('?');
48
49 if (q != -1)
50 {
51 path = file.substring(0, q);
52 }
53 else
54 {
55 path = file;
56 }
57 }
58
59 return path;
60 }
61
62 /**
63 * Returns the query string of the URL. This method is needed for
64 * JDK 1.2 support as <code>URL.getQuery()</code> does not exist in
65 * JDK 1.2 (only for JDK 1.3+).
66 *
67 * @param theURL the URL from which to extract the query string
68 * @return the query string portion of the URL
69 */
70 public static String getQuery(URL theURL)
71 {
72 String file = theURL.getFile();
73 String query = null;
74
75 if (file != null)
76 {
77 int q = file.lastIndexOf('?');
78
79 if (q != -1)
80 {
81 query = file.substring(q + 1);
82 }
83 else
84 {
85 query = "";
86 }
87 }
88
89 return query;
90 }
91 }