Example usage for javax.xml.stream XMLStreamReader nextTag

List of usage examples for javax.xml.stream XMLStreamReader nextTag

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamReader nextTag.

Prototype

public int nextTag() throws XMLStreamException;

Source Link

Document

Skips any white space (isWhiteSpace() returns true), COMMENT, or PROCESSING_INSTRUCTION, until a START_ELEMENT or END_ELEMENT is reached.

Usage

From source file:org.eclipse.smila.datamodel.record.stax.RecordReader.java

/**
 * read MObject from the XML stream. The stream must be currently at the first attribute or annotation tag of the
 * MObject./* w ww .  j  ava 2  s.  co m*/
 * 
 * @param staxReader
 *          source XML stream
 * @return Record read from stream or null, if stream is not currently at a Record start tag.
 * @throws XMLStreamException
 *           StAX error.
 */
private MObject readMetadata(final XMLStreamReader staxReader) throws XMLStreamException {
    final MObject mobject = _recordFactory.createMetadataObject();
    while (isStartTag(staxReader, RecordParser.TAG_ATTRIBUTE)) {
        readAttribute(staxReader, mobject);
        staxReader.nextTag();
    }
    readAnnotations(staxReader, mobject);
    return mobject;
}

From source file:org.eclipse.smila.datamodel.record.stax.RecordReader.java

/**
 * read an attribute from the XML stream and set it on the given {@link MObject}.
 * //w  ww . ja  v  a2  s.c o m
 * @param staxReader
 *          XML stream
 * @param mobject
 *          metadata object
 * @throws XMLStreamException
 *           StAX error.
 */
private void readAttribute(final XMLStreamReader staxReader, final MObject mobject) throws XMLStreamException {
    final Attribute attribute = _recordFactory.createAttribute();
    final String attributeName = staxReader.getAttributeValue(null, RecordParser.ATTRIBUTE_NAME);
    attribute.setName(attributeName);
    mobject.setAttribute(attributeName, attribute);
    staxReader.nextTag();
    readAnnotations(staxReader, attribute);
    while (isStartTag(staxReader, RecordParser.TAG_LITERAL)) {
        readLiteralElement(staxReader, attribute);
        staxReader.nextTag();
    }
    while (isStartTag(staxReader, RecordParser.TAG_OBJECT)) {
        staxReader.nextTag();
        attribute.addObject(readMetadata(staxReader));
        staxReader.nextTag();
    }
}

From source file:org.eclipse.smila.datamodel.record.stax.RecordReader.java

/**
 * read an L element and add {@link Literal}s to the given {@link Attribute}.
 * /*  www . j a  va2 s.  c o m*/
 * @param staxReader
 *          XML stream
 * @param attribute
 *          current attribute
 * @throws XMLStreamException
 *           StAX error
 */
private void readLiteralElement(final XMLStreamReader staxReader, final Attribute attribute)
        throws XMLStreamException {
    final String defaultSemanticType = staxReader.getAttributeValue(null, RecordParser.ATTRIBUTE_SEMANTICTYPE);
    staxReader.nextTag();
    Literal literal = null;
    while (isStartTag(staxReader, RecordParser.TAG_VALUE)) {
        literal = _recordFactory.createLiteral();
        String semanticType = staxReader.getAttributeValue(null, RecordParser.ATTRIBUTE_SEMANTICTYPE);
        if (semanticType == null) {
            semanticType = defaultSemanticType;
        }
        literal.setSemanticType(semanticType);
        final String dataType = staxReader.getAttributeValue(null, RecordParser.ATTRIBUTE_TYPE);
        final String stringValue = staxReader.getElementText();
        setLiteralValue(literal, dataType, stringValue);
        attribute.addLiteral(literal);
        staxReader.nextTag();
    }
    if (isStartTag(staxReader, RecordParser.TAG_ANNOTATION)) {
        if (literal == null) { // literal without value, this is actually allowed by the schema.
            literal = _recordFactory.createLiteral();
        }
        readAnnotations(staxReader, attribute);
    }
}

From source file:org.eclipse.smila.datamodel.record.stax.RecordReader.java

/**
 * read an annotations from the stream and add them to the given {@link Annotatable}.
 * //  w  ww . j a  v  a  2 s  .  c o  m
 * @param staxReader
 *          XML stream
 * @param annotatable
 *          object to annotate
 * @throws XMLStreamException
 *           StAX error
 */
private void readAnnotations(final XMLStreamReader staxReader, final Annotatable annotatable)
        throws XMLStreamException {
    while (isStartTag(staxReader, RecordParser.TAG_ANNOTATION)) {
        readAnnotation(staxReader, annotatable);
        staxReader.nextTag();
    }
}

From source file:org.eclipse.smila.datamodel.record.stax.RecordReader.java

/**
 * read an annotation from the stream and add it to the given {@link Annotatable}.
 * //  w w w .j av a 2s  .co  m
 * @param staxReader
 *          XML stream
 * @param annotatable
 *          object to annotate
 * @throws XMLStreamException
 *           StAX error
 */
private void readAnnotation(final XMLStreamReader staxReader, final Annotatable annotatable)
        throws XMLStreamException {
    final Annotation annotation = _recordFactory.createAnnotation();
    final String annotationName = staxReader.getAttributeValue(null, RecordParser.ATTRIBUTE_NAME);
    staxReader.nextTag();
    while (isStartTag(staxReader, RecordParser.TAG_VALUE)) {
        final String valueName = staxReader.getAttributeValue(null, RecordParser.ATTRIBUTE_NAME);
        final String value = staxReader.getElementText();
        if (valueName == null) {
            annotation.addAnonValue(value);
        } else {
            annotation.setNamedValue(valueName, value);
        }
        staxReader.nextTag();
    }
    while (isStartTag(staxReader, RecordParser.TAG_ANNOTATION)) {
        readAnnotation(staxReader, annotation);
        staxReader.nextTag();
    }
    annotatable.addAnnotation(annotationName, annotation);
}

From source file:org.eclipse.smila.datamodel.record.stax.RecordReader.java

/**
 * read attachment names from the XML stream.
 * /*  www .j a  v a 2 s.c o m*/
 * @param staxReader
 *          source XML stream param record Record to add the attachments to.
 * @param record
 *          record to add attachments too.
 * @throws XMLStreamException
 *           StAX error.
 */
private void readAttachments(final XMLStreamReader staxReader, final Record record) throws XMLStreamException {
    while (isStartTag(staxReader, RecordParser.TAG_ATTACHMENT)) {
        final String attachmentName = staxReader.getElementText();
        if (attachmentName != null && attachmentName.length() > 0) {
            record.setAttachment(attachmentName, null);
        }
        staxReader.nextTag();
    }
}

From source file:org.eclipse.smila.datamodel.tools.DatamodelSerializationUtils.java

/**
 * De-serialize a serialized record (byte[]) to a record. If the serialized Record contained Attachments, the
 * deserialized record also contains these attachments as byte[].
 * //from   w w  w  .  j  a v  a 2  s  .c  o m
 * @param byteArray
 *          the byte array
 * 
 * @return the record
 * 
 * @throws DatamodelDeserializationException
 *           the datamodel deserialization exception
 */
public static Record deserialize(final byte[] byteArray) throws DatamodelDeserializationException {
    final ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
    ObjectInputStream wrapperInputStream = null;
    ByteArrayInputStream recordInputStream = new ByteArrayInputStream(byteArray);
    try {
        // deserialize record wrapper
        RecordByteWrapper wrapper = null;
        try {
            wrapperInputStream = new ObjectInputStream(bis);
            wrapper = (RecordByteWrapper) wrapperInputStream.readObject();
            recordInputStream = wrapper.getRecordAsStream();
        } catch (final Exception e) {
            throw new DatamodelDeserializationException(e);
        }

        // parse record from stream
        final XMLStreamReader reader = getStaxReaderFactory().createXMLStreamReader(recordInputStream);
        reader.nextTag(); // to Id tag.
        final Record record = getRecordReader().readRecord(reader);

        // append attachments to record
        if (wrapper.getAttachments() != null && !wrapper.getAttachments().isEmpty()) {
            for (final String attachmentName : wrapper.getAttachments().keySet()) {
                record.setAttachment(attachmentName, wrapper.getAttachments().get(attachmentName));
            } // for
        } // if
        return record;
    } catch (final XMLStreamException e) {
        throw new DatamodelDeserializationException(e);
    } finally {
        IOUtils.closeQuietly(bis);
        IOUtils.closeQuietly(wrapperInputStream);
        IOUtils.closeQuietly(recordInputStream);
    }
}

From source file:org.eclipse.smila.datamodel.tools.DatamodelSerializationUtils.java

/**
 * De-serialize a serialized Id (byte[]) to a record.
 * /*from  w ww  . j  a va 2  s .co m*/
 * @param byteArray
 *          the byte array
 * 
 * @return the Id
 * 
 * @throws DatamodelDeserializationException
 *           the datamodel deserialization exception
 */
public static Id deserializeId(final byte[] byteArray) throws DatamodelDeserializationException {
    final ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
    final ByteArrayInputStream idInputStream = new ByteArrayInputStream(byteArray);
    try {
        // parse record from stream
        final XMLStreamReader reader = getStaxReaderFactory().createXMLStreamReader(idInputStream);
        reader.nextTag(); // to Id tag.
        final Id id = getIdReader().readId(reader);
        return id;
    } catch (final XMLStreamException e) {
        throw new DatamodelDeserializationException(e);
    } finally {
        IOUtils.closeQuietly(bis);
        IOUtils.closeQuietly(idInputStream);
    }
}

From source file:org.eclipse.smila.datamodel.xml.XmlSerializationUtils.java

/**
 * De-serialize a serialized record (byte[]) to a record. If the serialized Record contained Attachments, the
 * deserialized record also contains these attachments as byte[].
 * /*from w w  w .  j  a v a  2  s . com*/
 * @param byteArray
 *          the byte array
 * 
 * @return the record
 * 
 * @throws DeserializationException
 *           the datamodel deserialization exception
 */
public static Record deserialize(final byte[] byteArray) throws DeserializationException {
    final ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
    ObjectInputStream wrapperInputStream = null;
    ByteArrayInputStream recordInputStream = new ByteArrayInputStream(byteArray);
    try {
        // deserialize record wrapper
        RecordByteWrapper wrapper = null;
        try {
            wrapperInputStream = new ObjectInputStream(bis);
            wrapper = (RecordByteWrapper) wrapperInputStream.readObject();
            recordInputStream = wrapper.getRecordAsStream();
        } catch (final Exception e) {
            throw new DeserializationException(e);
        }

        // parse record from stream
        final XMLStreamReader reader = getStaxReaderFactory().createXMLStreamReader(recordInputStream);
        reader.nextTag(); // to Id tag.
        final Record record = getRecordReader().readRecord(reader);

        // append attachments to record
        if (wrapper.getAttachments() != null && !wrapper.getAttachments().isEmpty()) {
            for (final String attachmentName : wrapper.getAttachments().keySet()) {
                record.setAttachment(attachmentName, wrapper.getAttachments().get(attachmentName));
            } // for
        } // if
        return record;
    } catch (final XMLStreamException e) {
        throw new DeserializationException(e);
    } finally {
        IOUtils.closeQuietly(bis);
        IOUtils.closeQuietly(wrapperInputStream);
        IOUtils.closeQuietly(recordInputStream);
    }
}

From source file:org.flockdata.integration.FileProcessor.java

private int processXMLFile(String file, ExtractProfile extractProfile) throws IOException, FlockException,
        IllegalAccessException, InstantiationException, ClassNotFoundException {
    try {/*ww  w . ja  v a 2  s  .  com*/
        int rows = 0;
        StopWatch watch = new StopWatch();
        StreamSource source = new StreamSource(file);
        XMLInputFactory xif = XMLInputFactory.newFactory();
        XMLStreamReader xsr = xif.createXMLStreamReader(source);

        XmlMappable mappable = (XmlMappable) Class.forName(extractProfile.getHandler()).newInstance();
        mappable.positionReader(xsr);

        String dataType = mappable.getDataType();
        watch.start();
        try {
            long then = new DateTime().getMillis();
            while (xsr.getLocalName().equals(dataType)) {
                EntityInputBean entityInputBean = Transformer.toEntity(mappable, xsr,
                        extractProfile.getContentModel());
                rows++;
                xsr.nextTag();
                getPayloadWriter().writeEntity(entityInputBean);

                if (stopProcessing(rows, then)) {
                    break;
                }

            }
        } finally {
            getPayloadWriter().flush();
        }
        return endProcess(watch, rows, 0);

    } catch (XMLStreamException | JAXBException e1) {
        throw new IOException(e1);
    }
}