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:com.microsoft.windowsazure.services.table.client.AtomPubParser.java

/**
 * Reserved for internal use. Reads the properties of an entity from the stream into a map of property names to
 * typed values. Reads the entity data as an AtomPub Entry Resource from the specified {@link XMLStreamReader} into
 * a map of <code>String</code> property names to {@link EntityProperty} data typed values.
 * /*from  w  w w. j av  a  2s .  c  om*/
 * @param xmlr
 *            The <code>XMLStreamReader</code> to read the data from.
 * @param opContext
 *            An {@link OperationContext} object used to track the execution of the operation.
 * 
 * @return
 *         A <code>java.util.HashMap</code> containing a map of <code>String</code> property names to
 *         {@link EntityProperty} data typed values found in the entity data.
 * @throws XMLStreamException
 *             if an error occurs accessing the stream.
 * @throws ParseException
 *             if an error occurs converting the input to a particular data type.
 */
protected static HashMap<String, EntityProperty> readProperties(final XMLStreamReader xmlr,
        final OperationContext opContext) throws XMLStreamException, ParseException {
    int eventType = xmlr.getEventType();
    xmlr.require(XMLStreamConstants.START_ELEMENT, null, ODataConstants.PROPERTIES);
    final HashMap<String, EntityProperty> properties = new HashMap<String, EntityProperty>();

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

        if (eventType == XMLStreamConstants.START_ELEMENT
                && xmlr.getNamespaceURI().equals(ODataConstants.DATA_SERVICES_NS)) {
            final String key = xmlr.getLocalName();
            String val = Constants.EMPTY_STRING;
            String edmType = null;

            if (xmlr.getAttributeCount() > 0) {
                edmType = xmlr.getAttributeValue(ODataConstants.DATA_SERVICES_METADATA_NS, ODataConstants.TYPE);
            }

            // move to chars
            eventType = xmlr.next();

            if (eventType == XMLStreamConstants.CHARACTERS) {
                val = xmlr.getText();

                // end element
                eventType = xmlr.next();
            }

            xmlr.require(XMLStreamConstants.END_ELEMENT, null, key);

            final EntityProperty newProp = new EntityProperty(val, EdmType.parse(edmType));
            properties.put(key, newProp);
        } else if (eventType == XMLStreamConstants.END_ELEMENT && xmlr.getName().toString()
                .equals(ODataConstants.BRACKETED_DATA_SERVICES_METADATA_NS + ODataConstants.PROPERTIES)) {
            // End read properties
            break;
        }
    }

    xmlr.require(XMLStreamConstants.END_ELEMENT, null, ODataConstants.PROPERTIES);
    return properties;
}

From source file:net.cloudkit.enterprises.ws.SuperPassQueryTest.java

public static String parsingReceiptStatus(String responseContext) throws XMLStreamException {
    String serviceResponseCode = "-1";
    XMLStreamReader reader = factory.createXMLStreamReader(new StringReader(responseContext));
    try {//from www . j ava  2  s  .c o  m
        int event = reader.getEventType();
        while (true) {
            switch (event) {
            case XMLStreamConstants.START_ELEMENT:
                // System.out.println(reader.getName());
                if (reader.getName().toString().equals("ServiceResponseCode")) {
                    // System.out.println(reader.getElementText());
                    serviceResponseCode = reader.getElementText();
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                // System.out.println("End Element:" + r.getName());
                break;
            }
            if (!reader.hasNext())
                break;
            event = reader.next();
        }
    } finally {
        reader.close();
    }
    return serviceResponseCode;
}

From source file:com.amalto.core.load.xml.SetId.java

public void parse(StateContext context, XMLStreamReader reader) throws XMLStreamException {
    int next = reader.next();
    if (next == XMLEvent.END_ELEMENT) {
        // Means we started an ID but got no text in it, so set it to empty string
        context.getMetadata().setId(context.getCurrentIdElement(), StringUtils.EMPTY);
        // ... and move to EndElement state
        context.setCurrent(EndElement.INSTANCE);
        return;//from  w w w  .j  a va 2 s . com
    } else if (next != XMLEvent.CHARACTERS) {
        // Everything else (not END_ELEMENT and not CHARACTERS is error).
        throw new IllegalStateException("Expected characters but got XML event id #" + next);
    }

    // We're parsing characters so call super.parse(context, reader)...
    super.parse(context, reader);
    // ...and we're also setting id for metadata
    context.getMetadata().setId(context.getCurrentIdElement(), reader.getText());
    // If we're ready, flush document
    if (doFlush(context)) {
        Utils.doParserCallback(context, reader, context.getMetadata());
    }
}

From source file:com.amalto.core.load.xml.Selector.java

public void parse(StateContext context, XMLStreamReader reader) throws XMLStreamException {
    int next = reader.next();

    switch (next) {
    case XMLEvent.START_ELEMENT:
        context.setCurrent(StartElement.INSTANCE);
        break;/*from  w w  w.j  a  v  a 2  s  .  c om*/
    case XMLEvent.END_ELEMENT:
        /*
         * Switch to EndPayload only if we're at the level of begin payload.
         * If the document has the following structure:
         *
         * <root>
         *    <root>...</root>
         * </root>
         *
         * We want to end the payload for the top level <root> element, hence the depth == 1 condition.
         */
        if (context.getDepth() == 1
                && context.getPayLoadElementName().equals(reader.getName().getLocalPart())) {
            context.setCurrent(EndPayload.INSTANCE);
        } else {
            context.setCurrent(EndElement.INSTANCE);
        }
        break;
    case XMLEvent.CHARACTERS:
        context.setCurrent(Characters.INSTANCE);
        break;
    case XMLEvent.END_DOCUMENT:
        context.setCurrent(End.INSTANCE);
        break;
    case XMLEvent.COMMENT:
        // Ignore comment storage in MDM
        break;
    default:
        // Nothing to do?
        throw new NotImplementedException("Support for event id #" + next);
    }
}

From source file:org.javelin.sws.ext.bind.SoapEncodingMarshaller.java

@Override
public Object unmarshal(Source source) throws IOException, XmlMappingException {
    try {// ww w .java  2  s .c o  m
        XMLStreamReader streamReader = StaxUtils.getXMLStreamReader(source);
        streamReader.next();
        return streamReader.getElementText();
    } catch (XMLStreamException e) {
        throw new UnmarshallingFailureException(e.getMessage(), e);
    }
}

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. ja  va 2 s .  co m
            }
            event = xmlStreamReader.next();
        }
    }

    return slide;
}

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);
                    }/*from w  ww  . j av a2  s .c  o m*/
                }
            }
            event = xmlStreamReader.next();
        }
    }

    return presentation;
}

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));
                }//  ww w . ja v a  2 s .c  om
                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.predic8.membrane.core.config.AbstractXmlElement.java

protected void parseChildren(XMLStreamReader token, String child) throws Exception {
    int count = 0;
    while (true) { // ignore child
        token.next();
        if (token.isEndElement() && child.equals(token.getName().getLocalPart())) {
            if (count == 0)
                return;
            count--;//ww  w.j  ava 2  s.  c  om
        } else if (token.isStartElement() && child.equals(token.getName().getLocalPart())) {
            count++;
        }
    }
}

From source file:com.lin.umws.service.impl.UserServiceImpl.java

public User login(String username, String password) throws UserException {
    System.out.println("spring: " + springService);
    HeaderList headerList = (HeaderList) cxt.getMessageContext()
            .get(JAXWSProperties.INBOUND_HEADER_LIST_PROPERTY);
    Header header = headerList.get(new QName("http://service.umws.lin.com/", "licenseType"), true);
    try {//from w w w.j a v  a2 s .c om
        XMLStreamReader reader = header.readHeader();
        while (reader.hasNext()) {
            int type = reader.next();
            if (type == XMLStreamReader.CHARACTERS) {
                System.out.println(reader.getText());
            }
        }
    } catch (XMLStreamException e) {
        e.printStackTrace();
    }

    for (User user : userMap.values()) {
        if (username.equals(user.getUsername()) && password.equals(user.getPassword())) {
            return user;
        }
    }

    throw new UserException("?");
}