Search in sources :

Example 91 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project structr by structr.

the class XMLStructureAnalyzer method getStructure.

public Map<String, Object> getStructure(final int threshold) {
    int level = 0;
    int exceptionCount = 0;
    while (reader.hasNext() && analysisCount < threshold && exceptionCount < XML_MAX_EXCEPTIONS) {
        try {
            final XMLEvent event = reader.nextEvent();
            final String tagName = getTagName(event);
            switch(event.getEventType()) {
                case XMLEvent.START_ELEMENT:
                    current = new Element(current, tagName, level++);
                    current.attributes.addAll(getAttributes(event.asStartElement()));
                    break;
                case XMLEvent.END_ELEMENT:
                    // one level up
                    current = current.parent;
                    level--;
                    // start analysis for elements with more than one occurrence
                    if (current != null && current.level == 0) {
                        analyze(current, structure);
                        analysisCount++;
                    }
                    break;
            }
        } catch (XMLStreamException strex) {
            logger.warn(strex.getMessage());
            exceptionCount++;
        }
    }
    if (exceptionCount == XML_MAX_EXCEPTIONS) {
        logger.info("Stopping XML processing at error threshold ({})", XML_MAX_EXCEPTIONS);
    }
    return structure;
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) StartElement(javax.xml.stream.events.StartElement) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 92 with XMLEvent

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

the class TransformingReader method nextTag.

/**
 * Forwards the call and keep trace of the XML elements opened up to this point.
 */
@Override
public XMLEvent nextTag() throws XMLStreamException {
    final XMLEvent event = in.nextTag();
    final XMLEvent next = nextEvent;
    if (next != null) {
        nextEvent = null;
        switch(event.getEventType()) {
            case START_ELEMENT:
            case END_ELEMENT:
                {
                    assert isWrapper(event, next) : event;
                    return event;
                }
        }
    }
    return convert(event);
}
Also used : XMLEvent(javax.xml.stream.events.XMLEvent)

Example 93 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project rpki-validator-3 by RIPE-NCC.

the class RrdpParser method delta.

public Delta delta(final InputStream inputStream) {
    final Map<String, DeltaElement> objects = new HashMap<>();
    try {
        final XMLInputFactory factory = XMLInputFactory.newInstance();
        final XMLEventReader eventReader = factory.createXMLEventReader(inputStream);
        String sessionId = null;
        BigInteger serial = null;
        String uri = null;
        String hash = null;
        StringBuilder base64 = new StringBuilder();
        boolean inPublishElement = false;
        final Base64.Decoder decoder = Base64.getDecoder();
        while (eventReader.hasNext()) {
            final XMLEvent event = eventReader.nextEvent();
            switch(event.getEventType()) {
                case XMLStreamConstants.START_ELEMENT:
                    final StartElement startElement = event.asStartElement();
                    final String qName = startElement.getName().getLocalPart();
                    switch(qName) {
                        case "publish":
                            uri = getAttr(startElement, "uri", "Uri is not present in 'publish' element");
                            hash = getAttr(startElement, "hash");
                            inPublishElement = true;
                            break;
                        case "withdraw":
                            uri = getAttr(startElement, "uri", "Uri is not present in 'publish' element");
                            hash = getAttr(startElement, "hash", "Hash is not present in 'withdraw' element");
                            break;
                        case "delta":
                            serial = new BigInteger(getAttr(startElement, "serial", "Notification serial is not present"));
                            sessionId = getAttr(startElement, "session_id", "Session id is not present");
                            break;
                    }
                    break;
                case XMLStreamConstants.CHARACTERS:
                    final Characters characters = event.asCharacters();
                    if (inPublishElement) {
                        final String thisBase64 = characters.getData();
                        base64.append(thisBase64.replaceAll("\\s", ""));
                    }
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    final EndElement endElement = event.asEndElement();
                    final String qqName = endElement.getName().getLocalPart();
                    switch(qqName) {
                        case "publish":
                            final byte[] decoded = decoder.decode(base64.toString());
                            objects.put(uri, new DeltaPublish(decoded, uri, Hex.parse(hash)));
                            base64 = new StringBuilder();
                            break;
                        case "withdraw":
                            objects.put(uri, new DeltaWithdraw(uri, Hex.parse(hash)));
                            break;
                    }
                    break;
            }
        }
        return new Delta(objects, sessionId, serial);
    } catch (XMLStreamException e) {
        throw new RrdpException("Couldn't parse snapshot: ", e);
    }
}
Also used : Base64(java.util.Base64) HashMap(java.util.HashMap) EndElement(javax.xml.stream.events.EndElement) Characters(javax.xml.stream.events.Characters) StartElement(javax.xml.stream.events.StartElement) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) BigInteger(java.math.BigInteger) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 94 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project service-proxy by membrane.

the class XML2HTTP method slurpCharacterData.

private static String slurpCharacterData(XMLEventReader parser, StartElement sevent) throws XMLStreamException, XML2HTTPException {
    String name = sevent.getName().getLocalPart();
    StringBuilder value = new StringBuilder();
    while (parser.hasNext()) {
        XMLEvent event = parser.nextEvent();
        if (event.isCharacters()) {
            value.append(event.asCharacters().getData());
        } else if (event.isEndElement()) {
            break;
        } else {
            throw new XML2HTTPException("XML-HTTP doc <" + name + "> element contains non-character data.");
        }
    }
    return value.toString();
}
Also used : XMLEvent(javax.xml.stream.events.XMLEvent)

Example 95 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project service-proxy by membrane.

the class TextUtil method isValidXMLSnippet.

/**
 * Checks whether s is a valid (well-formed and balanced) XML snippet.
 *
 * Note that attributes escaped by single quotes are accepted (which is illegal by spec).
 */
public static boolean isValidXMLSnippet(String s) {
    try {
        XMLEventReader parser;
        synchronized (xmlInputFactory) {
            parser = xmlInputFactory.createXMLEventReader(new StringReader("<a>" + s + "</a>"));
        }
        XMLEvent event = null;
        while (parser.hasNext()) {
            event = (XMLEvent) parser.next();
        }
        return event != null && event.isEndDocument();
    } catch (Exception e) {
        log.error("", e);
        return false;
    }
}
Also used : StringReader(java.io.StringReader) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) IOException(java.io.IOException)

Aggregations

XMLEvent (javax.xml.stream.events.XMLEvent)242 XMLEventReader (javax.xml.stream.XMLEventReader)111 XMLStreamException (javax.xml.stream.XMLStreamException)91 StartElement (javax.xml.stream.events.StartElement)86 XMLInputFactory (javax.xml.stream.XMLInputFactory)63 QName (javax.xml.namespace.QName)47 IOException (java.io.IOException)40 Attribute (javax.xml.stream.events.Attribute)40 EndElement (javax.xml.stream.events.EndElement)33 ArrayList (java.util.ArrayList)31 InputStream (java.io.InputStream)28 XMLEventWriter (javax.xml.stream.XMLEventWriter)23 Characters (javax.xml.stream.events.Characters)21 Test (org.junit.Test)17 ByteArrayInputStream (java.io.ByteArrayInputStream)16 StringWriter (java.io.StringWriter)16 StringReader (java.io.StringReader)14 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)14 HashMap (java.util.HashMap)13 XMLEventFactory (javax.xml.stream.XMLEventFactory)13