Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
liaochong committed May 25, 2024
1 parent 40d81db commit 60ea67b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.github.liaochong.myexcel.utils.ReflectUtil;
import com.github.liaochong.myexcel.utils.StringUtil;
import com.github.liaochong.myexcel.utils.TempFileOperator;
import com.github.liaochong.myexcel.utils.ValidatorUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
Expand All @@ -32,17 +33,13 @@
import org.apache.poi.poifs.filesystem.FileMagic;
import org.apache.poi.util.XMLHelper;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.hibernate.validator.HibernateValidator;
import org.slf4j.Logger;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -100,7 +97,6 @@ public SaxExcelReader<T> sheet(String sheetName) {
return sheets(sheetName);
}

public Validator validator;

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

private void doValidRead(T t, RowContext rowContext, ValidationListObject<T> validationListObject) {
ValidationObject<T> validationObject = new ValidationObject<>();
Set<ConstraintViolation<T>> violations = getValidator().validate(t, t.getClass());
Set<ConstraintViolation<T>> violations = ValidatorUtil.getValidator().validate(t, t.getClass());
validationObject.setRowNum(rowContext.getRowNum());
validationObject.setConstraintViolations(violations);
validationListObject.getValidationObjects().add(validationObject);
Expand Down Expand Up @@ -297,18 +293,6 @@ public static WorkbookMetaData getWorkbookMetaData(File file) {
return saxExcelReader.workbookMetaData;
}

private synchronized Validator getValidator() {
if (validator == null) {
try (ValidatorFactory validatorFactory = Validation
.byProvider(HibernateValidator.class)
.configure()
.buildValidatorFactory()) {
validator = validatorFactory.getValidator();
}
}
return validator;
}

private void doRead(InputStream fileInputStream) {
this.doRead(fileInputStream, false);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2019 liaochong
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.liaochong.myexcel.utils;

import org.hibernate.validator.HibernateValidator;

import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

/**
* @author liaochong
* @version 1.0
*/
public final class ValidatorUtil {

private static Validator validator;

public static synchronized Validator getValidator() {
if (validator == null) {
try (ValidatorFactory validatorFactory = Validation
.byProvider(HibernateValidator.class)
.configure()
.buildValidatorFactory()) {
validator = validatorFactory.getValidator();
}
}
return validator;
}
}

0 comments on commit 60ea67b

Please sign in to comment.