Example usage for javax.xml.stream XMLStreamReader nextTag

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

Introduction

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

Prototype

public int nextTag() throws XMLStreamException;

Source Link

Document

Skips any white space (isWhiteSpace() returns true), COMMENT, or PROCESSING_INSTRUCTION, until a START_ELEMENT or END_ELEMENT is reached.

Usage

From source file:Main.java

public static void readStartTag(XMLStreamReader reader, String localName) throws XMLStreamException {
    reader.nextTag();
    reader.require(XMLStreamConstants.START_ELEMENT, null, localName);
}

From source file:Main.java

public static void readEndTag(XMLStreamReader reader, String localName) throws XMLStreamException {
    reader.nextTag();
    reader.require(XMLStreamConstants.END_ELEMENT, null, localName);
}

From source file:Main.java

private static void skipStartDocument(XMLStreamReader parser) throws XMLStreamException {
    while (!parser.isStartElement()) {
        parser.nextTag();
    }//w  w  w  .j a v a 2s. c  o  m
}

From source file:it.govpay.core.utils.client.SOAPUtils.java

public static Object unmarshal(InputStream is, Schema schema)
        throws JAXBException, SAXException, IOException, XMLStreamException {

    XMLStreamReader xsr = xif.createXMLStreamReader(is);

    boolean isBody = false;
    while (!isBody) {
        xsr.nextTag();
        if (xsr.isStartElement()) {
            String local = xsr.getLocalName();
            isBody = local.equals("Body");
        }/*from ww w  .j ava  2  s  .  c o  m*/
    }

    xsr.nextTag();
    if (!xsr.isStartElement()) {
        // Body vuoto
        return null;
    } else {
        return JaxbUtils.unmarshal(xsr, schema);
    }
}

From source file:Main.java

/**
 * Determines if the given stream contains XML content. The stream will be
 * buffered and reset if necessary./*from  ww  w. ja  va2  s .co m*/
 * 
 * @param stream
 *            The InputStream to read.
 * @return true if the stream contains XML content; false otherwise.
 */
public static boolean isXML(InputStream stream) {
    if (!stream.markSupported()) {
        stream = new BufferedInputStream(stream, 1024);
    }
    stream.mark(1024);
    byte[] bytes = new byte[1024];
    try {
        try {
            stream.read(bytes);
        } finally {
            stream.reset();
        }
    } catch (IOException iox) {
        throw new RuntimeException("Failed to read or reset stream. " + iox.getMessage());
    }
    try {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader reader = factory.createXMLStreamReader(new ByteArrayInputStream(bytes));
        // If XML, now in START_DOCUMENT state; seek document element.
        reader.nextTag();
    } catch (XMLStreamException xse) {
        return false;
    }
    return true;
}

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

@Override
public Bind decode(XMLStreamReader xmlsr) {
    try {//from   w w w .  j a v a  2  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.RosterCodec.java

@Override
public Object decode(XMLStreamReader xmlsr) {
    try {/* w w w. j a v a  2 s.  c o  m*/
        xmlsr.nextTag();
        // check end element for empty tag
        // xmlsr.require(XMLStreamConstants.END_ELEMENT, XmppNamespace.ROSTER, "query");
        return new Roster();
    } catch (XMLStreamException e) {
        throw new XmppException(e, StreamError.INVALID_XML);
    }
}

From source file:org.maodian.flyingcat.xmpp.state.SelectState.java

@Override
public Result step(XmppContext context, String xml) {
    String fragment = context.wrapStreamTag(xml);
    try (Reader reader = new StringReader(fragment);) {
        try {//from w  w  w .j av a  2 s. c o  m
            XMLStreamReader xmlsr = XMLInputFactoryHolder.getXMLInputFactory().createXMLStreamReader(reader);
            // skip stream tag
            xmlsr.nextTag();
            xmlsr.nextTag();
            Decoder decoder = context.getGlobalContext().getDecoder(xmlsr.getName());
            ElementVisitee elem = (ElementVisitee) decoder.decode(xmlsr);
            State nextState = elem.acceptElementVisitor(context, elementVisitor);
            Result result = new DefaultResult(nextState);
            return result;
        } catch (XMLStreamException e) {
            throw new XmppException(e, StreamError.BAD_FORMAT);
        }
    } catch (IOException ioe) {
        // close a StringReader/StringWriter should not cause IOException, though
        throw new XmppException(ioe, StreamError.INTERNAL_SERVER_ERROR);
    }
}

From source file:org.maodian.flyingcat.xmpp.state.StreamState.java

private void doHandle(XmppContext context, XMLStreamReader xmlsr, XMLStreamWriter xmlsw)
        throws XMLStreamException {
    xmlsr.nextTag();
    QName qname = new QName(XmppNamespace.STREAM, "stream");
    if (!xmlsr.getName().equals(qname)) {
        throw new XmppException(StreamError.INVALID_NAMESPACE).set("QName", xmlsr.getName());
    }//from www. j a va  2s  .  c  om

    // 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:hudson.model.ExternalRun.java

/**
 * Instead of performing a build, accept the log and the return code from a
 * remote machine.//from   w w w  . j  av  a 2s.  c o  m
 *
 * <p> The format of the XML is:
 *
 * <pre><xmp>
 * <run>
 *  <log>...console output...</log>
 *  <result>exit code</result>
 * </run>
 * </xmp></pre>
 */
@SuppressWarnings({ "Since15" })
public void acceptRemoteSubmission(final Reader in) throws IOException {
    final long[] duration = new long[1];
    run(new Runner() {
        private String elementText(XMLStreamReader r) throws XMLStreamException {
            StringBuilder buf = new StringBuilder();
            while (true) {
                int type = r.next();
                if (type == CHARACTERS || type == CDATA) {
                    buf.append(r.getTextCharacters(), r.getTextStart(), r.getTextLength());
                } else {
                    return buf.toString();
                }
            }
        }

        public Result run(BuildListener listener) throws Exception {
            PrintStream logger = null;
            try {
                logger = new PrintStream(new DecodingStream(listener.getLogger()));

                XMLInputFactory xif = XMLInputFactory.newInstance();
                XMLStreamReader p = xif.createXMLStreamReader(in);

                p.nextTag(); // get to the <run>
                p.nextTag(); // get to the <log>

                charset = p.getAttributeValue(null, "content-encoding");
                while (p.next() != END_ELEMENT) {
                    int type = p.getEventType();
                    if (type == CHARACTERS || type == CDATA) {
                        logger.print(p.getText());
                    }
                }
                p.nextTag(); // get to <result>

                Result r = Integer.parseInt(elementText(p)) == 0 ? Result.SUCCESS : Result.FAILURE;

                p.nextTag(); // get to <duration> (optional)
                if (p.getEventType() == START_ELEMENT && p.getLocalName().equals("duration")) {
                    duration[0] = Long.parseLong(elementText(p));
                }

                return r;
            } finally {
                IOUtils.closeQuietly(logger);
            }
        }

        public void post(BuildListener listener) {
            // do nothing
        }

        public void cleanUp(BuildListener listener) {
            // do nothing
        }
    });

    if (duration[0] != 0) {
        super.duration = duration[0];
        // save the updated duration
        save();
    }
}