Search in sources :

Example 31 with XMLEvent

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

the class SAMLAttributeValueParser method parse.

@Override
public Object parse(XMLEventReader xmlEventReader) throws ParsingException {
    StartElement element = StaxParserUtil.getNextStartElement(xmlEventReader);
    StaxParserUtil.validate(element, SAMLAssertionQNames.ATTRIBUTE_VALUE.getQName());
    Attribute nil = element.getAttributeByName(NIL);
    if (nil != null) {
        String nilValue = StaxParserUtil.getAttributeValue(nil);
        if (nilValue != null && (nilValue.equalsIgnoreCase("true") || nilValue.equals("1"))) {
            String elementText = StaxParserUtil.getElementText(xmlEventReader);
            if (elementText == null || elementText.isEmpty()) {
                return null;
            } else {
                throw logger.nullValueError("nil attribute is not in SAML20 format");
            }
        } else {
            throw logger.parserRequiredAttribute(JBossSAMLURIConstants.XSI_PREFIX.get() + ":nil");
        }
    }
    Attribute type = element.getAttributeByName(XSI_TYPE);
    if (type == null) {
        if (StaxParserUtil.hasTextAhead(xmlEventReader)) {
            return StaxParserUtil.getElementText(xmlEventReader);
        }
        // Else we may have Child Element
        XMLEvent xmlEvent = StaxParserUtil.peek(xmlEventReader);
        if (xmlEvent instanceof StartElement) {
            element = (StartElement) xmlEvent;
            final QName qName = element.getName();
            if (Objects.equals(qName, SAMLAssertionQNames.NAMEID.getQName())) {
                return SAMLParserUtil.parseNameIDType(xmlEventReader);
            }
        } else if (xmlEvent instanceof EndElement) {
            return "";
        }
        // when no type attribute assigned -> assume anyType
        return parseAnyTypeAsString(xmlEventReader);
    }
    // RK Added an additional type check for base64Binary type as calheers is passing this type
    String typeValue = StaxParserUtil.getAttributeValue(type);
    if (typeValue.contains(":string")) {
        return StaxParserUtil.getElementText(xmlEventReader);
    } else if (typeValue.contains(":anyType")) {
        return parseAnyTypeAsString(xmlEventReader);
    } else if (typeValue.contains(":base64Binary")) {
        return StaxParserUtil.getElementText(xmlEventReader);
    } else if (typeValue.contains(":date")) {
        return XMLTimeUtil.parse(StaxParserUtil.getElementText(xmlEventReader));
    } else if (typeValue.contains(":boolean")) {
        return StaxParserUtil.getElementText(xmlEventReader);
    }
    // KEYCLOAK-18417: Simply ignore unknown types
    logger.debug("Skipping attribute value of unsupported type " + typeValue);
    StaxParserUtil.bypassElementBlock(xmlEventReader);
    return null;
}
Also used : StartElement(javax.xml.stream.events.StartElement) Attribute(javax.xml.stream.events.Attribute) EndElement(javax.xml.stream.events.EndElement) QName(javax.xml.namespace.QName) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 32 with XMLEvent

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

the class StaxParserUtil method peekNextStartElement.

/**
 * Peek the next {@code StartElement }
 *
 * @param xmlEventReader
 *
 * @return
 *
 * @throws ParsingException
 */
public static StartElement peekNextStartElement(XMLEventReader xmlEventReader) throws ParsingException {
    try {
        XMLEvent xmlEvent = xmlEventReader.peek();
        while (xmlEvent != null && !xmlEvent.isStartElement()) {
            xmlEventReader.nextEvent();
            xmlEvent = xmlEventReader.peek();
        }
        return (StartElement) xmlEvent;
    } catch (XMLStreamException e) {
        throw logger.parserException(e);
    }
}
Also used : StartElement(javax.xml.stream.events.StartElement) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 33 with XMLEvent

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

the class AbstractParser method filterWhitespaces.

/**
 * Creates a derived {@link XMLEventReader} that ignores all events except for: {@link StartElement},
 * {@link EndElement}, and non-empty and non-whitespace-only {@link Characters}.
 *
 * @param xmlEventReader Original {@link XMLEventReader}
 * @return Derived {@link XMLEventReader}
 * @throws XMLStreamException
 */
private static XMLEventReader filterWhitespaces(XMLEventReader xmlEventReader) throws ParsingException {
    XMLInputFactory xmlInputFactory = XML_INPUT_FACTORY.get();
    try {
        xmlEventReader = xmlInputFactory.createFilteredReader(xmlEventReader, new EventFilter() {

            @Override
            public boolean accept(XMLEvent xmlEvent) {
                // We are going to disregard characters that are new line and whitespace
                if (xmlEvent.isCharacters()) {
                    Characters chars = xmlEvent.asCharacters();
                    String data = chars.getData();
                    return data != null && !WHITESPACE_ONLY.matcher(data).matches();
                } else {
                    return xmlEvent.isStartElement() || xmlEvent.isEndElement();
                }
            }
        });
    } catch (XMLStreamException ex) {
        throw logger.parserException(ex);
    }
    // Handle IBM JDK bug with Stax parsing when EventReader presented
    if (Environment.IS_IBM_JAVA) {
        final XMLEventReader origReader = xmlEventReader;
        xmlEventReader = new EventReaderDelegate(origReader) {

            @Override
            public boolean hasNext() {
                boolean hasNext = super.hasNext();
                try {
                    return hasNext && (origReader.peek() != null);
                } catch (XMLStreamException xse) {
                    throw new IllegalStateException(xse);
                }
            }
        };
    }
    return xmlEventReader;
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) Characters(javax.xml.stream.events.Characters) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) EventFilter(javax.xml.stream.EventFilter) XMLInputFactory(javax.xml.stream.XMLInputFactory) EventReaderDelegate(javax.xml.stream.util.EventReaderDelegate)

Example 34 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project EnderIO by SleepyTrousers.

the class RecipeFactory method readStax.

protected static <T extends RecipeRoot> T readStax(T target, String rootElement, InputStream in) throws XMLStreamException, InvalidRecipeConfigException {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
    StaxFactory factory = new StaxFactory(eventReader);
    while (eventReader.hasNext()) {
        XMLEvent event = eventReader.nextEvent();
        if (event.isStartElement()) {
            StartElement startElement = event.asStartElement();
            if (rootElement.equals(startElement.getName().getLocalPart())) {
                return factory.read(target, startElement);
            } else {
                throw new InvalidRecipeConfigException("Unexpected tag '" + startElement.getName() + "'");
            }
        }
    }
    throw new InvalidRecipeConfigException("Missing recipes tag");
}
Also used : StartElement(javax.xml.stream.events.StartElement) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 35 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project hibernate-orm by hibernate.

the class BufferedXMLEventReader method peek.

@Override
public XMLEvent peek() throws XMLStreamException {
    if (this.bufferReader != null) {
        final XMLEvent event = this.bufferReader.next();
        // move the iterator back
        this.bufferReader.previous();
        return event;
    }
    return super.peek();
}
Also used : 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