Update of /cvsroot/displaytag/table-ben/src/com/tablelib/core/taglibs
In directory sc8-pr-cvs1:/tmp/cvs-serv11591

Added Files:
        BaseTag.java 
Log Message:


--- NEW FILE: BaseTag.java ---
/**
 * Created by IntelliJ IDEA.
 * User: Benjamin Simpson
 * Date: Jan 11, 2003
 * Time: 8:44:51 PM
 */
package com.tablelib.core.taglibs;

import org.apache.commons.beanutils.PropertyUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.List;
import java.util.Collections;
import java.text.MessageFormat;
import java.lang.reflect.InvocationTargetException;

public class BaseTag extends javax.servlet.jsp.tagext.BodyTagSupport {
    private HttpServletRequest req = null;
    /**
     * Get the request from state : pageContext if null
     * @return HttpServletRequest
     */
    protected HttpServletRequest getRequest() {
        return req != null ? req : initRequest();
    }
    /**
     * Get the request from the pageContext
     * @return
     */
    private HttpServletRequest initRequest() {
        req = (HttpServletRequest) this.pageContext.getRequest();
        return req;
    }

    public HttpServletResponse getResponse() {
        return (HttpServletResponse) this.pageContext.getResponse();
    }

    public JspWriter getOut() {
        return pageContext.getOut();
    }

    public void setPageContextAttribute(String key, Object val) {
        pageContext.setAttribute(key,val);
    }

    protected String getParameter(String parm) {
        return getRequest().getParameter(parm);
    }

    protected String getParameter(String parmname, String def) {
        String val = getParameter(parmname);
        return val == null?def:val;
    }

    public static String getProperty(String key) {
        if(prop == null) loadDefaultProperties();
        return prop.getProperty(key);
    }

    public static void setProperty(String key, String value) {
        if(prop == null) loadDefaultProperties();
        prop.setProperty(key,value);
    }



    protected static Properties getProperties() {
        return prop;
    }
    private static Properties prop = null;
    private static void loadDefaultProperties() {
        prop = new Properties();

        prop.setProperty("basic.show.header", "true");
        prop.setProperty("basic.msg.empty_list", "Nothing found to display");

        prop.setProperty("sort.behavior", "page");

        prop.setProperty("export.banner", "Export options: {0}");
        prop.setProperty("export.banner.sepchar", " | ");
        prop.setProperty("export.csv", "true");
        prop.setProperty("export.csv.label", "CSV");
        prop.setProperty("export.csv.mimetype", "text/csv");
        prop.setProperty("export.csv.include_header", "false");
        prop.setProperty("export.excel", "true");
        prop.setProperty("export.excel.label", "Excel");
        prop.setProperty("export.excel.mimetype", "application/vnd.ms-excel");
        prop.setProperty("export.excel.include_header", "false");
        prop.setProperty("export.xml", "true");
        prop.setProperty("export.xml.label", "XML");
        prop.setProperty("export.xml.mimetype", "text/xml");
        prop.setProperty("export.amount", "page");
        prop.setProperty("export.decorated", "true");

        prop.setProperty("paging.banner.placement", "top");
        prop.setProperty("paging.banner.item_name", "item");
        prop.setProperty("paging.banner.items_name", "items");
        prop.setProperty("paging.banner.no_items_found", "No {0} found.");
        prop.setProperty("paging.banner.one_item_found", "1 {0} found.");
        prop.setProperty("paging.banner.all_items_found", "{0} {1} found, showing all 
{2}");
        prop.setProperty("paging.banner.some_items_found", "{0} {1} found, displaying 
{2} to {3}");
        prop.setProperty("paging.banner.include_first_last", "false");
        prop.setProperty("paging.banner.first_label", "First");
        prop.setProperty("paging.banner.last_label", "Last");
        prop.setProperty("paging.banner.prev_label", "Prev");
        prop.setProperty("paging.banner.next_label", "Next");
        prop.setProperty("paging.banner.group_size", "8");

        prop.setProperty("error.msg.cant_find_bean", "Could not find bean {0} in scope 
{1}");
        prop.setProperty("error.msg.invalid_bean", "The bean that you gave me is not a 
Collection I understand: {0}");
        prop.setProperty("error.msg.no_column_tags", "Please provide column tags.");
        prop.setProperty("error.msg.illegal_access_exception", "IllegalAccessException 
trying to fetch property {0} on bean {1}");
        prop.setProperty("error.msg.invocation_target_exception", 
"InvocationTargetException trying to fetch property {0} on bean {1}");
        prop.setProperty("error.msg.nosuchmethod_exception", "NoSuchMethodException 
trying to fetch property {0} on bean {1}");
        prop.setProperty("error.msg.invalid_decorator", "Decorator class is not a 
subclass of TableDecorator");
        prop.setProperty("error.msg.invalid_page", "Invalid page ({0}) provided, value 
should be between 1 and {1}");
    }

    protected List getListData(String key, String property, int scope) {
        Object ret =  pageContext.getAttribute(key,scope);
        if(property == null) return (List) ret;
        try {
            return (List) PropertyUtils.getProperty(ret,property);
        } catch(Exception e) {
            e.printStackTrace();
            return Collections.EMPTY_LIST;
        }
    }

    public Object lookup(String name,
                         String property,
                         int scope)
            throws JspException {


        // Look up the requested bean, and return if requested
        Object bean = pageContext.findAttribute(name);
        if (property == null) return bean;

        if (bean == null) {
            Object[] objs = {name, scope+""};
            throw new JspException(
                    MessageFormat.format(getProperty("error.msg.cant_find_bean"), 
objs));
        }

        // Locate and return the specified property

        try {
            return PropertyUtils.getProperty(bean, property);
        } catch (IllegalAccessException e) {
            Object[] objs = {property, name};
            throw new JspException(
                    
MessageFormat.format(getProperty("error.msg.illegal_access_exception"), objs));
        } catch (InvocationTargetException e) {
            Object[] objs = {property, name};
            throw new JspException(
                    
MessageFormat.format(getProperty("error.msg.invocation_target_exception"), objs));
        } catch (NoSuchMethodException e) {
            Object[] objs = {property, name};
            throw new JspException(
                    
MessageFormat.format(getProperty("error.msg.nosuchmethod_exception"), objs));
        }
    }

    public Object lookup(String name, int scope) throws JspException {
        return pageContext.getAttribute(name, scope);
    }


    protected void write(String val) throws JspTagException {
        try {
            pageContext.getOut().write(val);
        } catch (IOException e) {
            throw new JspTagException(
                    "JspWriter trew an ioexception: " + e.getMessage());
        }
    }

    public void write(Object val) throws JspTagException {
        this.write(val.toString());
    }
}




-------------------------------------------------------
This SF.net email is sponsored by: VM Ware
With VMware you can run multiple operating systems on a single machine.
WITHOUT REBOOTING! Mix Linux / Windows / Novell virtual machines at the
same time. Free trial click here: http://www.vmware.com/wl/offer/345/0
_______________________________________________
displaytag-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/displaytag-devel

Reply via email to