Example usage for javax.xml.stream XMLStreamReader getLocalName

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

Introduction

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

Prototype

public String getLocalName();

Source Link

Document

Returns the (local) name of the current event.

Usage

From source file:edu.harvard.iq.dvn.core.analysis.NetworkDataServiceBean.java

private void processXML(String fileName, NetworkDataFile ndf) throws XMLStreamException, IOException {

    File file = new File(fileName);
    FileReader fileReader = new FileReader(file);
    javax.xml.stream.XMLInputFactory xmlif = javax.xml.stream.XMLInputFactory.newInstance();
    xmlif.setProperty("javax.xml.stream.isCoalescing", java.lang.Boolean.TRUE);

    XMLStreamReader xmlr = xmlif.createXMLStreamReader(fileReader);
    for (int event = xmlr.next(); event != XMLStreamConstants.END_DOCUMENT; event = xmlr.next()) {
        if (event == XMLStreamConstants.START_ELEMENT) {

            if (xmlr.getLocalName().equals("key"))
                processKey(xmlr, ndf);//from   w  w w  . j  av a2  s  . co  m
            else if (xmlr.getLocalName().equals("graph"))
                processGraph(xmlr, ndf);

        } else if (event == XMLStreamConstants.END_ELEMENT) {
            if (xmlr.getLocalName().equals("graphml"))
                return;
        }
    }

    // If #nodes and #edges is not set, then go thru list to count them
}

From source file:com.amalto.core.load.context.StateContextSAXWriter.java

public void writeStartElement(XMLStreamReader reader) throws XMLStreamException, SAXException {
    // Namespace parsing
    Map<String, String> prefixToNamespace = Utils.parseNamespace(reader);
    Set<Map.Entry<String, String>> entries = prefixToNamespace.entrySet();
    for (Map.Entry<String, String> entry : entries) {
        contentHandler.startPrefixMapping(entry.getKey(), entry.getValue());
    }//  w w w  .j a  va  2s . c  o  m

    // Attribute parsing
    Attributes attributes = Utils.parseAttributes(reader);

    // Start new XML element
    contentHandler.startElement(getURI(reader), reader.getLocalName(), reader.getName().getLocalPart(),
            attributes);
}

From source file:edu.harvard.iq.dvn.core.analysis.NetworkDataServiceBean.java

private void processGraph(XMLStreamReader xmlr, NetworkDataFile ndf) throws XMLStreamException {
    for (int event = xmlr.next(); event != XMLStreamConstants.END_DOCUMENT; event = xmlr.next()) {
        if (event == XMLStreamConstants.START_ELEMENT) {
            if (xmlr.getLocalName().equals("node")) {
                Long caseQuantity = ndf.getVertexDataTable().getCaseQuantity();
                if (caseQuantity == null) {
                    caseQuantity = new Long(0);
                }/*from  w w w .j  av a  2  s  . c o m*/
                caseQuantity++;
                ndf.getVertexDataTable().setCaseQuantity(caseQuantity);
            } else if (xmlr.getLocalName().equals("edge")) {
                Long caseQuantity = ndf.getEdgeDataTable().getCaseQuantity();
                if (caseQuantity == null) {
                    caseQuantity = new Long(0);
                }
                caseQuantity++;
                ndf.getEdgeDataTable().setCaseQuantity(caseQuantity);
            }
        } else if (event == XMLStreamConstants.END_ELEMENT) {
            if (xmlr.getLocalName().equals("graph")) {
                ndf.getEdgeDataTable()
                        .setVarQuantity(new Long(ndf.getEdgeDataTable().getDataVariables().size()));
                ndf.getVertexDataTable()
                        .setVarQuantity(new Long(ndf.getVertexDataTable().getDataVariables().size()));
                return;
            }
        }
    }
}

From source file:fr.mobilit.neo4j.server.service.nantes.CycleRentTest.java

@BeforeClass
public void setUp() throws Exception {
    super.setUp(true);
    HttpClient client = new HttpClient();
    GetMethod get = null;/*from  ww w. j a  v a  2s . c  o  m*/
    try {
        // we do the http call and parse the xml response
        get = new GetMethod(CycleRentImpl.IMPORT_URL);
        client.executeMethod(get);
        javax.xml.stream.XMLInputFactory factory = javax.xml.stream.XMLInputFactory.newInstance();
        javax.xml.stream.XMLStreamReader parser = factory.createXMLStreamReader(get.getResponseBodyAsStream());
        ArrayList<String> currentXMLTags = new ArrayList<String>();
        int depth = 0;
        while (true) {
            int event = parser.next();
            if (event == javax.xml.stream.XMLStreamConstants.END_DOCUMENT) {
                break;
            }
            switch (event) {
            case javax.xml.stream.XMLStreamConstants.START_ELEMENT:
                currentXMLTags.add(depth, parser.getLocalName());
                String tagPath = currentXMLTags.toString();
                // here we have a match, so we construct the POI
                if (tagPath.equals("[carto, markers, marker]")) {
                    numberOfStations++;
                }
                depth++;
                break;
            case javax.xml.stream.XMLStreamConstants.END_ELEMENT:
                depth--;
                currentXMLTags.remove(depth);
                break;
            default:
                break;
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e.getCause());
    } finally {
        get.releaseConnection();
    }
}

From source file:com.norconex.collector.http.sitemap.impl.StandardSitemapResolver.java

private void parseLocation(InputStream is, HttpClient httpClient, SitemapURLAdder sitemapURLAdder,
        Set<String> resolvedLocations, String location) throws XMLStreamException {

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    inputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);
    XMLStreamReader xmlReader = inputFactory.createXMLStreamReader(is);
    ParseState parseState = new ParseState();

    String locationDir = StringUtils.substringBeforeLast(location, "/");
    int event = xmlReader.getEventType();
    while (true) {
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            String tag = xmlReader.getLocalName();
            parseStartElement(parseState, tag);
            break;
        case XMLStreamConstants.CHARACTERS:
            String value = xmlReader.getText();
            if (parseState.sitemapIndex && parseState.loc) {
                resolveLocation(value, httpClient, sitemapURLAdder, resolvedLocations);
                parseState.loc = false;//from  ww w .jav a2s .  c  o m
            } else if (parseState.baseURL != null) {
                parseCharacters(parseState, value);
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            tag = xmlReader.getLocalName();
            parseEndElement(sitemapURLAdder, parseState, locationDir, tag);
            break;
        }
        if (!xmlReader.hasNext()) {
            break;
        }
        event = xmlReader.next();
    }
}

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   ww w. j av a2s. co  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:net.bulletin.pdi.xero.step.support.XMLChunkerImpl.java

@Override
public String pullNextXmlChunk() throws KettleException {
    Stack<String> elementStack = xmlChunkerState.getElementStack();
    XMLStreamReader xmlStreamReader = xmlChunkerState.getXmlStreamReader();

    try {/*from w ww  .j  a v  a  2  s.  com*/

        while (xmlStreamReader.hasNext()) {

            switch (xmlStreamReader.next()) {

            case XMLStreamConstants.END_DOCUMENT:
                return null;

            case XMLStreamConstants.END_ELEMENT:
                elementStack.pop();
                break;

            case XMLStreamConstants.START_ELEMENT:
                elementStack.push(xmlStreamReader.getLocalName());

                if (actualElementStackHasExpectedElements(xmlChunkerState)) {
                    return pullNextXmlChunkFromTopElementOnStack(xmlChunkerState);
                }

                break;

            }

        }
    } catch (Exception e) {
        throw new KettleException("a problem has arisen reading the xero xml stream", e);
    }

    return null;
}

From source file:edu.indiana.d2i.htrc.portal.HTRCAgentClient.java

private boolean parseJobDeleteResponse(InputStream stream) throws XMLStreamException {
    XMLStreamReader parser = factory.createXMLStreamReader(stream);
    while (parser.hasNext()) {
        int event = parser.next();
        if (event == XMLStreamConstants.START_ELEMENT) {
            if (parser.hasName()) {
                String localName = parser.getLocalName();
                if (localName.equals("success"))
                    return true;
                else {
                    log.error(parser.getElementText());
                    return false;
                }//www. j a v a  2s  . co m
            }
        }
    }
    return false;
}

From source file:com.norconex.collector.http.sitemap.impl.DefaultSitemapResolver.java

private void parseLocation(InputStream is, DefaultHttpClient httpClient, SitemapURLStore sitemapURLStore,
        Set<String> resolvedLocations, String location) throws XMLStreamException {

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    inputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);
    XMLStreamReader xmlReader = inputFactory.createXMLStreamReader(is);
    ParseState parseState = new ParseState();

    String locationDir = StringUtils.substringBeforeLast(location, "/");
    int event = xmlReader.getEventType();
    while (true) {
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            String tag = xmlReader.getLocalName();
            parseStartElement(parseState, tag);
            break;
        case XMLStreamConstants.CHARACTERS:
            String value = xmlReader.getText();
            if (parseState.sitemapIndex && parseState.loc) {
                resolveLocation(value, httpClient, sitemapURLStore, resolvedLocations);
                parseState.loc = false;//ww  w.j  av a 2  s . co  m
            } else if (parseState.baseURL != null) {
                parseCharacters(parseState, value);
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            tag = xmlReader.getLocalName();
            parseEndElement(sitemapURLStore, parseState, locationDir, tag);
            break;
        }
        if (!xmlReader.hasNext()) {
            break;
        }
        event = xmlReader.next();
    }
}

From source file:com.hp.mqm.clt.XmlProcessorTest.java

private void assertXml(List<TestResult> expectedTestResults, Set<XmlElement> expectedElements, File xmlFile)
        throws FileNotFoundException, XMLStreamException {
    FileInputStream fis = new FileInputStream(xmlFile);
    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    xmlInputFactory.setProperty("javax.xml.stream.isCoalescing", true);
    XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(fis);

    boolean isFirstEvent = true;
    while (xmlStreamReader.hasNext()) {
        if (!isFirstEvent) {
            xmlStreamReader.next();//from  w w w  .j ava  2 s.c  om
        } else {
            isFirstEvent = false;
        }

        if (xmlStreamReader.getEventType() == XMLStreamReader.START_ELEMENT) {
            String localName = xmlStreamReader.getLocalName();
            if ("taxonomy".equals(localName)) {
                assertElement(localName, false, xmlStreamReader, expectedElements);
            } else if ("test_field".equals(localName)) {
                assertElement(localName, false, xmlStreamReader, expectedElements);
            } else if ("product_area_ref".equals(localName)) {
                assertElement(localName, true, xmlStreamReader, expectedElements);
            } else if ("backlog_item_ref".equals(localName)) {
                assertElement(localName, true, xmlStreamReader, expectedElements);
            } else if ("release_ref".equals(localName)) {
                assertElement(localName, true, xmlStreamReader, expectedElements);
            } else if ("test_run".equals(localName)) {
                assertXmlTest(xmlStreamReader, expectedTestResults);
            }
        }
    }
    xmlStreamReader.close();
    IOUtils.closeQuietly(fis);
    Assert.assertTrue(expectedElements.isEmpty());
    Assert.assertTrue(expectedTestResults.isEmpty());
}