@URLEncode and @URLDecode / extension to the extension Library


package de.eknori.extlib.javascript;

import com.ibm.designer.runtime.extensions.JavaScriptProvider;
import com.ibm.jscript.JSContext;

public class JSFunctions implements JavaScriptProvider {

	public void registerWrappers(JSContext jsContext) {
		jsContext.getRegistry().registerGlobalPrototype("@FunctionsEx",
				new MyFunctions(jsContext));

	}

}

==================================================================================

package de.eknori.extlib.javascript;

import java.net.URLDecoder;
import java.net.URLEncoder;

import com.ibm.commons.util.StringUtil;
import com.ibm.jscript.InterpretException;
import com.ibm.jscript.JSContext;
import com.ibm.jscript.JavaScriptException;
import com.ibm.jscript.engine.IExecutionContext;
import com.ibm.jscript.types.BuiltinFunction;
import com.ibm.jscript.types.FBSDefaultObject;
import com.ibm.jscript.types.FBSObject;
import com.ibm.jscript.types.FBSUtility;
import com.ibm.jscript.types.FBSValue;
import com.ibm.jscript.types.FBSValueVector;

public class MyFunctions extends FBSDefaultObject {

	private static final String UTF_DEFAULT = "UTF-8";
	// Functions IDs
	private static final int FCT_URLENCODE = 1;
	private static final int FCT_URLDECODE = 2;

	public MyFunctions(JSContext jsContext) {
		super(jsContext, null, false);
		addFunction(FCT_URLENCODE, "@URLEncode", "(s:T),(enc:T");
		addFunction(FCT_URLDECODE, "@URLDecode", "(s:T),(enc:T");
	}

	private void addFunction(int index, String functionName, String... params) {
		createMethod(functionName, FBSObject.P_NODELETE | FBSObject.P_READONLY,
				new NotesFunction(getJSContext(), index, functionName, params));
	}

	public static class NotesFunction extends BuiltinFunction {

		private String functionName;
		private int index;
		private String[] params;

		NotesFunction(JSContext jsContext, int index, String functionName,
				String[] params) {

			super(jsContext);
			this.functionName = functionName;
			this.index = index;
			this.params = params;
		}

		public int getIndex() {
			return this.index;
		}

		@Override
		protected String[] getCallParameters() {
			return this.params;
		}

		@Override
		public String getFunctionName() {
			return this.functionName;
		}

		public static boolean IsNullOrEmpty(String s) {
			boolean b = false;
			if (s == null || s.contentEquals("")) {
				b = true;
			}
			return b;
		}

		@Override
		public FBSValue call(IExecutionContext context, FBSValueVector args,
				FBSObject _this) throws JavaScriptException {

			try {
				switch (index) {


				case FCT_URLENCODE: {

					/**
					 * @URLEncode()
					 * @author Ulrich krause
					 * @since 05/2012 
					 **/

					if (args.size() >= 1) {
						String enc = "";
						String s = args.get(0).stringValue();
						try {
							enc = args.get(1).stringValue();
						} catch (Exception e) {
						}

						if (NotesFunction.IsNullOrEmpty(enc)) {
							enc = UTF_DEFAULT;
						}
						return FBSUtility.wrap(context.getJSContext(),
								URLEncoder.encode(s, enc));
					}
				}
					break;

				case FCT_URLDECODE: {

					/**
					 * @URLDecode()
					 * @author Ulrich krause
					 * @since 05/2012 
					 **/
					if (args.size() >= 1) {
						String enc = "";
						String s = args.get(0).stringValue();
						try {
							enc = args.get(1).stringValue();
						} catch (Exception e) {
						}

						if (NotesFunction.IsNullOrEmpty(enc)
								|| args.size() == 1) {
							enc = UTF_DEFAULT;
						}
						return FBSUtility.wrap(context.getJSContext(),
								URLDecoder.decode(s, enc));
					}
				}
					break;
					
				default: {
					throw new InterpretException(null, StringUtil.format(
							"Internal error: unknown function \'{0}\'",
							functionName));
				}
				}
			} catch (Exception e) {
				throw new InterpretException(e, StringUtil.format(
						"Error while executing function \'{0}\'", functionName));
			}

			throw new InterpretException(null, StringUtil.format(
					"Cannot evaluate function \'{0}\'", functionName));
		}

	}

}

All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.
No comments yetLogin first to comment...