01: /**********************************************************************************
02: * $URL: https://source.sakaiproject.org/svn/portal/tags/sakai_2-4-1/portal-util/util/src/java/org/sakaiproject/portal/util/URLUtils.java $
03: * $Id: URLUtils.java 29143 2007-04-19 01:10:38Z ajpoland@iupui.edu $
04: ***********************************************************************************
05: *
06: * Copyright (c) 2006 The Sakai Foundation.
07: *
08: * Licensed under the Educational Community License, Version 1.0 (the "License");
09: * you may not use this file except in compliance with the License.
10: * You may obtain a copy of the License at
11: *
12: * http://www.opensource.org/licenses/ecl1.php
13: *
14: * Unless required by applicable law or agreed to in writing, software
15: * distributed under the License is distributed on an "AS IS" BASIS,
16: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: * See the License for the specific language governing permissions and
18: * limitations under the License.
19: *
20: **********************************************************************************/package org.sakaiproject.portal.util;
21:
22: import java.io.UnsupportedEncodingException;
23: import java.net.URLEncoder;
24:
25: /**
26: * @author ieb
27: * @since Sakai 2.4
28: * @version $Rev: 29143 $
29: */
30:
31: public class URLUtils {
32:
33: public static String addParameter(String URL, String name,
34: String value) {
35: int qpos = URL.indexOf('?');
36: int hpos = URL.indexOf('#');
37: char sep = qpos == -1 ? '?' : '&';
38: String seg = sep + encodeUrl(name) + '=' + encodeUrl(value);
39: return hpos == -1 ? URL + seg : URL.substring(0, hpos) + seg
40: + URL.substring(hpos);
41: }
42:
43: /**
44: * The same behaviour as Web.escapeUrl, only without the "funky encoding" of
45: * the characters ? and ; (uses JDK URLEncoder directly).
46: *
47: * @param toencode
48: * The string to encode.
49: * @return <code>toencode</code> fully escaped using URL rules.
50: */
51: public static String encodeUrl(String url) {
52: try {
53: return URLEncoder.encode(url, "UTF-8");
54: } catch (UnsupportedEncodingException uee) {
55: throw new IllegalArgumentException(uee);
56: }
57: }
58:
59: }
|