Details
-
Bug
-
Status: Resolved
-
Major
-
Resolution: Fixed
-
Version 5.0.0
-
None
-
None
Description
Hello!
I ran into the following problem: in some cases, XmlObject.toString() returns an XML containing duplicated "xmlns" attribute, as shown bellow
<Ns2E1 xmlns="XmlToStringTest2Xsd.xsd"> <Ns2T1E1 xmlns="" xmlns="XmlToStringTest2Xsd.xsd"> <Ns1T1E1 xmlns="XmlToStringTest1Xsd.xsd">Ns1T1E1</Ns1T1E1> </Ns2T1E1> </Ns2E1>
I wrote a simple test that reproduces the error (you can find test XSDs in schemas.zip):
public class XmlToStringTest { @Test public void test() { try { final Ns2E1Document ns2e1Doc = Ns2E1Document.Factory.newInstance(); ns2e1Doc.addNewNs2E1().addNewNs2T1E1().setNs1T1E1("Ns1T1E1"); final Ns3E1Document ns3e1Doc = Ns3E1Document.Factory.newInstance(); ns3e1Doc.addNewNs3E1().setNs3T1E1(Ns1T1.Factory.parse(ns2e1Doc.getNs2E1().getNs2T1E1().toString())); final Ns3E1Document pNs3e1Doc = Ns3E1Document.Factory.parse(ns3e1Doc.toString()); final Ns2E1Document pNs2e1Doc = Ns2E1Document.Factory.newInstance(); pNs2e1Doc.addNewNs2E1().setNs2T1E1(pNs3e1Doc.getNs3E1().getNs3T1E1()); System.out.println(pNs2e1Doc.toString()); Ns2E1Document.Factory.parse(pNs2e1Doc.toString()); } catch (Exception ex) { ex.printStackTrace(); Assert.fail(); } } }
I investigated the problem and found the cause. In the Saver.tryPrefix() I found the following comment:
// If the prefix is currently mapped, then try another prefix. A
// special case is that of trying to map the default prefix ("").
// Here, there always exists a default mapping. If this is the
// mapping we found, then remap it anyways. I use != to compare
// strings because I want to test for the specific initial default
// uri I added when I initialized the saver.
But the code following this comment looks like this:
return existingUri == null || (prefix.length() <= 0 && Objects.equals(existingUri, _initialDefaultUri)); // Objects.equals, not ==
And _initialDefaultUri initialized like this:
_initialDefaultUri = "";
But should be
_initialDefaultUri = new String("");
because we talk about a specific value.
Hopefully this information helps fix this bug.