规则设计到三个对象。工具将XML 分三级解析。
类 |
对应
xml节点
|
作用 |
BeansSpecification | beans | 表示一个根节点,包含所有Bean定义信息。 |
BeanSpecification | bean | 代表bean的配置信息,包含多个propertys |
PropertySpecification | property | 代表bean 的属性配置信息 |
BeansSpecification.java
package com.ivfly.xlsbean;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* 1.去掉了父类
* 2.添加单例
* 根元素
*/
public class BeansSpecification{
private static BeansSpecification instance= null;
public static BeansSpecification getInstance(InputStream xmlStream){
if(instance== null){
instance = BeansSpecificationUtil.getBeans(xmlStream);
}
return instance;
}
/**
* 跟元素下所有的beans
*/
private List<BeanSpecification> beans = new ArrayList<BeanSpecification>();
public List<BeanSpecification> getBeans() {
return this.beans;
}
public void addBean(BeanSpecification field) {
this.beans.add(field);
}
/**
* 根据文件名和类名查找
* @param fileName
* @return
*/
public BeanSpecification getBean(String fileName) {
for (BeanSpecification bean : beans) {
if (FileUtil.wildcard(bean.getFileName(), fileName)||bean.getClassName().equals(fileName)) {
return bean;
}
}
return null;
}
public BeanSpecification getBeanByClassName(String className) {
for (BeanSpecification bean : beans) {
if (bean.getClassName().equals(className)) {
return bean;
}
}
return null;
}
}
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* 1.去掉了父类
* 2.添加单例
* 根元素
*/
public class BeansSpecification{
private static BeansSpecification instance= null;
public static BeansSpecification getInstance(InputStream xmlStream){
if(instance== null){
instance = BeansSpecificationUtil.getBeans(xmlStream);
}
return instance;
}
/**
* 跟元素下所有的beans
*/
private List<BeanSpecification> beans = new ArrayList<BeanSpecification>();
public List<BeanSpecification> getBeans() {
return this.beans;
}
public void addBean(BeanSpecification field) {
this.beans.add(field);
}
/**
* 根据文件名和类名查找
* @param fileName
* @return
*/
public BeanSpecification getBean(String fileName) {
for (BeanSpecification bean : beans) {
if (FileUtil.wildcard(bean.getFileName(), fileName)||bean.getClassName().equals(fileName)) {
return bean;
}
}
return null;
}
public BeanSpecification getBeanByClassName(String className) {
for (BeanSpecification bean : beans) {
if (bean.getClassName().equals(className)) {
return bean;
}
}
return null;
}
}
BeanSpecification.java
package com.ivfly.xlsbean;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.PropertyUtils;
/**
*
* 描述实体属性
*/
public class BeanSpecification{
private Map<String,PropertySpecification> properties = new HashMap<String,PropertySpecification>();
/**
* 类路径名
*/
private String className;
/**
* 描述对应Class的properties
*/
private Map describer;
/**
* 文件名
*/
private String fileName;
/**
* excel中数据记录的起点行数
*/
private Integer from;
/**
* 列标题所在行
*/
private Integer head;
/**
* 类属性和excel列名对应键值对
*/
private Map<String, String> nv= new LinkedHashMap<String,String>();
/**
* 类属性和excel列名对应键值对,反过来
*/
private Map<String, String> vn= new HashMap<String,String>();
private List<String> propertyList = new ArrayList<String>();
public void addProperty(PropertySpecification field) {
this.nv.put(field.getName(), field.getValue());
this.vn.put(field.getValue(),field.getName());
this.properties.put(field.getName(), field);
propertyList.add(field.getName());
}
public List<String> getPropertyList() {
return propertyList;
}
public String getClassName() {
return this.className;
}
public void setClassName(String classname) {
this.className = classname;
}
public Map<String,PropertySpecification> getProperty() {
return properties;
}
public String getPropertyNameByValue(String value) {
return vn.get(value);
}
public boolean nullable(String propertyName){
if( this.properties.containsKey(propertyName)){
return this.properties.get(propertyName).isNullable();
} else return false;
}
public void setProperty(Map<String,PropertySpecification> property) {
this.properties = property;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public Integer getFrom() {
return from;
}
public void setFrom(Integer from) {
this.from = from;
}
public Integer getHead() {
return head;
}
public void setHead(Integer head) {
this.head = head;
}
public Map<String, String> getNv() {
return nv;
}
public void setNv(Map<String, String> nv) {
this.nv = nv;
}
@SuppressWarnings( "unchecked")
public Map getDescriber() {
describer = new HashMap();
try {
Class cls = Class.forName( this.className);
Object bean = cls.newInstance();
describer = PropertyUtils.describe(bean);
} catch (Exception e) {
e.printStackTrace();
}
return describer;
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.PropertyUtils;
/**
*
* 描述实体属性
*/
public class BeanSpecification{
private Map<String,PropertySpecification> properties = new HashMap<String,PropertySpecification>();
/**
* 类路径名
*/
private String className;
/**
* 描述对应Class的properties
*/
private Map describer;
/**
* 文件名
*/
private String fileName;
/**
* excel中数据记录的起点行数
*/
private Integer from;
/**
* 列标题所在行
*/
private Integer head;
/**
* 类属性和excel列名对应键值对
*/
private Map<String, String> nv= new LinkedHashMap<String,String>();
/**
* 类属性和excel列名对应键值对,反过来
*/
private Map<String, String> vn= new HashMap<String,String>();
private List<String> propertyList = new ArrayList<String>();
public void addProperty(PropertySpecification field) {
this.nv.put(field.getName(), field.getValue());
this.vn.put(field.getValue(),field.getName());
this.properties.put(field.getName(), field);
propertyList.add(field.getName());
}
public List<String> getPropertyList() {
return propertyList;
}
public String getClassName() {
return this.className;
}
public void setClassName(String classname) {
this.className = classname;
}
public Map<String,PropertySpecification> getProperty() {
return properties;
}
public String getPropertyNameByValue(String value) {
return vn.get(value);
}
public boolean nullable(String propertyName){
if( this.properties.containsKey(propertyName)){
return this.properties.get(propertyName).isNullable();
} else return false;
}
public void setProperty(Map<String,PropertySpecification> property) {
this.properties = property;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public Integer getFrom() {
return from;
}
public void setFrom(Integer from) {
this.from = from;
}
public Integer getHead() {
return head;
}
public void setHead(Integer head) {
this.head = head;
}
public Map<String, String> getNv() {
return nv;
}
public void setNv(Map<String, String> nv) {
this.nv = nv;
}
@SuppressWarnings( "unchecked")
public Map getDescriber() {
describer = new HashMap();
try {
Class cls = Class.forName( this.className);
Object bean = cls.newInstance();
describer = PropertyUtils.describe(bean);
} catch (Exception e) {
e.printStackTrace();
}
return describer;
}
}
PropertySpecification.java
package com.ivfly.xlsbean;
/**
*
* 描述属性的映射
*
*/
public class PropertySpecification {
/**
* 顺序号 没有维护
*/
private int seq;
/**
* 格式
*/
private String formular;
/**
* 提示格式
*/
private String warringFormat;
private String type;
/**
* 属性名
*/
private String name;
/**
* 列名
*/
private String value;
/**
* 是否可以为空
*/
private boolean nullable= true;
private int index;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public int getIndex() {
return index;
}
public void setIndex( int index) {
this.index = index;
}
public boolean isNullable() {
return nullable;
}
public void setNullable( boolean nullable) {
this.nullable = nullable;
}
@Override
public String toString() {
return "name="+name+ ";value="+value+ ";seq="+seq+ ";type="+type+ ",nullable="+nullable;
}
public int getSeq() {
return seq;
}
public void setSeq( int seq) {
this.seq = seq;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getFormular() {
return formular;
}
public void setFormular(String formular) {
this.formular = formular;
}
public String getWarringFormat() {
return warringFormat;
}
public void setWarringFormat(String warringFormat) {
this.warringFormat = warringFormat;
}
}
/**
*
* 描述属性的映射
*
*/
public class PropertySpecification {
/**
* 顺序号 没有维护
*/
private int seq;
/**
* 格式
*/
private String formular;
/**
* 提示格式
*/
private String warringFormat;
private String type;
/**
* 属性名
*/
private String name;
/**
* 列名
*/
private String value;
/**
* 是否可以为空
*/
private boolean nullable= true;
private int index;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public int getIndex() {
return index;
}
public void setIndex( int index) {
this.index = index;
}
public boolean isNullable() {
return nullable;
}
public void setNullable( boolean nullable) {
this.nullable = nullable;
}
@Override
public String toString() {
return "name="+name+ ";value="+value+ ";seq="+seq+ ";type="+type+ ",nullable="+nullable;
}
public int getSeq() {
return seq;
}
public void setSeq( int seq) {
this.seq = seq;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getFormular() {
return formular;
}
public void setFormular(String formular) {
this.formular = formular;
}
public String getWarringFormat() {
return warringFormat;
}
public void setWarringFormat(String warringFormat) {
this.warringFormat = warringFormat;
}
}
本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/756533,如需转载请自行联系原作者