利用freemarker+SAX解析xml的方式对excel文件字段校验
利用freemarker对参数进行校验
这篇文章主要用到的技术点:
自定义注解的使用
反射机制
SAX解析xml
Freemarker的运用
我们在工作中经常需要上传excel文件,然后在对文件中的字段进行校验。如果文件里的字段是反复出现,或者文件的字段比较多的话,这是就会使代码变得繁琐,而且也不容易维护。
比如说像下面那张学生的表格
我们需要对上面表中的每个字段做校验
1.userName 必传字段
2. telephone 手机电话号码格式校验
3. email格式的校验
4. birthday 格式的校验
5. userId的生成规则等
有时候我们用自定义注解+反射,往往可以使代码变得清晰明了,不说了,直接上代码:
package com.example.demo.Controller;
import com.example.demo.utils.DealException;
import com.example.demo.utils.ExcelUtils;
import com.example.demo.validate.TemplateXmlUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static freemarker.template.Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS;
@RestController
public class UploadExcelController {
@RequestMapping("/upload")
public Object uploadFile(MultipartFile file) throws IOException, TemplateException {
//读取文件的基本信息
String filename = file.getOriginalFilename();
InputStream inputStream = file.getInputStream();
try {
// Workbook workbook = ExcelUtils.buildWorkookFromStream(filename, inputStream);
//解析excel
List<Map<String, Object>> maps = ExcelUtils.excelToList(filename, inputStream, 0, 0);
Configuration configuration = new Configuration(DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
URL url = this.getClass().getClassLoader().getResource("freemarkers");
configuration.setDirectoryForTemplateLoading(new File(url.getFile()));
Template template = configuration.getTemplate("userMapper.ftl");
HashMap<String, Object> objMap = new HashMap<>();
objMap.put("maps",maps);
StringWriter sw = new StringWriter();
template.process(objMap,sw);
System.out.println(sw.toString());
String s = TemplateXmlUtil.analysisXml(sw.toString(), "UTF-8");
System.out.println(s);
return s;
} catch (DealException e) {
e.printStackTrace();
}
return null;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
这段代码首先解析了excel文件,将文件转化成list,下面贴出excelutils的代码
package com.example.demo.utils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ExcelUtils {
public static Workbook buildWorkookFromStream(String excelFileName, InputStream excelStream) throws IOException, DealException {
Workbook book=null;
if (excelFileName.endsWith("xls")){
book = new HSSFWorkbook(excelStream);
}else if(excelFileName.endsWith("xlsx")){
book=new XSSFWorkbook(excelStream);
}else{
throw new DealException("000000","文件初始化失败");
}
return book;
}
public static List<Map<String,Object>> excelToList(String excelName, InputStream excelStream, Integer sheetIndex, Integer startRow) throws IOException, DealException {
List<Map<String,Object>> resultList=new ArrayList<>();
Workbook workbook = buildWorkookFromStream(excelName, excelStream);
Sheet sheet = workbook.getSheetAt(sheetIndex);
//获取第一行的抬头
Row row = sheet.getRow(startRow);
int cellsNum = row.getPhysicalNumberOfCells();
List<String> titleList = new ArrayList<>();
for (int i = 0; i <cellsNum ; i++) {
Cell cell = row.getCell(i);
cell.setCellType(CellType.STRING);
String cellValue = cell.getStringCellValue().trim();
titleList.add(cellValue);
}
if(titleList.size()==0){
throw new DealException("000001","表中没有抬头");
}
int lastRowNum = sheet.getLastRowNum();
for (int i = startRow+1; i <=lastRowNum ; i++) {
Map<String,Object> rowDataMap= new HashMap<>();
Row rows = sheet.getRow(i);
boolean blankFlag=true;
for (int cellIndex = 0; cellIndex <titleList.size() ; cellIndex++) {
Cell cell = rows.getCell(cellIndex);
if(null==cell){
rowDataMap.put(titleList.get(cellIndex),"");
continue;
}
cell.setCellType(CellType.STRING);
String cellVal = cell.getStringCellValue().trim();
rowDataMap.put(titleList.get(cellIndex),cellVal);
if(!StringUtils.isEmpty(cellVal)){
blankFlag=false;
}
}
if(!blankFlag){
resultList.add(rowDataMap);
}
}
return resultList;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
这里我就不对这个工具类做具体的介绍了。
下面贴出userMapper.ftl
<?xml version="1.0" encoding="UTF-8"?>
<root>
<#list maps as map>
<user>
<#list map?keys as itemKey>
<#if itemKey=="userName">
<userName isRequired="true">${map[itemKey]}</userName>
</#if>
<#if itemKey=="telephone">
<telephone isRequired="true">${map[itemKey]}</telephone>
</#if>
<#if itemKey=="email">
<email isRequired="true">${map[itemKey]}</email>
</#if>
<#if itemKey=="birthday">
<birthday isRequired="true">${map[itemKey]}</birthday>
</#if>
<#if itemKey=="userId">
<userId isRequired="true">${map[itemKey]}</userId>
</#if>
</#list>
</user>
</#list>
</root>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
freemarker 中调用process方法,将数据模型和模板进行整合。
主要验证的代码如下
package com.example.demo.validate;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ValidatorAnnotion {
public String validatorType();
public String message();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.example.demo.validate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Component
public class TemplateValidator {
private final static String regexEmail="^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
private final static String regexPhone="^1(3|4|5|7|8)\\d{9}$";
/**
* 验证是否为电子邮箱
*@param
*@return
*/
@ValidatorAnnotion(validatorType = "isEmail",message = "邮箱格式不正确")
public static boolean isEmail(Map contentMap){
String itemValue = (String)contentMap.get("itemValue");
return isMatchString(itemValue,regexEmail,true);
}
/**
* 验证手机号码的正确性
* @param contentMap
* @return
*/
@ValidatorAnnotion(validatorType = "isPhone",message = "电话号码格式不正确")
public static boolean isPhone(Map contentMap){
String itemValue= (String) contentMap.get("itemValue");
return isMatchString(itemValue,regexPhone,true);
}
/**
* 是否是必传
* @param contentMap
* @return
*/
@ValidatorAnnotion(validatorType = "isRequired",message = "必传")
public static boolean isRequired(Map contentMap){
String itemValue = (String) contentMap.get("itemValue");
return !StringUtils.isEmpty(itemValue);
}
/**
*
* @param itemValue
* @param regex
* @param caseSentivite
* @return
*/
private static boolean isMatchString(String itemValue, String regex, boolean caseSentivite) {
boolean result=false;
if(null==itemValue||null==regex){
throw new NullPointerException("error,itemValue or regx is null");
}
Pattern pattern = null;
if(!caseSentivite){
pattern=Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
}else{
pattern=Pattern.compile(regex);
}
Matcher matcher = pattern.matcher(itemValue);
result= matcher.matches();
return result;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.example.demo.validate;
import com.example.demo.utils.DealException;
import com.example.demo.utils.ExcelUtils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
public class ValidatorBeanFactory {
/**
* 默认的校验类路径
*/
private static final String DEFAULT_VALIDATE_CLASSNAME=TemplateValidator.class.getName();
static Class<?> validatorClass=null;
public static Method[] methods=null;
static{
try {
validatorClass=Class.forName(DEFAULT_VALIDATE_CLASSNAME);
methods = validatorClass.getMethods();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void validate(StringBuffer buffer, Map<String,Object> attrMap) throws DealException {
boolean result=true;
for(Method method:methods){
//获取method方法上的注解
if(method.isAnnotationPresent(ValidatorAnnotion.class)){
ValidatorAnnotion annotation = method.getAnnotation(ValidatorAnnotion.class);
String validatorType = annotation.validatorType();
String attrName = (String) attrMap.get("attrName");
if(validatorType.equals(attrName)){
try {
result = (boolean) method.invoke(validatorClass, attrMap);
} catch (Exception e) {
throw new DealException("000003","参数校验异常");
}
if(!result){
buffer.append("节点").append(attrMap.get("itemName")).append(",").append(annotation.message());
}
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.example.demo.validate;
import com.example.demo.utils.DealException;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class TemplateXmlUtil {
public static String analysisXml(String xmlStr,String charset){
xmlStr= xmlStr.replaceAll("\r","");
SAXReader reader = new SAXReader();
StringBuffer sb=null;
try {
Document document = reader.read(new ByteArrayInputStream(xmlStr.getBytes(charset)));
Element xmlroot = document.getRootElement();
sb = new StringBuffer();
iteratorXml(xmlroot,sb);
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
private static void iteratorXml(Element xmlroot, StringBuffer sb) throws DealException {
Iterator<Element> iterator = xmlroot.elementIterator();
while (iterator.hasNext()){
Element xmlItem = iterator.next();
//获取属性名和属性值
List<Attribute> attributes = xmlItem.attributes();
for (Attribute attribute:attributes){
Map<String, Object> attrMap = new HashMap<>();
attrMap.put("attrName",attribute.getName());
attrMap.put("attrValue",attribute.getValue());
attrMap.put("itemName",xmlItem.getName());
attrMap.put("itemValue",xmlItem.getStringValue(http://www.my516.com));
ValidatorBeanFactory.validate(sb,attrMap);
}
iteratorXml(xmlItem,sb);
}
}
}
---------------------
利用freemarker+SAX解析xml的方式对excel文件字段校验的更多相关文章
- 利用django如何解析用户上传的excel文件
https://www.jb51.net/article/119452.htm 前言 我们在工作中的时候,会有这种需求:用户上传一个格式固定excel表格到网站上,然后程序负债解析内容并进行处理.我最 ...
- Python—使用xml.sax解析xml文件
什么是sax? SAX是一种基于事件驱动的API. 利用SAX解析XML文档牵涉到两个部分:解析器和事件处理器. 解析器负责读取XML文档,并向事件处理器发送事件,如元素开始跟元素结束事件; 而事件处 ...
- (转)Android 创建与解析XML—— Dom4j方式 .
转:http://blog.csdn.net/ithomer/article/details/7521605 1.Dom4j概述 dom4j is an easy to use, open sourc ...
- Android之SAX解析XML
一.SAX解析方法介绍 SAX(Simple API for XML)是一个解析速度快并且占用内存少的XML解析器,非常适合用于Android等移动设备. SAX解析器是一种基于事件的解析器,事件驱动 ...
- DOM&SAX解析XML
在上一篇随笔中分析了xml以及它的两种验证方式.我们有了xml,但是里面的内容要怎么才能得到呢?如果得不到的话,那么还是没用的,解析xml的方式主要有DOM跟SAX,其中DOM是W3C官方的解析方式, ...
- SAX解析xml浅析
SAX解析XML文件采用事件驱动的方式进行,也就是说,SAX是逐行扫描文件,遇到符合条件的设定条件后就会触发特定的事件,回调你写好的事件处理程序.使用SAX的优势在于其解析速度较快,占用内存较少(相对 ...
- JavaWeb学习日记----SAX解析XML
1.SAX解析XML文档的方式: 与DOM方式解析不同,DOM方式解析是根据XML的层级结构在内存中分配一个树形结构,把xml的标签,属性和文本都封装成对象.优点是可以很方便实现增删改操作.缺点是,如 ...
- Android SAX解析XML
本篇讲解一下SAX解析XML这种方式,首先来看一下它的基本介绍: SAX是一种以事件驱动的XML API,由它定义的事件流可以指定从解析器传到专门的处理程序的代码的XML结构,简单的讲,它是种解析速度 ...
- Dom,pull,Sax解析XML
本篇随笔将详细讲解如何在Android当中解析服务器端传过来的XML数据,这里将会介绍解析xml数据格式的三种方式,分别是DOM.SAX以及PULL. 一.DOM解析XML 我们首先来看看DOM(Do ...
随机推荐
- Eclipse 常用快捷键及使用技巧
做 java 开发的,经常会用 Eclipse 或者 MyEclise 集成开发环境,一些实用的 Eclipse 快捷键和使用技巧,可以在平常开发中节约出很多时间提高工作效率,下面我就结合自己开发中的 ...
- 【Codeforces】Round #376 (Div. 2)
http://codeforces.com/contest/731 不发题面了,自己点链接 总结一下 考场上 原以为这次要加很多raiting... 但FST狗记邓,只加了58rating 总结一下 ...
- Mac os x下配置 Android ndk 开发环境
1.阅读下面之前,请确保你android sdk的开发环境已经搭建好,ADT也最好是目前最新的. 2.到http://developer.android.com/tools/sdk/ndk/index ...
- uva 12265 贩卖土地
题目大意: 有一个矩阵 有些点可以取有些不能 求以每个点为右下角的子矩阵(里面点都可以取)的周长最大值 最后统计出每个周长对应矩阵的个数 思路: 单调栈 先预处理出每个点向上最多能延伸多长记为h(i, ...
- [noip模拟赛]跑跑步
https://www.zybuluo.com/ysner/note/1298652 题面 小胡同学是个热爱运动的好孩子. 每天晚上,小胡都会去操场上跑步,学校的操场可以看成一个由\(n\)个格子排成 ...
- ZOJ 3955 Saddle Point 校赛 一道计数题
ZOJ3955 题意是这样的 给定一个n*m的整数矩阵 n和m均小于1000 对这个矩阵删去任意行和列后剩余一个矩阵为M{x1,x2,,,,xm;y1,y2,,,,,yn}表示删除任意的M行N列 对于 ...
- bzoj1875 [SDOI2009]HH去散步——矩阵快速幂
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1875 有个限制是不能走回头路,比较麻烦: 所以把矩阵中的元素设成边的经过次数,单向边之间就好 ...
- Real-Time Compressive Tracking,实时压缩感知跟踪算法解读
这是Kaihua Zhang发表在ECCV2012的paper,文中提出了一种基于压缩感知(compressive sensing)的单目标跟踪算法,该算法利用满足压缩感知(compressive s ...
- 5. extjs 中buttonAlign什么意思
转自:https://zhidao.baidu.com/question/1174901985976576339.html指定Panel中按钮的位置.可配置的值有'right', 'left' 和 ' ...
- Newtonsoft.Json序列化日期时间去T的几种方式。
原文地址:MVC web api 返回JSON的几种方式,Newtonsoft.Json序列化日期时间去T的几种方式. http://www.cnblogs.com/wuball/p/4231343. ...