Java Code Examples for javax.xml.stream.events.Attribute#getName()

The following examples show how to use javax.xml.stream.events.Attribute#getName() . 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 openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
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: XmlPolicyModelUnmarshaller.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
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 3
Source File: XmlPolicyModelUnmarshaller.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
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 4
Source File: StaxUtils.java    From cxf with Apache License 2.0 6 votes vote down vote up
private static void writeAttributeEvent(XMLEvent event, XMLStreamWriter writer)
    throws XMLStreamException {

    Attribute attr = (Attribute)event;
    QName name = attr.getName();
    String nsURI = name.getNamespaceURI();
    String localName = name.getLocalPart();
    String prefix = name.getPrefix();
    String value = attr.getValue();

    if (prefix != null) {
        writer.writeAttribute(prefix, nsURI, localName, value);
    } else if (nsURI != null) {
        writer.writeAttribute(nsURI, localName, value);
    } else {
        writer.writeAttribute(localName, value);
    }
}
 
Example 5
Source File: TubelineFeatureReader.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public TubelineFeature parse(XMLEventReader reader) throws WebServiceException {
    try {
        final StartElement element = reader.nextEvent().asStartElement();
        boolean attributeEnabled = true;
        final Iterator iterator = element.getAttributes();
        while (iterator.hasNext()) {
            final Attribute nextAttribute = (Attribute) iterator.next();
            final QName attributeName = nextAttribute.getName();
            if (ENABLED_ATTRIBUTE_NAME.equals(attributeName)) {
                attributeEnabled = ParserUtil.parseBooleanValue(nextAttribute.getValue());
            } else if (NAME_ATTRIBUTE_NAME.equals(attributeName)) {
                // TODO use name attribute
            } else {
                // TODO logging message
                throw LOGGER.logSevereException(new WebServiceException("Unexpected attribute"));
            }
        }
        return parseFactories(attributeEnabled, element, reader);
    } catch (XMLStreamException e) {
        throw LOGGER.logSevereException(new WebServiceException("Failed to unmarshal XML document", e));
    }
}
 
Example 6
Source File: XmlPolicyModelUnmarshaller.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
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: TubelineFeatureReader.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public TubelineFeature parse(XMLEventReader reader) throws WebServiceException {
    try {
        final StartElement element = reader.nextEvent().asStartElement();
        boolean attributeEnabled = true;
        final Iterator iterator = element.getAttributes();
        while (iterator.hasNext()) {
            final Attribute nextAttribute = (Attribute) iterator.next();
            final QName attributeName = nextAttribute.getName();
            if (ENABLED_ATTRIBUTE_NAME.equals(attributeName)) {
                attributeEnabled = ParserUtil.parseBooleanValue(nextAttribute.getValue());
            } else if (NAME_ATTRIBUTE_NAME.equals(attributeName)) {
                // TODO use name attribute
            } else {
                // TODO logging message
                throw LOGGER.logSevereException(new WebServiceException("Unexpected attribute"));
            }
        }
        return parseFactories(attributeEnabled, element, reader);
    } catch (XMLStreamException e) {
        throw LOGGER.logSevereException(new WebServiceException("Failed to unmarshal XML document", e));
    }
}
 
Example 8
Source File: TubelineFeatureReader.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public TubelineFeature parse(XMLEventReader reader) throws WebServiceException {
    try {
        final StartElement element = reader.nextEvent().asStartElement();
        boolean attributeEnabled = true;
        final Iterator iterator = element.getAttributes();
        while (iterator.hasNext()) {
            final Attribute nextAttribute = (Attribute) iterator.next();
            final QName attributeName = nextAttribute.getName();
            if (ENABLED_ATTRIBUTE_NAME.equals(attributeName)) {
                attributeEnabled = ParserUtil.parseBooleanValue(nextAttribute.getValue());
            } else if (NAME_ATTRIBUTE_NAME.equals(attributeName)) {
                // TODO use name attribute
            } else {
                // TODO logging message
                throw LOGGER.logSevereException(new WebServiceException("Unexpected attribute"));
            }
        }
        return parseFactories(attributeEnabled, element, reader);
    } catch (XMLStreamException e) {
        throw LOGGER.logSevereException(new WebServiceException("Failed to unmarshal XML document", e));
    }
}
 
Example 9
Source File: XmlPolicyModelUnmarshaller.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private void parseAssertionData(NamespaceVersion nsVersion, String value, ModelNode childNode, final StartElement childElement) throws IllegalArgumentException, PolicyException {
    // finish assertion node processing: create and set assertion data...
    final Map<QName, String> attributeMap = new HashMap<QName, String>();
    boolean optional = false;
    boolean ignorable = false;

    final Iterator iterator = childElement.getAttributes();
    while (iterator.hasNext()) {
        final Attribute nextAttribute = (Attribute) iterator.next();
        final QName name = nextAttribute.getName();
        if (attributeMap.containsKey(name)) {
            throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0059_MULTIPLE_ATTRS_WITH_SAME_NAME_DETECTED_FOR_ASSERTION(nextAttribute.getName(), childElement.getName())));
        } else {
            if (nsVersion.asQName(XmlToken.Optional).equals(name)) {
                optional = parseBooleanValue(nextAttribute.getValue());
            } else if (nsVersion.asQName(XmlToken.Ignorable).equals(name)) {
                ignorable = parseBooleanValue(nextAttribute.getValue());
            } else {
                attributeMap.put(name, nextAttribute.getValue());
            }
        }
    }
    final AssertionData nodeData = new AssertionData(childElement.getName(), value, attributeMap, childNode.getType(), optional, ignorable);

    // check visibility value syntax if present...
    if (nodeData.containsAttribute(PolicyConstants.VISIBILITY_ATTRIBUTE)) {
        final String visibilityValue = nodeData.getAttributeValue(PolicyConstants.VISIBILITY_ATTRIBUTE);
        if (!PolicyConstants.VISIBILITY_VALUE_PRIVATE.equals(visibilityValue)) {
            throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0004_UNEXPECTED_VISIBILITY_ATTR_VALUE(visibilityValue)));
        }
    }

    childNode.setOrReplaceNodeData(nodeData);
}
 
Example 10
Source File: StAXEventConnector.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the attributes associated with the given START_ELEMENT StAXevent.
 *
 * @return the StAX attributes converted to an org.xml.sax.Attributes
 */
private Attributes getAttributes(StartElement event) {
    attrs.clear();

    // in SAX, namespace declarations are not part of attributes by default.
    // (there's a property to control that, but as far as we are concerned
    // we don't use it.) So don't add xmlns:* to attributes.

    // gather non-namespace attrs
    for (Iterator i = event.getAttributes(); i.hasNext();) {
        Attribute staxAttr = (Attribute)i.next();

        QName name = staxAttr.getName();
        String uri = fixNull(name.getNamespaceURI());
        String localName = name.getLocalPart();
        String prefix = name.getPrefix();
        String qName;
        if (prefix == null || prefix.length() == 0)
            qName = localName;
        else
            qName = prefix + ':' + localName;
        String type = staxAttr.getDTDType();
        String value = staxAttr.getValue();

        attrs.addAttribute(uri, localName, qName, type, value);
    }

    return attrs;
}
 
Example 11
Source File: AclConfigParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private ACLProviderEntry getEntry(XMLEventReader xmlEventReader) throws XMLStreamException
{
   XMLEvent xmlEvent = xmlEventReader.nextEvent(); 
   Map<String, Object> options = new HashMap<String,Object>();
   
   
   String codeName = null;
   ControlFlag controlFlag = ControlFlag.REQUIRED;
   
   //We got the login-module element
   StartElement policyModuleElement = (StartElement) xmlEvent;
   //We got the login-module element
   Iterator<Attribute> attrs = policyModuleElement.getAttributes();
   while(attrs.hasNext())
   {
      Attribute attribute = attrs.next();
      
      QName attQName = attribute.getName();
      String attributeValue = StaxParserUtil.getAttributeValue(attribute);
      if("code".equals(attQName.getLocalPart()))
      {
         codeName = attributeValue; 
      }
      else if("flag".equals(attQName.getLocalPart()))
      {
         controlFlag = ControlFlag.valueOf(attributeValue);
      } 
   } 
   //See if there are options
   ModuleOptionParser moParser = new ModuleOptionParser();
   options.putAll(moParser.parse(xmlEventReader));
   
   ACLProviderEntry entry =  new ACLProviderEntry(codeName, options); 
   entry.setControlFlag(controlFlag);
   return entry;
}
 
Example 12
Source File: StAXEventConnector.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the attributes associated with the given START_ELEMENT StAXevent.
 *
 * @return the StAX attributes converted to an org.xml.sax.Attributes
 */
private Attributes getAttributes(StartElement event) {
    attrs.clear();

    // in SAX, namespace declarations are not part of attributes by default.
    // (there's a property to control that, but as far as we are concerned
    // we don't use it.) So don't add xmlns:* to attributes.

    // gather non-namespace attrs
    for (Iterator i = event.getAttributes(); i.hasNext();) {
        Attribute staxAttr = (Attribute)i.next();

        QName name = staxAttr.getName();
        String uri = fixNull(name.getNamespaceURI());
        String localName = name.getLocalPart();
        String prefix = name.getPrefix();
        String qName;
        if (prefix == null || prefix.length() == 0)
            qName = localName;
        else
            qName = prefix + ':' + localName;
        String type = staxAttr.getDTDType();
        String value = staxAttr.getValue();

        attrs.addAttribute(uri, localName, qName, type, value);
    }

    return attrs;
}
 
Example 13
Source File: XmlPolicyModelUnmarshaller.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private void parseAssertionData(NamespaceVersion nsVersion, String value, ModelNode childNode, final StartElement childElement) throws IllegalArgumentException, PolicyException {
    // finish assertion node processing: create and set assertion data...
    final Map<QName, String> attributeMap = new HashMap<QName, String>();
    boolean optional = false;
    boolean ignorable = false;

    final Iterator iterator = childElement.getAttributes();
    while (iterator.hasNext()) {
        final Attribute nextAttribute = (Attribute) iterator.next();
        final QName name = nextAttribute.getName();
        if (attributeMap.containsKey(name)) {
            throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0059_MULTIPLE_ATTRS_WITH_SAME_NAME_DETECTED_FOR_ASSERTION(nextAttribute.getName(), childElement.getName())));
        } else {
            if (nsVersion.asQName(XmlToken.Optional).equals(name)) {
                optional = parseBooleanValue(nextAttribute.getValue());
            } else if (nsVersion.asQName(XmlToken.Ignorable).equals(name)) {
                ignorable = parseBooleanValue(nextAttribute.getValue());
            } else {
                attributeMap.put(name, nextAttribute.getValue());
            }
        }
    }
    final AssertionData nodeData = new AssertionData(childElement.getName(), value, attributeMap, childNode.getType(), optional, ignorable);

    // check visibility value syntax if present...
    if (nodeData.containsAttribute(PolicyConstants.VISIBILITY_ATTRIBUTE)) {
        final String visibilityValue = nodeData.getAttributeValue(PolicyConstants.VISIBILITY_ATTRIBUTE);
        if (!PolicyConstants.VISIBILITY_VALUE_PRIVATE.equals(visibilityValue)) {
            throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0004_UNEXPECTED_VISIBILITY_ATTR_VALUE(visibilityValue)));
        }
    }

    childNode.setOrReplaceNodeData(nodeData);
}
 
Example 14
Source File: NonNsStreamWriter.java    From woodstox with Apache License 2.0 5 votes vote down vote up
@Override
public void writeStartElement(StartElement elem)
       throws XMLStreamException
   {
       QName name = elem.getName();
       writeStartElement(name.getLocalPart());
       @SuppressWarnings("unchecked")
       Iterator<Attribute> it = elem.getAttributes();
       while (it.hasNext()) {
           Attribute attr = it.next();
           name = attr.getName();
           writeAttribute(name.getLocalPart(), attr.getValue());
       }
   }
 
Example 15
Source File: StAXEventConnector.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the attributes associated with the given START_ELEMENT StAXevent.
 *
 * @return the StAX attributes converted to an org.xml.sax.Attributes
 */
private Attributes getAttributes(StartElement event) {
    attrs.clear();

    // in SAX, namespace declarations are not part of attributes by default.
    // (there's a property to control that, but as far as we are concerned
    // we don't use it.) So don't add xmlns:* to attributes.

    // gather non-namespace attrs
    for (Iterator i = event.getAttributes(); i.hasNext();) {
        Attribute staxAttr = (Attribute)i.next();

        QName name = staxAttr.getName();
        String uri = fixNull(name.getNamespaceURI());
        String localName = name.getLocalPart();
        String prefix = name.getPrefix();
        String qName;
        if (prefix == null || prefix.length() == 0)
            qName = localName;
        else
            qName = prefix + ':' + localName;
        String type = staxAttr.getDTDType();
        String value = staxAttr.getValue();

        attrs.addAttribute(uri, localName, qName, type, value);
    }

    return attrs;
}
 
Example 16
Source File: StAXEventConnector.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the attributes associated with the given START_ELEMENT StAXevent.
 *
 * @return the StAX attributes converted to an org.xml.sax.Attributes
 */
private Attributes getAttributes(StartElement event) {
    attrs.clear();

    // in SAX, namespace declarations are not part of attributes by default.
    // (there's a property to control that, but as far as we are concerned
    // we don't use it.) So don't add xmlns:* to attributes.

    // gather non-namespace attrs
    for (Iterator i = event.getAttributes(); i.hasNext();) {
        Attribute staxAttr = (Attribute)i.next();

        QName name = staxAttr.getName();
        String uri = fixNull(name.getNamespaceURI());
        String localName = name.getLocalPart();
        String prefix = name.getPrefix();
        String qName;
        if (prefix == null || prefix.length() == 0)
            qName = localName;
        else
            qName = prefix + ':' + localName;
        String type = staxAttr.getDTDType();
        String value = staxAttr.getValue();

        attrs.addAttribute(uri, localName, qName, type, value);
    }

    return attrs;
}
 
Example 17
Source File: IdentityTrustConfigParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private IdentityTrustModuleEntry getEntry(XMLEventReader xmlEventReader) throws XMLStreamException
{
   XMLEvent xmlEvent = xmlEventReader.nextEvent(); 
   Map<String, Object> options = new HashMap<String,Object>(); 
   
   String codeName = null; 
   ControlFlag flag = null;
   
   //We got the login-module element
   StartElement policyModuleElement = (StartElement) xmlEvent;
   //We got the login-module element
   Iterator<Attribute> attrs = policyModuleElement.getAttributes();
   while(attrs.hasNext())
   {
      Attribute attribute = attrs.next();
      
      QName attQName = attribute.getName();
      String attributeValue = StaxParserUtil.getAttributeValue(attribute);
      
      if("code".equals(attQName.getLocalPart()))
      {
         codeName = attributeValue; 
      }
      if ("flag".equals(attQName.getLocalPart()))
      {
         flag = ControlFlag.valueOf(attributeValue);
      }
   } 
   //See if there are options
   ModuleOptionParser moParser = new ModuleOptionParser();
   options.putAll(moParser.parse(xmlEventReader));
   
   IdentityTrustModuleEntry entry =  new IdentityTrustModuleEntry(codeName, options);  
   entry.setControlFlag(flag);
   return entry;
}
 
Example 18
Source File: AuthenticationJASPIConfigParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private AppConfigurationEntry getJAASEntry(XMLEventReader xmlEventReader) throws XMLStreamException
{
   XMLEvent xmlEvent = xmlEventReader.nextEvent();
   Map<String, Object> options = new HashMap<String, Object>();

   String codeName = null;
   LoginModuleControlFlag controlFlag = LoginModuleControlFlag.REQUIRED;

   //We got the login-module element
   StartElement loginModuleElement = (StartElement) xmlEvent;
   //We got the login-module element
   Iterator<Attribute> attrs = loginModuleElement.getAttributes();
   while (attrs.hasNext())
   {
      Attribute attribute = attrs.next();
      QName attQName = attribute.getName();
      String attributeValue = StaxParserUtil.getAttributeValue(attribute);

      if ("code".equals(attQName.getLocalPart()))
      {
         codeName = attributeValue;
      }
      else if ("flag".equals(attQName.getLocalPart()))
      {
         controlFlag = getControlFlag(attributeValue);
      }
   }
   //See if there are options
   ModuleOptionParser moParser = new ModuleOptionParser();
   options.putAll(moParser.parse(xmlEventReader));

   return new AppConfigurationEntry(codeName, controlFlag, options);
}
 
Example 19
Source File: TransformedEvent.java    From sis with Apache License 2.0 4 votes vote down vote up
/** Cast or wrap the given attribute to an {@code Attr} instance. */
static Attr castOrWrap(final Attribute a) {
    if (a instanceof Attr) return (Attr) a;
    else return new Attr(a, a.getName());
}
 
Example 20
Source File: ApplicationPolicyParser.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public List<ApplicationPolicy> parse(XMLEventReader xmlEventReader) throws XMLStreamException
{
   List<ApplicationPolicy> policies = new ArrayList<ApplicationPolicy>();
   while(xmlEventReader.hasNext())
   { 
       XMLEvent xmlEvent = xmlEventReader.nextEvent();
       int eventType = xmlEvent.getEventType();
       switch(eventType)
       {
           case XMLStreamConstants.START_ELEMENT:  
              StartElement appPolicyElement = (StartElement) xmlEvent;
              String elementName = StaxParserUtil.getStartElementName(appPolicyElement);
              if("application-policy".equals(elementName) == false)
                 throw StaxParserUtil.unexpectedElement(elementName, xmlEvent);
              //We got the application-policy element. It just has one attribute "name"
              Iterator<Attribute> attrs = appPolicyElement.getAttributes(); 
              String extendsName = null;
              String appPolicyName = null;
              
              while(attrs.hasNext())
              {
                 Attribute attribute = attrs.next();
                 QName attributeName = attribute.getName();
                 String attributeValue = StaxParserUtil.getAttributeValue(attribute);
                 
                 if("name".equals(attributeName.getLocalPart()))
                    appPolicyName = attributeValue; 
                 else if("extends".equals(attributeName.getLocalPart()))
                    extendsName = attributeValue;  
              }

              ApplicationPolicy applicationPolicy = new ApplicationPolicy(appPolicyName); 
              if(extendsName != null)
                 applicationPolicy.setBaseApplicationPolicyName(extendsName);
               
              route(xmlEventReader, applicationPolicy);
              policies.add(applicationPolicy); 
       } 
   }
   return policies;
}