Example usage for javax.xml.stream XMLEventReader nextEvent

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

Introduction

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

Prototype

public XMLEvent nextEvent() throws XMLStreamException;

Source Link

Document

Gets the next XMLEvent.

Usage

From source file:org.jonross.coercion.v2.BinderSpec.java

private Block parse(XMLEventReader reader, StartElement start) throws XMLStreamException {
    Block block = new Block(start.getName(), blockId++);
    String lastText = "";

    while (reader.hasNext()) {
        XMLEvent e = reader.nextEvent();
        switch (e.getEventType()) {
        case XMLEvent.START_ELEMENT:
            if (block.children.size() == 0)
                block.setPrologue(lastText);
            block.children.add(parse(reader, e.asStartElement()));
            lastText = "";
            break;

        case XMLEvent.CHARACTERS:
            lastText += e.asCharacters().getData();
            break;

        case XMLEvent.END_ELEMENT:
            block.setEpilogue(lastText);
            if (log.isDebugEnabled())
                log.debug("Completed " + block);
            return block;

        default:/* www  .  j ava  2s.co m*/
            lastText = "";
            break;
        }
    }

    // Shouldn't get here, END_ELEMENT above should return.
    throw new IllegalStateException("Coercer.parse impossible state, probably malformed XML");
}

From source file:org.jonross.coercion.v2.BinderSpec.java

private StartElement nextStartElement(XMLEventReader reader) throws XMLStreamException {
    while (reader.hasNext()) {
        XMLEvent e = reader.nextEvent();
        if (e.getEventType() == XMLEvent.START_ELEMENT)
            return e.asStartElement();
    }//from   ww  w  .  j a v  a 2 s .  c o m
    return null;
}

From source file:org.jwifisd.eyefi.Main.java

private static void getPhotoStatus(Request request, Response response, final String date)
        throws FactoryConfigurationError, XMLStreamException, IOException {
    String macaddress = null;/*from www. j  a  va 2s . c o m*/
    String credential = null;
    String filename = null;
    String filesize = null;
    String filesignature = null;
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLEventReader eventReader = inputFactory.createXMLEventReader(request.getInputStream());
    while (eventReader.hasNext()) {
        XMLEvent event = eventReader.nextEvent();
        if (event.isStartElement()) {
            if (event.asStartElement().getName().getLocalPart().equals("macaddress")) {
                event = eventReader.nextEvent();
                macaddress = event.asCharacters().getData();
                continue;
            }
            if (event.asStartElement().getName().getLocalPart().equals("credential")) {
                event = eventReader.nextEvent();
                credential = event.asCharacters().getData();
                continue;
            }
            if (event.asStartElement().getName().getLocalPart().equals("filename")) {
                event = eventReader.nextEvent();
                filename = event.asCharacters().getData();
                continue;
            }
            if (event.asStartElement().getName().getLocalPart().equals("filesize")) {
                event = eventReader.nextEvent();
                filesize = event.asCharacters().getData();
                continue;
            }
            if (event.asStartElement().getName().getLocalPart().equals("filesignature")) {
                event = eventReader.nextEvent();
                filesignature = event.asCharacters().getData();
                continue;
            }
        }
    }
    System.out.println("macaddress:" + macaddress);
    System.out.println("credential:" + credential);
    System.out.println("filename:" + filename);
    System.out.println("filesize:" + filesize);
    System.out.println("filesignature:" + filesignature);

    response.setHeader("Server", "Eye-Fi Agent/2.0.4.0");
    response.setHeader("Date", date);
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Content-Type", "text/xml; charset=\"utf-8\"");

    String responseText = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + //
            "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">" + //
            /**/"<SOAP-ENV:Body>\n" + //
            /*  */"<GetPhotoStatusResponse xmlns=\"http://localhost/api/soap/eyefilm\">" + //
            /*    */"<fileid>1</fileid><offset>0</offset>" + //
            /*  */"</GetPhotoStatusResponse>" + //
            /**/"</SOAP-ENV:Body>" + //
            "</SOAP-ENV:Envelope>";

    response.setContentLength(responseText.length());
    response.getWriter().write(responseText);
}

From source file:org.jwifisd.eyefi.Main.java

private static void startSession(Request request, Response response, final String date)
        throws Exception, IOException {
    String macaddress = null;/* ww w  .j a  v  a  2 s.c  om*/
    String cnonce = null;
    String transfermode = null;
    String transfermodetimestamp = null;
    System.out.println("connectx");
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLEventReader eventReader = inputFactory.createXMLEventReader(request.getInputStream());
    while (eventReader.hasNext()) {
        XMLEvent event = eventReader.nextEvent();
        if (event.isStartElement()) {
            if (event.asStartElement().getName().getLocalPart().equals("macaddress")) {
                event = eventReader.nextEvent();
                macaddress = event.asCharacters().getData();
                continue;
            }
            if (event.asStartElement().getName().getLocalPart().equals("cnonce")) {
                event = eventReader.nextEvent();
                cnonce = event.asCharacters().getData();
                continue;
            }
            if (event.asStartElement().getName().getLocalPart().equals("transfermode")) {
                event = eventReader.nextEvent();
                transfermode = event.asCharacters().getData();
                continue;
            }
            if (event.asStartElement().getName().getLocalPart().equals("transfermodetimestamp")) {
                event = eventReader.nextEvent();
                transfermodetimestamp = event.asCharacters().getData();
                continue;
            }
        }
    }
    System.out.println("macaddress:" + macaddress);
    System.out.println("cnonce:" + cnonce);
    System.out.println("transfermode:" + transfermode);
    System.out.println("transfermodetimestamp:" + transfermodetimestamp);
    byte[] cnonceBytes = Hex.decodeHex(cnonce.toCharArray());
    byte[] uploadKey = Hex.decodeHex("00000000000000000000000000000000".toCharArray());
    byte[] credential = md5(Hex.decodeHex(macaddress.toCharArray()), cnonceBytes, uploadKey);
    String credentialStr = Hex.encodeHexString(credential);

    response.setHeader("Server", "Eye-Fi Agent/2.0.4.0");
    response.setHeader("Date", date);
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Content-Type", "text/xml; charset=\"utf-8\"");

    String responseText = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + //
            "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">" + //
            /**/"<SOAP-ENV:Body>" + //
            /*  */"<StartSessionResponse xmlns=\"http://localhost/api/soap/eyefilm\">" + //
            /*    */"<credential>" + credentialStr + "</credential>" + //
            /*    */"<snonce>" + cnonce + "</snonce>" + //
            /*    */"<transfermode>2</transfermode>" + //
            /*    */"<transfermodetimestamp>1230268824</transfermodetimestamp>" + //
            /*    */"<upsyncallowed>false</upsyncallowed>" + //
            /*  */"</StartSessionResponse>" + //
            /**/"</SOAP-ENV:Body>" + //
            "</SOAP-ENV:Envelope>";
    System.out.println(responseText);
    response.setContentLength(responseText.length());
    response.getWriter().write(responseText);
}

From source file:org.keycloak.saml.common.util.StaxParserUtilTest.java

@Test
public void testBypassElementBlock() throws XMLStreamException, ParsingException {
    String xml = "<a><b><c>test</c>" + "<d>aa</d></b></a>";
    XMLEventReader reader = StaxParserUtil
            .getXMLEventReader(IOUtils.toInputStream(xml, Charset.defaultCharset()));

    assertThat(reader.nextEvent(), instanceOf(StartDocument.class));

    assertStartTag(reader.nextEvent(), "a");
    assertStartTag(reader.nextEvent(), "b");
    assertStartTag(reader.nextEvent(), "c");
    assertCharacters(reader.nextEvent(), is("test"));
    assertEndTag(reader.nextEvent(), "c");

    StaxParserUtil.bypassElementBlock(reader, "d");

    assertEndTag(reader.nextEvent(), "b");
    assertEndTag(reader.nextEvent(), "a");
}

From source file:org.keycloak.saml.common.util.StaxParserUtilTest.java

@Test
public void testBypassElementBlockAnon() throws XMLStreamException, ParsingException {
    String xml = "<a><b><c>test</c>" + "<d>aa</d></b></a>";
    XMLEventReader reader = StaxParserUtil
            .getXMLEventReader(IOUtils.toInputStream(xml, Charset.defaultCharset()));

    assertThat(reader.nextEvent(), instanceOf(StartDocument.class));

    assertStartTag(reader.nextEvent(), "a");
    assertStartTag(reader.nextEvent(), "b");
    assertStartTag(reader.nextEvent(), "c");
    assertCharacters(reader.nextEvent(), is("test"));
    assertEndTag(reader.nextEvent(), "c");

    StaxParserUtil.bypassElementBlock(reader);

    assertEndTag(reader.nextEvent(), "b");
    assertEndTag(reader.nextEvent(), "a");
}

From source file:org.keycloak.saml.common.util.StaxParserUtilTest.java

@Test
public void testBypassElementBlockNested() throws XMLStreamException, ParsingException {
    String xml = "<a><b><c>test</c>" + "<d>aa<d>nestedD</d></d></b></a>";
    XMLEventReader reader = StaxParserUtil
            .getXMLEventReader(IOUtils.toInputStream(xml, Charset.defaultCharset()));

    assertThat(reader.nextEvent(), instanceOf(StartDocument.class));

    assertStartTag(reader.nextEvent(), "a");
    assertStartTag(reader.nextEvent(), "b");
    assertStartTag(reader.nextEvent(), "c");
    assertCharacters(reader.nextEvent(), is("test"));
    assertEndTag(reader.nextEvent(), "c");

    StaxParserUtil.bypassElementBlock(reader, "d");

    assertEndTag(reader.nextEvent(), "b");
    assertEndTag(reader.nextEvent(), "a");
}

From source file:org.keycloak.saml.common.util.StaxParserUtilTest.java

@Test
public void testBypassElementBlockNestedAnon() throws XMLStreamException, ParsingException {
    String xml = "<a><b><c>test</c>" + "<d>aa<d>nestedD</d></d></b></a>";
    XMLEventReader reader = StaxParserUtil
            .getXMLEventReader(IOUtils.toInputStream(xml, Charset.defaultCharset()));

    assertThat(reader.nextEvent(), instanceOf(StartDocument.class));

    assertStartTag(reader.nextEvent(), "a");
    assertStartTag(reader.nextEvent(), "b");
    assertStartTag(reader.nextEvent(), "c");
    assertCharacters(reader.nextEvent(), is("test"));
    assertEndTag(reader.nextEvent(), "c");

    StaxParserUtil.bypassElementBlock(reader);

    assertEndTag(reader.nextEvent(), "b");
    assertEndTag(reader.nextEvent(), "a");
}

From source file:org.keycloak.saml.common.util.StaxParserUtilTest.java

@Test
public void testBypassElementBlockWrongPairing() throws XMLStreamException, ParsingException {
    String xml = "<a><b><c>test</c>" + "<d><b>aa</d><d>nestedD</d></d></b></a>";
    XMLEventReader reader = StaxParserUtil
            .getXMLEventReader(IOUtils.toInputStream(xml, Charset.defaultCharset()));

    assertThat(reader.nextEvent(), instanceOf(StartDocument.class));

    assertStartTag(reader.nextEvent(), "a");
    assertStartTag(reader.nextEvent(), "b");
    assertStartTag(reader.nextEvent(), "c");
    assertCharacters(reader.nextEvent(), is("test"));
    assertEndTag(reader.nextEvent(), "c");

    expectedException.expect(ParsingException.class);
    StaxParserUtil.bypassElementBlock(reader, "d");
}

From source file:org.keycloak.saml.common.util.StaxParserUtilTest.java

@Test
public void testBypassElementBlockNestedPrematureEnd() throws XMLStreamException, ParsingException {
    String xml = "<a><b><c>test</c>" + "<d>aa<d>nestedD</d></d>";
    XMLEventReader reader = StaxParserUtil
            .getXMLEventReader(IOUtils.toInputStream(xml, Charset.defaultCharset()));

    assertThat(reader.nextEvent(), instanceOf(StartDocument.class));

    assertStartTag(reader.nextEvent(), "a");
    assertStartTag(reader.nextEvent(), "b");
    assertStartTag(reader.nextEvent(), "c");
    assertCharacters(reader.nextEvent(), is("test"));
    assertEndTag(reader.nextEvent(), "c");

    StaxParserUtil.bypassElementBlock(reader, "d");

    expectedException.expect(XMLStreamException.class);
    reader.nextEvent();/* w w  w .j  a  va2  s. co m*/
}