Community
Participate
Working Groups
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.
[contrib email="pnehrer@freeshell.org"/] The suggested changes have been committed to CVS.
Fixed in I200612141025.
Move to verified as per bug 206558.