com.pureinfo.srm.view.function.MakeSelectOptions.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.srm.view.function.MakeSelectOptions.java

Source

/**
 * PureInfo Command
 * @(#)MakeSelectOptions.java   1.0 2006-3-20
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

package com.pureinfo.srm.view.function;

import org.apache.commons.lang.ArrayUtils;

import com.pureinfo.dolphin.script.execute.IContext;
import com.pureinfo.dolphin.script.function.handler.FunctionHandlerUtil;
import com.pureinfo.dolphin.script.function.handler.IFunctionHandler;
import com.pureinfo.force.exception.PureException;
import com.pureinfo.force.html.HtmlUtil;

/**
 * <P>
 * Created on 2006-3-20 15:55:14 <BR>
 * Last modified on 2006-3-20
 * </P>
 * TODO describe MakeSelectOptions here ...
 * 
 * @author elmar.chen
 * @version 1.0, 2006-3-20
 * @since Command 1.0
 */
public class MakeSelectOptions implements IFunctionHandler {

    //    private static Logger logger = Logger.getLogger(MakeSelectOptions.class);
    public static final int IDX_DATAS = 0;

    public static final int IDX_DEFAULTS = 1;

    public static final int IDX_EXCLUDES = 2;

    /**
     * @see com.pureinfo.dolphin.script.function.handler.IFunctionHandler#perform(java.lang.Object[],
     *      com.pureinfo.dolphin.script.execute.IContext)
     */
    public Object perform(Object[] _args, IContext _context) throws PureException {
        FunctionHandlerUtil.validateArgsNum(_args, 1);

        String[] sDefault = new String[0];
        if (_args.length > IDX_DEFAULTS && _args[IDX_DEFAULTS] != null) {
            Object def = _args[IDX_DEFAULTS];
            if (def.getClass().isArray()) {
                Object[] objs = (Object[]) def;
                sDefault = new String[objs.length];
                for (int i = 0; i < objs.length; i++) {
                    sDefault[i] = objs[i].toString();
                }
            } else {
                sDefault = new String[] { def.toString() };
            }

        }
        String[] sExcludes = new String[0];
        if (_args.length > IDX_EXCLUDES && _args[IDX_EXCLUDES] != null) {
            String str = (String) _args[IDX_EXCLUDES];
            if (str.length() > 0) {
                sExcludes = str.split(",");
            }
        }

        String[][] datas = (String[][]) _args[IDX_DATAS];
        StringBuffer sbuff = null;
        try {
            sbuff = new StringBuffer();
            for (int i = 0; i < datas.length; i++) {
                if (ArrayUtils.contains(sExcludes, datas[i][0])) {
                    continue;
                }
                sbuff.append("<option value=\"").append(HtmlUtil.encode(datas[i][0])).append('"');
                sbuff.append(ArrayUtils.contains(sDefault, datas[i][0]) ? " selected" : "");
                sbuff.append('>');
                sbuff.append(HtmlUtil.encode(datas[i][1]));
            }
            return sbuff.toString();
        } finally {
            if (sbuff != null)
                sbuff.setLength(0);
        }
    }

}