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.slc.sli.api.resources.config.StAXMsgBodyReader.java

/**
 * Helper method for digesting XML documents
 * @param reader XML reader/*ww  w .  j av a  2s .  c o  m*/
 * @return EntityBody representation that corresponds to the xml
 * @throws XMLStreamException on malformed XML
 */
private static final EntityBody readDocument(final XMLStreamReader reader) throws XMLStreamException {
    if (XMLStreamConstants.START_DOCUMENT == reader.getEventType()) {
        EntityBody body = null;
        while (reader.hasNext()) {
            reader.next();
            switch (reader.getEventType()) {
            case XMLStreamConstants.START_ELEMENT: {
                body = readDocumentElement(reader);
                return body;
            }
            case XMLStreamConstants.END_DOCUMENT: {
                return body;
            }
            case XMLStreamConstants.CHARACTERS: {
                // Ignore
                break;
            }
            default: {
                throw new XMLStreamException();
            }
            }
        }
    } else {
        throw new XMLStreamException(reader.getLocalName());
    }
    throw new XMLStreamException();
}

From source file:org.slc.sli.api.resources.config.StAXMsgBodyReader.java

/**
 * Reads everything under the main document wrapper tag
 * @param reader Reader that we have for XML
 * @return EntityBody representation of the document
 * @throws XMLStreamException on malformed XML
 *//*from   ww  w .  j a v  a2s . c om*/
private static final EntityBody readDocumentElement(final XMLStreamReader reader) throws XMLStreamException {
    final Map<String, Object> elements = new HashMap<String, Object>();
    while (reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            final Pair<Object, Boolean> memberDataPair = readElement(reader);
            addToElements(reader.getLocalName(), memberDataPair, elements);
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            return new EntityBody(elements);
        }
        case XMLStreamConstants.CHARACTERS: {
            // Ignore
            break;
        }
        default: {
            throw new XMLStreamException();
        }
        }
    }
    throw new XMLStreamException();
}

From source file:org.slc.sli.api.resources.config.StAXMsgBodyReader.java

/**
 * Reads individual elements inside of a XML Document
 * @param reader xml reader//www .  jav  a 2  s .  com
 * @return a pair representing the Object value of the element (Left) as well as
 *         a boolean value representing either true (part of a list) or false
 *         (single value)
 * @throws XMLStreamException on malformed XML
 */
private static final Pair<Object, Boolean> readElement(final XMLStreamReader reader) throws XMLStreamException {
    final QName elementName = reader.getName();
    final StringBuilder sb = new StringBuilder();
    final Map<String, Object> data = new HashMap<String, Object>();
    final Boolean member = isMember(reader);
    while (reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            final QName key = reader.getName();
            final Pair<Object, Boolean> elem = readElement(reader);
            addToElements(key.getLocalPart(), elem, data);
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            if (elementName.equals(reader.getName())) {
                if (data.size() > 0) {
                    return new ImmutablePair<Object, Boolean>(data, member);
                } else {
                    return new ImmutablePair<Object, Boolean>(sb.toString(), member);
                }
            } else {
                throw new XMLStreamException(reader.getName().getLocalPart());
            }
        }
        case XMLStreamConstants.CHARACTERS: {
            sb.append(reader.getText());
            break;
        }
        default: {
            throw new XMLStreamException();
        }
        }
    }
    throw new XMLStreamException();
}

From source file:org.slc.sli.modeling.wadl.reader.WadlReader.java

private static final Application readApplication(final XMLStreamReader reader) throws XMLStreamException {
    assertName(WadlElementName.APPLICATION, reader);
    final List<Documentation> doc = new LinkedList<Documentation>();
    Grammars grammars = null;//w  w w  . j  ava2 s  .co m
    Resources resources = null;
    final List<ResourceType> resourceTypes = new LinkedList<ResourceType>();
    final List<Method> methods = new LinkedList<Method>();
    final List<Representation> representations = new LinkedList<Representation>();
    final List<Representation> faults = new LinkedList<Representation>();
    while (reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            if (match(WadlElementName.DOCUMENTATION, reader)) {
                doc.add(readDocumentation(reader));
                break;
            } else if (match(WadlElementName.GRAMMARS, reader)) {
                grammars = assertNotNull(readGrammars(reader));
                break;
            } else if (match(WadlElementName.RESOURCES, reader)) {
                resources = assertNotNull(readResources(reader));
                break;
            } else {
                throw new AssertionError(reader.getLocalName());
            }
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(WadlElementName.APPLICATION, reader);
            return new Application(doc, grammars, resources, resourceTypes, methods, representations, faults);
        }
        case XMLStreamConstants.CHARACTERS: {
            // Ignore.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}

From source file:org.slc.sli.modeling.wadl.reader.WadlReader.java

private static final Application readDocument(final XMLStreamReader reader) {
    try {//from  w  w w .  java 2  s.  c om
        if (XMLStreamConstants.START_DOCUMENT == reader.getEventType()) {
            Application app = null;
            while (reader.hasNext()) {
                reader.next();
                switch (reader.getEventType()) {
                case XMLStreamConstants.START_ELEMENT: {
                    if (match(WadlElementName.APPLICATION, reader)) {
                        app = readApplication(reader);
                        if (app == null) {
                            throw new IllegalStateException();
                        }
                        break;
                    } else {
                        throw new AssertionError(reader.getLocalName());
                    }
                }
                case XMLStreamConstants.END_DOCUMENT: {
                    if (app == null) {
                        throw new IllegalStateException();
                    }
                    return app;
                }
                default: {
                    throw new AssertionError(reader.getEventType());
                }
                }
            }
            throw new AssertionError();
        } else {
            throw new AssertionError(reader.getLocalName());
        }
    } catch (final XMLStreamException e) {
        throw new WadlRuntimeException(e);
    }
}

From source file:org.slc.sli.modeling.wadl.reader.WadlReader.java

private static final Documentation readDocumentation(final XMLStreamReader reader) throws XMLStreamException {
    assertName(WadlElementName.DOCUMENTATION, reader);
    final String title = getTitle(reader, null);
    final String lang = getLang(reader, null);
    final List<DmNode> content = new LinkedList<DmNode>();
    while (reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            content.add(readMixedElement(reader));
            break;
        }//from  w  ww  .  ja v a 2s  . c o  m
        case XMLStreamConstants.END_ELEMENT: {
            assertName(WadlElementName.DOCUMENTATION, reader);
            return new Documentation(title, lang, content);
        }
        case XMLStreamConstants.CHARACTERS: {
            content.add(new DmText(reader.getText()));
            break;
        }
        case XMLStreamConstants.COMMENT: {
            content.add(new DmComment(reader.getText()));
            break;
        }
        case XMLStreamConstants.PROCESSING_INSTRUCTION: {
            content.add(new DmProcessingInstruction(reader.getPITarget(), reader.getPIData()));
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}

From source file:org.slc.sli.modeling.wadl.reader.WadlReader.java

private static final Option readOption(final XMLStreamReader reader) throws XMLStreamException {
    assertName(WadlElementName.OPTION, reader);
    final String value = getStringAttribute(WadlAttributeName.VALUE, null, reader);
    final List<Documentation> doc = new LinkedList<Documentation>();
    while (reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            if (match(WadlElementName.DOCUMENTATION, reader)) {
                doc.add(readDocumentation(reader));
            } else {
                skipForeignNamespaceElement(reader);
            }//w w w  .ja  v a2 s .  co  m
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(WadlElementName.OPTION, reader);
            return new Option(value, doc);
        }
        case XMLStreamConstants.CHARACTERS: {
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}

From source file:org.slc.sli.modeling.wadl.reader.WadlReader.java

private static final Grammars readGrammars(final XMLStreamReader reader) throws XMLStreamException {
    assertName(WadlElementName.GRAMMARS, reader);
    final List<Documentation> documentation = new LinkedList<Documentation>();
    final List<Include> includes = new LinkedList<Include>();
    while (reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            if (match(WadlElementName.INCLUDE, reader)) {
                includes.add(readInclude(reader));
            } else {
                skipForeignNamespaceElement(reader);
            }/*w  ww . j  a  v a 2s . co  m*/
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(WadlElementName.GRAMMARS, reader);
            return new Grammars(documentation, includes);
        }
        case XMLStreamConstants.CHARACTERS: {
            // Ignore.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}

From source file:org.slc.sli.modeling.wadl.reader.WadlReader.java

private static final Include readInclude(final XMLStreamReader reader) throws XMLStreamException {
    assertName(WadlElementName.INCLUDE, reader);
    final String href = getHRef(reader);
    final List<Documentation> doc = new LinkedList<Documentation>();
    while (reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            if (match(WadlElementName.DOCUMENTATION, reader)) {
                doc.add(readDocumentation(reader));
            } else {
                skipForeignNamespaceElement(reader);
            }//from www .j  a  v a 2s . c om
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(WadlElementName.INCLUDE, reader);
            return new Include(href, doc);
        }
        case XMLStreamConstants.CHARACTERS: {
            // Ignore.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}

From source file:org.slc.sli.modeling.wadl.reader.WadlReader.java

private static final Method readMethod(final XMLStreamReader reader) throws XMLStreamException {
    assertName(WadlElementName.METHOD, reader);
    final String id = getId(reader);
    final String name = getName(reader, null);
    @SuppressWarnings("unused")
    final String href = getHRef(reader, null);
    final List<Documentation> doc = new LinkedList<Documentation>();
    Request request = null;/*from   ww w .j a v  a2 s  .com*/
    final List<Response> responses = new LinkedList<Response>();
    while (reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            if (match(WadlElementName.REQUEST, reader)) {
                request = readRequest(reader);
            } else if (match(WadlElementName.RESPONSE, reader)) {
                responses.add(readResponse(reader));
            } else if (match(WadlElementName.DOCUMENTATION, reader)) {
                doc.add(readDocumentation(reader));
            } else {
                skipForeignNamespaceElement(reader);
            }
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(WadlElementName.METHOD, reader);
            return new Method(id, name, doc, request, responses);
        }
        case XMLStreamConstants.CHARACTERS: {
            // Ignore.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}