Skip to content

Commit

Permalink
lookup proper attribute prefix based on the nsURI
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Jungmann <lukas.jungmann@oracle.com>
  • Loading branch information
lukasj committed Jun 15, 2021
1 parent 2a8748e commit 5f6ccf8
Showing 1 changed file with 19 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
Expand All @@ -20,6 +20,8 @@
import jakarta.xml.soap.SOAPElement;
import jakarta.xml.soap.SOAPException;
import jakarta.xml.soap.SOAPMessage;
import java.util.HashMap;
import java.util.Map;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

Expand Down Expand Up @@ -329,7 +331,7 @@ public String getPrefix(final String namespaceURI) {
return currentElement.lookupPrefix(namespaceURI);
}
@Override
public Iterator getPrefixes(final String namespaceURI) {
public Iterator<String> getPrefixes(final String namespaceURI) {
return new Iterator<String>() {
String prefix = getPrefix(namespaceURI);
@Override
Expand Down Expand Up @@ -388,12 +390,12 @@ static class DeferredElement {
private String prefix;
private String localName;
private String namespaceUri;
private final List<NamespaceDeclaration> namespaceDeclarations;
private final Map<String, String> namespaceDeclarations;
private final List<AttributeDeclaration> attributeDeclarations;

DeferredElement() {
this.namespaceDeclarations = new LinkedList<NamespaceDeclaration>();
this.attributeDeclarations = new LinkedList<AttributeDeclaration>();
this.namespaceDeclarations = new HashMap<>();
this.attributeDeclarations = new LinkedList<>();
reset();
}

Expand Down Expand Up @@ -438,10 +440,10 @@ public void setNamespaceUri(final String namespaceUri) {
* @param namespaceUri namespace uri
*/
public void addNamespaceDeclaration(final String prefix, final String namespaceUri) {
if (null == this.namespaceUri && null != namespaceUri && prefix.equals(emptyIfNull(this.prefix))) {
if (null == this.namespaceUri && null != namespaceUri && emptyIfNull(this.prefix).equals(prefix)) {
this.namespaceUri = namespaceUri;
}
this.namespaceDeclarations.add(new NamespaceDeclaration(prefix, namespaceUri));
this.namespaceDeclarations.put(emptyIfNull(namespaceUri), prefix);
}

/**
Expand All @@ -453,9 +455,9 @@ public void addNamespaceDeclaration(final String prefix, final String namespaceU
*/
public void addAttribute(final String prefix, final String ns, final String ln, final String value) {
if (ns == null && prefix == null && xmlns.equals(ln)) {
this.addNamespaceDeclaration(prefix, value);
this.addNamespaceDeclaration(null, value);
} else {
this.attributeDeclarations.add(new AttributeDeclaration(prefix, ns, ln, value));
this.attributeDeclarations.add(new AttributeDeclaration(emptyIfNull(prefix), ns, ln, value));
}
}

Expand Down Expand Up @@ -489,13 +491,18 @@ public SOAPElement flushTo(final SOAPElement target) throws XMLStreamException {
newElement = target.addChildElement(this.localName, this.prefix, this.namespaceUri);
}
// add namespace declarations
for (NamespaceDeclaration namespace : this.namespaceDeclarations) {
newElement.addNamespaceDeclaration(namespace.prefix, namespace.namespaceUri);
for (Map.Entry<String, String> namespace : this.namespaceDeclarations.entrySet()) {
newElement.addNamespaceDeclaration(namespace.getValue(), namespace.getKey());
}
// add attribute declarations
for (AttributeDeclaration attribute : this.attributeDeclarations) {
String pfx = attribute.prefix;
String p = this.namespaceDeclarations.get(attribute.namespaceUri);
if (p != null) {
pfx = p;
}
addAttibuteToElement(newElement,
attribute.prefix, attribute.namespaceUri, attribute.localName, attribute.value);
pfx, attribute.namespaceUri, attribute.localName, attribute.value);
}
// reset state
this.reset();
Expand Down Expand Up @@ -531,16 +538,6 @@ private static String emptyIfNull(String s) {
}
}

static class NamespaceDeclaration {
final String prefix;
final String namespaceUri;

NamespaceDeclaration(String prefix, String namespaceUri) {
this.prefix = prefix;
this.namespaceUri = namespaceUri;
}
}

static class AttributeDeclaration {
final String prefix;
final String namespaceUri;
Expand Down

0 comments on commit 5f6ccf8

Please sign in to comment.