Example usage for javax.xml.stream XMLStreamConstants END_ELEMENT

List of usage examples for javax.xml.stream XMLStreamConstants END_ELEMENT

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamConstants END_ELEMENT.

Prototype

int END_ELEMENT

To view the source code for javax.xml.stream XMLStreamConstants END_ELEMENT.

Click Source Link

Document

Indicates an event is an end element

Usage

From source file:org.apache.axiom.om.impl.llom.OMStAXWrapper.java

/**
 * Returns the next tag.//w ww  .  j  av a2  s .c  om
 *
 * @return Returns int.
 * @throws org.apache.axiom.om.impl.exception.OMStreamingException
 *
 * @throws XMLStreamException
 */
public int nextTag() throws XMLStreamException {
    int eventType = next();
    while ((eventType == XMLStreamConstants.CHARACTERS && isWhiteSpace()) // skip whitespace
            || (eventType == XMLStreamConstants.CDATA && isWhiteSpace()) // skip whitespace
            || eventType == XMLStreamConstants.SPACE || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
            || eventType == XMLStreamConstants.COMMENT) {
        eventType = next();
    }
    if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) {
        throw new XMLStreamException("expected start or end tag", getLocation());
    }
    return eventType;
}

From source file:org.apache.axiom.om.impl.llom.OMStAXWrapper.java

/**
 * @return Returns String.//from   w  w w  .  j a va 2 s .co  m
 * @throws XMLStreamException
 * @see javax.xml.stream.XMLStreamReader#getElementText()
 */
public String getElementText() throws XMLStreamException {
    if (parser != null) {
        try {
            return parser.getElementText();
        } catch (XMLStreamException e) {
            throw new OMStreamingException(e);
        }
    } else {
        ///////////////////////////////////////////////////////
        //// Code block directly from the API documentation ///
        if (getEventType() != XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException("parser must be on START_ELEMENT to read next text", getLocation());
        }
        int eventType = next();
        StringBuffer content = new StringBuffer();
        while (eventType != XMLStreamConstants.END_ELEMENT) {
            if (eventType == XMLStreamConstants.CHARACTERS || eventType == XMLStreamConstants.CDATA
                    || eventType == XMLStreamConstants.SPACE
                    || eventType == XMLStreamConstants.ENTITY_REFERENCE) {
                content.append(getText());
            } else if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
                    || eventType == XMLStreamConstants.COMMENT) {
                // skipping
            } else if (eventType == XMLStreamConstants.END_DOCUMENT) {
                throw new XMLStreamException("unexpected end of document when reading element text content");
            } else if (eventType == XMLStreamConstants.START_ELEMENT) {
                throw new XMLStreamException("element text content may not contain START_ELEMENT");
            } else {
                throw new XMLStreamException("Unexpected event type " + eventType, getLocation());
            }
            eventType = next();
        }
        return content.toString();
        ///////////////////////////////////////////////////////////////
    }

}

From source file:org.apache.axiom.om.impl.SwitchingWrapper.java

/**
 * @return Returns String./*from  ww w  .j  av a  2  s  . c o  m*/
 * @throws XMLStreamException
 * @see javax.xml.stream.XMLStreamReader#getElementText()
 */
public String getElementText() throws XMLStreamException {
    if (parser != null) {
        try {
            String elementText = parser.getElementText();
            currentEvent = END_ELEMENT;
            return elementText;
        } catch (XMLStreamException e) {
            throw new OMStreamingException(e);
        }
    } else {
        ///////////////////////////////////////////////////////
        //// Code block directly from the API documentation ///
        if (getEventType() != XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException("parser must be on START_ELEMENT to read next text", getLocation());
        }
        int eventType = next();
        StringBuffer content = new StringBuffer();
        while (eventType != XMLStreamConstants.END_ELEMENT) {
            if (eventType == XMLStreamConstants.CHARACTERS || eventType == XMLStreamConstants.CDATA
                    || eventType == XMLStreamConstants.SPACE
                    || eventType == XMLStreamConstants.ENTITY_REFERENCE) {
                content.append(getText());
            } else if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
                    || eventType == XMLStreamConstants.COMMENT) {
                // skipping
            } else if (eventType == XMLStreamConstants.END_DOCUMENT) {
                throw new XMLStreamException("unexpected end of document when reading element text content");
            } else if (eventType == XMLStreamConstants.START_ELEMENT) {
                throw new XMLStreamException("element text content may not contain START_ELEMENT");
            } else {
                throw new XMLStreamException("Unexpected event type " + eventType, getLocation());
            }
            eventType = next();
        }
        return content.toString();
        ///////////////////////////////////////////////////////////////
    }

}

From source file:org.apache.axiom.om.util.OMXMLStreamReaderValidator.java

public int next() throws XMLStreamException {
    int event = delegate.next();
    logParserState();//from  w  w w  .  j  a  v a 2s.com

    // Make sure that the start element and end element events match.
    // Mismatched events are a key indication that the delegate stream reader is 
    // broken or corrupted.
    switch (event) {
    case XMLStreamConstants.START_ELEMENT:
        stack.push(delegate.getName());
        break;
    case XMLStreamConstants.END_ELEMENT:
        QName delegateQName = delegate.getName();
        if (stack.isEmpty()) {
            reportMismatchedEndElement(null, delegateQName);
        } else {
            QName expectedQName = (QName) stack.pop();

            if (!expectedQName.equals(delegateQName)) {
                reportMismatchedEndElement(expectedQName, delegateQName);
            }
        }
        break;

    default:

    }

    return event;
}

From source file:org.apache.axiom.om.util.OMXMLStreamReaderValidator.java

/**
 * Dump the current event of the delegate.
 *///from  ww  w  .j  av  a  2 s. co m
protected void logParserState() {
    if (IS_ADV_DEBUG_ENABLED) {
        int currentEvent = delegate.getEventType();

        switch (currentEvent) {
        case XMLStreamConstants.START_ELEMENT:
            log.trace("START_ELEMENT: ");
            log.trace("  QName: " + delegate.getName());
            break;
        case XMLStreamConstants.START_DOCUMENT:
            log.trace("START_DOCUMENT: ");
            break;
        case XMLStreamConstants.CHARACTERS:
            log.trace("CHARACTERS: ");
            log.trace("[" + delegate.getText() + "]");
            break;
        case XMLStreamConstants.CDATA:
            log.trace("CDATA: ");
            log.trace("[" + delegate.getText() + "]");
            break;
        case XMLStreamConstants.END_ELEMENT:
            log.trace("END_ELEMENT: ");
            log.trace("  QName: " + delegate.getName());
            break;
        case XMLStreamConstants.END_DOCUMENT:
            log.trace("END_DOCUMENT: ");
            break;
        case XMLStreamConstants.SPACE:
            log.trace("SPACE: ");
            log.trace("[" + delegate.getText() + "]");
            break;
        case XMLStreamConstants.COMMENT:
            log.trace("COMMENT: ");
            log.trace("[" + delegate.getText() + "]");
            break;
        case XMLStreamConstants.DTD:
            log.trace("DTD: ");
            log.trace("[" + delegate.getText() + "]");
            break;
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            log.trace("PROCESSING_INSTRUCTION: ");
            log.trace("   [" + delegate.getPITarget() + "][" + delegate.getPIData() + "]");
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            log.trace("ENTITY_REFERENCE: ");
            log.trace("    " + delegate.getLocalName() + "[" + delegate.getText() + "]");
            break;
        default:
            log.trace("UNKNOWN_STATE: " + currentEvent);

        }
    }
}

From source file:org.apache.axiom.util.stax.debug.XMLStreamReaderValidator.java

private void trackEvent(int event) throws XMLStreamException {
    logParserState();// www .jav a2 s.c  om

    // Make sure that the start element and end element events match.
    // Mismatched events are a key indication that the delegate stream reader is 
    // broken or corrupted.
    switch (event) {
    case XMLStreamConstants.START_ELEMENT:
        stack.push(super.getName());
        break;
    case XMLStreamConstants.END_ELEMENT:
        QName delegateQName = super.getName();
        if (stack.isEmpty()) {
            reportMismatchedEndElement(null, delegateQName);
        } else {
            QName expectedQName = (QName) stack.pop();

            if (!expectedQName.equals(delegateQName)) {
                reportMismatchedEndElement(expectedQName, delegateQName);
            }
        }
        break;

    default:

    }
}

From source file:org.apache.axiom.util.stax.debug.XMLStreamReaderValidator.java

/**
 * Dump the current event of the delegate.
 *///from   ww w.  j  a  va  2s  .c  o  m
protected void logParserState() {
    if (IS_ADV_DEBUG_ENABLED) {
        int currentEvent = super.getEventType();

        switch (currentEvent) {
        case XMLStreamConstants.START_ELEMENT:
            log.trace("START_ELEMENT: ");
            log.trace("  QName: " + super.getName());
            break;
        case XMLStreamConstants.START_DOCUMENT:
            log.trace("START_DOCUMENT: ");
            break;
        case XMLStreamConstants.CHARACTERS:
            log.trace("CHARACTERS: ");
            log.trace("[" + super.getText() + "]");
            break;
        case XMLStreamConstants.CDATA:
            log.trace("CDATA: ");
            log.trace("[" + super.getText() + "]");
            break;
        case XMLStreamConstants.END_ELEMENT:
            log.trace("END_ELEMENT: ");
            log.trace("  QName: " + super.getName());
            break;
        case XMLStreamConstants.END_DOCUMENT:
            log.trace("END_DOCUMENT: ");
            break;
        case XMLStreamConstants.SPACE:
            log.trace("SPACE: ");
            log.trace("[" + super.getText() + "]");
            break;
        case XMLStreamConstants.COMMENT:
            log.trace("COMMENT: ");
            log.trace("[" + super.getText() + "]");
            break;
        case XMLStreamConstants.DTD:
            log.trace("DTD: ");
            log.trace("[" + super.getText() + "]");
            break;
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            log.trace("PROCESSING_INSTRUCTION: ");
            log.trace("   [" + super.getPITarget() + "][" + super.getPIData() + "]");
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            log.trace("ENTITY_REFERENCE: ");
            log.trace("    " + super.getLocalName() + "[" + super.getText() + "]");
            break;
        default:
            log.trace("UNKNOWN_STATE: " + currentEvent);

        }
    }
}

From source file:org.apache.axiom.util.stax.XMLStreamReaderUtils.java

/**
 * Get a {@link DataHandler} for the binary data encoded in an element. The method supports
 * base64 encoded character data as well as optimized binary data through the
 * {@link DataHandlerReader} extension./*from   www.ja  v  a2  s.  c o  m*/
 * <p>
 * <em>Precondition</em>: the reader is on a {@link XMLStreamConstants#START_ELEMENT}
 * <p>
 * <em>Postcondition</em>: the reader is on the corresponding
 * {@link XMLStreamConstants#END_ELEMENT}
 * 
 * @param reader the stream to read the data from
 * @return the binary data from the element
 */
public static DataHandler getDataHandlerFromElement(XMLStreamReader reader) throws XMLStreamException {

    int event = reader.next();
    if (event == XMLStreamConstants.END_ELEMENT) {
        // This means that the element is actually empty -> return empty DataHandler
        return new DataHandler(new EmptyDataSource("application/octet-stream"));
    } else if (event != XMLStreamConstants.CHARACTERS) {
        throw new XMLStreamException("Expected a CHARACTER event");
    }
    DataHandlerReader dhr = getDataHandlerReader(reader);
    if (dhr != null && dhr.isBinary()) {
        DataHandler dh = dhr.getDataHandler();
        reader.next();
        return dh;
    } else {
        WritableBlob blob = new MemoryBlob();
        Writer out = new Base64DecodingOutputStreamWriter(blob.getOutputStream());
        try {
            writeTextTo(reader, out);
            // Take into account that in non coalescing mode, there may be additional
            // CHARACTERS events
            loop: while (true) {
                switch (reader.next()) {
                case XMLStreamConstants.CHARACTERS:
                    writeTextTo(reader, out);
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    break loop;
                default:
                    throw new XMLStreamException("Expected a CHARACTER event");
                }
            }
            out.close();
        } catch (IOException ex) {
            throw new XMLStreamException("Error during base64 decoding", ex);
        }
        return new DataHandler(new BlobDataSource(blob, "application/octet-string"));
    }
}

From source file:org.apache.hadoop.hdfs.tools.offlineImageViewer.OfflineImageReconstructor.java

private void expectTagEnd(String expected) throws IOException {
    XMLEvent ev = expectTag(expected, true);
    if (ev.getEventType() != XMLStreamConstants.END_ELEMENT) {
        throw new IOException("Expected tag end event for " + expected + ", but got: " + ev);
    }/*w w  w  .jav  a  2s. c  om*/
    if (!expected.startsWith("[")) {
        String tag = ev.asEndElement().getName().getLocalPart();
        if (!tag.equals(expected)) {
            throw new IOException(
                    "Expected tag end event for " + expected + ", but got tag end event for " + tag);
        }
    }
}

From source file:org.apache.hadoop.hdfs.tools.offlineImageViewer.OfflineImageReconstructor.java

/**
 * Processes the XML file back into an fsimage.
 *///from   w  w  w .  j a va2  s .c o  m
private void processXml() throws Exception {
    LOG.debug("Loading <fsimage>.");
    expectTag("fsimage", false);
    // Read the <version> tag.
    readVersion();
    // Write the HDFSIMG1 magic number which begins the fsimage file.
    out.write(FSImageUtil.MAGIC_HEADER);
    // Write a series of fsimage sections.
    sectionStartOffset = FSImageUtil.MAGIC_HEADER.length;
    final HashSet<String> unprocessedSections = new HashSet<>(sections.keySet());
    while (!unprocessedSections.isEmpty()) {
        XMLEvent ev = expectTag("[section header]", true);
        if (ev.getEventType() == XMLStreamConstants.END_ELEMENT) {
            if (ev.asEndElement().getName().getLocalPart().equals("fsimage")) {
                throw new IOException("FSImage XML ended prematurely, without " + "including section(s) "
                        + StringUtils.join(", ", unprocessedSections));
            }
            throw new IOException(
                    "Got unexpected tag end event for " + ev.asEndElement().getName().getLocalPart()
                            + " while looking " + "for section header tag.");
        } else if (ev.getEventType() != XMLStreamConstants.START_ELEMENT) {
            throw new IOException(
                    "Expected section header START_ELEMENT; " + "got event of type " + ev.getEventType());
        }
        String sectionName = ev.asStartElement().getName().getLocalPart();
        if (!unprocessedSections.contains(sectionName)) {
            throw new IOException("Unknown or duplicate section found for " + sectionName);
        }
        SectionProcessor sectionProcessor = sections.get(sectionName);
        if (sectionProcessor == null) {
            throw new IOException("Unknown FSImage section " + sectionName + ".  Valid section names are ["
                    + StringUtils.join(", ", sections.keySet()) + "]");
        }
        unprocessedSections.remove(sectionName);
        sectionProcessor.process();
    }

    // Write the StringTable section to disk.
    // This has to be done after the other sections, since some of them
    // add entries to the string table.
    writeStringTableSection();

    // Write the FileSummary section to disk.
    // This section is always last.
    long prevOffset = out.getCount();
    FileSummary fileSummary = fileSummaryBld.build();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Writing FileSummary: {" + TextFormat.printToString(fileSummary) + "}");
    }
    // Even though the last 4 bytes of the file gives the FileSummary length,
    // we still write a varint first that also contains the length.
    fileSummary.writeDelimitedTo(out);

    // Write the length of the FileSummary section as a fixed-size big
    // endian 4-byte quantity.
    int summaryLen = Ints.checkedCast(out.getCount() - prevOffset);
    byte[] summaryLenBytes = new byte[4];
    ByteBuffer.wrap(summaryLenBytes).asIntBuffer().put(summaryLen);
    out.write(summaryLenBytes);
}