Example usage for javax.xml.stream XMLStreamReader getName

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

Introduction

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

Prototype

public QName getName();

Source Link

Document

Returns a QName for the current START_ELEMENT or END_ELEMENT event

Usage

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

/**
 * Reads individual elements inside of a XML Document
 * @param reader xml reader/*from ww  w .jav 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 DmElement readMixedElement(final XMLStreamReader reader) throws XMLStreamException {
    final QName name = reader.getName();
    final List<DmNode> children = new LinkedList<DmNode>();
    while (reader.hasNext()) {
        reader.next();/*  w w  w .j  a v  a 2s .  c  om*/
        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();
}

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

private static final void skipForeignNamespaceElement(final XMLStreamReader reader) throws XMLStreamException {
    final String namespaceURI = reader.getNamespaceURI();
    if (!WadlSyntax.NAMESPACE.equals(namespaceURI)) {
        skipElementReally(reader);/*w  w  w .  ja v a 2  s. c om*/
    } else {
        throw new AssertionError(reader.getName());
    }
}

From source file:org.slc.sli.modeling.xmi.comp.XmiMappingReader.java

private static final XmiFeature readFeature(final QName elementName, final XMLStreamReader reader)
        throws XMLStreamException {
    assertStartElement(reader);/*from w  w w.  ja  v a  2s . c om*/
    assertName(elementName, reader);
    String className = null;
    String name = null;
    boolean exists = true;
    boolean classExists = true;
    boolean done = false;
    while (!done && reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            if (match(XmiMappingConstants.NAME, reader)) {
                name = readStringContent(reader.getName(), reader);
            } else if (match(XmiMappingConstants.OWNER_NAME, reader)) {
                className = readStringContent(reader.getName(), reader);
            } else if (match(XmiMappingConstants.EXISTS, reader)) {
                exists = readBooleanContent(reader.getName(), reader, true);
            } else if (match(XmiMappingConstants.OWNER_EXISTS, reader)) {
                classExists = readBooleanContent(reader.getName(), reader, true);
            } else {
                throw new AssertionError(reader.getLocalName());
            }
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(elementName, reader);
            done = true;
            break;
        }
        case XMLStreamConstants.CHARACTERS:
        case XMLStreamConstants.COMMENT: {
            // Ignore.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    return new XmiFeature(name, exists, className, classExists);
}

From source file:org.slc.sli.modeling.xmi.comp.XmiMappingReader.java

private static final XmiMapping readMapping(final XMLStreamReader reader) throws XMLStreamException {
    assertStartElement(reader);/*from ww w .  j a v a  2s . c o m*/
    assertName(XmiMappingConstants.MAPPING, reader);
    XmiFeature lhs = null;
    XmiFeature rhs = null;
    XmiMappingStatus status = null;
    String comment = null;
    String tracking = "";
    boolean done = false;
    while (!done && reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            if (match(XmiMappingConstants.LHS_FEATURE, reader)) {
                if (lhs == null) {
                    lhs = readFeature(reader.getName(), reader);
                } else {
                    throw new XMLStreamException("duplicate " + XmiMappingConstants.LHS_FEATURE + " tag.");
                }
            } else if (match(XmiMappingConstants.RHS_FEATURE, reader)) {
                rhs = readFeature(reader.getName(), reader);
            } else if (match(XmiMappingConstants.STATUS, reader)) {
                status = readStatus(reader);
            } else if (match(XmiMappingConstants.COMMENT, reader)) {
                comment = readStringContent(reader.getName(), reader);
            } else if (match(XmiMappingConstants.TRACKING, reader)) {
                tracking = readStringContent(reader.getName(), reader);
            } else if (match(XmiMappingConstants.LHS_MISSING, reader)) {
                XMLStreamReaderTools.skipElement(reader);
            } else if (match(XmiMappingConstants.RHS_MISSING, reader)) {
                XMLStreamReaderTools.skipElement(reader);
            } else {
                throw new AssertionError(reader.getLocalName());
            }
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(XmiMappingConstants.MAPPING, reader);
            done = true;
            break;
        }
        case XMLStreamConstants.CHARACTERS:
        case XMLStreamConstants.COMMENT: {
            // Ignore.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    return new XmiMapping(lhs, rhs, status, tracking, comment);
}

From source file:org.slc.sli.modeling.xmi.comp.XmiMappingReader.java

private static final XmiDefinition readModel(final XMLStreamReader reader) throws XMLStreamException {
    assertStartElement(reader);//from   w  w  w. j  ava  2 s .co m
    final QName elementName = reader.getName();
    assertName(elementName, reader);
    String name = null;
    String version = null;
    String xmi = null;
    boolean done = false;
    while (!done && reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            if (match(XmiMappingConstants.NAME, reader)) {
                name = readStringContent(reader.getName(), reader);
            } else if (match(XmiMappingConstants.VERSION, reader)) {
                version = readStringContent(reader.getName(), reader);
            } else if (match(XmiMappingConstants.FILE, reader)) {
                xmi = readStringContent(reader.getName(), reader);
            } else {
                throw new AssertionError(reader.getLocalName());
            }
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(elementName, reader);
            done = true;
            break;
        }
        case XMLStreamConstants.CHARACTERS:
        case XMLStreamConstants.COMMENT: {
            // Ignore.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    return new XmiDefinition(name, version, xmi);
}

From source file:org.slc.sli.modeling.xmi.reader.XmiReader.java

protected static final boolean match(final QName name, final XMLStreamReader reader) {
    return name.equals(reader.getName());
}

From source file:org.slc.sli.modeling.xmi.reader.XmiReader.java

protected static final AssociationEnd readAssociationEnd(final XMLStreamReader reader) {
    try {// www .ja  v a2  s.  c  o  m
        assertName(XmiElementName.ASSOCIATION_END, reader);
        final List<TaggedValue> taggedValues = new LinkedList<TaggedValue>();
        final Identifier id = getId(reader);
        final String name = getName(reader, DEFAULT_EMPTY_NAME, XmiAttributeName.NAME);
        final String associatedAttribName = getName(reader, DEFAULT_EMPTY_NAME,
                XmiAttributeName.ASSOCIATED_ATTRIBUTE_NAME);
        final boolean isNavigable = getBoolean(XmiAttributeName.IS_NAVIGABLE, true, reader);
        Identifier participant = null;
        final Range range = new Range(Identifier.random(), Occurs.ONE, Occurs.ONE, EMPTY_TAGGED_VALUE_LIST);
        Multiplicity multiplicity = new Multiplicity(Identifier.random(), EMPTY_TAGGED_VALUE_LIST, range);
        while (reader.hasNext()) {
            reader.next();
            switch (reader.getEventType()) {
            case XMLStreamConstants.START_ELEMENT: {
                if (match(XmiElementName.MODEL_ELEMENT_DOT_TAGGED_VALUE, reader)) {
                    taggedValues.addAll(readTaggedValueGroup(reader));
                } else if (match(XmiElementName.ASSOCIATION_END_DOT_MULTIPLICITY, reader)) {
                    multiplicity = assertNotNull(readAssociationEndMultiplicity(reader));
                } else if (match(XmiElementName.ASSOCIATION_END_DOT_PARTICIPANT, reader)) {
                    participant = readAssociationEndParticipant(reader);
                } else {
                    throw new UnsupportedOperationException(reader.getName().toString());
                    // skipElement(reader);
                }
                break;
            }
            case XMLStreamConstants.END_ELEMENT: {
                assertName(XmiElementName.ASSOCIATION_END, reader);
                if (participant == null) {
                    throw new AssertionError(XmiElementName.ASSOCIATION_END_DOT_PARTICIPANT);
                }
                return new AssociationEnd(multiplicity, name, isNavigable, id, taggedValues, participant,
                        associatedAttribName);
            }
            case XMLStreamConstants.CHARACTERS: {
                // Ignore.
                break;
            }
            default: {
                throw new AssertionError(reader.getEventType());
            }
            }
        }
    } catch (final XMLStreamException e) {
        throw new XmiRuntimeException(e);
    }
    throw new AssertionError();
}

From source file:org.slc.sli.modeling.xmi.reader.XmiReader.java

/**
 * The UML:Namespace.ownedElement is simply a list of model elements that can belong in a
 * name-space.//from www .  ja v a2 s  .  com
 *
 * It has no identity of its own and is only present in the serialization format.
 */
protected static final List<NamespaceOwnedElement> readNamespaceOwnedElement(final XMLStreamReader reader)
        throws XMLStreamException {
    assertName(XmiElementName.NAMESPACE_DOT_OWNED_ELEMENT, reader);
    final List<NamespaceOwnedElement> ownedElements = new LinkedList<NamespaceOwnedElement>();
    while (reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            if (match(XmiElementName.CLASS, reader)) {
                ownedElements.add(assertNotNull(readClassType(reader.getName(), reader)));
            } else if (match(XmiElementName.ASSOCIATION_CLASS, reader)) {
                ownedElements.add(assertNotNull(readClassType(reader.getName(), reader)));
            } else if (match(XmiElementName.ASSOCIATION, reader)) {
                ownedElements.add(assertNotNull(readClassType(reader.getName(), reader)));
            } else if (match(XmiElementName.DATA_TYPE, reader)) {
                ownedElements.add(assertNotNull(readDataType(reader)));
            } else if (match(XmiElementName.ENUMERATION, reader)) {
                ownedElements.add(assertNotNull(readEnumeration(reader)));
            } else if (match(XmiElementName.TAG_DEFINITION, reader)) {
                ownedElements.add(assertNotNull(readTagDefinition(reader)));
            } else if (match(XmiElementName.GENERALIZATION, reader)) {
                ownedElements.add(assertNotNull(readGeneralization(reader)));
            } else if (match(XmiElementName.COMMENT, reader)) {
                skipElement(reader, false);
            } else if (match(XmiElementName.PACKAGE, reader)) {
                ownedElements.add(assertNotNull(readPackage(reader)));
            } else {
                throw new AssertionError(reader.getName());
                // skipElement(reader);
            }
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(XmiElementName.NAMESPACE_DOT_OWNED_ELEMENT, reader);
            return Collections.unmodifiableList(ownedElements);
        }
        case XMLStreamConstants.CHARACTERS: {
            // Ignore.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}

From source file:org.slc.sli.modeling.xmi.reader.XmiReader.java

/**
 * Skips (recursively) over the element in question. Also useful during development.
 *
 * @param reader//from  ww w  .  j a va  2 s. com
 *            The StAX {@link XMLStreamReader}.
 */
protected static final void skipElement(final XMLStreamReader reader, final boolean check)
        throws XMLStreamException {
    if (check) {
        throw new AssertionError(reader.getName());
    }
    final String localName = reader.getLocalName();
    while (reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            skipElement(reader, check);
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            if (localName.equals(reader.getLocalName())) {
                return;
            } else {
                throw new AssertionError(reader.getLocalName());
            }
        }
        case XMLStreamConstants.CHARACTERS: {
            // Ignore.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}