01: /*
02: * @(#)encodeURL.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.servlet;
10:
11: import pnuts.servlet.*;
12: import pnuts.lang.*;
13: import java.io.*;
14: import javax.servlet.*;
15: import org.pnuts.net.URLEncoding;
16:
17: /*
18: * encodeURL(str {, encoding})
19: */
20: public class encodeURL extends PnutsFunction {
21:
22: public encodeURL() {
23: super ("encodeURL");
24: }
25:
26: public boolean defined(int narg) {
27: return (narg == 1 || narg == 2);
28: }
29:
30: protected Object exec(Object[] args, Context context) {
31: int nargs = args.length;
32: String str;
33: String encoding;
34: if (nargs == 1) {
35: str = (String) args[0];
36: encoding = ServletEncoding
37: .getDefaultOutputEncoding(context);
38: } else if (nargs == 2) {
39: str = (String) args[0];
40: encoding = (String) args[1];
41: } else {
42: undefined(args, context);
43: return null;
44: }
45: try {
46: if (str == null) {
47: return "";
48: } else {
49: return URLEncoding.encode(str, encoding);
50: }
51: } catch (UnsupportedEncodingException e) {
52: throw new PnutsException(e, context);
53: }
54: }
55:
56: public String toString() {
57: return "function encodeURL(str {, encoding})";
58: }
59: }
|