Java tutorial
/** * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) * <p> * 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 * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * 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 cn.afterturn.easypoi.excel.imports.sax.parse; import cn.afterturn.easypoi.excel.annotation.ExcelTarget; import cn.afterturn.easypoi.excel.entity.ImportParams; import cn.afterturn.easypoi.excel.entity.params.ExcelCollectionParams; import cn.afterturn.easypoi.excel.entity.params.ExcelImportEntity; import cn.afterturn.easypoi.excel.entity.sax.SaxReadCellEntity; import cn.afterturn.easypoi.excel.imports.CellValueService; import cn.afterturn.easypoi.excel.imports.base.ImportBaseService; import cn.afterturn.easypoi.exception.excel.ExcelImportException; import cn.afterturn.easypoi.handler.inter.IReadHandler; import cn.afterturn.easypoi.util.PoiPublicUtil; import cn.afterturn.easypoi.util.PoiReflectorUtil; import com.google.common.collect.Lists; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.lang.reflect.Field; import java.util.*; /** * ?? * * @author JueYue * 201511 ?7:59:39 */ @SuppressWarnings({ "rawtypes", "unchecked" }) public class SaxRowRead extends ImportBaseService implements ISaxRowRead { private static final Logger LOGGER = LoggerFactory.getLogger(SaxRowRead.class); /** * ?? **/ private List list; /** * **/ private Class<?> pojoClass; /** * ? **/ private ImportParams params; /** * **/ private Map<Integer, String> titlemap = new HashMap<Integer, String>(); /** * ? **/ private Object object = null; private Map<String, ExcelImportEntity> excelParams = new HashMap<String, ExcelImportEntity>(); private List<ExcelCollectionParams> excelCollection = new ArrayList<ExcelCollectionParams>(); private String targetId; private CellValueService cellValueServer; private IReadHandler handler; public SaxRowRead(Class<?> pojoClass, ImportParams params, IReadHandler handler) { list = Lists.newArrayList(); this.params = params; this.pojoClass = pojoClass; cellValueServer = new CellValueService(); this.handler = handler; initParams(pojoClass, params); } private void initParams(Class<?> pojoClass, ImportParams params) { try { Field[] fileds = PoiPublicUtil.getClassFields(pojoClass); ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class); if (etarget != null) { targetId = etarget.value(); } getAllExcelField(targetId, fileds, excelParams, excelCollection, pojoClass, null, null); } catch (Exception e) { LOGGER.error(e.getMessage(), e); throw new ExcelImportException(e.getMessage()); } } @Override public <T> List<T> getList() { return list; } @Override public void parse(int index, List<SaxReadCellEntity> datas) { try { if (datas == null || datas.size() == 0) { return; } // if (index < params.getTitleRows()) { return; } // if (index < params.getTitleRows() + params.getHeadRows()) { addHeadData(datas); } else { addListData(datas); } } catch (Exception e) { LOGGER.error(e.getMessage(), e); throw new ExcelImportException(e.getMessage()); } } /** * ?? * * @param datas */ private void addListData(List<SaxReadCellEntity> datas) throws Exception { // ???,?,? if (params.getKeyIndex() != null && (datas.get(params.getKeyIndex()) == null || StringUtils.isEmpty(String.valueOf(datas.get(params.getKeyIndex()).getValue()))) && object != null) { for (ExcelCollectionParams param : excelCollection) { addListContinue(object, param, datas, titlemap, targetId, params); } } else { if (Map.class.equals(pojoClass)) { object = new HashMap<>(); } else { object = PoiPublicUtil.createObject(pojoClass, targetId); } SaxReadCellEntity entity; for (int i = 0, le = datas.size(); i < le; i++) { entity = datas.get(i); String titleString = (String) titlemap.get(i); if (excelParams.containsKey(titleString) || Map.class.equals(pojoClass)) { saveFieldValue(params, object, entity, excelParams, titleString); } } if (object != null && handler != null) { handler.handler(object); } for (ExcelCollectionParams param : excelCollection) { addListContinue(object, param, datas, titlemap, targetId, params); } if (handler == null) { list.add(object); } } } /** * ?List? * * @param object * @param param * @param datas * @param titlemap * @param targetId * @param params * @throws Exception */ private void addListContinue(Object object, ExcelCollectionParams param, List<SaxReadCellEntity> datas, Map<Integer, String> titlemap, String targetId, ImportParams params) throws Exception { Collection collection = (Collection) PoiReflectorUtil.fromCache(pojoClass).getValue(object, param.getName()); Object entity = PoiPublicUtil.createObject(param.getType(), targetId); boolean isUsed = false; for (int i = 0; i < datas.size(); i++) { String titleString = (String) titlemap.get(i); if (param.getExcelParams().containsKey(titleString)) { saveFieldValue(params, entity, datas.get(i), param.getExcelParams(), titleString); isUsed = true; } } if (isUsed) { collection.add(entity); } } /** * * * @param params * @param object * @param entity * @param excelParams * @param titleString * @throws Exception */ private void saveFieldValue(ImportParams params, Object object, SaxReadCellEntity entity, Map<String, ExcelImportEntity> excelParams, String titleString) throws Exception { if (Map.class.equals(pojoClass)) { ((Map) object).put(titleString, entity.getValue()); } else { Object value = cellValueServer.getValue(params.getDataHandler(), object, entity, excelParams, titleString); setValues(excelParams.get(titleString), object, value); } } /** * put ? * * @param datas */ private void addHeadData(List<SaxReadCellEntity> datas) { for (int i = 0; i < datas.size(); i++) { if (StringUtils.isNotEmpty(String.valueOf(datas.get(i).getValue()))) { titlemap.put(i, String.valueOf(datas.get(i).getValue())); } } } }