Example usage for javax.xml.stream XMLStreamReader next

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

Introduction

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

Prototype

public int next() throws XMLStreamException;

Source Link

Document

Get next parsing event - a processor may return all contiguous character data in a single chunk, or it may split it into several chunks.

Usage

From source file:org.netbeans.jbatch.modeler.spec.core.Definitions.java

public static Definitions load(ModelerFile file, String definitionId) {
    File savedFile = file.getFile();
    Definitions definition_Load = null;//from   w w w .  j av  a2s.  co m
    boolean definitionExist = false;
    XMLStreamReader xsr = null;
    try {
        if (savedFile.length() != 0) {

            XMLInputFactory xif = XMLInputFactory.newFactory();
            StreamSource xml = new StreamSource(savedFile);
            xsr = xif.createXMLStreamReader(xml);
            xsr.nextTag();
            if (definitionId == null) {
                while (xsr.hasNext() && !definitionExist) {
                    if (xsr.getEventType() == XMLStreamConstants.START_ELEMENT
                            && xsr.getLocalName().equals("definitions")
                            && xsr.getAttributeValue(null, "id") == null) {
                        definitionExist = true;
                    } else {
                        xsr.next();
                    }
                }
            } else {
                while (xsr.hasNext() && !definitionExist) {
                    if (xsr.getEventType() == XMLStreamConstants.START_ELEMENT
                            && xsr.getLocalName().equals("definitions")
                            && definitionId.equals(xsr.getAttributeValue(null, "id"))) {
                        definitionExist = true;
                    } else {
                        xsr.next();
                    }
                }
            }

        }
        JAXBContext jobContext;
        Unmarshaller jobUnmarshaller;
        //            if (jobContext == null) {
        jobContext = JAXBContext.newInstance(new Class<?>[] { ShapeDesign.class, Definitions.class });
        //            }
        //            if (jobUnmarshaller == null) {
        jobUnmarshaller = jobContext.createUnmarshaller();
        jobUnmarshaller.setEventHandler(new ValidateJAXB());
        //            }
        if (definitionExist) {
            definition_Load = jobUnmarshaller.unmarshal(xsr, Definitions.class).getValue();//new StreamSource(savedFile)
        }
        if (xsr != null) {
            xsr.close();
        }

    } catch (XMLStreamException ex) {
        Exceptions.printStackTrace(ex);
    } catch (JAXBException ex) {
        //            io.getOut().println("Exception: " + ex.toString());
        ex.printStackTrace();
        System.out.println("Document XML Not Exist");
    }
    return definition_Load;
}

From source file:org.netbeans.jbatch.modeler.spec.core.Definitions.java

static void transformXMLStream(XMLStreamReader xmlStreamReader, XMLStreamWriter xmlStreamWriter) {
    try {/*from  www.  j ava 2  s.c  o  m*/
        //            TransformerFactory tf = TransformerFactory.newInstance();
        //            Transformer t = tf.newTransformer();
        //            StAXSource source = new StAXSource(xmlStreamReader);
        //            StAXResult result = new StAXResult(xmlStreamWriter);
        //            t.transform(source, result);
        System.out.println("Defnition Id : " + xmlStreamReader.getAttributeValue(null, "id"));
        boolean finish = false;
        while (xmlStreamReader.hasNext() && !finish) {
            switch (xmlStreamReader.getEventType()) {
            case XMLEvent.START_ELEMENT:
                String prefix = xmlStreamReader.getPrefix();
                String namespaceURI = xmlStreamReader.getNamespaceURI();
                if (namespaceURI != null) {
                    if (prefix != null) {
                        xmlStreamWriter.writeStartElement(xmlStreamReader.getPrefix(),
                                xmlStreamReader.getLocalName(), xmlStreamReader.getNamespaceURI());
                    } else {
                        xmlStreamWriter.writeStartElement(xmlStreamReader.getNamespaceURI(),
                                xmlStreamReader.getLocalName());
                    }
                } else {
                    xmlStreamWriter.writeStartElement(xmlStreamReader.getLocalName());
                }

                for (int i = 0; i < xmlStreamReader.getNamespaceCount(); i++) {
                    xmlStreamWriter.writeNamespace(xmlStreamReader.getNamespacePrefix(i),
                            xmlStreamReader.getNamespaceURI(i));
                }
                int count = xmlStreamReader.getAttributeCount();
                for (int i = 0; i < count; i++) {
                    //                            xmlStreamWriter.writeAttribute(xmlStreamReader.getAttributePrefix(i),
                    //                                    xmlStreamReader.getAttributeNamespace(i),
                    //                                    xmlStreamReader.getAttributeLocalName(i),
                    //                                    xmlStreamReader.getAttributeValue(i));

                    String attrNamespaceURI = xmlStreamReader.getAttributeNamespace(i),
                            attrPrefix = xmlStreamReader.getAttributePrefix(i);
                    if (attrNamespaceURI != null) {
                        if (attrPrefix != null) {
                            xmlStreamWriter.writeAttribute(attrPrefix, attrNamespaceURI,
                                    xmlStreamReader.getAttributeLocalName(i),
                                    xmlStreamReader.getAttributeValue(i));
                        } else {
                            xmlStreamWriter.writeAttribute(attrNamespaceURI,
                                    xmlStreamReader.getAttributeLocalName(i),
                                    xmlStreamReader.getAttributeValue(i));
                        }
                    } else {
                        xmlStreamWriter.writeAttribute(xmlStreamReader.getAttributeLocalName(i),
                                xmlStreamReader.getAttributeValue(i));
                    }

                }
                break;
            case XMLEvent.END_ELEMENT:
                xmlStreamWriter.writeEndElement();
                if (xmlStreamReader.getLocalName().equals("definitions")) {
                    finish = true;
                }
                break;
            case XMLEvent.SPACE:
            case XMLEvent.CHARACTERS:
                xmlStreamWriter.writeCharacters(xmlStreamReader.getTextCharacters(),
                        xmlStreamReader.getTextStart(), xmlStreamReader.getTextLength());
                break;
            case XMLEvent.PROCESSING_INSTRUCTION:
                xmlStreamWriter.writeProcessingInstruction(xmlStreamReader.getPITarget(),
                        xmlStreamReader.getPIData());
                break;
            case XMLEvent.CDATA:
                xmlStreamWriter.writeCData(xmlStreamReader.getText());
                break;

            case XMLEvent.COMMENT:
                xmlStreamWriter.writeComment(xmlStreamReader.getText());
                break;
            case XMLEvent.ENTITY_REFERENCE:
                xmlStreamWriter.writeEntityRef(xmlStreamReader.getLocalName());
                break;
            case XMLEvent.START_DOCUMENT:
                String encoding = xmlStreamReader.getCharacterEncodingScheme();
                String version = xmlStreamReader.getVersion();

                if (encoding != null && version != null) {
                    xmlStreamWriter.writeStartDocument(encoding, version);
                } else if (version != null) {
                    xmlStreamWriter.writeStartDocument(xmlStreamReader.getVersion());
                }
                break;
            case XMLEvent.END_DOCUMENT:
                xmlStreamWriter.writeEndDocument();
                break;
            case XMLEvent.DTD:
                xmlStreamWriter.writeDTD(xmlStreamReader.getText());
                break;

            }
            if (!finish) {
                xmlStreamReader.next();
            }
        }
    } catch (XMLStreamException ex) {
        Exceptions.printStackTrace(ex);
    }
}

From source file:org.netbeans.jbatch.modeler.spec.core.Definitions.java

static void skipXMLStream(XMLStreamReader xmlStreamReader) {
    try {//from   ww  w.  j a v a2s  .  c o m
        boolean finish = false;
        while (xmlStreamReader.hasNext() && !finish) {
            switch (xmlStreamReader.getEventType()) {
            case XMLEvent.END_ELEMENT:
                if (xmlStreamReader.getLocalName().equals("definitions")) {
                    finish = true;
                }
                break;
            }
            if (!finish) {
                xmlStreamReader.next();
            }
        }
    } catch (XMLStreamException ex) {
        Exceptions.printStackTrace(ex);
    }
}

From source file:org.netbeans.jbatch.modeler.specification.model.job.util.JobUtil.java

void transformXMLStream(XMLStreamReader xmlStreamReader, XMLStreamWriter xmlStreamWriter) {
    try {/*w ww .j av  a2  s  .c o  m*/
        //            TransformerFactory tf = TransformerFactory.newInstance();
        //            Transformer t = tf.newTransformer();
        //            StAXSource source = new StAXSource(xmlStreamReader);
        //            StAXResult result = new StAXResult(xmlStreamWriter);
        //            t.transform(source, result);

        boolean finish = false;
        while (xmlStreamReader.hasNext() && !finish) {
            switch (xmlStreamReader.getEventType()) {
            case XMLEvent.START_ELEMENT:
                String prefix = xmlStreamReader.getPrefix();
                String namespaceURI = xmlStreamReader.getNamespaceURI();
                if (namespaceURI != null) {
                    if (prefix != null) {
                        xmlStreamWriter.writeStartElement(xmlStreamReader.getPrefix(),
                                xmlStreamReader.getLocalName(), xmlStreamReader.getNamespaceURI());
                    } else {
                        xmlStreamWriter.writeStartElement(xmlStreamReader.getNamespaceURI(),
                                xmlStreamReader.getLocalName());
                    }
                } else {
                    xmlStreamWriter.writeStartElement(xmlStreamReader.getLocalName());
                }

                for (int i = 0; i < xmlStreamReader.getNamespaceCount(); i++) {
                    xmlStreamWriter.writeNamespace(xmlStreamReader.getNamespacePrefix(i),
                            xmlStreamReader.getNamespaceURI(i));
                }
                int count = xmlStreamReader.getAttributeCount();
                for (int i = 0; i < count; i++) {
                    //                            xmlStreamWriter.writeAttribute(xmlStreamReader.getAttributePrefix(i),
                    //                                    xmlStreamReader.getAttributeNamespace(i),
                    //                                    xmlStreamReader.getAttributeLocalName(i),
                    //                                    xmlStreamReader.getAttributeValue(i));

                    String attrNamespaceURI = xmlStreamReader.getAttributeNamespace(i),
                            attrPrefix = xmlStreamReader.getAttributePrefix(i);
                    if (attrNamespaceURI != null) {
                        if (attrPrefix != null) {
                            xmlStreamWriter.writeAttribute(attrPrefix, attrNamespaceURI,
                                    xmlStreamReader.getAttributeLocalName(i),
                                    xmlStreamReader.getAttributeValue(i));
                        } else {
                            xmlStreamWriter.writeAttribute(attrNamespaceURI,
                                    xmlStreamReader.getAttributeLocalName(i),
                                    xmlStreamReader.getAttributeValue(i));
                        }
                    } else {
                        xmlStreamWriter.writeAttribute(xmlStreamReader.getAttributeLocalName(i),
                                xmlStreamReader.getAttributeValue(i));
                    }

                }
                break;
            case XMLEvent.END_ELEMENT:
                xmlStreamWriter.writeEndElement();
                if (xmlStreamReader.getLocalName().equals("definitions")) {
                    finish = true;
                }
                break;
            case XMLEvent.SPACE:
            case XMLEvent.CHARACTERS:
                xmlStreamWriter.writeCharacters(xmlStreamReader.getTextCharacters(),
                        xmlStreamReader.getTextStart(), xmlStreamReader.getTextLength());
                break;
            case XMLEvent.PROCESSING_INSTRUCTION:
                xmlStreamWriter.writeProcessingInstruction(xmlStreamReader.getPITarget(),
                        xmlStreamReader.getPIData());
                break;
            case XMLEvent.CDATA:
                xmlStreamWriter.writeCData(xmlStreamReader.getText());
                break;

            case XMLEvent.COMMENT:
                xmlStreamWriter.writeComment(xmlStreamReader.getText());
                break;
            case XMLEvent.ENTITY_REFERENCE:
                xmlStreamWriter.writeEntityRef(xmlStreamReader.getLocalName());
                break;
            case XMLEvent.START_DOCUMENT:
                String encoding = xmlStreamReader.getCharacterEncodingScheme();
                String version = xmlStreamReader.getVersion();

                if (encoding != null && version != null) {
                    xmlStreamWriter.writeStartDocument(encoding, version);
                } else if (version != null) {
                    xmlStreamWriter.writeStartDocument(xmlStreamReader.getVersion());
                }
                break;
            case XMLEvent.END_DOCUMENT:
                xmlStreamWriter.writeEndDocument();
                break;
            case XMLEvent.DTD:
                xmlStreamWriter.writeDTD(xmlStreamReader.getText());
                break;

            }
            if (!finish) {
                xmlStreamReader.next();
            }
        }
    } catch (XMLStreamException ex) {
        Exceptions.printStackTrace(ex);
    }
}

From source file:org.openanzo.services.serialization.XMLBackupReader.java

/**
 * Parse the data within the reader, passing the results to the IRepositoryHandler
 * //from  w w w. j  a v  a  2s.  c  o m
 * @param reader
 *            reader containing the data
 * @param handler
 *            Handler which will handle the elements within the data
 * @throws AnzoException
 */
private void parseBackup(Reader reader, final IBackupHandler handler) throws AnzoException {
    try {
        XMLStreamReader parser = XMLFactoryFinder.getXMLInputFactory().createXMLStreamReader(reader);

        for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
            switch (event) {
            case XMLStreamConstants.START_ELEMENT:
                if (SerializationConstants.namedGraph.equals(parser.getLocalName())) {
                    graphUri = parser.getAttributeValue(null, SerializationConstants.namedGraphUri);
                    String meta = parser.getAttributeValue(null, SerializationConstants.metadataGraphUri);
                    String uuidString = parser.getAttributeValue(null, SerializationConstants.namedGraphUUID);
                    namedGraphUri = Constants.valueFactory.createURI(graphUri);
                    metaURI = Constants.valueFactory.createURI(meta);
                    uuid = Constants.valueFactory.createURI(uuidString);
                    revisioned = Boolean
                            .parseBoolean(parser.getAttributeValue(null, SerializationConstants.revisioned));
                } else if (SerializationConstants.revisions.equals(parser.getLocalName())) {
                    revisions = new ArrayList<BackupRevision>();
                } else if (SerializationConstants.revisionInfo.equals(parser.getLocalName())) {
                    long revision = Long
                            .parseLong(parser.getAttributeValue(null, SerializationConstants.revision));
                    Long start = Long.valueOf(parser.getAttributeValue(null, SerializationConstants.start));
                    String endString = parser.getAttributeValue(null, SerializationConstants.end);
                    Long end = (endString != null) ? Long.valueOf(endString) : -1;
                    lastModified = Constants.valueFactory
                            .createURI(parser.getAttributeValue(null, SerializationConstants.lastModifiedBy));
                    revisions.add(new BackupRevision(revision, start, end, lastModified));
                } else if (SerializationConstants.statement.equals(parser.getLocalName())) {
                    if (processGraph) {
                        if (revisioned) {
                            String startString = parser.getAttributeValue(null, SerializationConstants.start);
                            String endString = parser.getAttributeValue(null, SerializationConstants.end);
                            if (startString != null)
                                start = Long.valueOf(startString);
                            if (endString != null)
                                end = Long.valueOf(endString);
                        }
                    }
                } else if (SerializationConstants.statements.equals(parser.getLocalName())) {
                    if (processGraph) {
                        metadata = !parser.getAttributeValue(null, SerializationConstants.namedGraphUri)
                                .equals(graphUri);
                    }
                } else if (SerializationConstants.subject.equals(parser.getLocalName())) {
                    if (processGraph) {
                        nodeType = parser.getAttributeValue(null, SerializationConstants.subjectType);
                    }
                } else if (SerializationConstants.predicate.equals(parser.getLocalName())) {
                } else if (SerializationConstants.object.equals(parser.getLocalName())) {
                    if (processGraph) {
                        nodeType = parser.getAttributeValue(null, SerializationConstants.objectType);
                        language = parser.getAttributeValue(null, SerializationConstants.language);
                        datatype = parser.getAttributeValue(null, SerializationConstants.dataType);
                    }
                } else if (SerializationConstants.namedGraphUri.equals(parser.getLocalName())) {
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                if (SerializationConstants.namedGraph.equals(parser.getLocalName())) {
                    graphUri = null;
                    namedGraphUri = null;
                    metaURI = null;
                    uuid = null;
                    revisioned = true;
                    revisions = null;
                } else if (SerializationConstants.revisionInfo.equals(parser.getLocalName())) {
                } else if (SerializationConstants.revisions.equals(parser.getLocalName())) {
                    processGraph = handler.handleNamedGraph(revisioned, namedGraphUri, metaURI, uuid,
                            revisions);
                    revisions = null;
                } else if (SerializationConstants.statement.equals(parser.getLocalName())) {
                    if (processGraph) {
                        handler.handleStatement(metadata, revisioned,
                                Constants.valueFactory.createStatement(currentSubject, currentPredicate,
                                        currentObject, currentNamedGraphURI),
                                start, end);
                        start = null;
                        end = null;
                    }
                } else if (SerializationConstants.subject.equals(parser.getLocalName())) {
                    if (processGraph) {
                        if (NodeType.BNODE.name().equals(nodeType)) {
                            currentSubject = Constants.valueFactory.createBNode(currentValue);
                        } else {
                            currentSubject = Constants.valueFactory.createURI(currentValue);
                        }
                        currentValue = null;
                        nodeType = null;
                    }
                } else if (SerializationConstants.predicate.equals(parser.getLocalName())) {
                    if (processGraph) {
                        currentPredicate = Constants.valueFactory.createURI(currentValue);
                        currentValue = null;
                    }
                } else if (SerializationConstants.object.equals(parser.getLocalName())) {
                    if (processGraph) {
                        if (NodeType.BNODE.name().equals(nodeType)) {
                            currentObject = Constants.valueFactory.createBNode(currentValue);
                        } else if (NodeType.URI.name().equals(nodeType)) {
                            currentObject = Constants.valueFactory.createURI(currentValue);
                        } else if (NodeType.LITERAL.name().equals(nodeType)) {
                            if (currentValue == null) {
                                currentValue = "";
                            } else if (Base64
                                    .isArrayByteBase64(currentValue.getBytes(Constants.byteEncoding))) {
                                currentValue = new String(
                                        Base64.decodeBase64(currentValue.getBytes(Constants.byteEncoding)),
                                        Constants.byteEncoding);
                            }
                            if (datatype != null) {
                                currentObject = Constants.valueFactory.createLiteral(currentValue,
                                        Constants.valueFactory.createURI(datatype));
                            } else if (language != null) {
                                currentObject = Constants.valueFactory.createLiteral(currentValue, language);
                            } else {
                                currentObject = Constants.valueFactory.createLiteral(currentValue);
                            }
                        }
                        currentValue = null;
                        nodeType = null;
                        language = null;
                        datatype = null;
                    }
                } else if (SerializationConstants.namedGraphUri.equals(parser.getLocalName())) {
                    if (processGraph) {
                        currentNamedGraphURI = Constants.valueFactory.createURI(currentValue);
                        currentValue = null;
                    }
                }
                break;
            case XMLStreamConstants.CHARACTERS:
                if (parser.hasText())
                    currentValue = parser.getText();
                break;
            case XMLStreamConstants.CDATA:
                if (parser.hasText())
                    currentValue = parser.getText();
                break;
            }
        }
        parser.close();
    } catch (XMLStreamException ex) {
        throw new AnzoException(ExceptionConstants.IO.READ_ERROR, ex, ex.getMessage());
    } catch (UnsupportedEncodingException uee) {
        throw new AnzoException(ExceptionConstants.IO.ENCODING_ERROR, uee);
    }
}

From source file:org.openanzo.services.serialization.XMLNamedGraphUpdatesReader.java

/**
 * Parse the data within the reader, passing the results to the IRepositoryHandler
 * /*from w w  w. j  a  va2 s.c om*/
 * @param reader
 *            reader containing the data
 * @param handler
 *            Handler which will handle the elements within the data
 * @throws AnzoException
 */
public void parseUpdates(Reader reader, final INamedGraphUpdateHandler handler) throws AnzoException {
    try {
        XMLStreamReader parser = XMLFactoryFinder.getXMLInputFactory().createXMLStreamReader(reader);

        for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
            switch (event) {
            case XMLStreamConstants.START_ELEMENT:
                if (SerializationConstants.namedGraph.equals(parser.getLocalName())) {
                    String uri = parser.getAttributeValue(null, SerializationConstants.namedGraphUri);
                    String uuid = parser.getAttributeValue(null, SerializationConstants.namedGraphUUID);
                    currentNamedGraphUpdate = new NamedGraphUpdate(MemURI.create(uri));
                    if (uuid != null) {
                        currentNamedGraphUpdate.setUUID(MemURI.create(uuid));
                    }
                    String revision = parser.getAttributeValue(null, SerializationConstants.revision);
                    if (revision != null) {
                        currentNamedGraphUpdate.setRevision(Long.parseLong(revision));
                    }
                } else if (SerializationConstants.additions.equals(parser.getLocalName())) {
                    currentStatements = currentNamedGraphUpdate.getAdditions();
                } else if (SerializationConstants.metaAdditions.equals(parser.getLocalName())) {
                    currentStatements = currentNamedGraphUpdate.getMetaAdditions();
                } else if (SerializationConstants.removals.equals(parser.getLocalName())) {
                    currentStatements = currentNamedGraphUpdate.getRemovals();
                } else if (SerializationConstants.metaRemovals.equals(parser.getLocalName())) {
                    currentStatements = currentNamedGraphUpdate.getMetaRemovals();
                } else if (SerializationConstants.statement.equals(parser.getLocalName())) {
                } else if (SerializationConstants.subject.equals(parser.getLocalName())) {
                    nodeType = parser.getAttributeValue(null, SerializationConstants.subjectType);
                } else if (SerializationConstants.predicate.equals(parser.getLocalName())) {
                } else if (SerializationConstants.object.equals(parser.getLocalName())) {
                    nodeType = parser.getAttributeValue(null, SerializationConstants.objectType);
                    language = parser.getAttributeValue(null, SerializationConstants.language);
                    datatype = parser.getAttributeValue(null, SerializationConstants.dataType);
                } else if (SerializationConstants.namedGraphUri.equals(parser.getLocalName())) {
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                if (SerializationConstants.namedGraph.equals(parser.getLocalName())) {
                    handler.handleNamedGraphUpdate(currentNamedGraphUpdate);
                    currentNamedGraphUpdate = null;
                } else if (SerializationConstants.additions.equals(parser.getLocalName())) {
                    currentStatements = null;
                } else if (SerializationConstants.metaAdditions.equals(parser.getLocalName())) {
                    currentStatements = null;
                } else if (SerializationConstants.removals.equals(parser.getLocalName())) {
                    currentStatements = null;
                } else if (SerializationConstants.metaRemovals.equals(parser.getLocalName())) {
                    currentStatements = null;
                } else if (SerializationConstants.statement.equals(parser.getLocalName())) {
                    currentStatements.add(Constants.valueFactory.createStatement(currentSubject,
                            currentPredicate, currentObject, currentNamedGraphURI));
                } else if (SerializationConstants.subject.equals(parser.getLocalName())) {
                    if (NodeType.BNODE.name().equals(nodeType)) {
                        currentSubject = Constants.valueFactory.createBNode(currentValue);
                    } else {
                        currentSubject = Constants.valueFactory.createURI(currentValue);
                    }
                    currentValue = null;
                    nodeType = null;
                } else if (SerializationConstants.predicate.equals(parser.getLocalName())) {
                    currentPredicate = Constants.valueFactory.createURI(currentValue);
                    currentValue = null;
                } else if (SerializationConstants.object.equals(parser.getLocalName())) {
                    if (NodeType.BNODE.name().equals(nodeType)) {
                        currentObject = Constants.valueFactory.createBNode(currentValue);
                    } else if (NodeType.URI.name().equals(nodeType)) {
                        currentObject = Constants.valueFactory.createURI(currentValue);
                    } else if (NodeType.LITERAL.name().equals(nodeType)) {
                        if (Base64.isArrayByteBase64(currentValue.getBytes(Constants.byteEncoding))) {
                            currentValue = new String(
                                    Base64.decodeBase64(currentValue.getBytes(Constants.byteEncoding)),
                                    Constants.byteEncoding);
                        }
                        if (datatype != null) {
                            currentObject = Constants.valueFactory.createLiteral(currentValue,
                                    Constants.valueFactory.createURI(datatype));
                        } else if (language != null) {
                            currentObject = Constants.valueFactory.createLiteral(currentValue, language);
                        } else {
                            currentObject = Constants.valueFactory.createLiteral(currentValue);
                        }
                    }
                    currentValue = null;
                    nodeType = null;
                    language = null;
                    datatype = null;
                } else if (SerializationConstants.namedGraphUri.equals(parser.getLocalName())) {
                    currentNamedGraphURI = Constants.valueFactory.createURI(currentValue);
                    currentValue = null;
                }
                break;
            case XMLStreamConstants.CHARACTERS:
                currentValue = parser.getText();
                break;
            case XMLStreamConstants.CDATA:
                currentValue = parser.getText();
                break;
            }
        }
        parser.close();
    } catch (XMLStreamException ex) {
        throw new AnzoException(ExceptionConstants.IO.READ_ERROR, ex, ex.getMessage());
    } catch (UnsupportedEncodingException uee) {
        throw new AnzoException(ExceptionConstants.IO.ENCODING_ERROR, uee);
    }
}

From source file:org.openanzo.services.serialization.XMLUpdatesReader.java

/**
 * Parse the data within the reader, passing the results to the IRepositoryHandler
 * //from  w ww.j a va2s  .  c o  m
 * @param reader
 *            reader containing the data
 * @param handler
 *            Handler which will handle the elements within the data
 * @throws AnzoException
 */
private void parseUpdateTransactions(Reader reader, final IUpdatesHandler handler) throws AnzoException {
    try {
        XMLStreamReader parser = XMLFactoryFinder.getXMLInputFactory().createXMLStreamReader(reader);

        for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
            switch (event) {
            case XMLStreamConstants.START_ELEMENT:
                if (SerializationConstants.transaction.equals(parser.getLocalName())) {
                    currentTransaction = parseTransaction(parser);
                } else if (SerializationConstants.transactionContext.equals(parser.getLocalName())) {
                    currentStatements = currentTransaction.getTransactionContext();
                } else if (SerializationConstants.preconditions.equals(parser.getLocalName())) {
                    currentStatements = new ArrayList<Statement>();
                } else if (SerializationConstants.namedGraphUpdates.equals(parser.getLocalName())) {
                    currentValue = null;
                } else if (SerializationConstants.namedGraph.equals(parser.getLocalName())) {
                    String uri = parser.getAttributeValue(null, SerializationConstants.namedGraphUri);
                    String uuid = parser.getAttributeValue(null, SerializationConstants.namedGraphUUID);
                    currentNamedGraphUpdate = new NamedGraphUpdate(MemURI.create(uri));
                    if (uuid != null) {
                        currentNamedGraphUpdate.setUUID(MemURI.create(uuid));
                    }
                    String revision = parser.getAttributeValue(null, SerializationConstants.revision);
                    if (revision != null) {
                        currentNamedGraphUpdate.setRevision(Long.parseLong(revision));
                    }
                } else if (SerializationConstants.additions.equals(parser.getLocalName())) {
                    currentStatements = currentNamedGraphUpdate.getAdditions();
                } else if (SerializationConstants.metaAdditions.equals(parser.getLocalName())) {
                    currentStatements = currentNamedGraphUpdate.getMetaAdditions();
                } else if (SerializationConstants.removals.equals(parser.getLocalName())) {
                    currentStatements = currentNamedGraphUpdate.getRemovals();
                } else if (SerializationConstants.metaRemovals.equals(parser.getLocalName())) {
                    currentStatements = currentNamedGraphUpdate.getMetaRemovals();
                } else if (SerializationConstants.statement.equals(parser.getLocalName())) {
                } else if (SerializationConstants.subject.equals(parser.getLocalName())) {
                    nodeType = parser.getAttributeValue(null, SerializationConstants.subjectType);
                } else if (SerializationConstants.predicate.equals(parser.getLocalName())) {
                } else if (SerializationConstants.object.equals(parser.getLocalName())) {
                    nodeType = parser.getAttributeValue(null, SerializationConstants.objectType);
                    language = parser.getAttributeValue(null, SerializationConstants.language);
                    datatype = parser.getAttributeValue(null, SerializationConstants.dataType);
                } else if (SerializationConstants.namedGraphUri.equals(parser.getLocalName())) {
                } else if (SerializationConstants.errorResult.equals(parser.getLocalName())) {
                    String errorCodeStr = parser.getAttributeValue(null, SerializationConstants.errorCode);
                    errorCode = Long.parseLong(errorCodeStr);
                    errorArgs = new ArrayList<String>();
                } else if (SerializationConstants.errorMessageArg.equals(parser.getLocalName())) {

                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                if (SerializationConstants.transaction.equals(parser.getLocalName())) {
                    handler.handleTransaction(currentTransaction);
                    currentTransaction = null;
                } else if (SerializationConstants.transactionContext.equals(parser.getLocalName())) {
                    currentStatements = null;
                } else if (SerializationConstants.preconditions.equals(parser.getLocalName())) {
                    currentTransaction.getPreconditions()
                            .addAll(CommonSerializationUtils.parsePreconditionStatements(currentStatements));
                } else if (SerializationConstants.namedGraphUpdates.equals(parser.getLocalName())) {
                    if (currentValue != null)
                        currentTransaction.getUpdatedNamedGraphRevisions()
                                .putAll(CommonSerializationUtils.readNamedGraphRevisions(currentValue));
                } else if (SerializationConstants.namedGraph.equals(parser.getLocalName())) {
                    currentTransaction.addNamedGraphUpdate(currentNamedGraphUpdate);
                } else if (SerializationConstants.additions.equals(parser.getLocalName())) {
                    currentStatements = null;
                } else if (SerializationConstants.metaAdditions.equals(parser.getLocalName())) {
                    currentStatements = null;
                } else if (SerializationConstants.removals.equals(parser.getLocalName())) {
                    currentStatements = null;
                } else if (SerializationConstants.metaRemovals.equals(parser.getLocalName())) {
                    currentStatements = null;
                } else if (SerializationConstants.statement.equals(parser.getLocalName())) {
                    currentStatements.add(Constants.valueFactory.createStatement(currentSubject,
                            currentPredicate, currentObject, currentNamedGraphURI));
                } else if (SerializationConstants.subject.equals(parser.getLocalName())) {
                    if (NodeType.BNODE.name().equals(nodeType)) {
                        currentSubject = Constants.valueFactory.createBNode(currentValue);
                    } else {
                        currentSubject = Constants.valueFactory.createURI(currentValue);
                    }
                    currentValue = null;
                    nodeType = null;
                } else if (SerializationConstants.predicate.equals(parser.getLocalName())) {
                    currentPredicate = Constants.valueFactory.createURI(currentValue);
                    currentValue = null;
                } else if (SerializationConstants.object.equals(parser.getLocalName())) {
                    if (NodeType.BNODE.name().equals(nodeType)) {
                        currentObject = Constants.valueFactory.createBNode(currentValue);
                    } else if (NodeType.URI.name().equals(nodeType)) {
                        currentObject = Constants.valueFactory.createURI(currentValue);
                    } else if (NodeType.LITERAL.name().equals(nodeType)) {
                        if (currentValue == null) {
                            currentValue = "";
                        } else if (Base64.isArrayByteBase64(currentValue.getBytes(Constants.byteEncoding))) {
                            currentValue = new String(
                                    Base64.decodeBase64(currentValue.getBytes(Constants.byteEncoding)),
                                    Constants.byteEncoding);
                        }
                        if (datatype != null) {
                            currentObject = Constants.valueFactory.createLiteral(currentValue,
                                    Constants.valueFactory.createURI(datatype));
                        } else if (language != null) {
                            currentObject = Constants.valueFactory.createLiteral(currentValue, language);
                        } else {
                            currentObject = Constants.valueFactory.createLiteral(currentValue);
                        }
                    }
                    currentValue = null;
                    nodeType = null;
                    language = null;
                    datatype = null;
                } else if (SerializationConstants.namedGraphUri.equals(parser.getLocalName())) {
                    currentNamedGraphURI = Constants.valueFactory.createURI(currentValue);
                    currentValue = null;
                } else if (SerializationConstants.errorResult.equals(parser.getLocalName())) {
                    currentTransaction.getErrors()
                            .add(new AnzoException(errorCode, errorArgs.toArray(new String[0])));
                    errorCode = 0;
                    errorArgs = null;
                } else if (SerializationConstants.errorMessageArg.equals(parser.getLocalName())) {
                    errorArgs.add(currentValue);
                    currentValue = null;
                }
                break;
            case XMLStreamConstants.CHARACTERS:
                if (parser.hasText())
                    currentValue = parser.getText();
                break;
            case XMLStreamConstants.CDATA:
                if (parser.hasText())
                    currentValue = parser.getText();
                break;
            }
        }
        parser.close();
    } catch (XMLStreamException ex) {
        throw new AnzoException(ExceptionConstants.IO.READ_ERROR, ex, ex.getMessage());
    } catch (UnsupportedEncodingException uee) {
        throw new AnzoException(ExceptionConstants.IO.ENCODING_ERROR, uee);
    }
}

From source file:org.opendatakit.briefcase.export.SubmissionParser.java

private static Optional<String> parseAttribute(Path submission, Reader ioReader, String attributeName,
        SubmissionExportErrorCallback onParsingError) {
    try {//from   w  ww  .  j a v  a2s . co  m
        XMLStreamReader reader = xmlInputFactory.createXMLStreamReader(ioReader);
        while (reader.hasNext())
            if (reader.next() == START_ELEMENT)
                for (int i = 0, c = reader.getAttributeCount(); i < c; ++i)
                    if (reader.getAttributeLocalName(i).equals(attributeName))
                        return Optional.of(reader.getAttributeValue(i));
    } catch (XMLStreamException e) {
        log.error("Can't parse submission", e);
        onParsingError.accept(submission, "parsing error");
    }
    return Optional.empty();
}

From source file:org.openengsb.connector.promreport.internal.ProcessFileStore.java

private ProcessInstance readFrom(File processFile, String processInstanceId) {
    BufferedInputStream fis = null;
    try {/*from ww w. j av  a  2 s.  c  o m*/
        fis = new BufferedInputStream(new FileInputStream(processFile));
        XMLStreamReader xmlr = xmlif.createXMLStreamReader(fis);
        int eventType;
        while (xmlr.hasNext()) {
            eventType = xmlr.next();
            if (eventType == XMLStreamConstants.START_ELEMENT && xmlr.getLocalName().equals("ProcessInstance")
                    && xmlr.getAttributeValue(null, "id").equals(processInstanceId)) {
                return (ProcessInstance) createUnmarshaller().unmarshal(xmlr);
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(fis);
    }
    return null;
}

From source file:org.openo.nfvo.emsdriver.collector.TaskThread.java

private boolean processCMXml(File tempfile, String nename, String type) {

    String csvpath = localPath + nename + "/" + type + "/";
    File csvpathfile = new File(csvpath);
    if (!csvpathfile.exists()) {
        csvpathfile.mkdirs();//from w w  w. j  a  v a2  s  .c o  m
    }
    String csvFileName = nename + dateFormat.format(new Date()) + System.nanoTime();
    String csvpathAndFileName = csvpath + csvFileName + ".csv";
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(csvpathAndFileName, false);
        bos = new BufferedOutputStream(fos, 10240);
    } catch (FileNotFoundException e1) {
        log.error("FileNotFoundException " + StringUtil.getStackTrace(e1));
    }

    boolean FieldNameFlag = false;
    boolean FieldValueFlag = false;
    //line num
    int countNum = 0;
    String xmlPathAndFileName = null;
    String localName = null;
    String endLocalName = null;
    String rmUID = null;
    int index = -1;
    ArrayList<String> names = new ArrayList<String>();// colname
    LinkedHashMap<String, String> nameAndValue = new LinkedHashMap<String, String>();

    FileInputStream fis = null;
    InputStreamReader isr = null;
    XMLStreamReader reader = null;
    try {
        fis = new FileInputStream(tempfile);
        isr = new InputStreamReader(fis, Constant.ENCODING_UTF8);
        XMLInputFactory fac = XMLInputFactory.newInstance();
        reader = fac.createXMLStreamReader(isr);
        int event = -1;
        boolean setcolum = true;
        while (reader.hasNext()) {
            try {
                event = reader.next();
                switch (event) {
                case XMLStreamConstants.START_ELEMENT:
                    localName = reader.getLocalName();
                    if ("FieldName".equalsIgnoreCase(localName)) {
                        FieldNameFlag = true;
                    }
                    if (FieldNameFlag) {
                        if ("N".equalsIgnoreCase(localName)) {
                            String colName = reader.getElementText().trim();
                            names.add(colName);
                        }
                    }
                    if ("FieldValue".equalsIgnoreCase(localName)) {
                        FieldValueFlag = true;

                    }
                    if (FieldValueFlag) {
                        if (setcolum) {
                            xmlPathAndFileName = this.setColumnNames(nename, names, type);
                            setcolum = false;
                        }

                        if ("Object".equalsIgnoreCase(localName)) {
                            int ac = reader.getAttributeCount();
                            for (int i = 0; i < ac; i++) {
                                if ("rmUID".equalsIgnoreCase(reader.getAttributeLocalName(i))) {
                                    rmUID = reader.getAttributeValue(i).trim();
                                }
                            }
                            nameAndValue.put("rmUID", rmUID);
                        }
                        if ("V".equalsIgnoreCase(localName)) {
                            index = Integer.parseInt(reader.getAttributeValue(0)) - 1;
                            String currentName = names.get(index);
                            String v = reader.getElementText().trim();
                            nameAndValue.put(currentName, v);
                        }
                    }
                    break;
                case XMLStreamConstants.CHARACTERS:
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    endLocalName = reader.getLocalName();

                    if ("FieldName".equalsIgnoreCase(endLocalName)) {
                        FieldNameFlag = false;
                    }
                    if ("FieldValue".equalsIgnoreCase(endLocalName)) {
                        FieldValueFlag = false;
                    }
                    if ("Object".equalsIgnoreCase(endLocalName)) {
                        countNum++;
                        this.appendLine(nameAndValue, bos);
                        nameAndValue.clear();
                    }
                    break;
                }
            } catch (Exception e) {
                log.error("" + StringUtil.getStackTrace(e));
                event = reader.next();
            }
        }

        if (bos != null) {
            bos.close();
            bos = null;
        }
        if (fos != null) {
            fos.close();
            fos = null;
        }

        String[] fileKeys = this.createZipFile(csvpathAndFileName, xmlPathAndFileName, nename);
        //ftp store
        Properties ftpPro = configurationInterface.getProperties();
        String ip = ftpPro.getProperty("ftp_ip");
        String port = ftpPro.getProperty("ftp_port");
        String ftp_user = ftpPro.getProperty("ftp_user");
        String ftp_password = ftpPro.getProperty("ftp_password");

        String ftp_passive = ftpPro.getProperty("ftp_passive");
        String ftp_type = ftpPro.getProperty("ftp_type");
        String remoteFile = ftpPro.getProperty("ftp_remote_path");
        this.ftpStore(fileKeys, ip, port, ftp_user, ftp_password, ftp_passive, ftp_type, remoteFile);
        //create Message
        String message = this.createMessage(fileKeys[1], ftp_user, ftp_password, ip, port, countNum, nename);

        //set message
        this.setMessage(message);
    } catch (Exception e) {
        log.error("" + StringUtil.getStackTrace(e));
        return false;
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
            if (isr != null) {
                isr.close();
            }
            if (fis != null) {
                fis.close();
            }
            if (bos != null) {
                bos.close();
            }

            if (fos != null) {
                fos.close();
            }
        } catch (Exception e) {
            log.error(e);
        }
    }
    return true;
}