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

The following examples show how to use javax.xml.stream.events.StartElement#getNamespaces() . 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: StAXEventConnector.java    From TencentKona-8 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 2
Source File: StAXEventConnector.java    From openjdk-8 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 3
Source File: StaxEventXMLReader.java    From spring4-understanding with Apache License 2.0 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 4
Source File: TransformerUtil.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private String containsBaseNamespace(StartElement startElement) {
    String localPart, prefix, qual = null;

    Iterator<Namespace> namespaces = startElement.getNamespaces();
    while (namespaces != null && namespaces.hasNext()) {
        Namespace namespace = namespaces.next();
        QName name = namespace.getName();
        localPart = name.getLocalPart();
        prefix = name.getPrefix();
        if (prefix != null && ! prefix.isEmpty())
            qual = (localPart != null && ! localPart.isEmpty()) ? prefix + ":" + localPart : prefix;

        if (qual != null && qual.equals("xmlns"))
            return namespace.getNamespaceURI();
    }
    return null;
}
 
Example 5
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 6
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 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: StaxEventXMLReader.java    From lams with GNU General Public License v2.0 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 9
Source File: StAXEventConnector.java    From openjdk-jdk8u 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 10
Source File: StAXEventConnector.java    From jdk8u60 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 11
Source File: StaxEventXMLReader.java    From java-technology-stack 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 12
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 13
Source File: HbmEventReader.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private StartElement applyNamespace(StartElement startElement) {
	final List<Namespace> targetNamespaces = new ArrayList<Namespace>();

	if ( "".equals( startElement.getName().getNamespaceURI() ) ) {
		// add the default namespace mapping
		targetNamespaces.add( xmlEventFactory.createNamespace( MappingXsdSupport.INSTANCE.hbmXsd().getNamespaceUri() ) );
	}

	// transfer any namespaces directly, unless it is in the "to map" list in which case
	// we transfer a mapped copy pointing to the new namespace
	final Iterator<Namespace> originalNamespaces = startElement.getNamespaces();
	while ( originalNamespaces.hasNext() ) {
		Namespace namespace = originalNamespaces.next();
		if ( NAMESPACE_URIS_TO_MAP.contains( namespace.getNamespaceURI() ) ) {
			// this is a namespace "to map" so map it
			namespace = xmlEventFactory.createNamespace( namespace.getPrefix(), MappingXsdSupport.INSTANCE.hbmXsd().getNamespaceUri() );
		}
		targetNamespaces.add( namespace );
	}

	// Transfer the location info from the incoming event to the event factory
	// so that the event we ask it to generate for us has the same location info
	xmlEventFactory.setLocation( startElement.getLocation() );
	return xmlEventFactory.createStartElement(
			new QName( MappingXsdSupport.INSTANCE.hbmXsd().getNamespaceUri(), startElement.getName().getLocalPart() ),
			startElement.getAttributes(),
			targetNamespaces.iterator()
	);
}
 
Example 14
Source File: JaxbCfgProcessor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private StartElement withNamespace(StartElement startElement) {
	// otherwise, wrap the start element event to provide a default namespace mapping
	final List<Namespace> namespaces = new ArrayList<Namespace>();
	namespaces.add( xmlEventFactory.createNamespace( "", namespaceUri ) );
	Iterator<?> originalNamespaces = startElement.getNamespaces();
	while ( originalNamespaces.hasNext() ) {
		namespaces.add( (Namespace) originalNamespaces.next() );
	}
	return xmlEventFactory.createStartElement(
			new QName( namespaceUri, startElement.getName().getLocalPart() ),
			startElement.getAttributes(),
			namespaces.iterator()
	);
}
 
Example 15
Source File: DefaultAttributeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testEventReader() {
    try {
        XMLInputFactory ifac = XMLInputFactory.newInstance();
        XMLEventReader read = ifac.createXMLEventReader(this.getClass().getResource(INPUT_FILE).toExternalForm(),
                this.getClass().getResourceAsStream(INPUT_FILE));
        while (read.hasNext()) {
            XMLEvent event = read.nextEvent();
            if (event.isStartElement()) {
                StartElement startElement = event.asStartElement();
                if (startElement.getName().getLocalPart().equals("bookurn")) {
                    Iterator iterator = startElement.getNamespaces();
                    int count = 0;
                    while (iterator.hasNext()) {
                        iterator.next();
                        count++;
                    }
                    Assert.assertTrue(count == 2, "Two namespaces are expected for <bookurn> ");

                    Iterator attributes = startElement.getAttributes();
                    count = 0;
                    while (attributes.hasNext()) {
                        iterator.next();
                        count++;
                    }
                    Assert.assertTrue(count == 0, "Zero attributes are expected for <bookurn> ");
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Exception occured: " + e.getMessage());
    }
}
 
Example 16
Source File: StreamingXmlParser.java    From datacollector with Apache License 2.0 5 votes vote down vote up
Map<String, Field> toField(StartElement startE) {
  Map<String, Field> map = new LinkedHashMap<>();
  Iterator attrs = startE.getAttributes();
  while (attrs.hasNext()) {
    Attribute attr = (Attribute) attrs.next();
    map.put(getName(ATTR_PREFIX_KEY, attr), Field.create(attr.getValue()));
  }
  Iterator nss = startE.getNamespaces();
  while (nss.hasNext()) {
    Namespace ns = (Namespace) nss.next();
    map.put(getName(NS_PREFIX_KEY, ns), Field.create(ns.getNamespaceURI()));
  }
  return map;
}
 
Example 17
Source File: XmlFilterReader.java    From knox with Apache License 2.0 5 votes vote down vote up
private void bufferNamespaces( StartElement event, Element element ) {
  Iterator namespaces = event.getNamespaces();
  while( namespaces.hasNext() ) {
    Namespace namespace = (Namespace)namespaces.next();
    if( namespace.isDefaultNamespaceDeclaration() ) {
      element.setAttribute( "xmlns", namespace.getNamespaceURI() );
    } else {
      element.setAttribute( "xmlns:" + namespace.getPrefix(), namespace.getNamespaceURI() );
    }
  }
}
 
Example 18
Source File: XmlFilterReader.java    From knox with Apache License 2.0 5 votes vote down vote up
private void streamNamespaces( StartElement event ) {
  Iterator i = event.getNamespaces();
  while( i.hasNext() ) {
    Namespace ns = (Namespace)i.next();
    writer.write( " xmlns" );
    if( !ns.isDefaultNamespaceDeclaration() ) {
      writer.write( ":" );
      writer.write( ns.getPrefix() );
    }
    writer.write( "=\"" );
    writer.write( ns.getNamespaceURI() );
    writer.write( "\"" );
  }
}
 
Example 19
Source File: JpaOrmXmlEventReader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private Iterator<Namespace> existingXmlNamespacesIterator(StartElement startElement) {
	return startElement.getNamespaces();
}
 
Example 20
Source File: XmlResourceValues.java    From bazel with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public static Iterator<Namespace> iterateNamespacesFrom(StartElement start) {
  return start.getNamespaces();
}