From e4e1f6780dbc76db5af244966467c67e0812e596 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torbj=C3=B6rn=20Svensson?= Date: Thu, 20 Aug 2020 22:08:39 +0200 Subject: [PATCH] Bug 565628: Unify line endings for memory block configuration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As the memory browser configuration is preserved in the launch configuration file as an indented serialized XML string, the string will contain the result of System.lineSeparator(). As the launch configuration file can be shared among developers with different platforms, there is a risk that the launch configuration file is always modified although there is no real modification, just line endings. To avoid this scenario, always save the XML string without any indentation or line endings. Change-Id: I94497a924f7aa5a881ac6a32f146d2cbceb6324f Signed-off-by: Torbjörn Svensson --- .../org.eclipse.cdt.core/META-INF/MANIFEST.MF | 1 + .../eclipse/cdt/internal/core/XmlUtil.java | 40 +++++++++++++------ .../memory/GdbMemoryBlockRetrieval.java | 3 +- dsf/org.eclipse.cdt.dsf/META-INF/MANIFEST.MF | 2 +- .../debug/model/DsfMemoryBlockRetrieval.java | 3 +- 5 files changed, 33 insertions(+), 16 deletions(-) diff --git a/core/org.eclipse.cdt.core/META-INF/MANIFEST.MF b/core/org.eclipse.cdt.core/META-INF/MANIFEST.MF index 0de39d9b106..d7174cb4e98 100644 --- a/core/org.eclipse.cdt.core/META-INF/MANIFEST.MF +++ b/core/org.eclipse.cdt.core/META-INF/MANIFEST.MF @@ -49,6 +49,7 @@ Export-Package: org.eclipse.cdt.core, org.eclipse.cdt.internal.core; x-friends:="org.eclipse.cdt.codan.core.cxx, org.eclipse.cdt.debug.core, + org.eclipse.cdt.dsf, org.eclipse.cdt.dsf.gdb, org.eclipse.cdt.make.core, org.eclipse.cdt.make.ui, diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/internal/core/XmlUtil.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/internal/core/XmlUtil.java index af9095b8bd4..f89e557ec04 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/internal/core/XmlUtil.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/internal/core/XmlUtil.java @@ -292,16 +292,12 @@ public class XmlUtil { */ public static void serializeXml(Document doc, URI uriLocation, String lineSeparator) throws IOException, TransformerException, CoreException { - XmlUtil.prettyFormat(doc); - java.io.File storeFile = new java.io.File(uriLocation); if (!storeFile.exists()) { storeFile.createNewFile(); } - String utfString = new String(toByteArray(doc), ENCODING_UTF_8); - utfString = XmlUtil.replaceLineSeparatorInternal(utfString, lineSeparator); - utfString = XmlUtil.insertNewlineAfterXMLVersionTag(utfString, lineSeparator); + String utfString = toString(doc, lineSeparator); FileOutputStream output = getFileOutputStreamWorkaround(storeFile); output.write(utfString.getBytes(ENCODING_UTF_8)); @@ -351,8 +347,6 @@ public class XmlUtil { * @throws CoreException if something goes wrong. */ private static byte[] toByteArray(Document doc) throws CoreException { - XmlUtil.prettyFormat(doc); - try { ByteArrayOutputStream stream = new ByteArrayOutputStream(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); @@ -420,13 +414,9 @@ public class XmlUtil { * @throws CoreException if something goes wrong. */ public static void serializeXml(Document doc, IFile file) throws CoreException { - XmlUtil.prettyFormat(doc); - try { - String utfString = new String(toByteArray(doc), ENCODING_UTF_8); String lineSeparator = Util.getLineSeparator(file); - utfString = XmlUtil.replaceLineSeparatorInternal(utfString, lineSeparator); - utfString = XmlUtil.insertNewlineAfterXMLVersionTag(utfString, lineSeparator); + String utfString = toString(doc, lineSeparator); byte[] newContents = utfString.getBytes(ENCODING_UTF_8); InputStream input = new ByteArrayInputStream(newContents); @@ -466,12 +456,36 @@ public class XmlUtil { /** * Serialize XML Document into a string. + * Note: This will return a non-pretty formatted string * * @param doc - DOM Document to serialize. * @return XML as a String. * @throws CoreException if something goes wrong. */ public static String toString(Document doc) throws CoreException { - return new String(toByteArray(doc)); + try { + return new String(toByteArray(doc), ENCODING_UTF_8); + } catch (UnsupportedEncodingException e) { + throw new CoreException(CCorePlugin.createStatus(Messages.XmlUtil_InternalErrorSerializing, e)); + } + } + + /** + * Serialize XML Document into a pretty formatted string. + * Note: This will return a pretty formatted string + * + * @param doc - DOM Document to serialize. + * @param lineSeparator - line separator + * @return XML as a pretty formatted String. + * @throws CoreException if something goes wrong. + */ + public static String toString(Document doc, String lineSeparator) throws CoreException { + XmlUtil.prettyFormat(doc); + + String utfString = toString(doc); + utfString = XmlUtil.replaceLineSeparatorInternal(utfString, lineSeparator); + utfString = XmlUtil.insertNewlineAfterXMLVersionTag(utfString, lineSeparator); + + return utfString; } } diff --git a/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/internal/memory/GdbMemoryBlockRetrieval.java b/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/internal/memory/GdbMemoryBlockRetrieval.java index c0f8e14f2e7..65e543e8dbf 100644 --- a/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/internal/memory/GdbMemoryBlockRetrieval.java +++ b/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/internal/memory/GdbMemoryBlockRetrieval.java @@ -42,6 +42,7 @@ import org.eclipse.cdt.dsf.gdb.service.IGDBMemory; import org.eclipse.cdt.dsf.gdb.service.IGDBMemory2; import org.eclipse.cdt.dsf.service.DsfServices; import org.eclipse.cdt.dsf.service.DsfSession; +import org.eclipse.cdt.internal.core.XmlUtil; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.IStatus; @@ -445,7 +446,7 @@ public class GdbMemoryBlockRetrieval extends DsfMemoryBlockRetrieval implements } } document.appendChild(expressionList); - return DebugPlugin.serializeDocument(document); + return XmlUtil.toString(document); } /* (non-Javadoc) diff --git a/dsf/org.eclipse.cdt.dsf/META-INF/MANIFEST.MF b/dsf/org.eclipse.cdt.dsf/META-INF/MANIFEST.MF index fc444751fd0..0e38648a9cb 100644 --- a/dsf/org.eclipse.cdt.dsf/META-INF/MANIFEST.MF +++ b/dsf/org.eclipse.cdt.dsf/META-INF/MANIFEST.MF @@ -3,7 +3,7 @@ Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-Vendor: %providerName Bundle-SymbolicName: org.eclipse.cdt.dsf;singleton:=true -Bundle-Version: 2.8.200.qualifier +Bundle-Version: 2.8.300.qualifier Bundle-Activator: org.eclipse.cdt.dsf.internal.DsfPlugin Bundle-Localization: plugin Require-Bundle: org.eclipse.core.runtime, diff --git a/dsf/org.eclipse.cdt.dsf/src/org/eclipse/cdt/dsf/debug/model/DsfMemoryBlockRetrieval.java b/dsf/org.eclipse.cdt.dsf/src/org/eclipse/cdt/dsf/debug/model/DsfMemoryBlockRetrieval.java index 89957850042..7d2488024c3 100644 --- a/dsf/org.eclipse.cdt.dsf/src/org/eclipse/cdt/dsf/debug/model/DsfMemoryBlockRetrieval.java +++ b/dsf/org.eclipse.cdt.dsf/src/org/eclipse/cdt/dsf/debug/model/DsfMemoryBlockRetrieval.java @@ -40,6 +40,7 @@ import org.eclipse.cdt.dsf.internal.DsfPlugin; import org.eclipse.cdt.dsf.service.DsfServices; import org.eclipse.cdt.dsf.service.DsfSession; import org.eclipse.cdt.dsf.service.IDsfService; +import org.eclipse.cdt.internal.core.XmlUtil; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.IStatus; @@ -321,7 +322,7 @@ public class DsfMemoryBlockRetrieval extends PlatformObject implements IMemoryBl } } document.appendChild(expressionList); - return DebugPlugin.serializeDocument(document); + return XmlUtil.toString(document); } ///////////////////////////////////////////////////////////////////////////