📄 foreachtag.java
字号:
package net.sf.jxls.tag;import java.lang.reflect.InvocationTargetException;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;import java.util.Map;import net.sf.jxls.exception.ParsePropertyException;import net.sf.jxls.parser.Expression;import net.sf.jxls.transformation.ResultTransformation;import net.sf.jxls.transformer.Configuration;import net.sf.jxls.transformer.SheetTransformer;import net.sf.jxls.util.GroupData;import net.sf.jxls.util.ReportUtil;import net.sf.jxls.util.Util;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.poi.hssf.usermodel.HSSFRow;/** * jx:forEach tag implementation * * @author Leonid Vysochyn */public class ForEachTag extends BaseTag { protected final Log log = LogFactory.getLog(getClass()); public static final String TAG_NAME = "forEach"; Configuration configuration = new Configuration(); static final String GROUP_DATA_KEY = "group"; private String select; public ForEachTag() { name = TAG_NAME; } private String items; private String var; private String varStatus; private String itemsKey; private String collectionPropertyName; private String groupBy; private String groupOrder; private Collection itemsCollection; public String getSelect() { return select; } public void setSelect(String select) { this.select = select; } public String getGroupOrder() { return groupOrder; } public void setGroupOrder(String groupOrder) { this.groupOrder = groupOrder; } public String getItems() { return items; } public void setItems(String items) { this.items = items; } public String getVar() { return var; } public void setVar(String var) { this.var = var; } public String getGroupBy() { return groupBy; } public void setGroupBy(String groupBy) { this.groupBy = groupBy; } public String getVarStatus() { return varStatus; } public void setVarStatus(String varStatus) { this.varStatus = varStatus; } public void init(TagContext tagContext) { super.init(tagContext); configuration = tagContext.getSheet().getConfiguration(); parseItemsProperty(); parseSelectProperty(); if (tagContext.getBeans().containsKey(itemsKey)) { Object itemsObject = tagContext.getBeans().get(itemsKey); if (collectionPropertyName != null) { itemsCollection = (Collection) Util.getProperty(itemsObject, collectionPropertyName); } else { itemsCollection = (Collection) itemsObject; } } } private void parseSelectProperty() { if (select != null) { if (select.startsWith(configuration.getStartExpressionToken()) && select.endsWith(configuration.getEndExpressionToken())) { select = select.substring(2, select.length() - 1); } else { log.error("select attribute should start with " + configuration.getStartExpressionToken() + " and end with " + configuration.getEndExpressionToken()); } } } private void parseItemsProperty() { if (items != null) { if (items.startsWith(configuration.getStartExpressionToken()) && items.endsWith(configuration.getEndExpressionToken())) { items = items.substring(2, items.length() - 1); try { Expression expr = new Expression(items, tagContext.getBeans(), configuration); Object obj = expr.evaluate(); if (obj instanceof Collection) { itemsCollection = (Collection) obj; } else { throw new RuntimeException("items property in forEach tag must be a collection. " + items + " is not "); } } catch (Exception e) { throw new RuntimeException("Can't parse an expression " + items, e); } } else { log.error("items attribute should start from " + configuration.getStartExpressionToken() + " and end " + "with " + configuration.getEndExpressionToken()); } } else { log.error("Collection key is null"); } } public ResultTransformation process(SheetTransformer sheetTransformer) { if (log.isDebugEnabled()) { log.debug("forEach tag processing. Attributes: var = " + var + ", items=" + items); log.debug("Current tagContext: " + tagContext); log.debug("Items Collection: " + itemsCollection); } Block body = tagContext.getTagBody(); if (body.getNumberOfRows() == 1) { return processOneRowTag(sheetTransformer); } int shiftNumber = 0; if (itemsCollection != null && !itemsCollection.isEmpty()) { tagContext.getSheetTransformationController().removeBorders(body); shiftNumber += -2; // due to the borders ResultTransformation shift = new ResultTransformation(0); Map beans = tagContext.getBeans(); if (groupBy == null || groupBy.length() == 0) { Collection collectionToProcess = selectCollectionDataToProcess(beans); shiftNumber += tagContext.getSheetTransformationController().duplicateDown(body, collectionToProcess.size() - 1); shift = processCollectionItems(collectionToProcess, beans, body, sheetTransformer); } else { try { Collection groupedData = ReportUtil.groupCollectionData(itemsCollection, groupBy, groupOrder, select, configuration); shiftNumber += tagContext.getSheetTransformationController().duplicateDown(body, groupedData.size() - 1); Object savedGroupData = null; if (beans.containsKey(GROUP_DATA_KEY)) { savedGroupData = beans.get(GROUP_DATA_KEY); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -