From 173d74ac603cd6598ea4ea045c2609ce89c17adc Mon Sep 17 00:00:00 2001 From: Claes Rosell Date: Thu, 8 Oct 2026 13:43:18 +0200 Subject: [PATCH] "Generated by" header insertion algorithm needs love #44 --- .../resource/ser/PropertiesDeserializer.java | 7 ++- .../resource/ser/PropertiesSerializer.java | 56 ++++++++++++++----- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/org.eclipse.babel.core/src/org/eclipse/babel/core/message/resource/ser/PropertiesDeserializer.java b/org.eclipse.babel.core/src/org/eclipse/babel/core/message/resource/ser/PropertiesDeserializer.java index 62eeda7..6cb3e18 100644 --- a/org.eclipse.babel.core/src/org/eclipse/babel/core/message/resource/ser/PropertiesDeserializer.java +++ b/org.eclipse.babel.core/src/org/eclipse/babel/core/message/resource/ser/PropertiesDeserializer.java @@ -124,13 +124,18 @@ public void deserialize(IMessagesBundle messagesBundle, String properties) { value = value.replaceAll("\\\\n", "\n"); //$NON-NLS-1$//$NON-NLS-2$ } IMessage entry = messagesBundle.getMessage(key); + boolean addEntry = false; if (entry == null) { entry = new Message(key, locale); - messagesBundle.addMessage(entry); + addEntry = true; } entry.setActive(!isCommentedLine); entry.setComment(comment); entry.setText(value); + if ( addEntry ) { + messagesBundle.addMessage(entry); + } + newKeys.add(key); // parse comment line } else if (lineBuf.indexOf("#") == 0) { //$NON-NLS-1$ diff --git a/org.eclipse.babel.core/src/org/eclipse/babel/core/message/resource/ser/PropertiesSerializer.java b/org.eclipse.babel.core/src/org/eclipse/babel/core/message/resource/ser/PropertiesSerializer.java index 9c12591..3f0a973 100644 --- a/org.eclipse.babel.core/src/org/eclipse/babel/core/message/resource/ser/PropertiesSerializer.java +++ b/org.eclipse.babel.core/src/org/eclipse/babel/core/message/resource/ser/PropertiesSerializer.java @@ -10,6 +10,7 @@ ******************************************************************************/ package org.eclipse.babel.core.message.resource.ser; +import java.io.StringReader; import java.util.Arrays; import java.util.Properties; @@ -26,8 +27,9 @@ public class PropertiesSerializer { /** Generator header comment. */ - public static final String GENERATED_BY = "#Generated by Eclipse Messages Editor " //$NON-NLS-1$ - + "(Eclipse Babel)"; //$NON-NLS-1$ + public static final String GENERATED_BY_PRE = "#Generated by"; //$NON-NLS-1$ + public static final String MESSAGE_EDITOR_NAME = "Eclipse Messages Editor (Eclipse Babel)"; //$NON-NLS-1$ + public static final String GENERATED_BY = GENERATED_BY_PRE + " " + MESSAGE_EDITOR_NAME; /** A table of hex digits */ private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', @@ -78,15 +80,36 @@ public String serialize(IMessagesBundle messagesBundle) { int numOfLineBreaks = config.getGroupSepBlankLineCount(); StringBuffer text = new StringBuffer(); - // Header comment + // Header comment String headComment = messagesBundle.getComment(); - if (config.isShowSupportEnabled() - && !headComment.startsWith(GENERATED_BY)) { + + // If enabled, insert exactly one current header at the very top + if (config.isShowSupportEnabled()) { text.append(GENERATED_BY); text.append(SYSTEM_LINE_SEP); } + + // Process the existing comment line by line if (headComment != null && headComment.length() > 0) { - text.append(headComment); + // Split the comment into lines. + // The '-1' limit ensures we don't lose trailing empty lines. + String[] lines = headComment.split("\r\n|\r|\n", -1); + + for (int i = 0; i < lines.length; i++) { + String line = lines[i]; + + // Skip ANY line that starts with "#Generated by" + if (line.startsWith(GENERATED_BY_PRE)) { + continue; + } + + text.append(line); + + // Restore the line break, unless it's the absolute last element in the array + if (i < lines.length - 1) { + text.append(SYSTEM_LINE_SEP); + } + } } // Format @@ -126,7 +149,7 @@ public String serialize(IMessagesBundle messagesBundle) { // handle group equal align and line break options if (config.isGroupKeysEnabled()) { String newGroup = getKeyGroup(key); - if (newGroup == null || !newGroup.equals(group)) { + if (group == null ? newGroup != null : !group.equals(newGroup)) { group = newGroup; equalIndex = getEqualIndex(key, group, messagesBundle); for (int j = 0; j < numOfLineBreaks; j++) { @@ -161,8 +184,12 @@ public String serialize(IMessagesBundle messagesBundle) { * @see java.util.Properties */ private String convertUnicodeToEncoded(String str) { + if (str == null) { + return null; + } + int len = str.length(); - StringBuffer outBuffer = new StringBuffer(len * 2); + StringBuilder outBuffer = new StringBuilder(len * 2); for (int x = 0; x < len; x++) { char aChar = str.charAt(x); @@ -239,16 +266,15 @@ private void appendValue(StringBuffer text, String value, int equalIndex, if (breakPos != -1) { endPos = breakPos + SYSTEM_LINE_SEP.length(); saveValue(text, valueBuf.substring(0, endPos)); - // text.append(valueBuf.substring(0, endPos)); } else { breakPos = line.lastIndexOf(' '); - if (breakPos != -1) { - endPos = breakPos + 1; - saveValue(text, valueBuf.substring(0, endPos)); - // text.append(valueBuf.substring(0, endPos)); - text.append("\\"); //$NON-NLS-1$ - text.append(SYSTEM_LINE_SEP); + if (breakPos == -1) { + breakPos = endPos - 1; } + endPos = breakPos + 1; + saveValue(text, valueBuf.substring(0, endPos)); + text.append("\\"); //$NON-NLS-1$ + text.append(SYSTEM_LINE_SEP); } valueBuf.delete(0, endPos); // Figure out starting position for next line