01: /*
02: * @(#)decodeURL.java 1.1 05/06/14
03: *
04: * Copyright (c) 1997-2005 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.net;
10:
11: import pnuts.lang.*;
12: import java.io.*;
13: import java.net.*;
14:
15: /*
16: * decodeURL(str {, encoding})
17: */
18: public class decodeURL extends PnutsFunction {
19:
20: public decodeURL() {
21: super ("decodeURL");
22: }
23:
24: public boolean defined(int narg) {
25: return (narg == 1 || narg == 2);
26: }
27:
28: protected Object exec(Object[] args, Context context) {
29: int nargs = args.length;
30: String str;
31: String encoding;
32: if (nargs == 1) {
33: str = (String) args[0];
34: encoding = null;
35: } else if (nargs == 2) {
36: str = (String) args[0];
37: encoding = (String) args[1];
38: } else {
39: undefined(args, context);
40: return null;
41: }
42: try {
43: if (str == null) {
44: return "";
45: } else {
46: if (encoding == null) {
47: return URLDecoder.decode(str);
48: } else {
49: return URLDecoder.decode(str, encoding);
50: }
51: }
52: } catch (UnsupportedEncodingException e) {
53: throw new PnutsException(e, context);
54: }
55: }
56:
57: public String toString() {
58: return "function decodeURL(str {, encoding})";
59: }
60: }
|