Example usage for javax.xml.stream.events XMLEvent asEndElement

List of usage examples for javax.xml.stream.events XMLEvent asEndElement

Introduction

In this page you can find the example usage for javax.xml.stream.events XMLEvent asEndElement.

Prototype

public EndElement asEndElement();

Source Link

Document

Returns this event as an end element event, may result in a class cast exception if this event is not a end element.

Usage

From source file:org.pentaho.di.trans.steps.xmlinputstream.XMLInputStream.java

private Object[] processEvent() throws KettleException {

    Object[] outputRowData = RowDataUtil.allocateRowData(data.outputRowMeta.size());
    XMLEvent e = null;
    try {/*from  w w w  . j  a v a  2  s  .  co  m*/
        e = data.xmlEventReader.nextEvent();
    } catch (XMLStreamException ex) {
        throw new KettleException(ex);
    }

    int eventType = e.getEventType();
    if (data.pos_xml_data_type_numeric != -1) {
        outputRowData[data.pos_xml_data_type_numeric] = new Long(eventType);
    }
    if (data.pos_xml_data_type_description != -1) {
        if (eventType == 0 || eventType > eventDescription.length) {
            // unknown eventType
            outputRowData[data.pos_xml_data_type_description] = eventDescription[0] + "(" + eventType + ")";
        } else {
            outputRowData[data.pos_xml_data_type_description] = eventDescription[eventType];
        }
    }
    if (data.pos_xml_location_line != -1) {
        outputRowData[data.pos_xml_location_line] = new Long(e.getLocation().getLineNumber());
    }
    if (data.pos_xml_location_column != -1) {
        outputRowData[data.pos_xml_location_column] = new Long(e.getLocation().getColumnNumber());
    }

    switch (eventType) {

    case XMLStreamConstants.START_ELEMENT:
        data.elementLevel++;
        if (data.elementLevel > PARENT_ID_ALLOCATE_SIZE - 1) {
            throw new KettleException(BaseMessages.getString(PKG, "XMLInputStream.Log.TooManyNestedElements",
                    PARENT_ID_ALLOCATE_SIZE));
        }
        if (data.elementParentID[data.elementLevel] == null) {
            data.elementParentID[data.elementLevel] = data.elementID;
        }
        data.elementID++;
        data.elementLevelID[data.elementLevel] = data.elementID;

        String xml_data_name;
        if (meta.isEnableNamespaces()) {
            String prefix = e.asStartElement().getName().getPrefix();
            if (Utils.isEmpty(prefix)) {
                xml_data_name = e.asStartElement().getName().getLocalPart();
            } else { // add namespace prefix:
                xml_data_name = prefix + ":" + e.asStartElement().getName().getLocalPart();
            }
        } else {
            xml_data_name = e.asStartElement().getName().getLocalPart();
        }
        if (data.pos_xml_data_name >= 0) {
            outputRowData[data.pos_xml_data_name] = xml_data_name;
        }
        // store the name
        data.elementName[data.elementLevel] = xml_data_name;
        // store simple path
        data.elementPath[data.elementLevel] = data.elementPath[data.elementLevel - 1] + "/" + xml_data_name;

        // write Namespaces out
        if (meta.isEnableNamespaces()) {
            outputRowData = parseNamespaces(outputRowData, e);
        }

        // write Attributes out
        outputRowData = parseAttributes(outputRowData, e);

        break;

    case XMLStreamConstants.END_ELEMENT:
        parseEndElement(outputRowData, e.asEndElement());
        putRowOut(outputRowData);
        data.elementParentID[data.elementLevel + 1] = null;
        data.elementLevel--;
        outputRowData = null; // continue
        break;

    case XMLStreamConstants.SPACE:
        outputRowData = null; // ignore & continue
        break;

    case XMLStreamConstants.CHARACTERS:
    case XMLStreamConstants.CDATA:
        if (data.pos_xml_data_name >= 0) {
            outputRowData[data.pos_xml_data_name] = data.elementName[data.elementLevel];
        }
        String xml_data_value = e.asCharacters().getData();
        if (data.pos_xml_data_value >= 0) {
            if (meta.isEnableTrim()) {
                // optional trim is also eliminating white spaces, tab, cr, lf
                xml_data_value = Const.trim(xml_data_value);
            }
            outputRowData[data.pos_xml_data_value] = xml_data_value;
        }

        if (data.pos_xml_data_value < 0 || Utils.isEmpty((String) outputRowData[data.pos_xml_data_value])) {
            outputRowData = null; // ignore & continue
        }
        break;

    case XMLStreamConstants.PROCESSING_INSTRUCTION:
        outputRowData = null; // ignore & continue
        // TODO test if possible
        break;

    case XMLStreamConstants.COMMENT:
        outputRowData = null; // ignore & continue
        // TODO test if possible
        break;

    case XMLStreamConstants.ENTITY_REFERENCE:
        // should be resolved by default
        outputRowData = null; // ignore & continue
        break;

    case XMLStreamConstants.START_DOCUMENT:
        // just get this information out
        break;

    case XMLStreamConstants.END_DOCUMENT:
        // just get this information out
        break;

    default:
        logBasic("Event:" + eventType);
        outputRowData = null; // ignore & continue
    }

    return outputRowData;
}

From source file:org.qi4j.valueserialization.stax.StaxValueDeserializer.java

@Override
protected <T> Collection<T> readArrayInCollection(XMLEventReader input,
        Function<XMLEventReader, T> deserializer, Collection<T> collection) throws Exception {
    if (!input.hasNext()) {
        return null;
    }/*w w  w.j ava  2s  . co m*/
    XMLEvent nextTag = input.nextTag();
    if (nextTag.isStartElement() && "null".equals(nextTag.asStartElement().getName().getLocalPart())) {
        input.nextTag();
        return null;
    }
    if (!nextTag.isStartElement() || !"array".equals(nextTag.asStartElement().getName().getLocalPart())) {
        throw new ValueSerializationException("Expected an <array/> but got: " + nextTag);
    }
    WHILE: while (input.hasNext()) {
        XMLEvent currentTag = input.nextTag();
        if (currentTag.isEndElement()) {
            String endElementName = currentTag.asEndElement().getName().getLocalPart();
            switch (endElementName) {
            case "array":
                break WHILE;
            case "value":
                continue;
            }
        }
        if (!"value".equals(currentTag.asStartElement().getName().getLocalPart())) {
            throw new ValueSerializationException("Expected a <value/> but got: " + currentTag);
        }
        T item = deserializer.map(input);
        collection.add(item);
    }
    return collection;
}

From source file:org.qi4j.valueserialization.stax.StaxValueDeserializer.java

@Override
protected <K, V> Map<K, V> readMapInMap(XMLEventReader input, Function<XMLEventReader, K> keyDeserializer,
        Function<XMLEventReader, V> valueDeserializer, Map<K, V> map) throws Exception {
    if (!input.hasNext()) {
        return null;
    }/*ww  w  . j  a  va  2 s .  c om*/
    XMLEvent nextTag = input.nextTag();
    if (nextTag.isStartElement() && "null".equals(nextTag.asStartElement().getName().getLocalPart())) {
        input.nextTag();
        return null;
    }
    if (!nextTag.isStartElement() || !"array".equals(nextTag.asStartElement().getName().getLocalPart())) {
        throw new ValueSerializationException("Expected an <array/> but got: " + nextTag);
    }
    XMLEvent currentTag = input.nextTag(); // <object>
    while (!currentTag.isEndElement() || !"array".equals(currentTag.asEndElement().getName().getLocalPart())) {
        if (!currentTag.isStartElement()
                || !"object".equals(currentTag.asStartElement().getName().getLocalPart())) {
            throw new ValueSerializationException("Expected an <object/> but got: " + nextTag);
        }
        currentTag = input.nextTag(); // <field>
        K key = null;
        V value = null;
        while (!currentTag.isEndElement()
                || !"object".equals(currentTag.asEndElement().getName().getLocalPart())) {
            input.nextTag(); // <name>
            String keyOrValue = input.nextEvent().asCharacters().getData();
            input.nextTag(); // </name>
            input.nextTag(); // <value>
            switch (keyOrValue) {
            case "key":
                key = keyDeserializer.map(input);
                break;
            case "value":
                value = valueDeserializer.map(input);
                break;
            default:
                readObjectTree(input);
                break;
            }
            input.nextTag(); // </value>
            input.nextTag(); // </field>
            currentTag = input.nextTag();
        }
        if (key != null) {
            map.put(key, value);
        }
        currentTag = input.nextTag();
    }
    return map;
}

From source file:org.slc.sli.ingestion.parser.impl.EdfiRecordParserImpl.java

private static String extractTagName(XMLEvent e) {
    String result = "";
    if (e.isEndElement()) {
        result = e.asEndElement().getName().getLocalPart();
    } else if (e.isStartElement()) {
        result = e.asStartElement().getName().getLocalPart();
    }/*from   w  w w. j a v a2s  .  co m*/
    return result;
}

From source file:org.talend.dataprep.schema.xls.streaming.StreamingSheetReader.java

/**
 * Handles a Stream event./*from   www  .  j av  a2s  .c  o  m*/
 *
 * @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;
        }

    }
}

From source file:org.xmlsh.commands.internal.xml2json.java

private byte[] serializeAsXML(XMLEventReader reader) throws XMLStreamException {

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    XMLOutputFactory fact = new OutputFactory();

    XMLEventWriter writer = fact.createXMLEventWriter(bos, kENCODING_UTF_8);
    while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();

        if (event.isEndElement() && event.asEndElement().getName().equals(kELEM_STRING))
            break;
        writer.add(event);//from ww  w  .  jav  a  2  s  .  c  o  m
    }

    writer.flush();
    writer.close();
    return bos.toByteArray();

}

From source file:org.xmlsh.commands.internal.xml2json.java

private byte[] serializeAsString(XMLEventReader reader)
        throws XMLStreamException, UnsupportedEncodingException, IOException {

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();

        if (event.isEndElement() && event.asEndElement().getName().equals(kELEM_STRING))
            break;
        if (event.isCharacters())
            bos.write(event.asCharacters().getData().getBytes("UTF-8"));
    }//from w  w w  .  j  a  v a 2 s  . c  o m

    return bos.toByteArray();

}

From source file:org.zuinnote.hadoop.office.format.common.parser.msexcel.internal.XSSFPullParser.java

public Object[] getNext() throws XMLStreamException, FormatNotUnderstoodException {
    Object[] result = null;/*www .j a v  a2  s  . c o m*/
    if (!this.nextBeingCalled) { // skip to the next tag

        if (this.hasNext() == false) {

            return null;
        }
    }
    if (this.finalized) { // no more to read
        return null;
    }
    // check
    ArrayList<SpreadSheetCellDAO> cells = new ArrayList<>();
    if (this.currentRow == this.nextRow) { // only if we have a row to report
        // read through row, cf.
        // http://download.microsoft.com/download/3/E/3/3E3435BD-AA68-4B32-B84D-B633F0D0F90D/SpreadsheetMLBasics.ppt
        int currentCellCount = 0;
        while (this.xer.hasNext()) {
            XMLEvent xe = xer.nextEvent();
            // read+
            if (xe.isEndElement()) {
                if (xe.asEndElement().getName().getLocalPart().equalsIgnoreCase("row")) {
                    break; // end of row
                }
            } else if (xe.isStartElement()) {
                if (xe.asStartElement().getName().getLocalPart().equalsIgnoreCase("c")) {

                    Attribute cellAddressAT = xe.asStartElement().getAttributeByName(new QName("r"));
                    // check if cell is a subsequent cell and add null, if needed
                    CellAddress currentCellAddress = new CellAddress(cellAddressAT.getValue());
                    for (int i = currentCellCount; i < currentCellAddress.getColumn(); i++) {
                        cells.add(null);
                        currentCellCount++;
                    }
                    currentCellCount++; // current cell
                    Attribute cellTypeTAT = xe.asStartElement().getAttributeByName(new QName("t"));
                    Attribute cellTypeSAT = xe.asStartElement().getAttributeByName(new QName("s"));
                    String cellFormattedValue = "";
                    String cellFormula = "";
                    String cellAddress = cellAddressAT.getValue();

                    String cellComment = "";
                    String cellSheetName = this.sheetName;

                    while ((!xe.isEndElement()) || !((xe.isEndElement())
                            && (xe.asEndElement().getName().getLocalPart().equalsIgnoreCase("c")))) {
                        xe = xer.nextEvent();
                        if ((xe.isStartElement())
                                && (xe.asStartElement().getName().getLocalPart().equalsIgnoreCase("v"))) {
                            // if a cell data type is set (e.g. b boolean (covered?), d date in ISO8601 format, e
                            // error, inlineStr (covered), n number (covered), s shared string (covered), str formula string (covered)
                            // we return as string
                            if (cellTypeTAT != null) {
                                XMLEvent xeSubCharacters = xer.nextEvent();
                                if (!xeSubCharacters.isCharacters()) {
                                    LOG.error(
                                            "Error parsing excel file. Value attribute (v) of cell does not contains characters");
                                } else {
                                    cellFormattedValue = xeSubCharacters.asCharacters().getData();

                                    if (XSSFPullParser.CELLTYPE_STRING.equals(cellTypeTAT.getValue())) { // need to
                                        // read
                                        // from
                                        // Shared
                                        // String
                                        // Table
                                        int strIdx = Integer.valueOf(cellFormattedValue);
                                        if ((this.sst != null) && (this.sst.getCount() > strIdx)) {
                                            cellFormattedValue = this.sst.getItemAt(strIdx).getString();
                                        } else {
                                            cellFormattedValue = "";
                                        }
                                    } else if (XSSFPullParser.CELLTYPE_NUMBER.equals(cellTypeTAT.getValue())) {
                                        // need to read number and format it (e.g. if it is a date etc.) according
                                        // to style
                                        int strStyleIdx = Integer.valueOf(cellTypeSAT.getValue());
                                        XSSFCellStyle cellStyle = this.styles.getStyleAt(strStyleIdx);
                                        cellFormattedValue = this.dataFormatter.formatRawCellContents(
                                                Double.valueOf(cellFormattedValue), cellStyle.getDataFormat(),
                                                cellStyle.getDataFormatString(), this.isDate1904);
                                    }

                                }
                            } else {
                                LOG.error("Cannot read celltype");
                            }
                        } else if ((xe.isStartElement())
                                && (xe.asStartElement().getName().getLocalPart().equalsIgnoreCase("f"))) {
                            // read formula
                            XMLEvent xeSubCharacters = xer.nextEvent();

                            if (!xeSubCharacters.isCharacters()) {
                                LOG.error(
                                        "Error parsing excel file. Formula attribute (f) of cell does not contains characters");
                            } else {
                                cellFormula = xeSubCharacters.asCharacters().getData();
                            }
                        } else if ((xe.isStartElement())
                                && (xe.asStartElement().getName().getLocalPart().equalsIgnoreCase("is"))) {
                            // read inline string
                            cellFormattedValue = this.parseCellInlineStringText(xer);
                        }
                    }
                    cells.add(new SpreadSheetCellDAO(cellFormattedValue, cellComment, cellFormula, cellAddress,
                            cellSheetName));

                }
            }
            // else ignore (e.g. col)
        }
    }

    // convert to array
    result = new SpreadSheetCellDAO[cells.size()];
    result = cells.toArray(result);
    // read all cells in row and create SpreadSheetCellDAOs
    this.nextBeingCalled = false;
    this.currentRow++;
    return result;
}