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;
}
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);
}
}
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;
}
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");
}
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();
}
Aggregations