List of usage examples for org.apache.wicket.markup.parser XmlTag getAttribute
public CharSequence getAttribute(final String key)
From source file:org.wicketstuff.poi.excel.GeneralPurposeExporter.java
License:Apache License
public void exportCell(XmlTag tag, XmlPullParser parser, Cell cell, Component gridComponent) throws ParseException { XmlTag firstMostNestedTag = tag; // find the most inner tag value while ((tag = parser.nextTag()).getName().equals("td") == false) { if (tag.isOpen() || tag.isOpenClose()) { firstMostNestedTag = tag;//from www .j a v a 2s. c om } else { break; } } CharSequence possibleComponentReference = firstMostNestedTag .getAttribute(TableParser.OutputPathBehavior.PATH_ATTRIBUTE); if (possibleComponentReference != null) { Component firstMostNestedComponent = gridComponent.getPage().get(possibleComponentReference.toString()); // exclude auto links if (firstMostNestedComponent.getClass().getName().contains("ResourceReferenceAutolink")) { cell.setCellValue(""); return; } // handle links if (firstMostNestedComponent instanceof Link) { Link<?> link = (Link<?>) firstMostNestedComponent; firstMostNestedComponent = getLinkInnerComponent(link); if (firstMostNestedComponent == null) { cell.setCellValue(""); return; } } Object modelValue = firstMostNestedComponent.getDefaultModelObject(); if (modelValue != null) { if (modelValue instanceof Number) { handleNumber(cell, (Number) modelValue); } else if (modelValue instanceof CharSequence) { cell.setCellValue(modelValue.toString()); } else if (modelValue instanceof CharSequence) { cell.setCellValue(modelValue.toString()); } else if (modelValue instanceof Boolean) { cell.setCellValue((Boolean) modelValue); } else if (modelValue instanceof Calendar) { handleCalendar(cell, (Calendar) modelValue); } else if (modelValue instanceof Date) { handleDate(cell, (Date) modelValue); } else { cell.setCellValue(modelValue.toString()); } } } else { // simply set the first most nested tag value String value = parser .getInput(firstMostNestedTag.getPos() + firstMostNestedTag.getLength(), tag.getPos()) .toString(); cell.setCellValue(value); } }
From source file:org.wicketstuff.poi.excel.TableParser.java
License:Apache License
private void doParse(CharSequence gridComponentMarkup, Component tableComponent) throws IOException, ResourceStreamNotFoundException, ParseException { XmlPullParser parser = new XmlPullParser(); parser.parse(gridComponentMarkup);//from w w w. ja v a 2 s. c o m XmlTag tag; int tableDeep = 0; while ((tag = parser.nextTag()) != null) { if ("table".equals(tag.getName().toLowerCase())) { if (tag.isOpen()) { tableDeep++; } else { tableDeep--; } } if (tableDeep > 1) { // we don't want to read inner tables continue; } if (tag.isOpen()) { String tagName = tag.getName().toLowerCase(); if ("tr".equals(tagName)) { if (tableDeep == 0) { // means that root table is outside the component markup tableDeep = 1; } int index = row == null ? 0 : row.getRowNum() + 1; row = targetSheet.createRow(index); cell = null; } else if ("td".equals(tagName) || "th".equals(tagName)) { int index = cell == null ? 0 : cell.getColumnIndex() + 1 + colsToSpan; if (skipColumn(index)) { index += columnSpan.get(index); } colsToSpan = 0; CharSequence rowspan = tag.getAttribute("rowspan"); CharSequence colspan = tag.getAttribute("colspan"); cell = row.createCell(index); if (rowspan != null || colspan != null) { int rowsToSpan = rowspan == null ? 0 : Integer.valueOf(rowspan.toString()) - 1; colsToSpan = colspan == null ? 0 : Integer.valueOf(colspan.toString()) - 1; if (rowsToSpan > 0) { rowsToSpanByColumn.put(index, rowsToSpan); columnSpan.put(index, colsToSpan + 1); } int lastRowNum = row.getRowNum() + rowsToSpan; int lastColIndex = index + colsToSpan; targetSheet.addMergedRegion(new CellRangeAddress(// row.getRowNum(), // first row (0-based) lastRowNum, // last row (0-based) index, // first column (0-based) lastColIndex // last column (0-based) )); } cellExporter.exportCell(tag, parser, cell, tableComponent); } } } }