Skip to content

Commit bd74bbb

Browse files
laurentschoelensmattrpav
authored andcommitted
[#457] add multiple namespaces prefix bindings
1 parent 0d0828b commit bd74bbb

File tree

7 files changed

+185
-6
lines changed

7 files changed

+185
-6
lines changed

jaxb-plugins-parent/jaxb-plugins/src/main/java/org/jvnet/jaxb/plugin/namespace_prefix/NamespacePrefixPlugin.java

+17-4
Original file line numberDiff line numberDiff line change
@@ -186,26 +186,39 @@ private static List<Pair> getPrefixBinding(Model packageModel, Set<String> packa
186186
continue;
187187
}
188188

189+
189190
// get the prefix's name
190191
String prefix = "";
191192
for (BIDeclaration declaration : b.getDecls()) {
193+
// get targeted prefix and targeted NS
194+
String targetedPrefix = "";
195+
String targetedNS = "";
192196
if (declaration instanceof BIXPluginCustomization) {
193197
final BIXPluginCustomization customization = (BIXPluginCustomization) declaration;
194198
if (customization.element.getNamespaceURI().equals(Customizations.NAMESPACE_URI)) {
195199
if (!customization.element.getLocalName().equals(Customizations.PREFIX_NAME)) {
196200
throw new RuntimeException("Unrecognized element [" + customization.element.getLocalName() + "]");
197201
}
198-
prefix = customization.element.getAttribute("name");
202+
targetedPrefix = customization.element.getAttribute("name");
203+
targetedNS = customization.element.getAttribute("namespaceURI");
199204
customization.markAsAcknowledged();
200-
break;
201205
} else if (customization.element.getNamespaceURI().equals(LegacyCustomizations.NAMESPACE_URI)) {
202206
if (!customization.element.getLocalName().equals(LegacyCustomizations.PREFIX_NAME)) {
203207
throw new RuntimeException("Unrecognized element [" + customization.element.getLocalName() + "]");
204208
}
205209
logger.warn("Please migrate your namespace in xsd / xjb from " + LegacyCustomizations.NAMESPACE_URI + " to " + Customizations.NAMESPACE_URI);
206-
prefix = customization.element.getAttribute("name");
210+
targetedPrefix = customization.element.getAttribute("name");
211+
targetedNS = customization.element.getAttribute("namespaceURI");
207212
customization.markAsAcknowledged();
208-
break;
213+
}
214+
}
215+
216+
if (targetedPrefix != null && !"".equals(targetedPrefix)) {
217+
if (targetedNS != null && !"".equals(targetedNS) && !targetedNS.equals(targetNS)) {
218+
list.add(new Pair(targetedNS, targetedPrefix));
219+
} else if ("".equals(prefix)) {
220+
// only take first binding as actual true binding for current targetNS (break condition used before in for loop)
221+
prefix = targetedPrefix;
209222
}
210223
}
211224
}

jaxb-plugins-parent/tests/namespace/src/main/resources/binding.xjb

+13-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb"
55
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
66
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
7-
xmlns:namespace="http://jaxb2-commons.dev.java.net/basic/namespace-prefix"
7+
xmlns:namespace="urn:jaxb.jvnet.org:plugin:namespace-prefix"
88

99
jaxb:extensionBindingPrefixes="xjc">
1010

@@ -22,6 +22,18 @@
2222
</jaxb:schemaBindings>
2323
<jaxb:bindings>
2424
<namespace:prefix name="b" />
25+
<namespace:prefix name="aprefix" namespaceURI="a" />
2526
</jaxb:bindings>
2627
</jaxb:bindings>
28+
<jaxb:bindings schemaLocation="c.xsd">
29+
<jaxb:schemaBindings>
30+
<jaxb:package name="org.jvnet.jaxb.test.namespace.c" />
31+
</jaxb:schemaBindings>
32+
<jaxb:bindings>
33+
<namespace:prefix name="creal" />
34+
<namespace:prefix name="otherfake" />
35+
<namespace:prefix name="aprefix" namespaceURI="a" />
36+
<namespace:prefix name="bprefix" namespaceURI="b" />
37+
</jaxb:bindings>
38+
</jaxb:bindings>
2739
</jaxb:bindings>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<xsd:schema
2+
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
3+
targetNamespace="c"
4+
xmlns:a="a"
5+
xmlns:c="c"
6+
elementFormDefault="qualified">
7+
8+
<xsd:import namespace="a" schemaLocation="a.xsd"/>
9+
10+
<xsd:element name="c" type="c:CType"/>
11+
12+
<xsd:complexType name="CType">
13+
<xsd:complexContent>
14+
<xsd:extension base="a:AType">
15+
<xsd:sequence>
16+
<xsd:element name="c" type="c:C1Type"/>
17+
<xsd:element name="ca" type="a:AType" minOccurs="0" maxOccurs="unbounded"/>
18+
</xsd:sequence>
19+
</xsd:extension>
20+
</xsd:complexContent>
21+
</xsd:complexType>
22+
23+
<xsd:complexType name="C1Type">
24+
<xsd:complexContent>
25+
<xsd:extension base="a:A1Type">
26+
<xsd:sequence>
27+
<xsd:element name="c1" type="xsd:string"/>
28+
</xsd:sequence>
29+
</xsd:extension>
30+
</xsd:complexContent>
31+
</xsd:complexType>
32+
33+
<xsd:complexType name="issueHJIII24Mammal">
34+
<xsd:complexContent>
35+
<xsd:extension base="a:issueHJIII24Animal">
36+
</xsd:extension>
37+
</xsd:complexContent>
38+
</xsd:complexType>
39+
40+
<xsd:complexType name="issueHJIII24Dog">
41+
<xsd:complexContent>
42+
<xsd:extension base="c:issueHJIII24Mammal">
43+
</xsd:extension>
44+
</xsd:complexContent>
45+
</xsd:complexType>
46+
47+
</xsd:schema>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.jvnet.jaxb.tests.namespace;
2+
3+
import jakarta.xml.bind.annotation.XmlNs;
4+
import jakarta.xml.bind.annotation.XmlSchema;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
import org.jvnet.jaxb.test.namespace.a.AType;
8+
9+
public class ATest {
10+
11+
@Test
12+
public void testA() {
13+
AType a = new AType();
14+
XmlSchema schema = a.getClass().getPackage().getAnnotation(XmlSchema.class);
15+
XmlNs[] namespaces = schema.xmlns();
16+
Assert.assertNotNull(namespaces);
17+
Assert.assertEquals(1, namespaces.length);
18+
Assert.assertEquals("a", namespaces[0].namespaceURI());
19+
Assert.assertEquals("a", namespaces[0].prefix());
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.jvnet.jaxb.tests.namespace;
2+
3+
import jakarta.xml.bind.annotation.XmlNs;
4+
import jakarta.xml.bind.annotation.XmlSchema;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
import org.jvnet.jaxb.test.namespace.b.BType;
8+
9+
import java.util.Arrays;
10+
11+
public class BTest {
12+
13+
@Test
14+
public void testB() {
15+
BType b = new BType();
16+
XmlSchema schema = b.getClass().getPackage().getAnnotation(XmlSchema.class);
17+
XmlNs[] namespaces = schema.xmlns();
18+
Assert.assertNotNull(namespaces);
19+
Assert.assertEquals(2, namespaces.length);
20+
XmlNs aNs = Arrays.stream(namespaces).filter(ns -> "aprefix".equals(ns.prefix())).findFirst().orElse(null);
21+
Assert.assertNotNull(aNs);
22+
Assert.assertEquals("a", aNs.namespaceURI());
23+
Assert.assertEquals("aprefix", aNs.prefix());
24+
XmlNs bNs = Arrays.stream(namespaces).filter(ns -> "b".equals(ns.prefix())).findFirst().orElse(null);
25+
Assert.assertNotNull(bNs);
26+
Assert.assertEquals("b", bNs.namespaceURI());
27+
Assert.assertEquals("b", bNs.prefix());
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.jvnet.jaxb.tests.namespace;
2+
3+
import jakarta.xml.bind.JAXBContext;
4+
import jakarta.xml.bind.JAXBElement;
5+
import jakarta.xml.bind.JAXBException;
6+
import jakarta.xml.bind.annotation.XmlNs;
7+
import jakarta.xml.bind.annotation.XmlSchema;
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
import org.jvnet.jaxb.test.namespace.a.A1Type;
11+
import org.jvnet.jaxb.test.namespace.c.C1Type;
12+
import org.jvnet.jaxb.test.namespace.c.CType;
13+
14+
import javax.xml.namespace.QName;
15+
import java.io.StringWriter;
16+
import java.util.Arrays;
17+
18+
public class CTest {
19+
20+
@Test
21+
public void testC() throws JAXBException {
22+
CType c = new CType();
23+
XmlSchema schema = c.getClass().getPackage().getAnnotation(XmlSchema.class);
24+
XmlNs[] namespaces = schema.xmlns();
25+
Assert.assertEquals(3, namespaces.length);
26+
XmlNs aNs = Arrays.stream(namespaces).filter(ns -> "aprefix".equals(ns.prefix())).findFirst().orElse(null);
27+
Assert.assertNotNull(aNs);
28+
Assert.assertEquals("a", aNs.namespaceURI());
29+
Assert.assertEquals("aprefix", aNs.prefix());
30+
XmlNs bNs = Arrays.stream(namespaces).filter(ns -> "bprefix".equals(ns.prefix())).findFirst().orElse(null);
31+
Assert.assertNotNull(bNs);
32+
Assert.assertEquals("b", bNs.namespaceURI());
33+
Assert.assertEquals("bprefix", bNs.prefix());
34+
XmlNs cNs = Arrays.stream(namespaces).filter(ns -> "creal".equals(ns.prefix())).findFirst().orElse(null);
35+
Assert.assertNotNull(cNs);
36+
Assert.assertEquals("c", cNs.namespaceURI());
37+
Assert.assertEquals("creal", cNs.prefix());
38+
39+
c.setA(new A1Type());
40+
c.setC(new C1Type());
41+
42+
JAXBElement<CType> jaxbElement
43+
= new JAXBElement<>( new QName("c", "cPart"), CType.class, c);
44+
JAXBContext context = JAXBContext.newInstance(CType.class.getPackage().getName());
45+
StringWriter writer = new StringWriter();
46+
context.createMarshaller().marshal(jaxbElement, writer);
47+
String marshalled = writer.toString();
48+
49+
Assert.assertNotNull(marshalled);
50+
String expectedA = "xmlns:aprefix=\"a\"";
51+
Assert.assertTrue(marshalled.contains(expectedA));
52+
String expectedB = "xmlns:bprefix=\"b\"";
53+
Assert.assertTrue(marshalled.contains(expectedB));
54+
String expectedC = "xmlns:creal=\"c\"";
55+
Assert.assertTrue(marshalled.contains(expectedC));
56+
}
57+
}
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.jvnet.jaxb2_commons.tests.namespace;
1+
package org.jvnet.jaxb.tests.namespace;
22

33
import java.util.ArrayList;
44
import java.util.List;

0 commit comments

Comments
 (0)