⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datasourceconfig.java

📁 Excel Report是一款基于Excel的报表生成工具
💻 JAVA
字号:
/*
 * Created on 2006-6-2
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package net.excel.report.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.excel.report.Logger;
import net.excel.report.base.element.Variable;

import org.dom4j.Element;



/**
 * 数据源配置信息保存类,该类负解析和保存一条数据源配置信息。
 * @author juny
 */
public class DataSourceConfig {
    private static Logger log = Logger.getLogger(DataSourceConfig.class);
    public static void main(String[] args) {
    }
    
    public DataSourceConfig(String name){
        params = new ArrayList();
        paramTypes = new ArrayList();
        fields = new HashMap();
        sql = "";
        this.name = name;
    }
    
    public String getName(){
        return this.name;
    }
    
    /**
     * 解析datasource对应的数据源配置节点,并把解析出来的信息保存到当前对象中。
     * @param node
     * @return
     * @throws Exception
     */
    public boolean bindDataSource(Element node) throws Exception{
        //读取Datasource信息
        Iterator notes = node.elementIterator();
        setType(node.attributeValue("type"));
        
        Element subNode = null;
        String nodeName = null;
        while (notes.hasNext()){
            subNode = (Element)notes.next();
            nodeName = subNode.getName();
            if(nodeName.equals(DATASOURCE_NOTE_ARGUMENT)){
                //取得参数名称
                String param = subNode.attributeValue("name");
                params.add(param);
                //取得参数的数据类型
                String type = subNode.attributeValue("type");
                if(null == type || "".equals(type)){
                    this.paramTypes.add(Variable.STRING);
                }else{
                    this.paramTypes.add(type);
                }
            }else if(nodeName.equals(DATASOURCE_NOTE_FIELD)){
                String name = subNode.attributeValue("name");
                String type = subNode.attributeValue("type");
                if(null != name && null != type &&
                        !"".equals(name) && !"".equals(type)){
                    fields.put(name, type);
                }
            }else if(dsType.equals(DS_TYPE_DATABASE)){ //数据库数据源需要配置SQL
                if(nodeName.equals(DATASOURCE_NOTE_SQL)){
                    sql = subNode.attributeValue("value");
                    if(sql.trim().length() < 1){
                        throw new Exception(" Sql element mustn't be null!");
                    }
                }
            }
        }
        return true;
    }
    
    /**
     * 设置数据源类型。
     * @param type
     */
    private void setType(String type){
        //默认数据源类型为DS_TYPE_STATIC
        if(null == type || "".equals(type)){
            dsType = DS_TYPE_STATIC;
            return;
        }
        if(isSupportedType(type)){
            dsType = type;
            return;
        }
	    //用户在配置文件中配置了不被支持的类型。
	    log.warn("Warning: " + type + " is not a valid datasource type.");
        dsType = DS_TYPE_STATIC;
    }
    
    /**
     * 取得数据源参数配置信息
     * @return
     */
    public List getParams(){
        return params;
    }
    
    /**
     * 取得数据源对应参数的数据类型
     * @return
     */
    public List getParamTypes(){
        return paramTypes;
    }
    
    /**
     * 取得数据源中定义的字段值
     * @return
     */
    public Map getFields(){
        return fields;
    }
    
    /**
     * 取得数据源中SQL节点配置的sql信息,该函数只有在DS_TYPE_DATABASE类型风格的
     * 数据源才有效,其他类型的数据源该函数返回null.
     * @return
     */
    public String getSQL(){
        return sql;
    }
    
    /**
     * 取得数据源类型信息。
     * @return
     */
    public String getDataSourceType(){
        return dsType;
    }
    
    public String toString(){
        StringBuffer ret = new StringBuffer();
        ret.append("type:" + dsType);
        if(dsType.equals(DS_TYPE_DATABASE)){
            ret.append(" SQL: " + sql);
        }
        ret.append("\n paramaters : \n");
        for(int i=0; i<params.size(); i++){
            ret.append(" ---param(" + i + "):" + params.get(i));
            ret.append("  type:" + paramTypes.get(i));
            ret.append("---\n");
        }
        return ret.toString();
    }
    
    /**
     * 判断指定的数据源风格是否被支持
     * @param dsType
     * @return
     */
    public static boolean isSupportedType(String dsType){
        if(dsType.equals(DS_TYPE_DATABASE) ||
           dsType.equals(DS_TYPE_STATIC) ){
            return true;
        }
        return false;
    }
    
    private List params = null;
    private List paramTypes = null;
    private Map fields = null;
    private String sql = null;
    private String name = null;
    private String dsType = null; //数据源类型
    
    //定义数据源类型,添加新的数据源后一定要修改isSupportedType();
    /*
     * 标识数据源类型为数据库类型数据源
     */
    public static final String DS_TYPE_DATABASE = "database";
    /*
     * 标识数据源类型为静态数据源类型
     */
    public static final String DS_TYPE_STATIC = "static";
    /*
     * 标识数据源类型为流数据源类型
     */
    public static final String DS_TYPE_STREAM = "stream";
    
    public static final String DATASOURCE = "datasource"; 
    private static final String DATASOURCE_NOTE_SQL = "sql";
    private static final String DATASOURCE_NOTE_ARGUMENT = "parameter";
    private static final String DATASOURCE_NOTE_FIELD = "field";
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -