Search in sources :

Example 1 with XMLEvent

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

the class AtomFeedParser method getCharacterData.

private String getCharacterData(XMLEventReader eventReader) throws XMLStreamException {
    XMLEvent event = eventReader.nextEvent();
    StringBuilder sb = new StringBuilder();
    while (event.isCharacters()) {
        sb.append(event.asCharacters().getData());
        event = eventReader.nextEvent();
    }
    return sb.toString();
}
Also used : XMLEvent(javax.xml.stream.events.XMLEvent)

Example 2 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project hadoop by apache.

the class OfflineImageReconstructor method loadNodeChildrenHelper.

private void loadNodeChildrenHelper(Node parent, String expected, String[] terminators) throws IOException {
    XMLEvent ev = null;
    while (true) {
        try {
            ev = events.peek();
            switch(ev.getEventType()) {
                case XMLEvent.END_ELEMENT:
                    if (terminators.length != 0) {
                        return;
                    }
                    events.nextEvent();
                    return;
                case XMLEvent.START_ELEMENT:
                    String key = ev.asStartElement().getName().getLocalPart();
                    for (String terminator : terminators) {
                        if (terminator.equals(key)) {
                            return;
                        }
                    }
                    events.nextEvent();
                    Node node = new Node();
                    parent.addChild(key, node);
                    loadNodeChildrenHelper(node, expected, new String[0]);
                    break;
                case XMLEvent.CHARACTERS:
                    String val = XMLUtils.unmangleXmlString(ev.asCharacters().getData(), true);
                    parent.setVal(val);
                    events.nextEvent();
                    break;
                case XMLEvent.ATTRIBUTE:
                    throw new IOException("Unexpected XML event " + ev);
                default:
                    // Ignore other event types like comment, etc.
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Skipping XMLEvent " + ev);
                    }
                    events.nextEvent();
                    break;
            }
        } catch (XMLStreamException e) {
            throw new IOException("Expecting " + expected + ", but got XMLStreamException", e);
        }
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException)

Example 3 with XMLEvent

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

the class Jaxb2XmlDecoderTests method splitOneBranches.

@Test
public void splitOneBranches() {
    Flux<XMLEvent> xmlEvents = this.xmlEventDecoder.decode(Flux.just(stringBuffer(POJO_ROOT)), null, null, Collections.emptyMap());
    Flux<List<XMLEvent>> result = this.decoder.split(xmlEvents, new QName("pojo"));
    StepVerifier.create(result).consumeNextWith(events -> {
        assertEquals(8, events.size());
        assertStartElement(events.get(0), "pojo");
        assertStartElement(events.get(1), "foo");
        assertCharacters(events.get(2), "foofoo");
        assertEndElement(events.get(3), "foo");
        assertStartElement(events.get(4), "bar");
        assertCharacters(events.get(5), "barbar");
        assertEndElement(events.get(6), "bar");
        assertEndElement(events.get(7), "pojo");
    }).expectComplete().verify();
}
Also used : QName(javax.xml.namespace.QName) XMLEvent(javax.xml.stream.events.XMLEvent) List(java.util.List) Test(org.junit.Test)

Example 4 with XMLEvent

use of javax.xml.stream.events.XMLEvent 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 5 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project bazel by bazelbuild.

the class ArrayXmlResourceValue method parseArray.

public static XmlResourceValue parseArray(XMLEventReader eventReader, StartElement start, Namespaces.Collector namespacesCollector) throws XMLStreamException {
    List<String> values = new ArrayList<>();
    for (XMLEvent element = XmlResourceValues.nextTag(eventReader); !XmlResourceValues.isEndTag(element, start.getName()); element = XmlResourceValues.nextTag(eventReader)) {
        if (XmlResourceValues.isItem(element)) {
            if (!element.isStartElement()) {
                throw new XMLStreamException(String.format("Expected start element %s", element), element.getLocation());
            }
            String contents = XmlResourceValues.readContentsAsString(eventReader, element.asStartElement().getName(), namespacesCollector);
            values.add(contents != null ? contents : "");
        }
    }
    try {
        return of(ArrayType.fromTagName(start), values, ImmutableMap.copyOf(XmlResourceValues.parseTagAttributes(start)));
    } catch (IllegalArgumentException e) {
        throw new XMLStreamException(e.getMessage(), start.getLocation());
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) ArrayList(java.util.ArrayList) XMLEvent(javax.xml.stream.events.XMLEvent)

Aggregations

XMLEvent (javax.xml.stream.events.XMLEvent)269 XMLEventReader (javax.xml.stream.XMLEventReader)114 StartElement (javax.xml.stream.events.StartElement)107 XMLStreamException (javax.xml.stream.XMLStreamException)96 XMLInputFactory (javax.xml.stream.XMLInputFactory)65 QName (javax.xml.namespace.QName)60 Attribute (javax.xml.stream.events.Attribute)52 EndElement (javax.xml.stream.events.EndElement)52 IOException (java.io.IOException)39 ArrayList (java.util.ArrayList)32 InputStream (java.io.InputStream)29 XMLEventWriter (javax.xml.stream.XMLEventWriter)24 Characters (javax.xml.stream.events.Characters)22 ByteArrayInputStream (java.io.ByteArrayInputStream)17 StringWriter (java.io.StringWriter)17 Test (org.junit.Test)17 StringReader (java.io.StringReader)14 HashMap (java.util.HashMap)14 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)14 XMLEventFactory (javax.xml.stream.XMLEventFactory)13