Example usage for javax.xml.stream XMLEventReader nextTag

List of usage examples for javax.xml.stream XMLEventReader nextTag

Introduction

In this page you can find the example usage for javax.xml.stream XMLEventReader nextTag.

Prototype

public XMLEvent nextTag() throws XMLStreamException;

Source Link

Document

Skips any insignificant space events until a START_ELEMENT or END_ELEMENT is reached.

Usage

From source file:org.qi4j.valueserialization.stax.StaxValueDeserializer.java

@Override
protected Node readObjectTree(XMLEventReader input) throws Exception {
    XMLEvent peek = input.peek();
    if (peek.isStartElement() && "null".equals(peek.asStartElement().getName().getLocalPart())) {
        input.nextTag();// <null>
        input.nextTag();// </null>
        return null;
    }/*  ww w . ja  va 2  s  . com*/
    String elementBody = readElementBody(input);
    Transformer transformer = transformerFactory.newTransformer();
    DOMResult domResult = new DOMResult();
    transformer.transform(new StreamSource(new StringReader(elementBody)), domResult);
    return ((Document) domResult.getNode()).getDocumentElement();

}

From source file:org.sakaiproject.nakamura.auth.cas.CasAuthenticationHandler.java

private String retrieveCredentials(String responseBody) {
    String username = null;//from  w w w .  jav  a  2  s . c o  m
    String pgtIou = null;
    String failureCode = null;
    String failureMessage = null;

    try {
        XMLInputFactory xmlInputFactory = new WstxInputFactory();
        xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);
        xmlInputFactory.setProperty(XMLInputFactory.IS_VALIDATING, false);
        xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
        XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(new StringReader(responseBody));

        while (eventReader.hasNext()) {
            XMLEvent event = eventReader.nextEvent();

            // process the event if we're starting an element
            if (event.isStartElement()) {
                StartElement startEl = event.asStartElement();
                QName startElName = startEl.getName();
                String startElLocalName = startElName.getLocalPart();
                LOGGER.debug(responseBody);

                /*
                 * Example of failure XML
                <cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
                  <cas:authenticationFailure code='INVALID_REQUEST'>
                    &#039;service&#039; and &#039;ticket&#039; parameters are both required
                  </cas:authenticationFailure>
                </cas:serviceResponse>
                */
                if ("authenticationFailure".equalsIgnoreCase(startElLocalName)) {
                    // get code of the failure
                    Attribute code = startEl.getAttributeByName(QName.valueOf("code"));
                    failureCode = code.getValue();

                    // get the message of the failure
                    event = eventReader.nextEvent();
                    assert event.isCharacters();
                    Characters chars = event.asCharacters();
                    failureMessage = chars.getData();
                    break;
                }

                /*
                 * Example of success XML
                <cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
                  <cas:authenticationSuccess>
                    <cas:user>NetID</cas:user>
                  </cas:authenticationSuccess>
                </cas:serviceResponse>
                */
                if ("authenticationSuccess".equalsIgnoreCase(startElLocalName)) {
                    // skip to the user tag start
                    while (eventReader.hasNext()) {
                        event = eventReader.nextTag();
                        if (event.isEndElement()) {
                            if (eventReader.hasNext()) {
                                event = eventReader.nextTag();
                            } else {
                                break;
                            }
                        }
                        assert event.isStartElement();
                        startEl = event.asStartElement();
                        startElName = startEl.getName();
                        startElLocalName = startElName.getLocalPart();
                        if (proxy && "proxyGrantingTicket".equals(startElLocalName)) {
                            event = eventReader.nextEvent();
                            assert event.isCharacters();
                            Characters chars = event.asCharacters();
                            pgtIou = chars.getData();
                            LOGGER.debug("XML parser found pgt: {}", pgtIou);
                        } else if ("user".equals(startElLocalName)) {
                            // move on to the body of the user tag
                            event = eventReader.nextEvent();
                            assert event.isCharacters();
                            Characters chars = event.asCharacters();
                            username = chars.getData();
                            LOGGER.debug("XML parser found user: {}", username);
                        } else {
                            LOGGER.error("Found unexpected element [{}] while inside 'authenticationSuccess'",
                                    startElName);
                            break;
                        }
                        if (username != null && (!proxy || pgtIou != null)) {
                            break;
                        }
                    }
                }
            }
        }
    } catch (XMLStreamException e) {
        LOGGER.error(e.getMessage(), e);
    }

    if (failureCode != null || failureMessage != null) {
        LOGGER.error("Error response from server code={} message={}", failureCode, failureMessage);
    }
    String pgt = pgts.get(pgtIou);
    if (pgt != null) {
        savePgt(username, pgt, pgtIou);
    } else {
        LOGGER.debug("Caching '{}' as the IOU for '{}'", pgtIou, username);
        pgtIOUs.put(pgtIou, username);
    }
    return username;
}

From source file:org.sakaiproject.nakamura.auth.cas.CasAuthenticationHandler.java

private String getProxyTicketFromXml(String responseBody) {
    String ticket = null;/*  w  w  w .  ja  v a2 s . c  o  m*/

    try {
        XMLInputFactory xmlInputFactory = new WstxInputFactory();
        xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);
        xmlInputFactory.setProperty(XMLInputFactory.IS_VALIDATING, false);
        xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
        XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(new StringReader(responseBody));
        LOGGER.debug(responseBody);

        while (eventReader.hasNext()) {
            XMLEvent event = eventReader.nextEvent();

            // process the event if we're starting an element
            if (event.isStartElement()) {
                StartElement startEl = event.asStartElement();
                QName startElName = startEl.getName();
                String startElLocalName = startElName.getLocalPart();

                // Example XML
                // <cas:serviceResponse>
                // <cas:proxySuccess>
                // <cas:proxyTicket>PT-957-ZuucXqTZ1YcJw81T3dxf</cas:proxyTicket>
                // </cas:proxySuccess>
                // </cas:serviceResponse>

                if ("proxySuccess".equalsIgnoreCase(startElLocalName)) {
                    event = eventReader.nextTag();
                    assert event.isStartElement();
                    startEl = event.asStartElement();
                    startElName = startEl.getName();
                    startElLocalName = startElName.getLocalPart();
                    if ("proxyTicket".equalsIgnoreCase(startElLocalName)) {
                        event = eventReader.nextEvent();
                        assert event.isCharacters();
                        Characters chars = event.asCharacters();
                        ticket = chars.getData();
                    } else {
                        LOGGER.error("Found unexpected element [{}] while inside 'proxySuccess'", startElName);
                        break;
                    }
                }
            }
        }
    } catch (XMLStreamException e) {
        LOGGER.error(e.getMessage(), e);
    }
    return ticket;
}

From source file:org.zuinnote.hadoop.office.format.common.parser.msexcel.internal.EncryptedCachedDiskStringsTable.java

/**
 * Parses a string item from a SST in XML format
 * //w w w . ja  v  a2 s .  c  o  m
 * @param xer XMLEventReader from which to read the next string item
 * @throws XMLStreamException           in case the string item cannot be
 *                                      correctly read from the XML file
 * @throws FormatNotUnderstoodException in case a string item cannot be
 *                                      identified in the shared string table
 *                                      (e.g. unknown type)
 */
private String parseSIText(XMLEventReader xer) throws XMLStreamException, FormatNotUnderstoodException {
    String result = "";
    XMLEvent xe;
    while ((xe = xer.nextTag()).isStartElement()) {
        String elementName = xe.asStartElement().getName().getLocalPart().toUpperCase();
        switch (elementName) {
        case "T": // normal text
            result = xer.getElementText();
            break;
        case "R": // rich text (returned as normal text)
            result = this.parseSIRichText(xer);
            break;
        case "RPH": // phonetic (ignored)
        case "PHONETICPR": // phonetic properties (ignored)
            this.skipXMLElementHierarchy(xer);
            break;
        default:
            LOG.error("Unknown string item in shared string table: " + elementName);
            throw new FormatNotUnderstoodException(
                    "Unknown string item in shared string table: " + elementName);

        }
    }
    return result;
}

From source file:org.zuinnote.hadoop.office.format.common.parser.msexcel.internal.EncryptedCachedDiskStringsTable.java

/**
 * Parses a rich text item of a shared string table and returns the unformatted
 * text//from   w  w  w.ja v  a  2s  . c  o m
 * 
 * @param xer
 * @return unformatted text of rich text item
 * @throws FormatNotUnderstoodException
 * @throws XMLStreamException
 */
private String parseSIRichText(XMLEventReader xer) throws XMLStreamException, FormatNotUnderstoodException {
    String result = "";
    XMLEvent xe;
    while ((xe = xer.nextTag()).isStartElement()) {
        String elementName = xe.asStartElement().getName().getLocalPart().toUpperCase();
        switch (elementName) {
        case "T": // normal text
            result = xer.getElementText();
            break;
        case "RPR": // run properties (ignored)
        default:
            LOG.error("Unknown rich text string item in shared string table: " + elementName);
            throw new FormatNotUnderstoodException(
                    "Unknown rich text string item in shared string table: " + elementName);

        }
    }

    return result;
}

From source file:org.zuinnote.hadoop.office.format.common.parser.msexcel.internal.EncryptedCachedDiskStringsTable.java

/**
 * //from   ww w . j  a v  a 2 s  . c o m
 * Skips over an arbitrary deep hierarchy of XML tags
 * 
 * @param xer XMLEventReader from which the tags should be skipped
 * @throws XMLStreamException
 */
private void skipXMLElementHierarchy(XMLEventReader xer) throws XMLStreamException {
    while (xer.nextTag().isStartElement()) {
        skipXMLElementHierarchy(xer);
    }
}

From source file:org.zuinnote.hadoop.office.format.common.parser.msexcel.internal.XSSFPullParser.java

/**
 * Parses an inline string from cell in XML format
 * //from  w  w  w . j  a  v a2  s  .co m
 * @param xer XMLEventReader from which to read the inline string content
 * @throws XMLStreamException           in case the string item cannot be
 *                                      correctly read from the XML file
 * @throws FormatNotUnderstoodException in case a string cannot be identified in
 *                                      cell
 */
private String parseCellInlineStringText(XMLEventReader xer)
        throws XMLStreamException, FormatNotUnderstoodException {
    String result = "";
    XMLEvent xe;
    while ((xe = xer.nextTag()).isStartElement()) {
        String elementName = xe.asStartElement().getName().getLocalPart().toUpperCase();
        switch (elementName) {
        case "T": // normal text
            result = xer.getElementText();
            break;
        case "R": // rich text (returned as normal text)
            result = this.parseCellInlineStringRichText(xer);
            break;
        case "RPH": // phonetic (ignored)
        case "PHONETICPR": // phonetic properties (ignored)
            this.skipXMLElementHierarchy(xer);
            break;
        default:
            LOG.error("Unknown inline string tag: " + elementName);
            throw new FormatNotUnderstoodException("Unknown inline string tag: " + elementName);

        }
    }
    return result;
}

From source file:org.zuinnote.hadoop.office.format.common.parser.msexcel.internal.XSSFPullParser.java

/**
 * Parses a rich text item of a shared string table and returns the unformatted
 * text//from w ww  .  j  av a 2 s  .c o  m
 * 
 * @param xer
 * @return unformatted text of rich text item
 * @throws FormatNotUnderstoodException
 * @throws XMLStreamException
 */
private String parseCellInlineStringRichText(XMLEventReader xer)
        throws XMLStreamException, FormatNotUnderstoodException {
    String result = "";
    XMLEvent xe;
    while ((xe = xer.nextTag()).isStartElement()) {
        String elementName = xe.asStartElement().getName().getLocalPart().toUpperCase();
        switch (elementName) {
        case "T": // normal text
            result = xer.getElementText();
            break;
        case "RPR": // run properties (ignored)
        default:
            LOG.error("Unknown rich text inline string tag: " + elementName);
            throw new FormatNotUnderstoodException("Unknown rich text inline string tag: " + elementName);

        }
    }

    return result;
}