use of javax.xml.stream.events.Attribute in project iaf by ibissource.
the class SchemaUtils method xsdToXmlStreamWriter.
/**
* Including a {@link nl.nn.adapterframework.validation.XSD} into an
* {@link javax.xml.stream.XMLStreamWriter} while parsing it. It is parsed
* (using a low level {@link javax.xml.stream.XMLEventReader} so that
* certain things can be corrected on the fly.
* @param xsd
* @param xmlStreamWriter
* @param standalone
* When standalone the start and end document contants are ignored, hence
* the xml declaration is ignored.
* @param stripSchemaLocationFromImport
* Useful when generating a WSDL which should contain all XSD's inline
* (without includes or imports). The XSD might have an import with
* schemaLocation to make it valid on it's own, when
* stripSchemaLocationFromImport is true it will be removed.
* @throws java.io.IOException
* @throws javax.xml.stream.XMLStreamException
*/
public static void xsdToXmlStreamWriter(final XSD xsd, XMLStreamWriter xmlStreamWriter, boolean standalone, boolean stripSchemaLocationFromImport, boolean skipRootStartElement, boolean skipRootEndElement, List<Attribute> rootAttributes, List<Attribute> rootNamespaceAttributes, List<XMLEvent> imports, boolean noOutput) throws IOException, ConfigurationException {
Map<String, String> namespacesToCorrect = new HashMap<String, String>();
NamespaceCorrectingXMLStreamWriter namespaceCorrectingXMLStreamWriter = new NamespaceCorrectingXMLStreamWriter(xmlStreamWriter, namespacesToCorrect);
final XMLStreamEventWriter streamEventWriter = new XMLStreamEventWriter(namespaceCorrectingXMLStreamWriter);
InputStream in = null;
in = xsd.getInputStream();
if (in == null) {
throw new IllegalStateException("" + xsd + " not found");
}
XMLEvent event = null;
try {
XMLEventReader er = XmlUtils.INPUT_FACTORY.createXMLEventReader(in, XmlUtils.STREAM_FACTORY_ENCODING);
while (er.hasNext()) {
event = er.nextEvent();
switch(event.getEventType()) {
case XMLStreamConstants.START_DOCUMENT:
case XMLStreamConstants.END_DOCUMENT:
if (!standalone) {
continue;
}
// fall through
case XMLStreamConstants.SPACE:
case XMLStreamConstants.COMMENT:
break;
case XMLStreamConstants.START_ELEMENT:
StartElement startElement = event.asStartElement();
if (SCHEMA.equals(startElement.getName())) {
if (skipRootStartElement) {
continue;
}
if (rootAttributes != null) {
// Collect or write attributes of schema element.
if (noOutput) {
// First call to this method collecting
// schema attributes.
Iterator<Attribute> iterator = startElement.getAttributes();
while (iterator.hasNext()) {
Attribute attribute = iterator.next();
boolean add = true;
for (Attribute attribute2 : rootAttributes) {
if (XmlUtils.attributesEqual(attribute, attribute2)) {
add = false;
}
}
if (add) {
rootAttributes.add(attribute);
}
}
iterator = startElement.getNamespaces();
while (iterator.hasNext()) {
Attribute attribute = iterator.next();
boolean add = true;
for (Attribute attribute2 : rootNamespaceAttributes) {
if (XmlUtils.attributesEqual(attribute, attribute2)) {
add = false;
}
}
if (add) {
rootNamespaceAttributes.add(attribute);
}
}
} else {
// Second call to this method writing attributes
// from previous call.
startElement = XmlUtils.EVENT_FACTORY.createStartElement(startElement.getName().getPrefix(), startElement.getName().getNamespaceURI(), startElement.getName().getLocalPart(), rootAttributes.iterator(), rootNamespaceAttributes.iterator(), startElement.getNamespaceContext());
}
}
// (see http://www.w3.org/TR/xml-names/#ns-decl).
if (xsd.isAddNamespaceToSchema() && !xsd.getNamespace().equals("http://www.w3.org/XML/1998/namespace")) {
event = XmlUtils.mergeAttributes(startElement, Arrays.asList(new AttributeEvent(TNS, xsd.getNamespace()), new AttributeEvent(ELFORMDEFAULT, "qualified")).iterator(), Arrays.asList(XmlUtils.EVENT_FACTORY.createNamespace(xsd.getNamespace())).iterator(), XmlUtils.EVENT_FACTORY);
if (!event.equals(startElement)) {
Attribute tns = startElement.getAttributeByName(TNS);
if (tns != null) {
String s = tns.getValue();
if (!s.equals(xsd.getNamespace())) {
namespacesToCorrect.put(s, xsd.getNamespace());
}
}
}
} else {
event = startElement;
}
if (imports != null && !noOutput) {
// 2 on every iteration.
for (int i = 0; i < imports.size(); i = i + 2) {
boolean skip = false;
for (int j = 0; j < i; j = j + 2) {
Attribute attribute1 = imports.get(i).asStartElement().getAttributeByName(NAMESPACE);
Attribute attribute2 = imports.get(j).asStartElement().getAttributeByName(NAMESPACE);
if (attribute1 != null && attribute2 != null && attribute1.getValue().equals(attribute2.getValue())) {
skip = true;
}
}
if (!skip) {
streamEventWriter.add(event);
event = imports.get(i);
streamEventWriter.add(event);
event = imports.get(i + 1);
}
}
}
} else if (startElement.getName().equals(INCLUDE)) {
continue;
// } else if (startElement.getName().equals(REDEFINE)) {
// continue;
} else if (startElement.getName().equals(IMPORT)) {
if (imports == null || noOutput) {
// Not collecting or writing import elements.
Attribute schemaLocation = startElement.getAttributeByName(SCHEMALOCATION);
if (schemaLocation != null) {
String location = schemaLocation.getValue();
if (stripSchemaLocationFromImport) {
List<Attribute> attributes = new ArrayList<Attribute>();
Iterator<Attribute> iterator = startElement.getAttributes();
while (iterator.hasNext()) {
Attribute a = iterator.next();
if (!SCHEMALOCATION.equals(a.getName())) {
attributes.add(a);
}
}
event = new StartElementEvent(startElement.getName(), attributes.iterator(), startElement.getNamespaces(), startElement.getNamespaceContext(), startElement.getLocation(), startElement.getSchemaType());
} else {
String relativeTo = xsd.getParentLocation();
if (relativeTo.length() > 0 && location.startsWith(relativeTo)) {
location = location.substring(relativeTo.length());
}
event = XMLStreamUtils.mergeAttributes(startElement, Collections.singletonList(new AttributeEvent(SCHEMALOCATION, location)).iterator(), XmlUtils.EVENT_FACTORY);
}
}
}
if (imports != null) {
// Collecting or writing import elements.
if (noOutput) {
// First call to this method collecting
// imports.
imports.add(event);
}
continue;
}
}
break;
case XMLStreamConstants.END_ELEMENT:
EndElement endElement = event.asEndElement();
if (endElement.getName().equals(SCHEMA)) {
if (skipRootEndElement) {
continue;
}
} else if (endElement.getName().equals(INCLUDE)) {
continue;
// } else if (endElement.getName().equals(REDEFINE)) {
// continue;
} else if (imports != null) {
if (endElement.getName().equals(IMPORT)) {
if (noOutput) {
imports.add(event);
}
continue;
}
}
break;
default:
}
if (!noOutput) {
streamEventWriter.add(event);
}
}
streamEventWriter.flush();
} catch (XMLStreamException e) {
throw new ConfigurationException(xsd.toString() + " (" + event.getLocation() + "): " + e.getMessage(), e);
}
}
use of javax.xml.stream.events.Attribute in project iaf by ibissource.
the class SchemaUtils method mergeXsdsGroupedByNamespaceToSchemasWithoutIncludes.
/**
* @return XSD's when xmlStreamWriter is null, otherwise write to
* xmlStreamWriter
*/
public static Set<XSD> mergeXsdsGroupedByNamespaceToSchemasWithoutIncludes(IScopeProvider scopeProvider, Map<String, Set<XSD>> xsdsGroupedByNamespace, XMLStreamWriter xmlStreamWriter) throws XMLStreamException, IOException, ConfigurationException {
Set<XSD> resultXsds = new HashSet<XSD>();
for (String namespace : xsdsGroupedByNamespace.keySet()) {
Set<XSD> xsds = xsdsGroupedByNamespace.get(namespace);
// Get attributes of root elements and get import elements from all XSD's
List<Attribute> rootAttributes = new ArrayList<Attribute>();
List<Namespace> rootNamespaceAttributes = new ArrayList<Namespace>();
List<XMLEvent> imports = new ArrayList<XMLEvent>();
for (XSD xsd : xsds) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
XMLStreamWriter w = XmlUtils.REPAIR_NAMESPACES_OUTPUT_FACTORY.createXMLStreamWriter(byteArrayOutputStream, StreamUtil.DEFAULT_INPUT_STREAM_ENCODING);
xsdToXmlStreamWriter(xsd, w, false, true, false, false, rootAttributes, rootNamespaceAttributes, imports, true);
}
// Write XSD's with merged root element
XSD resultXsd = null;
XMLStreamWriter w;
if (xmlStreamWriter == null) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
resultXsd = new XSD();
// resultXsd.setClassLoader(classLoader);
// resultXsd.setNamespace(namespace);
resultXsd.setByteArrayOutputStream(byteArrayOutputStream);
// resultXsd.setSourceXsds(xsds);
w = XmlUtils.REPAIR_NAMESPACES_OUTPUT_FACTORY.createXMLStreamWriter(byteArrayOutputStream, StreamUtil.DEFAULT_INPUT_STREAM_ENCODING);
} else {
w = xmlStreamWriter;
}
int i = 0;
for (XSD xsd : xsds) {
i++;
boolean skipFirstElement = true;
boolean skipLastElement = true;
if (xsds.size() == 1) {
skipFirstElement = false;
skipLastElement = false;
} else {
if (i == 1) {
skipFirstElement = false;
} else if (i == xsds.size()) {
skipLastElement = false;
}
}
xsdToXmlStreamWriter(xsd, w, false, true, skipFirstElement, skipLastElement, rootAttributes, rootNamespaceAttributes, imports, false);
}
if (resultXsd != null) {
XSD firstXsd = xsds.iterator().next();
resultXsd.setImportedSchemaLocationsToIgnore(firstXsd.getImportedSchemaLocationsToIgnore());
resultXsd.setUseBaseImportedSchemaLocationsToIgnore(firstXsd.isUseBaseImportedSchemaLocationsToIgnore());
resultXsd.setImportedNamespacesToIgnore(firstXsd.getImportedNamespacesToIgnore());
resultXsd.initFromXsds(namespace, scopeProvider, xsds);
resultXsds.add(resultXsd);
}
}
return resultXsds;
}
use of javax.xml.stream.events.Attribute in project keycloak by keycloak.
the class SAML11ParserUtil method parseSAML11Attribute.
/**
* Parse a {@link SAML11AttributeType}
*
* @param xmlEventReader
*
* @return
*
* @throws ParsingException
*/
public static SAML11AttributeType parseSAML11Attribute(XMLEventReader xmlEventReader) throws ParsingException {
StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
StaxParserUtil.validate(startElement, JBossSAMLConstants.ATTRIBUTE.get());
SAML11AttributeType attributeType = null;
Attribute name = startElement.getAttributeByName(new QName(SAML11Constants.ATTRIBUTE_NAME));
if (name == null)
throw logger.parserRequiredAttribute("Name");
String attribName = StaxParserUtil.getAttributeValue(name);
Attribute namesp = startElement.getAttributeByName(new QName(SAML11Constants.ATTRIBUTE_NAMESPACE));
if (namesp == null)
throw logger.parserRequiredAttribute("Namespace");
String attribNamespace = StaxParserUtil.getAttributeValue(namesp);
attributeType = new SAML11AttributeType(attribName, URI.create(attribNamespace));
attributeType.add(parseAttributeValue(xmlEventReader));
parseAttributeType(xmlEventReader, startElement, JBossSAMLConstants.ATTRIBUTE.get(), attributeType);
return attributeType;
}
use of javax.xml.stream.events.Attribute in project keycloak by keycloak.
the class SAML11ParserUtil method parseAttributeValue.
/**
* Parse Attribute value
*
* @param xmlEventReader
*
* @return
*
* @throws ParsingException
*/
public static Object parseAttributeValue(XMLEventReader xmlEventReader) throws ParsingException {
StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
StaxParserUtil.validate(startElement, JBossSAMLConstants.ATTRIBUTE_VALUE.get());
Attribute type = startElement.getAttributeByName(new QName(JBossSAMLURIConstants.XSI_NSURI.get(), "type", "xsi"));
if (type == null) {
return StaxParserUtil.getElementText(xmlEventReader);
}
String typeValue = StaxParserUtil.getAttributeValue(type);
if (typeValue.contains(":string")) {
return StaxParserUtil.getElementText(xmlEventReader);
}
throw logger.parserUnknownXSI(typeValue);
}
use of javax.xml.stream.events.Attribute in project keycloak by keycloak.
the class SAML11ParserUtil method parseSubjectConfirmationData.
/**
* Parse the {@link SubjectConfirmationDataType}
*
* @param xmlEventReader
*
* @return
*
* @throws ParsingException
*/
public static SubjectConfirmationDataType parseSubjectConfirmationData(XMLEventReader xmlEventReader) throws ParsingException {
StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
StaxParserUtil.validate(startElement, JBossSAMLConstants.SUBJECT_CONFIRMATION_DATA.get());
SubjectConfirmationDataType subjectConfirmationData = new SubjectConfirmationDataType();
Attribute inResponseTo = startElement.getAttributeByName(new QName(JBossSAMLConstants.IN_RESPONSE_TO.get()));
if (inResponseTo != null) {
subjectConfirmationData.setInResponseTo(StaxParserUtil.getAttributeValue(inResponseTo));
}
Attribute notBefore = startElement.getAttributeByName(new QName(JBossSAMLConstants.NOT_BEFORE.get()));
if (notBefore != null) {
subjectConfirmationData.setNotBefore(XMLTimeUtil.parse(StaxParserUtil.getAttributeValue(notBefore)));
}
Attribute notOnOrAfter = startElement.getAttributeByName(new QName(JBossSAMLConstants.NOT_ON_OR_AFTER.get()));
if (notOnOrAfter != null) {
subjectConfirmationData.setNotOnOrAfter(XMLTimeUtil.parse(StaxParserUtil.getAttributeValue(notOnOrAfter)));
}
Attribute recipient = startElement.getAttributeByName(new QName(JBossSAMLConstants.RECIPIENT.get()));
if (recipient != null) {
subjectConfirmationData.setRecipient(StaxParserUtil.getAttributeValue(recipient));
}
Attribute address = startElement.getAttributeByName(new QName(JBossSAMLConstants.ADDRESS.get()));
if (address != null) {
subjectConfirmationData.setAddress(StaxParserUtil.getAttributeValue(address));
}
XMLEvent xmlEvent = StaxParserUtil.peek(xmlEventReader);
if (!(xmlEvent instanceof EndElement)) {
startElement = StaxParserUtil.peekNextStartElement(xmlEventReader);
String tag = StaxParserUtil.getElementName(startElement);
if (tag.equals(WSTrustConstants.XMLDSig.KEYINFO)) {
KeyInfoType keyInfo = parseKeyInfo(xmlEventReader);
subjectConfirmationData.setAnyType(keyInfo);
} else if (tag.equals(WSTrustConstants.XMLEnc.ENCRYPTED_KEY)) {
subjectConfirmationData.setAnyType(StaxParserUtil.getDOMElement(xmlEventReader));
} else
throw logger.parserUnknownTag(tag, startElement.getLocation());
}
// Get the end tag
EndElement endElement = (EndElement) StaxParserUtil.getNextEvent(xmlEventReader);
StaxParserUtil.matches(endElement, JBossSAMLConstants.SUBJECT_CONFIRMATION_DATA.get());
return subjectConfirmationData;
}
Aggregations