Java Code Examples for javax.xml.stream.events.StartElement#getAttributeByName()
The following examples show how to use
javax.xml.stream.events.StartElement#getAttributeByName() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XmlPolicyModelUnmarshaller.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
private Attribute getAttributeByName(final StartElement element, final QName attributeName) { // call standard API method to retrieve the attribute by name Attribute attribute = element.getAttributeByName(attributeName); // try to find the attribute without a prefix. if (attribute == null) { final String localAttributeName = attributeName.getLocalPart(); final Iterator iterator = element.getAttributes(); while (iterator.hasNext()) { final Attribute nextAttribute = (Attribute) iterator.next(); final QName aName = nextAttribute.getName(); final boolean attributeFoundByWorkaround = aName.equals(attributeName) || (aName.getLocalPart().equals(localAttributeName) && (aName.getPrefix() == null || "".equals(aName.getPrefix()))); if (attributeFoundByWorkaround) { attribute = nextAttribute; break; } } } return attribute; }
Example 2
Source File: SAML11ParserUtil.java From keycloak with Apache License 2.0 | 6 votes |
/** * 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); }
Example 3
Source File: SAML11ParserUtil.java From keycloak with Apache License 2.0 | 6 votes |
/** * 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; }
Example 4
Source File: XmlPolicyModelUnmarshaller.java From hottub with GNU General Public License v2.0 | 6 votes |
private Attribute getAttributeByName(final StartElement element, final QName attributeName) { // call standard API method to retrieve the attribute by name Attribute attribute = element.getAttributeByName(attributeName); // try to find the attribute without a prefix. if (attribute == null) { final String localAttributeName = attributeName.getLocalPart(); final Iterator iterator = element.getAttributes(); while (iterator.hasNext()) { final Attribute nextAttribute = (Attribute) iterator.next(); final QName aName = nextAttribute.getName(); final boolean attributeFoundByWorkaround = aName.equals(attributeName) || (aName.getLocalPart().equals(localAttributeName) && (aName.getPrefix() == null || "".equals(aName.getPrefix()))); if (attributeFoundByWorkaround) { attribute = nextAttribute; break; } } } return attribute; }
Example 5
Source File: EwsXmlReader.java From ews-java-api with MIT License | 6 votes |
/** * Read attribute value from QName. * * @param qName QName of the attribute * @return Attribute Value * @throws Exception thrown if attribute value can not be read */ private String readAttributeValue(QName qName) throws Exception { if (this.presentEvent.isStartElement()) { StartElement startElement = this.presentEvent.asStartElement(); Attribute attr = startElement.getAttributeByName(qName); if (null != attr) { return attr.getValue(); } else { return null; } } else { String errMsg = String.format("Could not fetch attribute %s", qName .toString()); throw new Exception(errMsg); } }
Example 6
Source File: XmlPolicyModelUnmarshaller.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
private Attribute getAttributeByName(final StartElement element, final QName attributeName) { // call standard API method to retrieve the attribute by name Attribute attribute = element.getAttributeByName(attributeName); // try to find the attribute without a prefix. if (attribute == null) { final String localAttributeName = attributeName.getLocalPart(); final Iterator iterator = element.getAttributes(); while (iterator.hasNext()) { final Attribute nextAttribute = (Attribute) iterator.next(); final QName aName = nextAttribute.getName(); final boolean attributeFoundByWorkaround = aName.equals(attributeName) || (aName.getLocalPart().equals(localAttributeName) && (aName.getPrefix() == null || "".equals(aName.getPrefix()))); if (attributeFoundByWorkaround) { attribute = nextAttribute; break; } } } return attribute; }
Example 7
Source File: ArtifactsXmlAbsoluteUrlRemover.java From nexus-repository-p2 with Eclipse Public License 1.0 | 6 votes |
private XMLEvent changeLocationAttribute( final StartElement locationElement, final URI remoteUrl) { QName locationAttrName = new QName("location"); Attribute locationAttribute = locationElement.getAttributeByName(locationAttrName); if (locationAttribute == null) { return locationElement; } String value = locationAttribute.getValue(); URI uri = URI.create(value); String assetPath = escapeUriToPath(uri.isAbsolute() ? uri.toString() : remoteUrl.resolve(uri).toString()); StartElement startElement = XMLEventFactory.newInstance().createStartElement(new QName("child"), Collections.singletonList( XMLEventFactory.newInstance() .createAttribute(locationAttrName, assetPath)) .iterator(), Collections.emptyIterator()); return startElement; }
Example 8
Source File: XmlPolicyModelUnmarshaller.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
private Attribute getAttributeByName(final StartElement element, final QName attributeName) { // call standard API method to retrieve the attribute by name Attribute attribute = element.getAttributeByName(attributeName); // try to find the attribute without a prefix. if (attribute == null) { final String localAttributeName = attributeName.getLocalPart(); final Iterator iterator = element.getAttributes(); while (iterator.hasNext()) { final Attribute nextAttribute = (Attribute) iterator.next(); final QName aName = nextAttribute.getName(); final boolean attributeFoundByWorkaround = aName.equals(attributeName) || (aName.getLocalPart().equals(localAttributeName) && (aName.getPrefix() == null || "".equals(aName.getPrefix()))); if (attributeFoundByWorkaround) { attribute = nextAttribute; break; } } } return attribute; }
Example 9
Source File: SAML11ResponseParser.java From keycloak with Apache License 2.0 | 5 votes |
/** * @see {@link ParserNamespaceSupport#parse(XMLEventReader)} */ public Object parse(XMLEventReader xmlEventReader) throws ParsingException { // Get the startelement StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader); StaxParserUtil.validate(startElement, RESPONSE); Attribute idAttr = startElement.getAttributeByName(new QName(SAML11Constants.RESPONSE_ID)); if (idAttr == null) throw logger.parserRequiredAttribute(SAML11Constants.RESPONSE_ID); String id = StaxParserUtil.getAttributeValue(idAttr); Attribute issueInstant = startElement.getAttributeByName(new QName(SAML11Constants.ISSUE_INSTANT)); if (issueInstant == null) throw logger.parserRequiredAttribute(SAML11Constants.ISSUE_INSTANT); XMLGregorianCalendar issueInstantVal = XMLTimeUtil.parse(StaxParserUtil.getAttributeValue(issueInstant)); SAML11ResponseType response = new SAML11ResponseType(id, issueInstantVal); while (xmlEventReader.hasNext()) { // Let us peek at the next start element startElement = StaxParserUtil.peekNextStartElement(xmlEventReader); if (startElement == null) break; String elementName = StaxParserUtil.getElementName(startElement); if (JBossSAMLConstants.SIGNATURE.get().equals(elementName)) { Element sig = StaxParserUtil.getDOMElement(xmlEventReader); response.setSignature(sig); } else if (JBossSAMLConstants.ASSERTION.get().equals(elementName)) { SAML11AssertionParser assertionParser = new SAML11AssertionParser(); response.add((SAML11AssertionType) assertionParser.parse(xmlEventReader)); } else if (JBossSAMLConstants.STATUS.get().equals(elementName)) { response.setStatus(parseStatus(xmlEventReader)); } else throw logger.parserUnknownStartElement(elementName, startElement.getLocation()); } return response; }
Example 10
Source File: SAML11RequestParser.java From keycloak with Apache License 2.0 | 5 votes |
protected SAML11RequestType parseRequiredAttributes(StartElement startElement) throws ParsingException { Attribute idAttr = startElement.getAttributeByName(new QName(SAML11Constants.REQUEST_ID)); if (idAttr == null) throw logger.parserRequiredAttribute(SAML11Constants.REQUEST_ID); String id = StaxParserUtil.getAttributeValue(idAttr); Attribute issueInstantAttr = startElement.getAttributeByName(new QName(SAML11Constants.ISSUE_INSTANT)); if (issueInstantAttr == null) throw logger.parserRequiredAttribute(SAML11Constants.ISSUE_INSTANT); XMLGregorianCalendar issueInstant = XMLTimeUtil.parse(StaxParserUtil.getAttributeValue(issueInstantAttr)); return new SAML11RequestType(id, issueInstant); }
Example 11
Source File: Metadata.java From olingo-odata4 with Apache License 2.0 | 5 votes |
private org.apache.olingo.fit.metadata.Property getProperty(final StartElement start) throws XMLStreamException { final org.apache.olingo.fit.metadata.Property property = new org.apache.olingo.fit.metadata.Property(start.getAttributeByName(new QName("Name")).getValue()); final Attribute type = start.getAttributeByName(new QName("Type")); property.setType(type == null ? "Edm.String" : type.getValue()); final Attribute nullable = start.getAttributeByName(new QName("Nullable")); property.setNullable(nullable == null || !"false".equals(nullable.getValue())); return property; }
Example 12
Source File: Metadata.java From olingo-odata4 with Apache License 2.0 | 5 votes |
private NavigationProperty getNavigationProperty(final StartElement start) throws XMLStreamException { final NavigationProperty property = new NavigationProperty(start.getAttributeByName(new QName("Name")).getValue()); final Attribute type = start.getAttributeByName(new QName("Type")); if (type != null) { property.setType(type.getValue()); } return property; }
Example 13
Source File: ODataXmlDeserializer.java From olingo-odata4 with Apache License 2.0 | 5 votes |
private Object complex(final XMLEventReader reader, final StartElement start, final EdmComplexType edmComplex) throws XMLStreamException, EdmPrimitiveTypeException, DeserializerException { ComplexValue value = new ComplexValue(); EdmType resolvedType = edmComplex; boolean foundEndProperty = false; while (reader.hasNext() && !foundEndProperty) { final XMLEvent event = reader.nextEvent(); if (event.isStartElement()) { //Get the derived type from the element tag final Attribute attrType = start.getAttributeByName(typeQName); if (attrType != null ) { String type = new EdmTypeInfo.Builder().setTypeExpression(attrType.getValue()).build().internal(); if (type.startsWith("Collection(") && type.endsWith(")")) { type = type.substring(11, type.length()-1); } resolvedType = getDerivedType(edmComplex, type); } StartElement se = event.asStartElement(); EdmProperty p = (EdmProperty) ((EdmComplexType)resolvedType).getProperty(se.getName().getLocalPart()); value.getValue().add(property(reader, se, p.getType(), p.isNullable(), p.getMaxLength(), p.getPrecision(), p.getScale(), p.isUnicode(), p.isCollection())); value.setTypeName(resolvedType.getFullQualifiedName().getFullQualifiedNameAsString()); } if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) { foundEndProperty = true; } } return value; }
Example 14
Source File: ODataXmlDeserializer.java From olingo-odata4 with Apache License 2.0 | 5 votes |
private String entityRef(final XMLEventReader reader, final StartElement start) throws XMLStreamException { boolean foundEndElement = false; final Attribute entityRefId = start.getAttributeByName(Constants.QNAME_ATOM_ATTR_ID); while (reader.hasNext() && !foundEndElement) { final XMLEvent event = reader.nextEvent(); if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) { foundEndElement = true; } } return entityRefId.getValue(); }
Example 15
Source File: SAML11ParserUtil.java From keycloak with Apache License 2.0 | 4 votes |
/** * 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; }
Example 16
Source File: SAML11ParserUtil.java From keycloak with Apache License 2.0 | 4 votes |
public static SAML11AuthorizationDecisionStatementType parseSAML11AuthorizationDecisionStatement( XMLEventReader xmlEventReader) throws ParsingException { SAML11AuthorizationDecisionStatementType authzDecision = null; StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader); StaxParserUtil.validate(startElement, SAML11Constants.AUTHORIZATION_DECISION_STATEMENT); Attribute decision = startElement.getAttributeByName(new QName(SAML11Constants.DECISION)); if (decision == null) throw logger.parserRequiredAttribute("Decision"); String decisionValue = StaxParserUtil.getAttributeValue(decision); Attribute resource = startElement.getAttributeByName(new QName(SAML11Constants.RESOURCE)); if (resource == null) throw logger.parserRequiredAttribute("Namespace"); String resValue = StaxParserUtil.getAttributeValue(resource); authzDecision = new SAML11AuthorizationDecisionStatementType(URI.create(resValue), SAML11DecisionType.valueOf(decisionValue)); while (xmlEventReader.hasNext()) { XMLEvent xmlEvent = StaxParserUtil.peek(xmlEventReader); if (xmlEvent instanceof EndElement) { EndElement end = StaxParserUtil.getNextEndElement(xmlEventReader); if (StaxParserUtil.matches(end, SAML11Constants.AUTHORIZATION_DECISION_STATEMENT)) break; } startElement = StaxParserUtil.peekNextStartElement(xmlEventReader); if (startElement == null) break; String tag = StaxParserUtil.getElementName(startElement); if (SAML11Constants.ACTION.equals(tag)) { startElement = StaxParserUtil.getNextStartElement(xmlEventReader); SAML11ActionType samlAction = new SAML11ActionType(); Attribute namespaceAttr = startElement.getAttributeByName(new QName(SAML11Constants.NAMESPACE)); if (namespaceAttr != null) { samlAction.setNamespace(StaxParserUtil.getAttributeValue(namespaceAttr)); } samlAction.setValue(StaxParserUtil.getElementText(xmlEventReader)); authzDecision.addAction(samlAction); } else if (JBossSAMLConstants.SUBJECT.get().equals(tag)) { SAML11SubjectParser parser = new SAML11SubjectParser(); authzDecision.setSubject((SAML11SubjectType) parser.parse(xmlEventReader)); } else throw logger.parserUnknownTag(tag, startElement.getLocation()); } return authzDecision; }
Example 17
Source File: SAML11ParserUtil.java From keycloak with Apache License 2.0 | 4 votes |
/** * Parse {@link org.keycloak.dom.saml.v1.assertion.SAML11ConditionsType} * * @param xmlEventReader * * @return * * @throws ParsingException */ public static SAML11ConditionsType parseSAML11Conditions(XMLEventReader xmlEventReader) throws ParsingException { StartElement startElement; SAML11ConditionsType conditions = new SAML11ConditionsType(); StartElement conditionsElement = StaxParserUtil.getNextStartElement(xmlEventReader); StaxParserUtil.validate(conditionsElement, JBossSAMLConstants.CONDITIONS.get()); String assertionNS = SAML11Constants.ASSERTION_11_NSURI; QName notBeforeQName = new QName("", JBossSAMLConstants.NOT_BEFORE.get()); QName notBeforeQNameWithNS = new QName(assertionNS, JBossSAMLConstants.NOT_BEFORE.get()); QName notAfterQName = new QName("", JBossSAMLConstants.NOT_ON_OR_AFTER.get()); QName notAfterQNameWithNS = new QName(assertionNS, JBossSAMLConstants.NOT_ON_OR_AFTER.get()); Attribute notBeforeAttribute = conditionsElement.getAttributeByName(notBeforeQName); if (notBeforeAttribute == null) notBeforeAttribute = conditionsElement.getAttributeByName(notBeforeQNameWithNS); Attribute notAfterAttribute = conditionsElement.getAttributeByName(notAfterQName); if (notAfterAttribute == null) notAfterAttribute = conditionsElement.getAttributeByName(notAfterQNameWithNS); if (notBeforeAttribute != null) { String notBeforeValue = StaxParserUtil.getAttributeValue(notBeforeAttribute); conditions.setNotBefore(XMLTimeUtil.parse(notBeforeValue)); } if (notAfterAttribute != null) { String notAfterValue = StaxParserUtil.getAttributeValue(notAfterAttribute); conditions.setNotOnOrAfter(XMLTimeUtil.parse(notAfterValue)); } while (xmlEventReader.hasNext()) { XMLEvent xmlEvent = StaxParserUtil.peek(xmlEventReader); if (xmlEvent instanceof EndElement) { EndElement end = StaxParserUtil.getNextEndElement(xmlEventReader); if (StaxParserUtil.matches(end, JBossSAMLConstants.CONDITIONS.get())) break; } startElement = StaxParserUtil.peekNextStartElement(xmlEventReader); if (startElement == null) break; String tag = StaxParserUtil.getElementName(startElement); if (SAML11Constants.AUDIENCE_RESTRICTION_CONDITION.equals(tag)) { startElement = StaxParserUtil.getNextStartElement(xmlEventReader); SAML11AudienceRestrictionCondition restrictCond = new SAML11AudienceRestrictionCondition(); startElement = StaxParserUtil.getNextStartElement(xmlEventReader); if (StaxParserUtil.getElementName(startElement).equals(JBossSAMLConstants.AUDIENCE.get())) { restrictCond.add(URI.create(StaxParserUtil.getElementText(xmlEventReader))); } EndElement theEndElement = StaxParserUtil.getNextEndElement(xmlEventReader); StaxParserUtil.validate(theEndElement, SAML11Constants.AUDIENCE_RESTRICTION_CONDITION); conditions.add(restrictCond); } else throw logger.parserUnknownTag(tag, startElement.getLocation()); } return conditions; }
Example 18
Source File: StaxParserUtil.java From keycloak with Apache License 2.0 | 2 votes |
/** * Get the Attribute value * * @param startElement * @param tag localpart of the qname of the attribute * * @return */ public static Duration getXmlDurationAttributeValue(StartElement startElement, HasQName attrName) throws ParsingException { Attribute attr = startElement.getAttributeByName(attrName.getQName()); String value = getAttributeValue(attr); return value == null ? null : XMLTimeUtil.parseAsDuration(value); }
Example 19
Source File: StaxParserUtil.java From keycloak with Apache License 2.0 | 2 votes |
/** * Get the Attribute value, replacing every occurrence of ${..} by corresponding system property value * * @param startElement * @param tag localpart of the qname of the attribute * * @return */ public static Integer getIntegerAttributeValueRP(StartElement startElement, HasQName attrName) { Attribute attr = startElement.getAttributeByName(attrName.getQName()); String value = getAttributeValueRP(attr); return value == null ? null : Integer.valueOf(value); }
Example 20
Source File: StaxParserUtil.java From keycloak with Apache License 2.0 | 2 votes |
/** * Get the Attribute value * * @param startElement * @param tag localpart of the qname of the attribute * * @return */ public static Integer getIntegerAttributeValue(StartElement startElement, HasQName attrName) { Attribute attr = startElement.getAttributeByName(attrName.getQName()); String value = getAttributeValue(attr); return value == null ? null : Integer.valueOf(value); }