Skip to content

Commit 60ea67b

Browse files
author
liaochong
committed
optimize
1 parent 40d81db commit 60ea67b

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

src/main/java/com/github/liaochong/myexcel/core/SaxExcelReader.java

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.github.liaochong.myexcel.utils.ReflectUtil;
2525
import com.github.liaochong.myexcel.utils.StringUtil;
2626
import com.github.liaochong.myexcel.utils.TempFileOperator;
27+
import com.github.liaochong.myexcel.utils.ValidatorUtil;
2728
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
2829
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
2930
import org.apache.poi.openxml4j.opc.OPCPackage;
@@ -32,17 +33,13 @@
3233
import org.apache.poi.poifs.filesystem.FileMagic;
3334
import org.apache.poi.util.XMLHelper;
3435
import org.apache.poi.xssf.eventusermodel.XSSFReader;
35-
import org.hibernate.validator.HibernateValidator;
3636
import org.slf4j.Logger;
3737
import org.xml.sax.ContentHandler;
3838
import org.xml.sax.InputSource;
3939
import org.xml.sax.SAXException;
4040
import org.xml.sax.XMLReader;
4141

4242
import javax.validation.ConstraintViolation;
43-
import javax.validation.Validation;
44-
import javax.validation.Validator;
45-
import javax.validation.ValidatorFactory;
4643
import javax.xml.parsers.ParserConfigurationException;
4744
import java.io.File;
4845
import java.io.FileInputStream;
@@ -100,7 +97,6 @@ public SaxExcelReader<T> sheet(String sheetName) {
10097
return sheets(sheetName);
10198
}
10299

103-
public Validator validator;
104100

105101
public SaxExcelReader<T> sheets(Integer... sheetIndexs) {
106102
this.readConfig.sheetIndexs.clear();
@@ -213,7 +209,7 @@ public ValidationListObject<T> validRead(File file) {
213209

214210
private void doValidRead(T t, RowContext rowContext, ValidationListObject<T> validationListObject) {
215211
ValidationObject<T> validationObject = new ValidationObject<>();
216-
Set<ConstraintViolation<T>> violations = getValidator().validate(t, t.getClass());
212+
Set<ConstraintViolation<T>> violations = ValidatorUtil.getValidator().validate(t, t.getClass());
217213
validationObject.setRowNum(rowContext.getRowNum());
218214
validationObject.setConstraintViolations(violations);
219215
validationListObject.getValidationObjects().add(validationObject);
@@ -297,18 +293,6 @@ public static WorkbookMetaData getWorkbookMetaData(File file) {
297293
return saxExcelReader.workbookMetaData;
298294
}
299295

300-
private synchronized Validator getValidator() {
301-
if (validator == null) {
302-
try (ValidatorFactory validatorFactory = Validation
303-
.byProvider(HibernateValidator.class)
304-
.configure()
305-
.buildValidatorFactory()) {
306-
validator = validatorFactory.getValidator();
307-
}
308-
}
309-
return validator;
310-
}
311-
312296
private void doRead(InputStream fileInputStream) {
313297
this.doRead(fileInputStream, false);
314298
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2019 liaochong
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
package com.github.liaochong.myexcel.utils;
16+
17+
import org.hibernate.validator.HibernateValidator;
18+
19+
import javax.validation.Validation;
20+
import javax.validation.Validator;
21+
import javax.validation.ValidatorFactory;
22+
23+
/**
24+
* @author liaochong
25+
* @version 1.0
26+
*/
27+
public final class ValidatorUtil {
28+
29+
private static Validator validator;
30+
31+
public static synchronized Validator getValidator() {
32+
if (validator == null) {
33+
try (ValidatorFactory validatorFactory = Validation
34+
.byProvider(HibernateValidator.class)
35+
.configure()
36+
.buildValidatorFactory()) {
37+
validator = validatorFactory.getValidator();
38+
}
39+
}
40+
return validator;
41+
}
42+
}

0 commit comments

Comments
 (0)