Example usage for javax.xml.stream XMLStreamReader getEventType

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

Introduction

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

Prototype

public int getEventType();

Source Link

Document

Returns an integer code that indicates the type of the event the cursor is pointing to.

Usage

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  w  w  w .ja v  a  2s.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//from w w w .j  a  v a  2 s  .  co m
 * @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;//from   w  w  w  .  j a v  a  2 s. c  o  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 www  .  j a va2 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();/*from   w  w  w .  j a v  a  2s.com*/
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            content.add(readMixedElement(reader));
            break;
        }
        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();//  w  w w  .  j  av  a2  s .c  o  m
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            if (match(WadlElementName.DOCUMENTATION, reader)) {
                doc.add(readDocumentation(reader));
            } else {
                skipForeignNamespaceElement(reader);
            }
            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();// w w  w  .  j  a  v a  2  s.c o m
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            if (match(WadlElementName.INCLUDE, reader)) {
                includes.add(readInclude(reader));
            } else {
                skipForeignNamespaceElement(reader);
            }
            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();/*from   w  w w . j  a va  2 s. c  o m*/
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            if (match(WadlElementName.DOCUMENTATION, reader)) {
                doc.add(readDocumentation(reader));
            } else {
                skipForeignNamespaceElement(reader);
            }
            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;//w w w. j  a v a  2 s. co  m
    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();
}

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

private static final DmElement readMixedElement(final XMLStreamReader reader) throws XMLStreamException {
    final QName name = reader.getName();
    final List<DmNode> children = new LinkedList<DmNode>();
    while (reader.hasNext()) {
        reader.next();/*from   w  w  w  .  ja v  a2 s.c o  m*/
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            children.add(readMixedElement(reader));
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            if (name.equals(reader.getName())) {
                return new DmElement(name, children);
            } else {
                throw new AssertionError(reader.getLocalName());
            }
        }
        case XMLStreamConstants.CHARACTERS: {
            // Assume that the element will coalesce text nodes.
            children.add(new DmText(reader.getText()));
            break;
        }
        case XMLStreamConstants.COMMENT: {
            // Ignore for now.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}