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.maodian.flyingcat.xmpp.state.StreamState.java

private void doHandle(XmppContext context, XMLStreamReader xmlsr, XMLStreamWriter xmlsw)
        throws XMLStreamException {
    xmlsr.nextTag();/*from   w ww . j av  a2 s  .  c  o m*/
    QName qname = new QName(XmppNamespace.STREAM, "stream");
    if (!xmlsr.getName().equals(qname)) {
        throw new XmppException(StreamError.INVALID_NAMESPACE).set("QName", xmlsr.getName());
    }

    // throw exception if client version > 1.0
    BigDecimal version = new BigDecimal(xmlsr.getAttributeValue("", "version"));
    if (version.compareTo(SUPPORTED_VERSION) > 0) {
        throw new XmppException(StreamError.UNSUPPORTED_VERSION);
    }

    xmlsw.writeStartDocument();
    xmlsw.writeStartElement("stream", "stream", XmppNamespace.STREAM);
    xmlsw.writeNamespace("stream", XmppNamespace.STREAM);
    xmlsw.writeDefaultNamespace(XmppNamespace.CLIENT_CONTENT);

    xmlsw.writeAttribute("id", RandomStringUtils.randomAlphabetic(32));
    xmlsw.writeAttribute("version", "1.0");
    xmlsw.writeAttribute("from", "localhost");
    xmlsw.writeAttribute(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI, "lang", "en");
    String from = xmlsr.getAttributeValue(null, "from");
    if (from != null) {
        xmlsw.writeAttribute("to", from);
    }

    // features
    xmlsw.writeStartElement(XmppNamespace.STREAM, "features");
    writeFeatures(xmlsw);
    xmlsw.writeEndElement();
}

From source file:edu.utah.further.core.xml.stax.XmlStreamElementPrinter.java

@Override
public void visitStartElement(final XMLStreamReader xmlReader) {
    XmlUtil.printElement(os, xmlReader, true);
    if (rootElement == null && depth == 1) {
        rootElement = xmlReader.getName();
    }/*  ww  w.  j av a2s  .c  o m*/
    depth++;
}

From source file:jodtemplate.pptx.io.xml.SlideXmlRelsReader.java

@Override
public Slide read(final String path, final Resources resources, final XMLInputFactory xmlInputFactory,
        final Slide slide) throws XMLStreamException, IOException {
    final Resource slideXmlRelsRes = resources.getResource(Utils.removePrefixSeparator(path));
    try (final InputStream is = slideXmlRelsRes.getInputStream()) {
        final XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(is);
        int event = xmlStreamReader.next();
        while (event != XMLStreamConstants.END_DOCUMENT) {
            if (xmlStreamReader.getEventType() == XMLStreamConstants.START_ELEMENT) {
                final String elementNS = xmlStreamReader.getName().getNamespaceURI();
                final String elementName = xmlStreamReader.getName().getLocalPart();
                if (OOXMLDocument.RELATIONSHIP_ELEMENT.equals(elementName)
                        && OOXMLDocument.RELATIONSHIPS_RELS_NAMESPACE.equals(elementNS)) {
                    final Relationship relationship = createRelationshipElement(xmlStreamReader);
                    slide.addOtherRelationship(relationship);
                }/*from  www  .j a  v a2s. c  o  m*/
            }
            event = xmlStreamReader.next();
        }
    }

    return slide;
}

From source file:com.predic8.membrane.core.config.AbstractXmlElement.java

/**
 * Needed to resolve interceptor IDs into interceptor beans
 *///from w w  w .  j ava  2  s. c  o  m

public XMLElement parse(XMLStreamReader token) throws Exception {
    move2RootElementIfNeeded(token);
    log.debug("<" + token.getLocalName() + ">");
    parseAttributes(token);
    while (token.hasNext()) {
        token.next();
        if (token.isStartElement()) {
            parseChildren(token, token.getName().getLocalPart());
        } else if (token.isCharacters()) {
            parseCharacters(token);
        } else if (token.isEndElement()) {
            log.debug("</" + token.getLocalName() + ">");
            break;
        }
    }
    doAfterParsing();
    return this;
}

From source file:edu.utah.further.core.xml.xquery.UTestXQueryServiceBaseX.java

/**
 * Execute a test XQuery document and put the results in a Stream.
 * //  www  . j av  a  2s .c o m
 * @throws XMLStreamException
 */
@Test
public void executeXQueryIntoStream() throws XMLStreamException {
    final XMLStreamReader result = xQueryService.executeIntoStream(testXQuery, testXmlDocument,
            new HashMap<String, String>());
    assertNotNull(result);

    String elementResult = null;
    while (result.hasNext()) {
        if (result.next() == XMLStreamReader.START_ELEMENT && result.getName().getLocalPart() == "message") {
            elementResult = result.getElementText();
        }
    }

    assertNotNull(elementResult);
    assertThat(elementResult, is("Hello World!"));
}

From source file:jodtemplate.pptx.io.xml.PresentationXmlRelsReader.java

@Override
public Presentation read(final String path, final Resources resources, final XMLInputFactory xmlInputFactory,
        final Presentation presentation) throws XMLStreamException, IOException {
    final Resource presentationXmlRelsRes = resources.getResource(Utils.removePrefixSeparator(path));
    try (final InputStream is = presentationXmlRelsRes.getInputStream()) {
        final XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(is);
        int event = xmlStreamReader.next();
        while (event != XMLStreamConstants.END_DOCUMENT) {
            if (xmlStreamReader.getEventType() == XMLStreamConstants.START_ELEMENT) {
                final String elementNS = xmlStreamReader.getName().getNamespaceURI();
                final String elementName = xmlStreamReader.getName().getLocalPart();
                if (OOXMLDocument.RELATIONSHIP_ELEMENT.equals(elementName)
                        && OOXMLDocument.RELATIONSHIPS_RELS_NAMESPACE.equals(elementNS)) {
                    final Relationship relationship = createRelationshipElement(xmlStreamReader);
                    if (Relationship.SLIDE_TYPE.equals(relationship.getType())) {
                        final Slide slide = new Slide();
                        slide.setRelationship(relationship);
                        slide.setPresentation(presentation);
                        presentation.addSlide(slide);
                    } else {
                        presentation.addOtherRelationship(relationship);
                    }//  w w w.  j av  a2 s  .  co  m
                }
            }
            event = xmlStreamReader.next();
        }
    }

    return presentation;
}

From source file:org.maodian.flyingcat.xmpp.extensions.xep0077.RegistrationCodec.java

@Override
public Object decode(XMLStreamReader xmlsr) {
    try {/*  w  ww  .j  a va2 s .c o m*/
        xmlsr.require(XMLStreamConstants.START_ELEMENT, InBandRegistration.REGISTER, "query");
        String username = null;
        String password = null;

        while (xmlsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
            if (xmlsr.getName().equals(new QName(InBandRegistration.REGISTER, "username"))) {
                username = xmlsr.getElementText();
            } else if (xmlsr.getName().equals(new QName(InBandRegistration.REGISTER, "password"))) {
                password = xmlsr.getElementText();
            }
        }
        return new Registration(username, password);
    } catch (XMLStreamException e) {
        throw new XmppException(e, StreamError.INVALID_XML);
    }
}

From source file:jodtemplate.io.xml.ContentTypesReader.java

@Override
public ContentTypes read(final String path, final Resources resources, final XMLInputFactory xmlInputFactory,
        final ContentTypes contentTypes) throws XMLStreamException, IOException {
    final Resource contentTypesRes = resources.getResource(Utils.removePrefixSeparator(path));
    try (final InputStream is = contentTypesRes.getInputStream()) {
        final XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(is);
        int event = xmlStreamReader.next();
        while (event != XMLStreamConstants.END_DOCUMENT) {
            if (xmlStreamReader.getEventType() == XMLStreamConstants.START_ELEMENT) {
                final String elementNS = xmlStreamReader.getName().getNamespaceURI();
                final String elementName = xmlStreamReader.getName().getLocalPart();
                if (OOXMLDocument.DEFAULT_ELEMENT.equals(elementName)
                        && OOXMLDocument.CONTENT_TYPES_NAMESPACE.equals(elementNS)) {
                    contentTypes.addDefaultElement(createDefaultElement(xmlStreamReader));
                }//from w  ww  .  j  a v a 2  s .co m
                if (OOXMLDocument.OVERRIDE_ELEMENT.equals(elementName)
                        && OOXMLDocument.CONTENT_TYPES_NAMESPACE.equals(elementNS)) {
                    contentTypes.addOverrideElement(createOverrideElement(xmlStreamReader));
                }
            }
            event = xmlStreamReader.next();
        }
    }

    return contentTypes;
}

From source file:com.microsoft.windowsazure.services.table.client.AtomPubParser.java

/**
 * Reserved for internal use. Parses the operation response as an entity. Parses the result returned in the
 * specified stream in AtomPub format into a {@link TableResult} containing an entity of the specified class type
 * projected using the specified resolver.
 * //from   ww  w. j a v a  2 s  . c om
 * @param xmlr
 *            An <code>XMLStreamReader</code> on the input stream.
 * @param clazzType
 *            The class type <code>T</code> implementing {@link TableEntity} for the entity returned. Set to
 *            <code>null</code> to ignore the returned entity and copy only response properties into the
 *            {@link TableResult} object.
 * @param resolver
 *            An {@link EntityResolver} instance to project the entity into an instance of type <code>R</code>. Set
 *            to <code>null</code> to return the entity as an instance of the class type <code>T</code>.
 * @param opContext
 *            An {@link OperationContext} object used to track the execution of the operation.
 * @return
 *         A {@link TableResult} containing the parsed entity result of the operation.
 * 
 * @throws XMLStreamException
 *             if an error occurs while accessing the stream.
 * @throws ParseException
 *             if an error occurs while parsing the stream.
 * @throws InstantiationException
 *             if an error occurs while constructing the result.
 * @throws IllegalAccessException
 *             if an error occurs in reflection while parsing the result.
 * @throws StorageException
 *             if a storage service error occurs.
 */
protected static <T extends TableEntity, R> TableResult parseEntity(final XMLStreamReader xmlr,
        final Class<T> clazzType, final EntityResolver<R> resolver, final OperationContext opContext)
        throws XMLStreamException, ParseException, InstantiationException, IllegalAccessException,
        StorageException {
    int eventType = xmlr.getEventType();
    final TableResult res = new TableResult();

    xmlr.require(XMLStreamConstants.START_ELEMENT, null, ODataConstants.ENTRY);

    res.setEtag(StringEscapeUtils.unescapeHtml4(
            xmlr.getAttributeValue(ODataConstants.DATA_SERVICES_METADATA_NS, ODataConstants.ETAG)));

    while (xmlr.hasNext()) {
        eventType = xmlr.next();
        if (eventType == XMLStreamConstants.CHARACTERS) {
            xmlr.getText();
            continue;
        }

        final String name = xmlr.getName().toString();

        if (eventType == XMLStreamConstants.START_ELEMENT) {
            if (name.equals(ODataConstants.BRACKETED_ATOM_NS + ODataConstants.ID)) {
                res.setId(Utility.readElementFromXMLReader(xmlr, ODataConstants.ID));
            } else if (name
                    .equals(ODataConstants.BRACKETED_DATA_SERVICES_METADATA_NS + ODataConstants.PROPERTIES)) {
                // Do read properties
                if (resolver == null && clazzType == null) {
                    return res;
                } else {
                    res.setProperties(readProperties(xmlr, opContext));
                    break;
                }
            }
        }
    }

    // Move to end Content
    eventType = xmlr.next();
    if (eventType == XMLStreamConstants.CHARACTERS) {
        eventType = xmlr.next();
    }
    xmlr.require(XMLStreamConstants.END_ELEMENT, null, ODataConstants.CONTENT);

    eventType = xmlr.next();
    if (eventType == XMLStreamConstants.CHARACTERS) {
        eventType = xmlr.next();
    }

    xmlr.require(XMLStreamConstants.END_ELEMENT, null, ODataConstants.ENTRY);

    String rowKey = null;
    String partitionKey = null;
    Date timestamp = null;

    // Remove core properties from map and set individually
    EntityProperty tempProp = res.getProperties().get(TableConstants.PARTITION_KEY);
    if (tempProp != null) {
        res.getProperties().remove(TableConstants.PARTITION_KEY);
        partitionKey = tempProp.getValueAsString();
    }

    tempProp = res.getProperties().get(TableConstants.ROW_KEY);
    if (tempProp != null) {
        res.getProperties().remove(TableConstants.ROW_KEY);
        rowKey = tempProp.getValueAsString();
    }

    tempProp = res.getProperties().get(TableConstants.TIMESTAMP);
    if (tempProp != null) {
        res.getProperties().remove(TableConstants.TIMESTAMP);
        timestamp = tempProp.getValueAsDate();
    }

    if (resolver != null) {
        // Call resolver
        res.setResult(resolver.resolve(partitionKey, rowKey, timestamp, res.getProperties(), res.getEtag()));
    } else if (clazzType != null) {
        // Generate new entity and return
        final T entity = clazzType.newInstance();
        entity.setEtag(res.getEtag());

        entity.setPartitionKey(partitionKey);
        entity.setRowKey(rowKey);
        entity.setTimestamp(timestamp);

        entity.readEntity(res.getProperties(), opContext);

        res.setResult(entity);
    }

    return res;
}

From source file:com.amalto.core.load.context.StateContextSAXWriter.java

public void writeEndElement(XMLStreamReader reader) throws XMLStreamException, SAXException {
    Map<String, String> prefixToNamespace = Utils.parseNamespace(reader);
    Set<Map.Entry<String, String>> entries = prefixToNamespace.entrySet();
    for (Map.Entry<String, String> entry : entries) {
        contentHandler.endPrefixMapping(entry.getKey());
    }//from  www  .  j a  v a2  s  . c o m

    contentHandler.endElement(getURI(reader), reader.getLocalName(), reader.getName().getLocalPart());
}