Skip to content

Commit

Permalink
[#457] add multiple namespaces prefix bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentschoelens committed Apr 10, 2024
1 parent 4e7a716 commit 4b10ef6
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,18 +177,31 @@ private static List<Pair> getPrefixBinding(Model packageModel, Set<String> packa
continue;
}


// get the prefix's name
String prefix = "";
for (BIDeclaration declaration : b.getDecls()) {
// get targeted prefix and targeted NS
String targetedPrefix = "";
String targetedNS = "";
if (declaration instanceof BIXPluginCustomization) {
final BIXPluginCustomization customization = (BIXPluginCustomization) declaration;
if (customization.element.getNamespaceURI().equals(NAMESPACE_URI)) {
if (!customization.element.getLocalName().equals("prefix")) {
throw new RuntimeException("Unrecognized element [" + customization.element.getLocalName() + "]");
}
prefix = customization.element.getAttribute("name");
targetedPrefix = customization.element.getAttribute("name");
targetedNS = customization.element.getAttribute("namespaceURI");
customization.markAsAcknowledged();
break;
}
}

if (targetedPrefix != null && !"".equals(targetedPrefix)) {
if (targetedNS != null && !"".equals(targetedNS) && !targetedNS.equals(targetNS)) {
list.add(new Pair(targetedNS, targetedPrefix));
} else if ("".equals(prefix)) {
// only take first binding as actual true binding for current targetNS (break condition used before in for loop)
prefix = targetedPrefix;
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions basics/tests/namespace/src/main/resources/binding.xjb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@
</jaxb:schemaBindings>
<jaxb:bindings>
<namespace:prefix name="b" />
<namespace:prefix name="aprefix" namespaceURI="a" />
</jaxb:bindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="c.xsd">
<jaxb:schemaBindings>
<jaxb:package name="org.jvnet.jaxb.test.namespace.c" />
</jaxb:schemaBindings>
<jaxb:bindings>
<namespace:prefix name="creal" />
<namespace:prefix name="otherfake" />
<namespace:prefix name="aprefix" namespaceURI="a" />
<namespace:prefix name="bprefix" namespaceURI="b" />
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
47 changes: 47 additions & 0 deletions basics/tests/namespace/src/main/resources/c.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="c"
xmlns:a="a"
xmlns:c="c"
elementFormDefault="qualified">

<xsd:import namespace="a" schemaLocation="a.xsd"/>

<xsd:element name="c" type="c:CType"/>

<xsd:complexType name="CType">
<xsd:complexContent>
<xsd:extension base="a:AType">
<xsd:sequence>
<xsd:element name="c" type="c:C1Type"/>
<xsd:element name="ca" type="a:AType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="C1Type">
<xsd:complexContent>
<xsd:extension base="a:A1Type">
<xsd:sequence>
<xsd:element name="c1" type="xsd:string"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="issueHJIII24Mammal">
<xsd:complexContent>
<xsd:extension base="a:issueHJIII24Animal">
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="issueHJIII24Dog">
<xsd:complexContent>
<xsd:extension base="c:issueHJIII24Mammal">
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>

</xsd:schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.jvnet.jaxb.tests.namespace;

import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlSchema;
import org.junit.Assert;
import org.junit.Test;
import org.jvnet.jaxb2_commons.test.namespace.a.AType;

public class ATest {

@Test
public void testA() {
AType a = new AType();
XmlSchema schema = a.getClass().getPackage().getAnnotation(XmlSchema.class);
XmlNs[] namespaces = schema.xmlns();
Assert.assertNotNull(namespaces);
Assert.assertEquals(1, namespaces.length);
Assert.assertEquals("a", namespaces[0].namespaceURI());
Assert.assertEquals("a", namespaces[0].prefix());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.jvnet.jaxb.tests.namespace;

import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlSchema;
import org.junit.Assert;
import org.junit.Test;
import org.jvnet.jaxb2_commons.test.namespace.b.BType;

import java.util.Arrays;

public class BTest {

@Test
public void testB() {
BType b = new BType();
XmlSchema schema = b.getClass().getPackage().getAnnotation(XmlSchema.class);
XmlNs[] namespaces = schema.xmlns();
Assert.assertNotNull(namespaces);
Assert.assertEquals(2, namespaces.length);
XmlNs aNs = Arrays.stream(namespaces).filter(ns -> "aprefix".equals(ns.prefix())).findFirst().orElse(null);
Assert.assertNotNull(aNs);
Assert.assertEquals("a", aNs.namespaceURI());
Assert.assertEquals("aprefix", aNs.prefix());
XmlNs bNs = Arrays.stream(namespaces).filter(ns -> "b".equals(ns.prefix())).findFirst().orElse(null);
Assert.assertNotNull(bNs);
Assert.assertEquals("b", bNs.namespaceURI());
Assert.assertEquals("b", bNs.prefix());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.jvnet.jaxb.tests.namespace;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlSchema;
import org.junit.Assert;
import org.junit.Test;
import org.jvnet.jaxb2_commons.test.namespace.a.A1Type;
import org.jvnet.jaxb.test.namespace.c.C1Type;
import org.jvnet.jaxb.test.namespace.c.CType;

import javax.xml.namespace.QName;
import java.io.StringWriter;
import java.util.Arrays;

public class CTest {

@Test
public void testC() throws JAXBException {
CType c = new CType();
XmlSchema schema = c.getClass().getPackage().getAnnotation(XmlSchema.class);
XmlNs[] namespaces = schema.xmlns();
Assert.assertEquals(3, namespaces.length);
XmlNs aNs = Arrays.stream(namespaces).filter(ns -> "aprefix".equals(ns.prefix())).findFirst().orElse(null);
Assert.assertNotNull(aNs);
Assert.assertEquals("a", aNs.namespaceURI());
Assert.assertEquals("aprefix", aNs.prefix());
XmlNs bNs = Arrays.stream(namespaces).filter(ns -> "bprefix".equals(ns.prefix())).findFirst().orElse(null);
Assert.assertNotNull(bNs);
Assert.assertEquals("b", bNs.namespaceURI());
Assert.assertEquals("bprefix", bNs.prefix());
XmlNs cNs = Arrays.stream(namespaces).filter(ns -> "creal".equals(ns.prefix())).findFirst().orElse(null);
Assert.assertNotNull(cNs);
Assert.assertEquals("c", cNs.namespaceURI());
Assert.assertEquals("creal", cNs.prefix());

c.setA(new A1Type());
c.setC(new C1Type());

JAXBElement<CType> jaxbElement
= new JAXBElement<>( new QName("c", "cPart"), CType.class, c);
JAXBContext context = JAXBContext.newInstance(CType.class.getPackage().getName());
StringWriter writer = new StringWriter();
context.createMarshaller().marshal(jaxbElement, writer);
String marshalled = writer.toString();

Assert.assertNotNull(marshalled);
String expectedA = "xmlns:aprefix=\"a\"";
Assert.assertTrue(marshalled.contains(expectedA));
String expectedB = "xmlns:bprefix=\"b\"";
Assert.assertTrue(marshalled.contains(expectedB));
String expectedC = "xmlns:creal=\"c\"";
Assert.assertTrue(marshalled.contains(expectedC));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jvnet.jaxb2_commons.tests.namespace;
package org.jvnet.jaxb.tests.namespace;

import java.util.ArrayList;
import java.util.List;
Expand Down

0 comments on commit 4b10ef6

Please sign in to comment.