Search in sources :

Example 1 with Namespace

use of javax.xml.stream.events.Namespace in project hibernate-orm by hibernate.

the class JpaNamespaceTransformingEventReader method updateElementNamespaces.

private List<Namespace> updateElementNamespaces(StartElement startElement) {
    List<Namespace> newNamespaceList = new ArrayList<Namespace>();
    Iterator<?> existingNamespaceIterator = startElement.getNamespaces();
    while (existingNamespaceIterator.hasNext()) {
        Namespace namespace = (Namespace) existingNamespaceIterator.next();
        if (NAMESPACE_MAPPING.containsKey(namespace.getNamespaceURI())) {
            newNamespaceList.add(xmlEventFactory.createNamespace(EMPTY_PREFIX, currentDocumentNamespaceUri));
        } else {
            newNamespaceList.add(namespace);
        }
    }
    // if there is no namespace at all we add the main one. All elements need the namespace
    if (newNamespaceList.isEmpty()) {
        newNamespaceList.add(xmlEventFactory.createNamespace(EMPTY_PREFIX, currentDocumentNamespaceUri));
    }
    return newNamespaceList;
}
Also used : ArrayList(java.util.ArrayList) Namespace(javax.xml.stream.events.Namespace)

Example 2 with Namespace

use of javax.xml.stream.events.Namespace in project spring-framework by spring-projects.

the class StaxEventXMLReader method getAttributes.

private Attributes getAttributes(StartElement event) {
    AttributesImpl attributes = new AttributesImpl();
    for (Iterator i = event.getAttributes(); i.hasNext(); ) {
        Attribute attribute = (Attribute) i.next();
        QName qName = attribute.getName();
        String namespace = qName.getNamespaceURI();
        if (namespace == null || !hasNamespacesFeature()) {
            namespace = "";
        }
        String type = attribute.getDTDType();
        if (type == null) {
            type = "CDATA";
        }
        attributes.addAttribute(namespace, qName.getLocalPart(), toQualifiedName(qName), type, attribute.getValue());
    }
    if (hasNamespacePrefixesFeature()) {
        for (Iterator i = event.getNamespaces(); i.hasNext(); ) {
            Namespace namespace = (Namespace) i.next();
            String prefix = namespace.getPrefix();
            String namespaceUri = namespace.getNamespaceURI();
            String qName;
            if (StringUtils.hasLength(prefix)) {
                qName = "xmlns:" + prefix;
            } else {
                qName = "xmlns";
            }
            attributes.addAttribute("", "", qName, "CDATA", namespaceUri);
        }
    }
    return attributes;
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) Iterator(java.util.Iterator) Namespace(javax.xml.stream.events.Namespace)

Example 3 with Namespace

use of javax.xml.stream.events.Namespace in project spring-framework by spring-projects.

the class StaxEventXMLReader method handleStartElement.

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));
        }
    }
}
Also used : Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) Iterator(java.util.Iterator) Namespace(javax.xml.stream.events.Namespace)

Example 4 with Namespace

use of javax.xml.stream.events.Namespace in project spring-framework by spring-projects.

the class XMLEventStreamReader method getNamespace.

@SuppressWarnings("rawtypes")
private Namespace getNamespace(int index) {
    Iterator namespaces;
    if (this.event.isStartElement()) {
        namespaces = this.event.asStartElement().getNamespaces();
    } else if (this.event.isEndElement()) {
        namespaces = this.event.asEndElement().getNamespaces();
    } else {
        throw new IllegalStateException();
    }
    int count = 0;
    while (namespaces.hasNext()) {
        Namespace namespace = (Namespace) namespaces.next();
        if (count == index) {
            return namespace;
        } else {
            count++;
        }
    }
    throw new IllegalArgumentException();
}
Also used : Iterator(java.util.Iterator) Namespace(javax.xml.stream.events.Namespace)

Example 5 with Namespace

use of javax.xml.stream.events.Namespace in project spring-framework by spring-projects.

the class XMLEventStreamWriter method doWriteNamespace.

@SuppressWarnings("rawtypes")
private void doWriteNamespace(Namespace namespace) throws XMLStreamException {
    int last = this.endElements.size() - 1;
    EndElement oldEndElement = this.endElements.get(last);
    Iterator oldNamespaces = oldEndElement.getNamespaces();
    List<Namespace> newNamespaces = new ArrayList<>();
    while (oldNamespaces.hasNext()) {
        Namespace oldNamespace = (Namespace) oldNamespaces.next();
        newNamespaces.add(oldNamespace);
    }
    newNamespaces.add(namespace);
    EndElement newEndElement = this.eventFactory.createEndElement(oldEndElement.getName(), newNamespaces.iterator());
    this.eventWriter.add(namespace);
    this.endElements.set(last, newEndElement);
}
Also used : EndElement(javax.xml.stream.events.EndElement) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) Namespace(javax.xml.stream.events.Namespace)

Aggregations

Namespace (javax.xml.stream.events.Namespace)14 QName (javax.xml.namespace.QName)9 Iterator (java.util.Iterator)6 Attribute (javax.xml.stream.events.Attribute)6 ArrayList (java.util.ArrayList)4 LinkedHashMap (java.util.LinkedHashMap)1 NamespaceContext (javax.xml.namespace.NamespaceContext)1 EndElement (javax.xml.stream.events.EndElement)1 AttributesImpl (org.xml.sax.helpers.AttributesImpl)1