01: /*
02: * @(#)decodeURL.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 javax.servlet.http.*;
16: import org.pnuts.net.URLEncoding;
17:
18: /*
19: * decodeURL(str {, encoding})
20: */
21: public class decodeURL extends PnutsFunction {
22:
23: public decodeURL() {
24: super ("decodeURL");
25: }
26:
27: public boolean defined(int narg) {
28: return (narg == 1 || narg == 2);
29: }
30:
31: protected Object exec(Object[] args, Context context) {
32: int nargs = args.length;
33: String str;
34: String encoding;
35: if (nargs == 1) {
36: str = (String) args[0];
37: encoding = ServletEncoding.getDefaultInputEncoding(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.decode(str, encoding);
50: }
51: } catch (UnsupportedEncodingException e) {
52: throw new PnutsException(e, context);
53: }
54: }
55:
56: public String toString() {
57: return "function decodeURL(str {, encoding})";
58: }
59: }
|