01: /*
02: * @(#)makeQueryString.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 java.io.UnsupportedEncodingException;
12: import java.util.Map;
13: import org.pnuts.net.URLEncoding;
14: import pnuts.lang.Context;
15: import pnuts.lang.PnutsException;
16: import pnuts.lang.PnutsFunction;
17:
18: /*
19: * makeQueryString(map {, encoding})
20: */
21: public class makeQueryString extends PnutsFunction {
22:
23: public makeQueryString() {
24: super ("makeQueryString");
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: Map map;
34: String encoding;
35: if (nargs == 1) {
36: map = (Map) args[0];
37: encoding = ServletEncoding
38: .getDefaultOutputEncoding(context);
39: } else if (nargs == 2) {
40: map = (Map) args[0];
41: encoding = (String) args[1];
42: } else {
43: undefined(args, context);
44: return null;
45: }
46: try {
47: if (map instanceof ServletParameter) {
48: return ((ServletParameter) map).toQueryString(encoding);
49: } else {
50: return URLEncoding.makeQueryString(map, encoding);
51: }
52: } catch (UnsupportedEncodingException e) {
53: throw new PnutsException(e, context);
54: }
55: }
56:
57: public String toString() {
58: return "makeQueryString(map {, encoding })";
59: }
60: }
|