Uploaded image for project: 'XMLBeans'
  1. XMLBeans
  2. XMLBEANS-228

Element order is mixed up on document creation after calling substitute()

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • Version 2, Version 2.1
    • Version 2, Version 2.1
    • XmlObject
    • None
    • N/A

    Description

      When trying to create XML documents with XMLBeans 2.x using xmlText() or save() that use substitution groups, usage of the substitute() method will mix up the order of the XML document (the order will remain correct when substitute() will not be called).

      We have attached a test case (schema XSD, JUnit test) that demonstrates the failure and also attached a patch against plain XMLBeans 2.0.0 that shows where the problem is and what is needed to fix problem (although currently at the cost of a quite severe perfomance penalty, as I am not an expert in XMLBeans internals):

      For the test case, XMLBeans 2.0.0 as well as the current SVN snapshot both fail as they return

      <?xml version="1.0" encoding="UTF-8"?>
      <Person xmlns="urn:www-apache-org:SubstitutionGroup/substitutionGroupInSequence">
      <FirstCommentElement>ThirdElement</FirstCommentElement>
      <FirstName>FirstElement</FirstName>
      <LastName>SecondElement</LastName>
      </Person>

      instead of

      <?xml version="1.0" encoding="UTF-8"?>
      <Person xmlns="urn:www-apache-org:SubstitutionGroup/substitutionGroupInSequence">
      <FirstName>FirstElement</FirstName>
      <LastName>SecondElement</LastName>
      <FirstCommentElement>ThirdElement</FirstCommentElement>
      </Person>

      as would be correct (and will be returned after applying the patches provided).

      Basically, the main problem is that XmlObjectBase#getElementEndingDelimiters() after having called SchemaProperty#getJavaSetterDelimiter(), does not take into account that the QNameSet returned by getJavaSetterDelimiter() does not yet include the "alias names" - i.e. possible substitutions - of those elements that happen to be heads of substitution groups.

      The patch for XmlObjectBase adds the missing substitutions to this QNameSet. This will fix the base problem, but so far at the cost of performance: In order to simulate a (non-existing) QNameSet iterator on the set of JavaSetterDelimiters, we have not been able to find a more intelligent solution than to iterate over the complete array of SchemaGlobalElements and check each head of a substitution group for inclusion in the QNameSet (using QNameSet#contains()). This definitely is suboptimal and should be replaced by an optimized implementation done by somebody who is familiar with the internals of XMLBeans. (Sorry!)

      Unfortunately, while creating the first fix, we ran into a second (small) issue:

      The static method QNameSet#forArray() does mistakenly pass null instead of new HashSet() to the private constructor, which results in a NullPointerException. The second fix (also included in the patch) will fix this problem.

      Please get back to me in case you need any additional details.

      Thanks & best regards,

      Andreas Loew & Patrik Streicher

      Attachments

        1. xmlbeans228.patch
          9 kB
          Lawrence Aston Jones
        2. xmlbeans228_QNameSet.patch
          1 kB
          Lawrence Aston Jones
        3. SubstitutionGroupTest.java
          2 kB
          Andreas Loew
        4. substitutionGroupInSequence.xsd
          1 kB
          Andreas Loew
        5. substitutionGroupFix.patch
          3 kB
          Andreas Loew
        6. substGroupTests.xsd
          2 kB
          Lawrence Aston Jones

        Activity

          People

            Unassigned Unassigned
            awloew Andreas Loew
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: