Search in sources :

Example 1 with StreamingRow

use of com.monitorjbl.xlsx.impl.StreamingRow in project data-prep by Talend.

the class StreamingSheetReader method handleEvent.

/**
 * Handles a Stream event.
 *
 * @param event
 * @throws SAXException
 */
private void handleEvent(XMLEvent event) throws SAXException {
    if (event.getEventType() == XMLStreamConstants.CHARACTERS) {
        Characters c = event.asCharacters();
        lastContents += c.getData();
    } else if (event.getEventType() == XMLStreamConstants.START_ELEMENT) {
        StartElement startElement = event.asStartElement();
        String tagLocalName = startElement.getName().getLocalPart();
        if ("row".equals(tagLocalName)) {
            Attribute rowIndex = startElement.getAttributeByName(new QName("r"));
            if (firstRowIndex == -1) {
                firstRowIndex = Integer.parseInt(rowIndex.getValue());
            }
            currentRow = new StreamingRow(Integer.parseInt(rowIndex.getValue()) - 1);
        } else if ("cols".equals(tagLocalName)) {
            parsingCols = true;
        } else if ("col".equals(tagLocalName) && parsingCols) {
            colNumber = colNumber + 1;
        } else if ("c".equals(tagLocalName)) {
            Attribute ref = startElement.getAttributeByName(new QName("r"));
            String[] coord = ref.getValue().split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
            currentCell = new StreamingCell(CellReference.convertColStringToIndex(coord[0]), Integer.parseInt(coord[1]) - 1);
            setFormatString(startElement, currentCell);
            Attribute type = startElement.getAttributeByName(new QName("t"));
            if (type != null) {
                currentCell.setType(type.getValue());
            } else {
                currentCell.setType("n");
            }
            Attribute style = startElement.getAttributeByName(new QName("s"));
            if (style != null) {
                String indexStr = style.getValue();
                try {
                    int index = Integer.parseInt(indexStr);
                    currentCell.setCellStyle(stylesTable.getStyleAt(index));
                } catch (NumberFormatException nfe) {
                    LOGGER.warn("Ignoring invalid style index {}", indexStr);
                }
            }
        // we store the dimension as well to revert with this method when cols not found
        // can happen see xlsx attached here https://jira.talendforge.org/browse/TDP-1957
        // <dimension ref="A1:B60"/>
        } else if ("dimension".equals(tagLocalName)) {
            Attribute attribute = startElement.getAttributeByName(new QName("ref"));
            if (attribute != null) {
                this.dimension = attribute.getValue();
            }
        }
        // Clear contents cache
        lastContents = "";
    } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT) {
        EndElement endElement = event.asEndElement();
        String tagLocalName = endElement.getName().getLocalPart();
        if ("v".equals(tagLocalName) || "t".equals(tagLocalName)) {
            currentCell.setRawContents(unformattedContents());
            currentCell.setContents(formattedContents());
        } else if ("row".equals(tagLocalName) && currentRow != null) {
            rowCache.add(currentRow);
        } else if ("c".equals(tagLocalName)) {
            currentRow.getCellMap().put(currentCell.getColumnIndex(), currentCell);
        } else if ("cols".equals(tagLocalName)) {
            parsingCols = false;
        }
    }
}
Also used : StartElement(javax.xml.stream.events.StartElement) Attribute(javax.xml.stream.events.Attribute) EndElement(javax.xml.stream.events.EndElement) QName(javax.xml.namespace.QName) Characters(javax.xml.stream.events.Characters) StreamingRow(com.monitorjbl.xlsx.impl.StreamingRow) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) StreamingCell(com.monitorjbl.xlsx.impl.StreamingCell)

Aggregations

StreamingCell (com.monitorjbl.xlsx.impl.StreamingCell)1 StreamingRow (com.monitorjbl.xlsx.impl.StreamingRow)1 QName (javax.xml.namespace.QName)1 Attribute (javax.xml.stream.events.Attribute)1 Characters (javax.xml.stream.events.Characters)1 EndElement (javax.xml.stream.events.EndElement)1 StartElement (javax.xml.stream.events.StartElement)1 XSSFRichTextString (org.apache.poi.xssf.usermodel.XSSFRichTextString)1