Skip to content

Commit ade0b99

Browse files
authored
Added namespace prefix mapper for ebxml envelope xsds (#195)
* Added namespace prefix mapper for ebxml envelope xsds * Fix default prefix values * Update prefixmapper property for jakarta --------- Co-authored-by: Thomas Burnett <[email protected]>
1 parent 07a9bf5 commit ade0b99

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

ebxml-processing-model/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ dependencies {
5555
implementation(libs.emottak.payload.xsd)
5656
implementation(libs.emottak.utils)
5757
implementation(libs.ktor.serialization.kotlinx.json)
58+
implementation(libs.jaxb.runtime)
5859
implementation(libs.bundles.logging)
5960
runtimeOnly("org.postgresql:postgresql:42.7.3")
6061
}

ebxml-processing-model/src/main/kotlin/no/nav/emottak/message/xml/XmlMarshaller.kt

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package no.nav.emottak.message.xml
22

33
import jakarta.xml.bind.JAXBContext
44
import jakarta.xml.bind.JAXBElement
5+
import jakarta.xml.bind.Marshaller
6+
import org.glassfish.jaxb.runtime.marshaller.NamespacePrefixMapper
57
import org.w3c.dom.Document
68
import org.w3c.dom.Node
79
import org.xmlsoap.schemas.soap.envelope.Envelope
@@ -23,12 +25,13 @@ class XmlMarshaller {
2325
org.oasis_open.committees.ebxml_msg.schema.msg_header_2_0.ObjectFactory::class.java,
2426
org.xmlsoap.schemas.soap.envelope.ObjectFactory::class.java,
2527
org.w3._1999.xlink.ObjectFactory::class.java,
26-
org.w3._2009.xmldsig11_.ObjectFactory::class.java,
27-
no.kith.xmlstds.msghead._2006_05_24.ObjectFactory::class.java,
28-
no.nav.tjeneste.ekstern.frikort.v1.types.ObjectFactory::class.java
28+
org.w3._2009.xmldsig11_.ObjectFactory::class.java
2929
)
3030

31-
private val marshaller = jaxbContext.createMarshaller()
31+
private val marshaller = jaxbContext.createMarshaller().apply {
32+
setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true)
33+
setProperty("org.glassfish.jaxb.namespacePrefixMapper", EbXMLNamespacePrefixMapper())
34+
}
3235
private val unmarshaller = jaxbContext.createUnmarshaller()
3336
private val marshlingMonitor = Any()
3437
private val unmarshlingMonitor = Any()
@@ -64,3 +67,25 @@ class XmlMarshaller {
6467
return if (unmarshalled is JAXBElement<*>) (unmarshalled as JAXBElement<T>).value else unmarshalled as T
6568
}
6669
}
70+
71+
class EbXMLNamespacePrefixMapper : NamespacePrefixMapper() {
72+
private val namespaceMap = mapOf(
73+
"http://www.oasis-open.org/committees/ebxml-msg/schema/msg-header-2_0.xsd" to "eb",
74+
"http://www.oasis-open.org/committees/ebxml-cppa/schema/cpp-cpa-2_0.xsd" to "cppa",
75+
"http://www.w3.org/2000/09/xmldsig#" to "ds",
76+
"http://www.w3.org/1999/xlink" to "xlink",
77+
"http://schemas.xmlsoap.org/soap/envelope/" to "SOAP"
78+
)
79+
80+
private val assignedPrefixes = mutableMapOf<String, String>()
81+
private var prefixCounter = 1
82+
83+
override fun getPreferredPrefix(namespaceUri: String?, suggestion: String?, requirePrefix: Boolean): String {
84+
if (namespaceUri == null) return "ns0"
85+
return namespaceMap[namespaceUri]
86+
?: assignedPrefixes[namespaceUri]
87+
?: "ns${prefixCounter++}".also {
88+
assignedPrefixes[namespaceUri] = it
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)