Search in sources :

Example 61 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project core by weld.

the class BeansXmlStreamParser method handleFilter.

private void handleFilter(StartElement filterElement, XMLEventReader reader, Consumer<XmlMetadata<Filter>> consumer) throws XMLStreamException {
    String name = getAttribute(filterElement, NAME_ATTRIBUTE_NAME);
    String pattern = name != null ? null : getAttribute(filterElement, PATTERN_ATTRIBUTE_NAME);
    if (name != null || pattern != null) {
        List<Metadata<SystemPropertyActivation>> systemPropertyActivations = new LinkedList<>();
        List<Metadata<ClassAvailableActivation>> classAvailableActivations = new LinkedList<>();
        while (reader.hasNext()) {
            XMLEvent event = reader.nextEvent();
            if (isEnd(event, EXCLUDE, SCANNING_URIS) || isEnd(event, INCLUDE, SCANNING_URIS)) {
                Filter filter;
                if (filterElement.getName().getNamespaceURI().equals(WELD_URI)) {
                    filter = new WeldFilterImpl(name, systemPropertyActivations, classAvailableActivations, pattern);
                } else {
                    filter = new FilterImpl(name, systemPropertyActivations, classAvailableActivations);
                }
                consumer.accept(new XmlMetadata<Filter>(filterElement.getName().toString(), filter, beansXml, filterElement.getLocation().getLineNumber()));
                return;
            } else if (event.isStartElement()) {
                StartElement element = (StartElement) event;
                if (isStartElement(element, IF_CLASS_AVAILABLE, SCANNING_URIS)) {
                    classAvailable(element, classAvailableActivations::add, false);
                } else if (isStartElement(element, IF_CLASS_NOT_AVAILABLE, SCANNING_URIS)) {
                    classAvailable(element, classAvailableActivations::add, true);
                } else if (isStartElement(element, IF_SYSTEM_PROPERTY, SCANNING_URIS)) {
                    systemProperty(element, systemPropertyActivations::add);
                }
            }
        }
    }
}
Also used : StartElement(javax.xml.stream.events.StartElement) WeldFilterImpl(org.jboss.weld.metadata.WeldFilterImpl) FilterImpl(org.jboss.weld.metadata.FilterImpl) Filter(org.jboss.weld.bootstrap.spi.Filter) Metadata(org.jboss.weld.bootstrap.spi.Metadata) XMLEvent(javax.xml.stream.events.XMLEvent) WeldFilterImpl(org.jboss.weld.metadata.WeldFilterImpl) LinkedList(java.util.LinkedList)

Example 62 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project core by weld.

the class BeansXmlStreamParser method parse.

public BeansXml parse() {
    if (beansXml == null) {
        throw XmlLogger.LOG.loadError("unknown", null);
    }
    try (InputStream in = beansXml.openStream()) {
        if (in.available() == 0) {
            // The file is just acting as a marker file
            return EMPTY_BEANS_XML;
        }
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLEventReader reader = factory.createXMLEventReader(in);
        StartElement element = nextStartElement(reader, BEANS, JAVAEE_URIS);
        if (element != null) {
            parseBeans(element);
            while (reader.hasNext()) {
                XMLEvent event = reader.nextEvent();
                if (isEnd(event, BEANS)) {
                    break;
                } else if (isStartElement(event, ALTERNATIVES)) {
                    parseAlternatives(reader, event);
                } else if (isStartElement(event, INTERCEPTORS)) {
                    parseInterceptors(reader, event);
                } else if (isStartElement(event, DECORATORS)) {
                    parseDecorators(reader, event);
                } else if (isStartElement(event, SCAN, SCANNING_URIS)) {
                    parseScan(reader, event);
                } else if (isStartElement(event, TRIM)) {
                    isTrimmed = true;
                }
            }
        }
        reader.close();
    } catch (IOException e) {
        throw XmlLogger.LOG.loadError(beansXml, e);
    } catch (XMLStreamException e) {
        throw XmlLogger.LOG.parsingError(beansXml, e);
    }
    return new BeansXmlImpl(orEmpty(selectedAlternatives), orEmpty(selectedAlternativeStereotypes), orEmpty(enabledDecorators), orEmpty(enabledInterceptors), new ScanningImpl(orEmpty(includes), orEmpty(excludes)), beansXml, discoveryMode, version, isTrimmed);
}
Also used : StartElement(javax.xml.stream.events.StartElement) XMLStreamException(javax.xml.stream.XMLStreamException) InputStream(java.io.InputStream) BeansXmlImpl(org.jboss.weld.metadata.BeansXmlImpl) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) ScanningImpl(org.jboss.weld.metadata.ScanningImpl) IOException(java.io.IOException) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 63 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project santuario-java by apache.

the class XMLSecEventTest method testwWiteAsEncodedUnicode.

@Test
public void testwWiteAsEncodedUnicode() throws Exception {
    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(this.getClass().getClassLoader().getResourceAsStream("org/apache/xml/security/c14n/inExcl/plain-soap-1.1.xml"));
    Deque<XMLSecEvent> xmlSecEventDeque = new ArrayDeque<XMLSecEvent>();
    do {
        xmlSecEventDeque.push(XMLSecEventFactory.allocate(xmlStreamReader, null));
        xmlStreamReader.next();
    } while (xmlStreamReader.hasNext());
    // EndDocumentEvent
    xmlSecEventDeque.push(XMLSecEventFactory.allocate(xmlStreamReader, null));
    XMLSecurityEventReader xmlSecurityEventReader = new XMLSecurityEventReader(xmlSecEventDeque, 0);
    XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(this.getClass().getClassLoader().getResourceAsStream("org/apache/xml/security/c14n/inExcl/plain-soap-1.1.xml"));
    final StringWriter stdWriter = new StringWriter();
    final StringWriter secWriter = new StringWriter();
    while (xmlEventReader.hasNext()) {
        XMLEvent stdXmlEvent = xmlEventReader.nextEvent();
        XMLEvent secXmlEvent = xmlSecurityEventReader.nextEvent();
        stdXmlEvent.writeAsEncodedUnicode(stdWriter);
        secXmlEvent.writeAsEncodedUnicode(secWriter);
    }
    Assert.assertEquals(secWriter.toString(), stdWriter.toString());
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) StringWriter(java.io.StringWriter) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLSecurityEventReader(org.apache.xml.security.stax.impl.XMLSecurityEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) ArrayDeque(java.util.ArrayDeque) Test(org.junit.Test)

Example 64 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project santuario-java by apache.

the class XMLSecurityEventWriterTest method testConformness.

@Test
public void testConformness() throws Exception {
    XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
    StringWriter secStringWriter = new StringWriter();
    XMLStreamWriter secXmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(secStringWriter);
    XMLSecurityEventWriter xmlSecurityEventWriter = new XMLSecurityEventWriter(secXmlStreamWriter);
    StringWriter stdStringWriter = new StringWriter();
    XMLEventWriter stdXmlEventWriter = xmlOutputFactory.createXMLEventWriter(stdStringWriter);
    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(this.getClass().getClassLoader().getResourceAsStream("org/apache/xml/security/c14n/inExcl/plain-soap-1.1.xml"));
    while (xmlStreamReader.hasNext()) {
        XMLEvent xmlEvent = XMLSecEventFactory.allocate(xmlStreamReader, null);
        xmlSecurityEventWriter.add(xmlEvent);
        stdXmlEventWriter.add(xmlEvent);
        xmlStreamReader.next();
    }
    xmlSecurityEventWriter.close();
    stdXmlEventWriter.close();
    XMLAssert.assertXMLEqual(stdStringWriter.toString(), secStringWriter.toString());
}
Also used : StringWriter(java.io.StringWriter) XMLEvent(javax.xml.stream.events.XMLEvent) XMLSecurityEventWriter(org.apache.xml.security.stax.impl.XMLSecurityEventWriter) Test(org.junit.Test)

Example 65 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project santuario-java by apache.

the class XMLSecurityEventReaderTest method testIndex.

@Test
public void testIndex() throws Exception {
    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(this.getClass().getClassLoader().getResourceAsStream("org/apache/xml/security/c14n/inExcl/plain-soap-1.1.xml"));
    Deque<XMLSecEvent> xmlSecEventDeque = new ArrayDeque<XMLSecEvent>();
    do {
        xmlSecEventDeque.push(XMLSecEventFactory.allocate(xmlStreamReader, null));
        xmlStreamReader.next();
    } while (xmlStreamReader.hasNext());
    // EndDocumentEvent
    xmlSecEventDeque.push(XMLSecEventFactory.allocate(xmlStreamReader, null));
    int skip = 100;
    XMLSecurityEventReader xmlSecurityEventReader = new XMLSecurityEventReader(xmlSecEventDeque, skip);
    XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(this.getClass().getClassLoader().getResourceAsStream("org/apache/xml/security/c14n/inExcl/plain-soap-1.1.xml"));
    int currentIndex = 0;
    while (xmlEventReader.hasNext()) {
        XMLEvent stdXmlEvent = xmlEventReader.nextEvent();
        if (currentIndex++ < skip) {
            continue;
        }
        XMLEvent secXmlEvent = xmlSecurityEventReader.nextEvent();
        Assert.assertEquals(stdXmlEvent.getEventType(), secXmlEvent.getEventType());
        XMLEvent stdPeekedXMLEvent = xmlEventReader.peek();
        XMLEvent secPeekedXMLEvent = xmlSecurityEventReader.peek();
        if (stdPeekedXMLEvent == null) {
            Assert.assertNull(secPeekedXMLEvent);
        } else {
            Assert.assertEquals(stdPeekedXMLEvent.getEventType(), secPeekedXMLEvent.getEventType());
        }
    }
    Assert.assertFalse(xmlEventReader.hasNext());
    Assert.assertFalse(xmlSecurityEventReader.hasNext());
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLSecurityEventReader(org.apache.xml.security.stax.impl.XMLSecurityEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) ArrayDeque(java.util.ArrayDeque) XMLSecEvent(org.apache.xml.security.stax.ext.stax.XMLSecEvent) Test(org.junit.Test)

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