@@ -2,6 +2,8 @@ package no.nav.emottak.message.xml
22
33import jakarta.xml.bind.JAXBContext
44import jakarta.xml.bind.JAXBElement
5+ import jakarta.xml.bind.Marshaller
6+ import org.glassfish.jaxb.runtime.marshaller.NamespacePrefixMapper
57import org.w3c.dom.Document
68import org.w3c.dom.Node
79import 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