Example usage for javax.xml.stream XMLStreamReader hasNext

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

Introduction

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

Prototype

public boolean hasNext() throws XMLStreamException;

Source Link

Document

Returns true if there are more parsing events and false if there are no more events.

Usage

From source file:org.deegree.protocol.ows.http.OwsHttpResponseTest.java

/**
 * Test method for {@link org.deegree.protocol.ows.http.OwsHttpResponse#getAsXMLStream()}.
 *//*  w  ww . ja va  2s .c  om*/
@Test
public void testGetAsXMLStreamScenario1() throws OWSExceptionReport, XMLStreamException {
    XMLStreamReader xmlStream = scenario1.getAsXMLStream();
    int i = 0;
    while (xmlStream.hasNext()) {
        xmlStream.next();
        i++;
    }
    xmlStream.close();
    assertEquals(8215, i);
}

From source file:com.gtdfree.test.XMLTest.java

public void testParserEncoding() {

    try {//from  ww  w . j  av a2 s . co m

        File file = new File("./src/test/resources/gtd-free-data_2.1.xml");
        InputStream is = new FileInputStream(file);
        XMLStreamReader r = XMLInputFactory.newInstance().createXMLStreamReader(is);
        System.out.println(r.getEncoding());
        assertEquals("UTF-8", r.getEncoding());
        while (r.hasNext()) {
            r.next();
        }
        r.close();
        is.close();

        file = new File("./src/test/resources/gtd-free-data_WIN1250_2.1.xml");
        is = new FileInputStream(file);
        r = XMLInputFactory.newInstance().createXMLStreamReader(is);
        System.out.println(r.getEncoding());
        assertEquals("UTF-8", r.getEncoding());
        try {
            while (r.hasNext()) {
                r.next();
            }
            fail("This should not happend.");
        } catch (Exception e) {
            //e.printStackTrace();
        }
        r.close();
        is.close();

        file = new File("./src/test/resources/gtd-free-data_2.1_enc.xml");
        is = new FileInputStream(file);
        r = XMLInputFactory.newInstance().createXMLStreamReader(is);
        System.out.println(r.getEncoding());
        assertEquals("UTF-8", r.getEncoding());
        while (r.hasNext()) {
            r.next();
        }
        r.close();
        is.close();

        file = new File("./src/test/resources/gtd-free-data_WIN1250_2.1_enc.xml");
        is = new FileInputStream(file);
        r = XMLInputFactory.newInstance().createXMLStreamReader(is);
        System.out.println(r.getEncoding());
        assertEquals("WINDOWS-1250", r.getEncoding());
        while (r.hasNext()) {
            r.next();
        }
        r.close();
        is.close();

    } catch (Exception e) {

        e.printStackTrace();
        fail(e.getMessage());

    }

}

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

public List<String> getIDList(String queryStr) {
    List<String> idlist = new ArrayList<String>();
    try {/*w  w w.j  ava  2 s  .  c  o  m*/
        // get num of hits
        String url = mainURL + QUERY_PREFIX + queryStr;

        System.out.println(url);

        HttpGet getRequest = new HttpGet(url);
        HttpResponse response = httpClient.execute(getRequest);
        InputStream content = response.getEntity().getContent();

        int numFound = 0;
        XMLStreamReader parser = factory.createXMLStreamReader(content);
        while (parser.hasNext()) {
            int event = parser.next();
            if (event == XMLStreamConstants.START_ELEMENT) {
                String attributeValue = parser.getAttributeValue(null, NUM_FOUND);
                if (attributeValue != null) {
                    numFound = Integer.valueOf(attributeValue);
                    break;
                }
            }
        }
        content.close();

        // 
        String idurl = mainURL + QUERY_PREFIX + queryStr + "&start=0&rows=" + numFound;
        //         String idurl = mainURL + QUERY_PREFIX + queryStr + "&start=0&rows=" + 10;
        HttpResponse idresponse = httpClient.execute(new HttpGet(idurl));
        InputStream idcontent = idresponse.getEntity().getContent();

        XMLStreamReader idparser = factory.createXMLStreamReader(idcontent);
        while (idparser.hasNext()) {
            int event = idparser.next();
            if (event == XMLStreamConstants.START_ELEMENT) {
                String attributeValue = idparser.getAttributeValue(null, "name");
                if (attributeValue != null) {
                    if (attributeValue.equals(SOLR_QUERY_ID)) {
                        String volumeId = idparser.getElementText();
                        idlist.add(volumeId);
                    }
                }
            }
        }
        idcontent.close();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (XMLStreamException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return idlist;
}

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

protected void handleProperty(XMLStreamReader reader, MultiStatusResponse multiStatusResponse)
        throws XMLStreamException {
    while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, "prop")) {
        reader.next();//from w ww. j  a v  a 2  s. c  om
        if (XMLStreamUtil.isStartTag(reader)) {
            Namespace namespace = Namespace.getNamespace(reader.getNamespaceURI());
            String tagLocalName = reader.getLocalName();
            if (reader.getAttributeCount() > 0 && "mv.string".equals(reader.getAttributeValue(0))) {
                handleMultiValuedProperty(reader, multiStatusResponse);
            } else {
                String tagContent = getTagContent(reader);
                if (tagContent != null) {
                    multiStatusResponse.add(new DefaultDavProperty(tagLocalName, tagContent, namespace));
                }
            }
        }
    }
}

From source file:com.autonomy.aci.client.services.impl.AbstractStAXProcessorTest.java

@Test(expected = XMLStreamException.class)
public void testForwardToNextStartElement() throws XMLStreamException {
    final XMLStreamReader mockXmlStreamReader = mock(XMLStreamReader.class);
    when(mockXmlStreamReader.hasNext()).thenReturn(true, true, true, true, false);
    when(mockXmlStreamReader.next()).thenReturn(XMLEvent.START_DOCUMENT, XMLEvent.START_ELEMENT,
            XMLEvent.END_ELEMENT, XMLEvent.END_DOCUMENT);

    try {//w ww .ja  va 2 s.  com
        final AbstractStAXProcessor<?> abstractStAXProcessor = spy(AbstractStAXProcessor.class);
        abstractStAXProcessor.forwardToNextStartElement(mockXmlStreamReader); // Should work...
        abstractStAXProcessor.forwardToNextStartElement(mockXmlStreamReader); // Should iterate out and thrown exception...
    } finally {
        verify(mockXmlStreamReader, times(5)).hasNext();
        verify(mockXmlStreamReader, times(4)).next();
        verifyNoMoreInteractions(mockXmlStreamReader);
    }
}

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

private Vector createVector(XMLStreamReader parser) throws XMLStreamException {
    Vector vector = new RandomAccessSparseVector(dictionary.size());
    while (parser.hasNext()) {
        int event = parser.next();
        if (event == XMLStreamConstants.START_ELEMENT) {
            String attributeValue = parser.getAttributeValue(null, "name");
            if (attributeValue != null) {
                //               if (dictionary.containsKey(attributeValue)) {
                //                  parser.next();
                //                  int tf = Integer.valueOf(parser.getElementText());
                //                  vector.setQuick(dictionary.get(attributeValue), tf);
                //               }

                parser.next();//from  ww  w .  j  a  va2 s .com
                int freq = Integer.valueOf(parser.getElementText());
                if (filter.accept(attributeValue, freq)) {
                    vector.setQuick(dictionary.get(attributeValue), freq);
                }
            }
        }
    }
    return vector;
}

From source file:com.autonomy.aci.client.services.impl.AbstractStAXProcessorTest.java

@Test(expected = XMLStreamException.class)
public void testForwardToNextStartOrEndElement() throws XMLStreamException {
    final XMLStreamReader mockXmlStreamReader = mock(XMLStreamReader.class);
    when(mockXmlStreamReader.hasNext()).thenReturn(true, true, true, true, false);
    when(mockXmlStreamReader.next()).thenReturn(XMLEvent.START_DOCUMENT, XMLEvent.START_ELEMENT,
            XMLEvent.END_ELEMENT, XMLEvent.END_DOCUMENT);

    try {//from w  w  w.j a  v  a 2s  .c om
        final AbstractStAXProcessor<?> abstractStAXProcessor = spy(AbstractStAXProcessor.class);
        abstractStAXProcessor.forwardToNextStartOrEndElement(mockXmlStreamReader); // Should work, start element...
        abstractStAXProcessor.forwardToNextStartOrEndElement(mockXmlStreamReader); // Should work, end element...
        abstractStAXProcessor.forwardToNextStartOrEndElement(mockXmlStreamReader); // Should iterate out and thrown exception...
    } finally {
        verify(mockXmlStreamReader, times(5)).hasNext();
        verify(mockXmlStreamReader, times(4)).next();
        verifyNoMoreInteractions(mockXmlStreamReader);
    }
}

From source file:com.autonomy.aci.client.services.impl.AbstractStAXProcessorTest.java

@Test(expected = XMLStreamException.class)
public void testForwardToNamedStartElement() throws XMLStreamException {
    final XMLStreamReader mockXmlStreamReader = mock(XMLStreamReader.class);
    when(mockXmlStreamReader.hasNext()).thenReturn(true, true, true, true, true, true, false);
    when(mockXmlStreamReader.next()).thenReturn(XMLEvent.START_DOCUMENT, XMLEvent.START_ELEMENT,
            XMLEvent.END_ELEMENT, XMLEvent.START_ELEMENT, XMLEvent.END_ELEMENT, XMLEvent.END_DOCUMENT);
    when(mockXmlStreamReader.getLocalName()).thenReturn("wibble", "wobble");

    try {/*from  w w  w. j av a2  s. c o  m*/
        final AbstractStAXProcessor<?> abstractStAXProcessor = spy(AbstractStAXProcessor.class);
        abstractStAXProcessor.forwardToNamedStartElement("wibble", mockXmlStreamReader); // Should work
        abstractStAXProcessor.forwardToNamedStartElement("wibble", mockXmlStreamReader); // Should iterate out and thrown exception...
    } finally {
        verify(mockXmlStreamReader, times(7)).hasNext();
        verify(mockXmlStreamReader, times(6)).next();
        verify(mockXmlStreamReader, times(2)).getLocalName();
        verifyNoMoreInteractions(mockXmlStreamReader);
    }
}

From source file:com.autonomy.aci.client.services.impl.AbstractStAXProcessor.java

/**
 * Move the cursor forward through the XML stream to the next start element.
 * @param xmlStreamReader The XML stream to use
 * @throws XMLStreamException If there was an error using the stream
 *///from  ww w  .ja v  a 2s  . c om
protected void forwardToNextStartElement(final XMLStreamReader xmlStreamReader) throws XMLStreamException {
    while (xmlStreamReader.hasNext()) {
        final int eventType = xmlStreamReader.next();
        if (XMLEvent.START_ELEMENT == eventType) {
            return;
        }
    }
    throw new XMLStreamException("No more START_ELEMENT events found");
}

From source file:com.autonomy.aci.client.services.impl.AbstractStAXProcessor.java

/**
 * Move the cursor forward through the XML stream to the next start or end element, which ever comes first.
 * @param xmlStreamReader The XML stream to use
 * @return The type of event forwarded to, i.e. <tt>XMLEvent.START_ELEMENT</tt> or <tt>XMLEvent.END_ELEMENT</tt>.
 * @throws XMLStreamException If there was an error using the stream
 *//*from  w  w w  .  ja va2  s  . co  m*/
protected int forwardToNextStartOrEndElement(final XMLStreamReader xmlStreamReader) throws XMLStreamException {
    while (xmlStreamReader.hasNext()) {
        final int eventType = xmlStreamReader.next();
        if ((XMLEvent.START_ELEMENT == eventType) || (XMLEvent.END_ELEMENT == eventType)) {
            return eventType;
        }
    }
    throw new XMLStreamException("No more START_ELEMENT or END_ELEMENT events found");
}