Search in sources :

Example 6 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project spring-framework by spring-projects.

the class StaxEventXMLReaderTests method partial.

@Test
public void partial() throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(CONTENT));
    // skip to root
    eventReader.nextTag();
    StaxEventXMLReader xmlReader = new StaxEventXMLReader(eventReader);
    ContentHandler contentHandler = mock(ContentHandler.class);
    xmlReader.setContentHandler(contentHandler);
    xmlReader.parse(new InputSource());
    verify(contentHandler).startDocument();
    verify(contentHandler).startElement(eq("http://springframework.org/spring-ws"), eq("child"), eq("child"), any(Attributes.class));
    verify(contentHandler).endElement("http://springframework.org/spring-ws", "child", "child");
    verify(contentHandler).endDocument();
}
Also used : InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) Attributes(org.xml.sax.Attributes) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) ContentHandler(org.xml.sax.ContentHandler) Test(org.junit.Test)

Example 7 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project spring-framework by spring-projects.

the class ListBasedXMLEventReaderTests method readEvents.

private List<XMLEvent> readEvents(String xml) throws XMLStreamException {
    XMLEventReader reader = this.inputFactory.createXMLEventReader(new StringReader(xml));
    List<XMLEvent> events = new ArrayList<>();
    while (reader.hasNext()) {
        events.add(reader.nextEvent());
    }
    return events;
}
Also used : StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader)

Example 8 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project camel by apache.

the class ScrHelper method getScrProperties.

public static Map<String, String> getScrProperties(String xmlLocation, String componentName) throws Exception {
    Map<String, String> result = new HashMap<>();
    XMLInputFactory inputFactory = XMLInputFactory.newFactory();
    inputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);
    XMLEventReader eventReader = inputFactory.createXMLEventReader(new FileReader(xmlLocation));
    boolean collect = false;
    while (eventReader.hasNext()) {
        XMLEvent event = eventReader.nextEvent();
        if (event.getEventType() == XMLStreamConstants.START_ELEMENT && event.asStartElement().getName().toString().equals("scr:component") && event.asStartElement().getAttributeByName(QName.valueOf("name")).getValue().equals(componentName)) {
            collect = true;
        } else if (collect && event.getEventType() == XMLStreamConstants.START_ELEMENT && event.asStartElement().getName().toString().equals("property")) {
            result.put(event.asStartElement().getAttributeByName(QName.valueOf("name")).getValue(), event.asStartElement().getAttributeByName(QName.valueOf("value")).getValue());
        } else if (collect && event.getEventType() == XMLStreamConstants.END_ELEMENT && event.asEndElement().getName().toString().equals("scr:component")) {
            break;
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) FileReader(java.io.FileReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 9 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project languagetool by languagetool-org.

the class XmlUsageCounter method countElementsAndAttributes.

private void countElementsAndAttributes(InputStream in) throws XMLStreamException {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
    while (eventReader.hasNext()) {
        XMLEvent event = eventReader.nextEvent();
        if (event.isStartElement()) {
            String elementName = event.asStartElement().getName().getLocalPart();
            add(elementName);
            Iterator attributes = event.asStartElement().getAttributes();
            while (attributes.hasNext()) {
                Attribute att = (Attribute) attributes.next();
                add(elementName + "/" + att.getName());
            }
        }
    }
}
Also used : Attribute(javax.xml.stream.events.Attribute) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 10 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project bazel by bazelbuild.

the class DataValueFileWithIds method parse.

public static void parse(XMLInputFactory xmlInputFactory, Path source, FullyQualifiedName fileKey, FullyQualifiedName.Factory fqnFactory, KeyValueConsumer<DataKey, DataResource> overwritingConsumer, KeyValueConsumer<DataKey, DataResource> combiningConsumer) throws IOException, XMLStreamException {
    ImmutableSet.Builder<String> newIds = ImmutableSet.builder();
    try (BufferedInputStream inStream = new BufferedInputStream(Files.newInputStream(source))) {
        XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(inStream, StandardCharsets.UTF_8.toString());
        // forgiving and allow even non-android namespaced attributes to define a new ID.
        while (eventReader.hasNext()) {
            XMLEvent event = eventReader.nextEvent();
            if (event.isStartElement()) {
                StartElement start = event.asStartElement();
                Iterator<Attribute> attributes = XmlResourceValues.iterateAttributesFrom(start);
                while (attributes.hasNext()) {
                    Attribute attribute = attributes.next();
                    String value = attribute.getValue();
                    if (value.startsWith(SdkConstants.NEW_ID_PREFIX)) {
                        String idName = value.substring(SdkConstants.NEW_ID_PREFIX.length());
                        newIds.add(idName);
                    }
                }
            }
        }
        eventReader.close();
    } catch (XMLStreamException e) {
        throw new XMLStreamException(source + ": " + e.getMessage(), e.getLocation(), e);
    } catch (RuntimeException e) {
        throw new RuntimeException("Error parsing " + source, e);
    }
    ImmutableSet<String> idResources = newIds.build();
    overwritingConsumer.consume(fileKey, DataValueFile.of(source));
    for (String id : idResources) {
        combiningConsumer.consume(fqnFactory.create(ResourceType.ID, id), DataResourceXml.createWithNoNamespace(source, IdXmlResourceValue.of()));
    }
}
Also used : StartElement(javax.xml.stream.events.StartElement) ImmutableSet(com.google.common.collect.ImmutableSet) XMLStreamException(javax.xml.stream.XMLStreamException) BufferedInputStream(java.io.BufferedInputStream) Attribute(javax.xml.stream.events.Attribute) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader)

Aggregations

XMLEventReader (javax.xml.stream.XMLEventReader)62 XMLInputFactory (javax.xml.stream.XMLInputFactory)30 XMLEvent (javax.xml.stream.events.XMLEvent)30 XMLStreamException (javax.xml.stream.XMLStreamException)21 StringReader (java.io.StringReader)18 InputStream (java.io.InputStream)15 Test (org.junit.Test)14 StAXSource (javax.xml.transform.stax.StAXSource)13 StartElement (javax.xml.stream.events.StartElement)10 IOException (java.io.IOException)8 StringWriter (java.io.StringWriter)6 Unmarshaller (javax.xml.bind.Unmarshaller)6 Attribute (javax.xml.stream.events.Attribute)6 Document (org.w3c.dom.Document)6 ArrayList (java.util.ArrayList)5 JAXBContext (javax.xml.bind.JAXBContext)5 Diff (org.custommonkey.xmlunit.Diff)5 InputSource (org.xml.sax.InputSource)5 JAXBException (javax.xml.bind.JAXBException)4 DOMSource (javax.xml.transform.dom.DOMSource)4