Example usage for javax.xml.stream XMLStreamReader getElementText

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

Introduction

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

Prototype

public String getElementText() throws XMLStreamException;

Source Link

Document

Reads the content of a text-only element, an exception is thrown if this is not a text-only element.

Usage

From source file:com.flexive.chemistry.webdav.TextDocumentResource.java

/**
 * Set a new name for the object, stream points to the start of the name tag.
 *
 * @param parser    the XML parser//from   w  ww . j  a v  a  2 s  .c o  m
 * @throws XMLStreamException   on parsing errors
 */
protected void processName(XMLStreamReader parser) throws XMLStreamException {
    final String name = parser.getElementText().trim();
    if (!name.equals(object.getName())) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Setting new name: " + name);
        }
        try {
            object.setName(name);
        } catch (CMISException e) {
            throw CMISExceptionWrapper.wrap(e);
        }
    }
}

From source file:org.maodian.flyingcat.xmpp.codec.BindCodec.java

@Override
public Bind decode(XMLStreamReader xmlsr) {
    try {/* w  w w .  jav  a2  s .  c om*/
        xmlsr.nextTag();
        xmlsr.require(XMLStreamConstants.START_ELEMENT, XmppNamespace.BIND, "resource");
        Bind bind = new Bind();
        bind.setResource(xmlsr.getElementText());
        return bind;
    } catch (XMLStreamException e) {
        throw new XmppException(e, StreamError.INVALID_XML);
    }
}

From source file:org.maodian.flyingcat.xmpp.codec.SASLCodec.java

@Override
public Object decode(XMLStreamReader xmlsr) {
    String mechanism = xmlsr.getAttributeValue("", "mechanism");
    if (StringUtils.equals("PLAIN", mechanism)) {
        String base64Data = null;
        try {/*  www  .j  a  va2s.  co m*/
            base64Data = xmlsr.getElementText();
        } catch (XMLStreamException e) {
            throw new RuntimeException(e);
        }

        if (!Base64.isBase64(base64Data)) {
            throw new XmppException(SASLError.INCORRECT_ENCODING);
        }

        byte[] value = Base64.decodeBase64(base64Data);
        String text = new String(value, StandardCharsets.UTF_8);

        // apply PLAIN SASL mechanism whose rfc locates at http://tools.ietf.org/html/rfc4616
        int[] nullPosition = { -1, -1 };
        int nullIndex = 0;
        for (int i = 0; i < text.length(); ++i) {
            if (text.codePointAt(i) == 0) {
                // a malicious base64 value may contain more than two null character
                if (nullIndex > 1) {
                    throw new XmppException(SASLError.MALFORMED_REQUEST);
                }
                nullPosition[nullIndex++] = i;
            }
        }

        if (nullPosition[0] == -1 || nullPosition[1] == -1) {
            throw new XmppException("The format is invalid", SASLError.MALFORMED_REQUEST);
        }
        String authzid = StringUtils.substring(text, 0, nullPosition[0]);
        String authcid = StringUtils.substring(text, nullPosition[0] + 1, nullPosition[1]);
        String password = StringUtils.substring(text, nullPosition[1] + 1);

        if (authzid.getBytes(StandardCharsets.UTF_8).length > 255
                || authcid.getBytes(StandardCharsets.UTF_8).length > 255
                || password.getBytes(StandardCharsets.UTF_8).length > 255) {
            throw new XmppException(
                    "authorization id, authentication id and password should be equal or less than 255 bytes",
                    SASLError.MALFORMED_REQUEST);
        }
        return new Auth(authzid, authcid, password);
    } else {
        throw new XmppException(SASLError.INVALID_MECHANISM).set("mechanism", mechanism);
    }
}

From source file:com.predic8.membrane.core.interceptor.balancer.XMLElementSessionIdExtractor.java

@Override
public String getSessionId(Message msg) throws Exception {
    if (!msg.isXML()) {
        log.debug("Didn't search a XML element in none XML message.");
        return null;
    }//from w w w.  j  a v  a 2 s  .  c  om

    log.debug("searching for sessionid");

    fac.setProperty("javax.xml.stream.isNamespaceAware", namespace != null);
    XMLStreamReader reader = new FixedStreamReader(
            fac.createXMLStreamReader(msg.getBodyAsStreamDecoded(), msg.getCharset()));
    while (reader.hasNext()) {
        reader.next();
        if (isSessionIdElement(reader)) {
            log.debug("sessionid element found");
            return reader.getElementText();
        }

    }

    log.debug("no sessionid element found");
    return null;
}

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

/**
 * Execute a test XQuery document and put the results in a Stream.
 * //  w w w .j  a  va 2 s  . 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:org.maodian.flyingcat.xmpp.extensions.xep0077.RegistrationCodec.java

@Override
public Object decode(XMLStreamReader xmlsr) {
    try {/*  w w w.  java 2s  .  com*/
        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:com.pocketsoap.salesforce.soap.ChatterClient.java

private RuntimeException handleSoapFault(XMLStreamReader rdr) throws XMLStreamException {
    String fc = null, fs = null;/*w  w  w.  ja  v a2  s.c  om*/
    while (rdr.next() != XMLStreamReader.END_DOCUMENT) {
        if (rdr.getEventType() == XMLStreamReader.START_ELEMENT) {
            String ln = rdr.getLocalName();
            if (ln.equals("faultcode"))
                fc = rdr.getElementText();
            else if (ln.equals("faultstring"))
                fs = rdr.getElementText();
        }
    }
    return new SoapFaultException(fc, fs);
}

From source file:edu.indiana.d2i.htrc.io.index.solr.SolrClient.java

private NamedVector parseOneVolume(InputStream content) throws XMLStreamException, IOException {
    //      java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(content));
    //      String line = "";
    //      while ((line = br.readLine()) != null) {
    //         System.out.println(line);
    //      }//from w w w.  j  av  a 2s .  c  o m
    //      br.close();

    String volumeID = null;
    Vector vector = null;
    XMLStreamReader parser = factory.createXMLStreamReader(content);
    while (parser.hasNext()) {
        int event = parser.next();
        if (event == XMLStreamConstants.START_ELEMENT) {
            String attributeValue = parser.getAttributeValue(null, "name");
            if (attributeValue != null) {
                if (attributeValue.equals(VOLUME_ID)) {
                    volumeID = parser.getElementText();
                    volumeID = pairtree.uncleanId(volumeID);
                } else if (attributeValue.equals(VOLUME_OCR)) {
                    vector = createVector(parser);
                    break;
                }
            }
        }
    }

    NamedVector tv = new NamedVector(vector, volumeID);
    return tv;
}

From source file:davmail.exchange.dav.ExchangePropPatchMethod.java

protected void handleResponse(XMLStreamReader reader) throws XMLStreamException {
    String href = null;/*  w  w  w  .  j av a 2  s . c o m*/
    String responseStatus = "";
    while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, "response")) {
        reader.next();
        if (XMLStreamUtil.isStartTag(reader)) {
            String tagLocalName = reader.getLocalName();
            if ("href".equals(tagLocalName)) {
                href = reader.getElementText();
            } else if ("status".equals(tagLocalName)) {
                responseStatus = reader.getElementText();
            } else if ("propstat".equals(tagLocalName)) {
                MultiStatusResponse multiStatusResponse = new MultiStatusResponse(href, responseStatus);
                handlePropstat(reader, multiStatusResponse);
                responses.add(multiStatusResponse);
            }
        }
    }

}

From source file:davmail.exchange.dav.ExchangeDavMethod.java

protected void handlePropstat(XMLStreamReader reader, MultiStatusResponse multiStatusResponse)
        throws XMLStreamException {
    int propstatStatus = 0;
    while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, "propstat")) {
        reader.next();/* w  w  w  .  j  a  va2  s  .c om*/
        if (XMLStreamUtil.isStartTag(reader)) {
            String tagLocalName = reader.getLocalName();
            if ("status".equals(tagLocalName)) {
                if ("HTTP/1.1 200 OK".equals(reader.getElementText())) {
                    propstatStatus = HttpStatus.SC_OK;
                } else {
                    propstatStatus = 0;
                }
            } else if ("prop".equals(tagLocalName) && propstatStatus == HttpStatus.SC_OK) {
                handleProperty(reader, multiStatusResponse);
            }
        }
    }

}