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

📄 excelconfigmanagerimpl.java

📁 J2EE项目开发Excel数据导入导出操作组件源代码(附带说明文档)
💻 JAVA
字号:
package com.javayjm.excel.config.impl;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.util.XMLErrorHandler;
import org.xml.sax.SAXException;

import com.ibatis.common.resources.Resources;
import com.javayjm.excel.config.ConfigConstant;
import com.javayjm.excel.config.ExcelConfigManager;
import com.javayjm.excel.config.RuturnConfig;
import com.javayjm.excel.config.RuturnPropertyParam;
import com.javayjm.excel.util.ValidateColumn;

public class ExcelConfigManagerImpl implements ExcelConfigManager {

    private String configName = "ImportExcelToModel.xml";

    private SAXReader saxReader;

    private Document doc;

    private Element root;

    public ExcelConfigManagerImpl() {
        String str = this.getClass().getResource("/").getPath();
        // this.getClass().getResourceAsStream("/");

        System.out.println(str + configName);

        // InputStream in = ClassLoader.getSystemResourceAsStream(configName);

        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(configName);

        saxReader = new SAXReader();

        try {
            // Reader reader = Resources.getResourceAsReader(configName);

            // doc = saxReader.read(new File(str + configName));
            
            //saxReader.read(in)支持 UTF-8
            doc = saxReader.read(in);
            saxReader.setValidation(true);

            // saxReader.setContentHandler(this);
            // XMLErrorHandler error = new XMLErrorHandler();
            // saxReader.setErrorHandler(error);
            //    
            // saxReader.setFeature("http://xml.org/sax/features/namespaces",true);
            // saxReader.setFeature("http://xml.org/sax/features/validation",true);
            // saxReader.setFeature("http://apache.org/xml/features/validation/schema",
            // true);
            // saxReader.setFeature("http://apache.org/xml/features/validation/schema-full-checking",true);
            //saxReader.read(reader); 不支持 UTF-8
           
            // doc = saxReader.read(reader);

        } catch (DocumentException e) {
            // TODO 自动生成 catch 块
            e.printStackTrace();
            // } catch (IOException e) {
            // // TODO Auto-generated catch block
            // e.printStackTrace();
        }
        root = doc.getRootElement();
    }

    public Element getModelElement(String modelName) {
        System.out.println("modelName = " + modelName + "-----------");
        List list = root.elements();
        Element model = null;
        Element returnModel = null;
        for (Iterator it = list.iterator(); it.hasNext();) {
            model = (Element) it.next();
            System.out.println(model.attributeValue("id"));
            if (model.attributeValue("id").equals(modelName)) {
                returnModel = model;
                break;
            }
        }
        if(returnModel==null){
            System.out.println(" >>> modelName : " + modelName + " 在配制文件中不存在!");
        }
        return returnModel;
    }

    public RuturnConfig getModel(String modelName, String flag) {
        Element model = this.getModelElement(modelName);
        RuturnConfig result = new RuturnConfig();
        if (model != null) {
            result.setClassName(model.attributeValue(ConfigConstant.MODEL_CLASS));
            // result.setPropertyMap(result,model);
            this.setPropertyMap(result, model);
        }
        return result;
    }

    private void setPropertyMap(RuturnConfig result, Element model) {
        Map<String, RuturnPropertyParam> propertyMap = new HashMap<String, RuturnPropertyParam>();
        Map<String, RuturnPropertyParam> columnMap = new HashMap<String, RuturnPropertyParam>();
        List list = model.elements();
        Element property = null;

        for (Iterator it = list.iterator(); it.hasNext();) {
            property = (Element) it.next();
            // System.out.println("Name = " + property.getName());
            if (property.getName().equals("property")) {
                RuturnPropertyParam modelProperty = new RuturnPropertyParam();
                modelProperty.setName(property.attributeValue(ConfigConstant.PROPERTY_NAME));
                modelProperty.setColumn(property.attributeValue(ConfigConstant.PROPERTY_CLOUMN));
                modelProperty.setExcelTitleName(property.attributeValue(ConfigConstant.PROPERTY_EXCEL_TITLE_NAME));
                modelProperty.setIsNull(property.attributeValue(ConfigConstant.PROPERTY_ISNULL));
                modelProperty.setColumnWidth(property.attributeValue(ConfigConstant.PROPERTY_COLUMN_WIDTH));
                modelProperty.setDataType(property.attributeValue(ConfigConstant.PROPERTY_DATA_TYPE));
                modelProperty.setMaxLength(property.attributeValue(ConfigConstant.PROPERTY_MAX_LENGTH));

                modelProperty.setFixity(property.attributeValue(ConfigConstant.PROPERTY_FIXITY));
                modelProperty.setCodeTableName(property.attributeValue(ConfigConstant.PROPERTY_CODE_TABLE_NAME));
                modelProperty.setDefaultValue(property.attributeValue(ConfigConstant.PROPERTY_DEFAULT));

                System.out.println(property.attributeValue("name"));
                
                String excelTitle = ValidateColumn.configValidate(propertyMap,modelProperty.getExcelTitleName());
                propertyMap.put(excelTitle, modelProperty);
                
                System.out.println("column = " + modelProperty.getColumn());
                columnMap.put(modelProperty.getColumn(), modelProperty);
            }
            if (property.getName().equals("flag")) {
                Map flagMap = new HashMap();
                flagMap.put(ConfigConstant.PROPERTY_NAME, property.attributeValue(ConfigConstant.PROPERTY_NAME));
                result.setFlagMap(flagMap);
            }
            if (property.getName().equals("message")) {
                Map messageMap = new HashMap();
                messageMap.put(ConfigConstant.PROPERTY_NAME, property.attributeValue(ConfigConstant.PROPERTY_NAME));
                messageMap.put(ConfigConstant.PROPERTY_EXCEL_TITLE_NAME, property.attributeValue(ConfigConstant.PROPERTY_EXCEL_TITLE_NAME));
                result.setMessageMap(messageMap);
            }
        }
        result.setPropertyMap(propertyMap);
        result.setColumnMap(columnMap);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ExcelConfigManagerImpl test = new ExcelConfigManagerImpl();
        test.getModel("deptModel", "");
        // System.out.println(model.attributeValue("class"));

    }
}

⌨️ 快捷键说明

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