Example usage for javax.xml.stream XMLEventReader hasNext

List of usage examples for javax.xml.stream XMLEventReader hasNext

Introduction

In this page you can find the example usage for javax.xml.stream XMLEventReader hasNext.

Prototype

@Override
public boolean hasNext();

Source Link

Document

Check if there are more events.

Usage

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

private void readToEnd(XMLEventReader reader) throws XMLStreamException {
    while (reader.hasNext() && !reader.peek().isEndElement())
        reader.nextEvent();//from  w w  w .j  av  a2s. c  o m

    if (reader.hasNext())
        reader.nextEvent();

}

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

private String readChars(XMLEventReader reader) throws XMLStreamException {

    StringBuffer sb = new StringBuffer();
    while (reader.hasNext() && reader.peek().isCharacters()) {
        Characters ch = reader.nextEvent().asCharacters();
        sb.append(ch.getData());/*w  w w.  j a  v a 2s  .  co  m*/

    }
    return sb.toString();

}

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

/**
 * Reads from the original Excel document the string table and puts it into a
 * compressed and encrypted file.//  w w  w .  j a v a  2s. c o  m
 * 
 * @param is
 * @throws java.io.IOException
 */

@Override
public void readFrom(InputStream is) throws IOException {
    this.currentItem = 0;
    // read from source and write into tempfile
    // open temp file depending on options compressed/encrypted
    OutputStream tempOS = null;
    if ((this.ca != null) || (this.compressTempFile)) {
        tempOS = new FileOutputStream(this.tempFile);
        if (this.ca != null) { // encrypt file if configured
            tempOS = new CipherOutputStream(tempOS, this.ciEncrypt);
        }
        if (this.compressTempFile) { // compress file if configured
            tempOS = new GZIPOutputStream(tempOS, EncryptedCachedDiskStringsTable.compressBufferSize);
        }
    } else { // not encrypted and not compressed: configure a random access file for
             // writing/reading = highest performance
        this.tempRAF = new RandomAccessFile(this.tempFile, "rw");
    }
    // read from source
    // use Stax event reader
    XMLEventReader xer = null;
    try {
        xer = StaxHelper.newXMLInputFactory().createXMLEventReader(this.originalIS);
        while (xer.hasNext()) {
            XMLEvent xe = xer.nextEvent();
            // check if it is a string item (entry in string table)
            if (xe.isStartElement() && xe.asStartElement().getName().getLocalPart().equalsIgnoreCase("si")) {
                String siText = this.parseSIText(xer);
                // add string to temp file
                this.addString(siText, tempOS);
                this.count++;
            }
        }
        // close tempfile
        // make tempFile available as a reader
    } catch (XMLStreamException e) {
        LOG.error("Cannot read original SharedStringTable from document. Exception " + e);
        throw new IOException(e);
    } catch (FormatNotUnderstoodException e) {
        LOG.error("Cannot read properly SharedStringTable from document. Exception " + e);
        throw new IOException(e);
    } finally {
        // close temporary Stream (tempfile should be deleted using the close method of
        // this class and not here)
        if (tempOS != null) {
            tempOS.close();
        }
    }
    // open the input stream towards the temp file
    this.accessTempFile(0L);

}

From source file:ru.codeinside.gws.crypto.cryptopro.CryptoProvider.java

private String saxFilter(Node node) {
    try {/* w  w  w. ja va 2 s .  c  om*/
        final Transformer transformer = TransformerFactory.newInstance().newTransformer();
        final StringWriter w1 = new StringWriter();
        transformer.transform(new DOMSource(node), new StreamResult(w1));
        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLEventReader eventReader = xif
                .createXMLEventReader(new StreamSource(new StringReader(w1.toString())));
        XMLEventReader filteredReader = xif.createFilteredReader(eventReader, new EventFilter() {
            @Override
            public boolean accept(XMLEvent event) {
                int type = event.getEventType();
                if (type == XMLStreamConstants.START_DOCUMENT || type == XMLStreamConstants.END_DOCUMENT) {
                    return false;
                }
                if (event.isStartElement()) {
                    StartElement startElement = (StartElement) event;
                    QName name = startElement.getName();
                    if ("".equals(name.getNamespaceURI()) && "root".equals(name.getLocalPart())) {
                        return false;
                    }
                }
                if (event.isEndElement()) {
                    EndElement endElement = (EndElement) event;
                    QName name = endElement.getName();
                    if ("".equals(name.getNamespaceURI()) && "root".equals(name.getLocalPart())) {
                        return false;
                    }
                }
                return true;
            }
        });
        StringWriter sw = new StringWriter();
        XMLOutputFactory xof = XMLOutputFactory.newInstance();
        XMLEventWriter writer = xof.createXMLEventWriter(sw);
        while (filteredReader.hasNext()) {
            writer.add(filteredReader.nextEvent());
        }
        return sw.toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:tds.itemrenderer.processing.ITSDocumentHelper.java

/**
 * Gets the root element of XML//from ww  w .j  av  a2  s .c om
 * 
 * @param uri
 * @return xml root element
 * @throws FileNotFoundException
 * @throws UnsupportedEncodingException
 * @throws XMLStreamException
 */
public static String getRootElementName(URI uri) {
    XMLInputFactory inputFactory = TdsXmlInputFactory.newInstance();
    try {
        InputStream input = null;
        String uriPath = getUriOriginalString(uri);
        if (isFtp(uri)) {
            input = new ByteArrayInputStream(FileFtpHandler.getBytes(uri));
        } else {
            input = new FileInputStream(uriPath);
        }
        XMLEventReader inputEventReader = inputFactory.createXMLEventReader(input);
        while (inputEventReader.hasNext()) {
            XMLEvent event = inputEventReader.nextEvent();
            if (event.isStartElement()) {
                return event.asStartElement().getName().getLocalPart();
            }
        }
    } catch (FileNotFoundException | XMLStreamException e) {
        throw new ITSDocumentProcessingException(e);
    }
    return null;
}