Bug 166967 - When serializing XML documents, trailing comments don't get placed on a new line
Summary: When serializing XML documents, trailing comments don't get placed on a new ...
Status: VERIFIED FIXED
Alias: None
Product: EMF
Classification: Modeling
Component: XML/XMI (show other bugs)
Version: 2.3.0   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Elena Litani CLA
QA Contact:
URL:
Whiteboard:
Keywords: contributed
Depends on:
Blocks:
 
Reported: 2006-12-06 10:59 EST by Peter Nehrer CLA
Modified: 2008-05-13 17:09 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Peter Nehrer CLA 2006-12-06 10:59:35 EST
When a model with a mixed-content Document Root gets serialized, trailing comments are not placed on a new line. Suppose we have a document consisting of a root element and a trailing comment:

    EPackage pkg = ExtendedMetaData.INSTANCE.demandPackage(null);
    XMLTypeDocumentRoot doc = (XMLTypeDocumentRoot) 
      EcoreUtil.create(ExtendedMetaData.INSTANCE.getDocumentRoot(pkg));
    doc.getMixed().add(
        ExtendedMetaData.INSTANCE.demandFeature(null, "root", true),
        XMLTypeFactory.eINSTANCE.createAnyType());
    doc.getMixed().add(XMLTypePackage.Literals.XML_TYPE_DOCUMENT_ROOT__COMMENT, "comment");
    Resource res = new XMLResourceImpl();
    res.getContents().add(doc);
    HashMap options = new HashMap();
    options.put(XMLResource.OPTION_EXTENDED_META_DATA, ExtendedMetaData.INSTANCE);
    options.put(XMLResource.OPTION_DECLARE_XML, Boolean.FALSE);
    res.save(System.out, options);

When saved, it would look like this:

<root/><!--comment-->

In method XMLString.addComment(String), I believe the intent was to add a LF before each trailing comment (i.e., one that appears after the root element). However, the if statement that checks if the comment is trailing returns false when a Document Root object is used as the model root:

    if (firstElementMark != null && elementNames.isEmpty())
    {
      addLine();
    }

The addLine() in this case never executes, because elementNames is not empty -- it contains a single null entry (which I believe stands for the Document Root).

It appears that changing the conditional like this fixes the problem:

    if (firstElementMark != null && (elementNames.isEmpty() || (elementNames.size() == 1 && elementNames.get(0) == null)))
    {
      addLine();
    }

The changed condition handles the original case as well as the case when a single null element is in elementNames.
Comment 1 Ed Merks CLA 2006-12-09 06:55:55 EST
[contrib email="pnehrer@freeshell.org"/]

The suggested changes have been committed to CVS.
Comment 2 Nick Boldt CLA 2006-12-14 17:42:47 EST
Fixed in I200612141025.
Comment 3 Nick Boldt CLA 2008-01-28 16:48:03 EST
Move to verified as per bug 206558.