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

The following examples show how to use javax.xml.stream.events.StartElement#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: StaxEventXMLReader.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private void handleStartElement(StartElement startElement) throws SAXException {
	if (getContentHandler() != null) {
		QName qName = startElement.getName();
		if (hasNamespacesFeature()) {
			for (Iterator i = startElement.getNamespaces(); i.hasNext();) {
				Namespace namespace = (Namespace) i.next();
				startPrefixMapping(namespace.getPrefix(), namespace.getNamespaceURI());
			}
			for (Iterator i = startElement.getAttributes(); i.hasNext();){
				Attribute attribute = (Attribute) i.next();
				QName attributeName = attribute.getName();
				startPrefixMapping(attributeName.getPrefix(), attributeName.getNamespaceURI());
			}

			getContentHandler().startElement(qName.getNamespaceURI(), qName.getLocalPart(), toQualifiedName(qName),
					getAttributes(startElement));
		}
		else {
			getContentHandler().startElement("", "", toQualifiedName(qName), getAttributes(startElement));
		}
	}
}
 
Example 2
Source File: SearchHandle.java    From java-client-api with Apache License 2.0 6 votes vote down vote up
private void handleTop(XMLEventReader reader, StartElement element) throws XMLStreamException {
  QName name = element.getName();
  if (!SEARCH_NS.equals(name.getNamespaceURI())) {
    logger.warn("unexpected top element "+name.toString());
    return;
  }

  String localName = name.getLocalPart();

  if ("response".equals(localName))           { handleResponse(reader, element);
  } else if ("result".equals(localName))      { handleResult(reader, element);
  } else if ("facet".equals(localName))       { handleFacet(reader, element);
  } else if ("boxes".equals(localName))       { handleGeoFacet(reader, element);
  } else if ("qtext".equals(localName))       { handleQText(reader, element);
  } else if ("query".equals(localName))       { handleQuery(reader, element);
  } else if ("constraint".equals(localName))  { handleConstraint(reader, element);
  } else if ("warning".equals(localName))     { handleWarning(reader, element);
  } else if ("report".equals(localName))      { handleReport(reader, element);
  } else if ("plan".equals(localName))        { handlePlan(reader, element);
  } else if ("metrics".equals(localName))     { handleMetrics(reader, element);
  } else {
    logger.warn("Unexpected top search element "+name.toString());
  }
}
 
Example 3
Source File: StAXEventConnector.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private void handleStartElement(StartElement event) throws SAXException {
    // start namespace bindings
    for (Iterator i = event.getNamespaces(); i.hasNext();) {
        Namespace ns = (Namespace)i.next();
        visitor.startPrefixMapping(
            fixNull(ns.getPrefix()),
            fixNull(ns.getNamespaceURI()));
    }

    // fire startElement
    QName qName = event.getName();
    tagName.uri = fixNull(qName.getNamespaceURI());
    String localName = qName.getLocalPart();
    tagName.uri = fixNull(qName.getNamespaceURI());
    tagName.local = localName;
    tagName.atts = getAttributes(event);
    visitor.startElement(tagName);

    seenText = false;
}
 
Example 4
Source File: XmlFilterReader.java    From knox with Apache License 2.0 6 votes vote down vote up
private Element bufferElement( StartElement event ) {
  QName qname = event.getName();
  String prefix = qname.getPrefix();
  String uri = qname.getNamespaceURI();
  Element element;
  if( uri == null || uri.isEmpty() ) {
    element = document.createElement( qname.getLocalPart() );
  } else {
    element = document.createElementNS( qname.getNamespaceURI(), qname.getLocalPart() );
    if( prefix != null && !prefix.isEmpty() ) {
      element.setPrefix( prefix );
    }
  }
  // Always need to buffer the namespaces regardless of what else happens so that XPath will work on attributes
  // namespace qualified attributes.
  bufferNamespaces( event, element );
  return element;
}
 
Example 5
Source File: StAXEventConnector.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private void handleStartElement(StartElement event) throws SAXException {
    // start namespace bindings
    for (Iterator i = event.getNamespaces(); i.hasNext();) {
        Namespace ns = (Namespace)i.next();
        visitor.startPrefixMapping(
            fixNull(ns.getPrefix()),
            fixNull(ns.getNamespaceURI()));
    }

    // fire startElement
    QName qName = event.getName();
    tagName.uri = fixNull(qName.getNamespaceURI());
    String localName = qName.getLocalPart();
    tagName.uri = fixNull(qName.getNamespaceURI());
    tagName.local = localName;
    tagName.atts = getAttributes(event);
    visitor.startElement(tagName);

    seenText = false;
}
 
Example 6
Source File: SAMLParser.java    From keycloak with Apache License 2.0 6 votes vote down vote up
/**
 * @see {@link org.keycloak.saml.common.parsers.ParserNamespaceSupport#parse(XMLEventReader)}
 */
@Override
public Object parse(XMLEventReader xmlEventReader) throws ParsingException {
    while (xmlEventReader.hasNext()) {
        XMLEvent xmlEvent = StaxParserUtil.peek(xmlEventReader);

        if (xmlEvent instanceof StartElement) {
            StartElement startElement = (StartElement) xmlEvent;
            final QName name = startElement.getName();

            ParserFactory pf = PARSERS.get(name);
            if (pf == null) {
                throw logger.parserException(new RuntimeException(ErrorCodes.UNKNOWN_START_ELEMENT + name + "::location="
                        + startElement.getLocation()));
            }

            return pf.create().parse(xmlEventReader);
        }

        StaxParserUtil.getNextEvent(xmlEventReader);
    }

    throw new RuntimeException(ErrorCodes.FAILED_PARSING + "SAML Parsing has failed");
}
 
Example 7
Source File: StAXEventConnector.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private void handleStartElement(StartElement event) throws SAXException {
    // start namespace bindings
    for (Iterator i = event.getNamespaces(); i.hasNext();) {
        Namespace ns = (Namespace)i.next();
        visitor.startPrefixMapping(
            fixNull(ns.getPrefix()),
            fixNull(ns.getNamespaceURI()));
    }

    // fire startElement
    QName qName = event.getName();
    tagName.uri = fixNull(qName.getNamespaceURI());
    String localName = qName.getLocalPart();
    tagName.uri = fixNull(qName.getNamespaceURI());
    tagName.local = localName;
    tagName.atts = getAttributes(event);
    visitor.startElement(tagName);

    seenText = false;
}
 
Example 8
Source File: XmlFilterReader.java    From knox with Apache License 2.0 6 votes vote down vote up
private void streamElement( StartElement event, Element element ) throws XPathExpressionException {
  writer.write( "<" );
  QName qname = event.getName();
  String prefix = event.getName().getPrefix();
  if( prefix != null && !prefix.isEmpty() ) {
    writer.write( prefix );
    writer.write( ":" );
  }
  writer.write( qname.getLocalPart() );
  streamNamespaces( event );
  streamAttributes( event, element );
  if( isEmptyElement ) {
    writer.write("/>");
  } else {
    writer.write(">");
  }
}
 
Example 9
Source File: AbstractKeycloakSamlAdapterV1Parser.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
protected void validateStartElement(StartElement startElement) {
    QName name = startElement.getName();
    QName validatedQName = ALTERNATE_NAMESPACES.contains(name.getNamespaceURI())
      ? new QName(name.getNamespaceURI(), expectedStartElement.getLocalPart())
      : expectedStartElement;
    StaxParserUtil.validate(startElement, validatedQName);
}
 
Example 10
Source File: ExternalAttachmentsUnmarshaller.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private void processStartTag(final StartElement element, final StartElement parent,
        final XMLEventReader reader, final Map<URI, Policy> map)
        throws PolicyException {
    try {
        final QName name = element.getName();
        if (parent == null) {
            if (!name.equals(POLICIES)) {
                throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0089_EXPECTED_ELEMENT("<Policies>", name, element.getLocation())));
            }
        } else {
            final QName parentName = parent.getName();
            if (parentName.equals(POLICIES)) {
                if (!name.equals(POLICY_ATTACHMENT)) {
                    throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0089_EXPECTED_ELEMENT("<PolicyAttachment>", name, element.getLocation())));
                }
            } else if (parentName.equals(POLICY_ATTACHMENT)) {
                if (name.equals(POLICY)) {
                    readPolicy(reader);
                    return;
                } else if (!name.equals(APPLIES_TO)) {
                    throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0089_EXPECTED_ELEMENT("<AppliesTo> or <Policy>", name, element.getLocation())));
                }
            } else if (parentName.equals(APPLIES_TO)) {
                if (!name.equals(URI)) {
                    throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0089_EXPECTED_ELEMENT("<URI>", name, element.getLocation())));
                }
            } else {
                throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0090_UNEXPECTED_ELEMENT(name, element.getLocation())));
            }
        }
        reader.nextEvent();
        this.unmarshal(reader, element);
    } catch (XMLStreamException e) {
        throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0088_FAILED_PARSE(element.getLocation()), e));
    }
}
 
Example 11
Source File: XmlPolicyModelUnmarshaller.java    From hottub 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 12
Source File: XmlPolicyModelUnmarshaller.java    From jdk8u60 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 13
Source File: ExternalAttachmentsUnmarshaller.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private void processStartTag(final StartElement element, final StartElement parent,
        final XMLEventReader reader, final Map<URI, Policy> map)
        throws PolicyException {
    try {
        final QName name = element.getName();
        if (parent == null) {
            if (!name.equals(POLICIES)) {
                throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0089_EXPECTED_ELEMENT("<Policies>", name, element.getLocation())));
            }
        } else {
            final QName parentName = parent.getName();
            if (parentName.equals(POLICIES)) {
                if (!name.equals(POLICY_ATTACHMENT)) {
                    throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0089_EXPECTED_ELEMENT("<PolicyAttachment>", name, element.getLocation())));
                }
            } else if (parentName.equals(POLICY_ATTACHMENT)) {
                if (name.equals(POLICY)) {
                    readPolicy(reader);
                    return;
                } else if (!name.equals(APPLIES_TO)) {
                    throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0089_EXPECTED_ELEMENT("<AppliesTo> or <Policy>", name, element.getLocation())));
                }
            } else if (parentName.equals(APPLIES_TO)) {
                if (!name.equals(URI)) {
                    throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0089_EXPECTED_ELEMENT("<URI>", name, element.getLocation())));
                }
            } else {
                throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0090_UNEXPECTED_ELEMENT(name, element.getLocation())));
            }
        }
        reader.nextEvent();
        this.unmarshal(reader, element);
    } catch (XMLStreamException e) {
        throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0088_FAILED_PARSE(element.getLocation()), e));
    }
}
 
Example 14
Source File: StartElementEvent.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public StartElementEvent(StartElement startelement) {
    this(startelement.getName());
    addAttributes(startelement.getAttributes());
    addNamespaces(startelement.getNamespaces());
}
 
Example 15
Source File: StartElementEvent.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public StartElementEvent(StartElement startelement) {
    this(startelement.getName());
    addAttributes(startelement.getAttributes());
    addNamespaceAttributes(startelement.getNamespaces());
}
 
Example 16
Source File: StartElementEvent.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public StartElementEvent(StartElement startelement) {
    this(startelement.getName());
    addAttributes(startelement.getAttributes());
    addNamespaceAttributes(startelement.getNamespaces());
}
 
Example 17
Source File: SearchHandle.java    From java-client-api with Apache License 2.0 4 votes vote down vote up
private void handleMetrics(XMLEventReader reader, StartElement element)
    throws XMLStreamException
  {
    DatatypeFactory dtFactory;
    try {
      dtFactory = DatatypeFactory.newInstance();
    } catch (DatatypeConfigurationException dce) {
      throw new MarkLogicIOException("Cannot instantiate datatypeFactory", dce);
    }

    Calendar now = Calendar.getInstance();

    QName queryName    = new QName(SEARCH_NS, "query-resolution-time");
    QName facetName    = new QName(SEARCH_NS, "facet-resolution-time");
    QName snippetName  = new QName(SEARCH_NS, "snippet-resolution-time");
    QName metadataName = new QName(SEARCH_NS, "metadata-resolution-time");
    QName extractName  = new QName(SEARCH_NS, "extract-resolution-time");
    QName totalName    = new QName(SEARCH_NS, "total-time");

    long qrTime = -1;
    long frTime = -1;
    long srTime = -1;
    long mrTime = -1;
    long erTime  = -1;
    long tTime  = -1;

    QName metricsName = element.getName();
    events: while (reader.hasNext()) {
      XMLEvent event = reader.nextEvent();

      int eventType = event.getEventType();
      switch (eventType) {
        case XMLStreamConstants.START_ELEMENT:
          StartElement startElement = event.asStartElement();
          QName startName = startElement.getName();
          String readerValue = reader.getElementText();
   if (readerValue != null && readerValue.length() > 0) {
if (queryName.equals(startName)) {
	qrTime = parseTime(dtFactory, now, readerValue);
} else if (facetName.equals(startName)) {
	frTime = parseTime(dtFactory, now, readerValue);
} else if (snippetName.equals(startName)) {
	srTime = parseTime(dtFactory, now, readerValue);
} else if (metadataName.equals(startName)) {
	mrTime = parseTime(dtFactory, now, readerValue);
} else if (extractName.equals(startName)) {
	erTime = parseTime(dtFactory, now, readerValue);
} else if (totalName.equals(startName)) {
	tTime = parseTime(dtFactory, now, readerValue);
} else {
	logger.warn("Unexpected metrics element " + startName.toString());
}
   }
          break;
        case XMLStreamConstants.END_ELEMENT:
          if (metricsName.equals(event.asEndElement().getName())) {
            break events;
          }
          break;
      }
    }

    tempMetrics = new SearchMetricsImpl(qrTime, frTime, srTime, mrTime, erTime, tTime);
  }
 
Example 18
Source File: StartElementEvent.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public StartElementEvent(StartElement startelement) {
    this(startelement.getName());
    addAttributes(startelement.getAttributes());
    addNamespaceAttributes(startelement.getNamespaces());
}
 
Example 19
Source File: StartElementEvent.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
public StartElementEvent(StartElement startelement) {
    this(startelement.getName());
    addAttributes(startelement.getAttributes());
    addNamespaceAttributes(startelement.getNamespaces());
}
 
Example 20
Source File: StartElementEvent.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public StartElementEvent(StartElement startelement) {
    this(startelement.getName());
    addAttributes(startelement.getAttributes());
    addNamespaces(startelement.getNamespaces());
}