Skip to content

Commit

Permalink
FIPS-5770/FIPS-11965 :: on windows load descriptions with CRLF
Browse files Browse the repository at this point in the history
Change-Id: I8b0d4bcf43150fb316e8a2b3201f1926cb0b69d8
  • Loading branch information
hookyAt committed Aug 13, 2024
1 parent bc7f03a commit 4beaa2f
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<setEntry value="org.apache.httpcomponents.httpclient@default:default"/>
<setEntry value="org.apache.httpcomponents.httpcore@default:default"/>
<setEntry value="org.apache.logging.log4j.api@default:default"/>
<setEntry value="org.apache.logging.log4j.to.slf4j@default:default"/>
<setEntry value="org.apache.lucene.analysis-common@default:default"/>
<setEntry value="org.apache.lucene.analysis-smartcn@default:default"/>
<setEntry value="org.apache.lucene.core@default:default"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.File;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.commons.lang3.SystemUtils;
import org.eclipse.core.runtime.ICoreRunnable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.faktorips.devtools.abstraction.ABuildKind;
Expand Down Expand Up @@ -63,4 +64,19 @@ public String getName(PlainJavaProject project) {
return project.file().getAbsolutePath();
}

@Override
public boolean isWindows() {
return SystemUtils.IS_OS_WINDOWS;
}

@Override
public boolean isLinux() {
return SystemUtils.IS_OS_LINUX;
}

@Override
public boolean isMac() {
return SystemUtils.IS_OS_MAC;
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*******************************************************************************
* Copyright (c) Faktor Zehn GmbH - faktorzehn.org
*
*
* This source code is available under the terms of the AGPL Affero General Public License version
* 3.
*
*
* Please see LICENSE.txt for full license terms, including the additional permissions and
* restrictions as well as the possibility of alternative license terms.
*******************************************************************************/
Expand Down Expand Up @@ -56,4 +56,18 @@ public interface AWorkspace extends AAbstraction {
*/
void build(ABuildKind buildKind, @CheckForNull IProgressMonitor monitor);

/**
* @return {@code true} if the current OS is Windows
*/
boolean isWindows();

/**
* @return {@code true} if the current OS is Linux
*/
boolean isLinux();

/**
* @return {@code true} if the current OS is MacOSX
*/
boolean isMac();
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*******************************************************************************
* Copyright (c) Faktor Zehn GmbH - faktorzehn.org
*
*
* This source code is available under the terms of the AGPL Affero General Public License version
* 3.
*
*
* Please see LICENSE.txt for full license terms, including the additional permissions and
* restrictions as well as the possibility of alternative license terms.
*******************************************************************************/
Expand All @@ -15,6 +15,7 @@
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.runtime.ICoreRunnable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform.OS;
import org.faktorips.devtools.abstraction.ABuildKind;
import org.faktorips.devtools.abstraction.AWorkspace;
import org.faktorips.devtools.abstraction.AWorkspaceRoot;
Expand Down Expand Up @@ -50,4 +51,19 @@ public void runAsync(ICoreRunnable action, IProgressMonitor monitor) {
public void build(ABuildKind buildKind, IProgressMonitor monitor) {
Wrappers.run(() -> workspace().build(buildKind(buildKind), monitor));
}

@Override
public boolean isWindows() {
return OS.isWindows();
}

@Override
public boolean isLinux() {
return OS.isLinux();
}

@Override
public boolean isMac() {
return OS.isMac();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.text.MessageFormat;
import java.util.Locale;

import org.faktorips.devtools.abstraction.Abstractions;
import org.faktorips.devtools.model.internal.IpsModel;
import org.faktorips.devtools.model.ipsobject.IDescription;
import org.faktorips.devtools.model.ipsobject.IIpsObjectPartContainer;
Expand Down Expand Up @@ -121,14 +122,25 @@ protected void initPropertiesFromXml(Element element, String id) {
locale = IpsStringUtils.isBlank(localeCode) ? null : new Locale(localeCode);
text = element.getTextContent();

if (isWindows() && text != null) {
text = text.replace("\n", "\r\n");
}

super.initPropertiesFromXml(element, id);
}

private boolean isWindows() {
return Abstractions.getWorkspace().isWindows();
}

@Override
protected void propertiesToXml(Element element) {
super.propertiesToXml(element);

element.setAttribute(PROPERTY_LOCALE, (locale == null) ? "" : locale.getLanguage()); //$NON-NLS-1$
if (isWindows() && text != null) {
text = text.replace("\r\n", "\n");
}
element.setTextContent(text);
element.removeAttribute(IpsObjectPart.PROPERTY_ID);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,9 @@ private static String removeOrEscapeUnwantedCharacters(String xml, boolean escap
replacementList.add(CARRIAGE_RETURN);
}

searchList.add("\r\n");
replacementList.add("\n");

for (int i = 0; i <= 31; i++) {
if (i != '\t' && i != '\n') {
String unicodeControlCharacter = "&#" + i + ";";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;
Expand All @@ -43,7 +45,9 @@

public class XmlUtilTest extends XmlAbstractTestCase {

private static final String LF = System.lineSeparator();
private static final String SYS_LINE_SEPARATOR = System.lineSeparator();
private static final String LF = "\n";
private static final String CR_LF = "\r\n";
private static final String UTF8 = "UTF-8";
private static final String XML_EXT_PROPERTIES_ELEMENT = "ExtensionProperties";

Expand Down Expand Up @@ -226,12 +230,12 @@ public void testJava9NoIndentationToXmlDataContent() throws Exception {

// java9 transformer has empty lines
assertThat(internalNodeToString(rootElement),
is("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LF
+ "<root>" + LF
+ " " + LF
+ " <element>SOME_DATA</element>" + LF
+ " " + LF
+ "</root>" + LF));
is("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + SYS_LINE_SEPARATOR
+ "<root>" + SYS_LINE_SEPARATOR
+ " " + SYS_LINE_SEPARATOR
+ " <element>SOME_DATA</element>" + SYS_LINE_SEPARATOR
+ " " + SYS_LINE_SEPARATOR
+ "</root>" + SYS_LINE_SEPARATOR));
// java9 fix with regex, removes empty lines
assertThat(XmlUtil.nodeToString(rootElement, UTF8),
is("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LF
Expand All @@ -251,32 +255,32 @@ public void testJava9NoIndentationToXmlDataContentWithTabs() throws Exception {

// TestDocument has Tabs on single empty lines
assertThat(internalNodeToString(root),
is("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LF
+ "<DocElement>" + LF
+ " \t" + LF
+ " <TestElement value=\"öäüÖÄÜß\">blabla</TestElement>" + LF
+ " \t" + LF
+ " <DifferentElement/>" + LF
+ " \t" + LF
+ " <TestElement value=\"2\"/>" + LF
+ " " + LF
+ " <!-- -->" + LF
+ " \t" + LF
+ " <ChildA id=\"0\" type=\"testtype1\"/>" + LF
+ " \t" + LF
+ " <ChildA id=\"1\" type=\"testtype1\"/>" + LF
+ " \t" + LF
+ " <ChildA id=\"2\" type=\"testtype2\"/>" + LF
+ " \t" + LF
+ " <ChildB>testValue</ChildB>" + LF
+ " \t" + LF
+ " <ChildC>" + LF
+ " \t\t" + LF
+ " <ChildA id=\"3\" type=\"deep\"/>" + LF
+ " \t" + LF
+ " </ChildC>" + LF
+ " " + LF
+ "</DocElement>" + LF));
is("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + SYS_LINE_SEPARATOR
+ "<DocElement>" + SYS_LINE_SEPARATOR
+ " \t" + SYS_LINE_SEPARATOR
+ " <TestElement value=\"öäüÖÄÜß\">blabla</TestElement>" + SYS_LINE_SEPARATOR
+ " \t" + SYS_LINE_SEPARATOR
+ " <DifferentElement/>" + SYS_LINE_SEPARATOR
+ " \t" + SYS_LINE_SEPARATOR
+ " <TestElement value=\"2\"/>" + SYS_LINE_SEPARATOR
+ " " + SYS_LINE_SEPARATOR
+ " <!-- -->" + SYS_LINE_SEPARATOR
+ " \t" + SYS_LINE_SEPARATOR
+ " <ChildA id=\"0\" type=\"testtype1\"/>" + SYS_LINE_SEPARATOR
+ " \t" + SYS_LINE_SEPARATOR
+ " <ChildA id=\"1\" type=\"testtype1\"/>" + SYS_LINE_SEPARATOR
+ " \t" + SYS_LINE_SEPARATOR
+ " <ChildA id=\"2\" type=\"testtype2\"/>" + SYS_LINE_SEPARATOR
+ " \t" + SYS_LINE_SEPARATOR
+ " <ChildB>testValue</ChildB>" + SYS_LINE_SEPARATOR
+ " \t" + SYS_LINE_SEPARATOR
+ " <ChildC>" + SYS_LINE_SEPARATOR
+ " \t\t" + SYS_LINE_SEPARATOR
+ " <ChildA id=\"3\" type=\"deep\"/>" + SYS_LINE_SEPARATOR
+ " \t" + SYS_LINE_SEPARATOR
+ " </ChildC>" + SYS_LINE_SEPARATOR
+ " " + SYS_LINE_SEPARATOR
+ "</DocElement>" + SYS_LINE_SEPARATOR));
// TestDocument has no Tabs and no empty lines
assertThat(XmlUtil.nodeToString(root, UTF8),
is("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LF
Expand Down Expand Up @@ -308,18 +312,19 @@ public void testJava9NoIndentationToXmlMixedContent() throws Exception {

// java9 transformer has empty lines
assertThat(internalNodeToString(rootElement),
is("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LF + "<root>" + LF + " " + LF
+ " <element>" + LF
+ " SOME_DATA_WITH_LEADING_SPACE" + LF
+ " " + LF
+ " <ExtensionProperties>" + LF
+ " " + LF
+ " <Value/>" + LF
+ " " + LF
+ " </ExtensionProperties>" + LF
+ " " + LF
+ " </element>" + LF + " " + LF
+ "</root>" + LF));
is("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + SYS_LINE_SEPARATOR + "<root>" + SYS_LINE_SEPARATOR
+ " " + SYS_LINE_SEPARATOR
+ " <element>" + SYS_LINE_SEPARATOR
+ " SOME_DATA_WITH_LEADING_SPACE" + SYS_LINE_SEPARATOR
+ " " + SYS_LINE_SEPARATOR
+ " <ExtensionProperties>" + SYS_LINE_SEPARATOR
+ " " + SYS_LINE_SEPARATOR
+ " <Value/>" + SYS_LINE_SEPARATOR
+ " " + SYS_LINE_SEPARATOR
+ " </ExtensionProperties>" + SYS_LINE_SEPARATOR
+ " " + SYS_LINE_SEPARATOR
+ " </element>" + SYS_LINE_SEPARATOR + " " + SYS_LINE_SEPARATOR
+ "</root>" + SYS_LINE_SEPARATOR));
// java9 fix with regex, removes empty lines
assertThat(XmlUtil.nodeToString(rootElement, UTF8),
is("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LF
Expand Down Expand Up @@ -390,6 +395,42 @@ public void testEscapeUnicodeControlCharactersToXml() throws TransformerExceptio
}
}

@Test
public void testConvertWindowsLinefeedsToLinuxLinefeeds() throws Exception {
File xmlFile = createXmlFileAndSaveWithIdent();
// simulate windows on build server
String content = Files.readString(xmlFile.toPath(), StandardCharsets.UTF_8);
if (!content.contains(CR_LF)) {
content = content.replace(LF, CR_LF);
}
Document doc = XmlUtil.parseDocument(new ByteArrayInputStream(content.getBytes()));
Element rootElement = XmlUtil.getFirstElement(doc, "root");

assertThat(XmlUtil.nodeToString(rootElement, UTF8),
is("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LF
+ "<root>" + LF
+ " <element>SOME_DATA</element>" + LF
+ "</root>" + LF));
}

@Test
public void testDoNotConvertLinuxLinefeeds() throws Exception {
File xmlFile = createXmlFileAndSaveWithIdent();
// simulate linux on windows machine
String content = Files.readString(xmlFile.toPath(), StandardCharsets.UTF_8);
if (content.contains(CR_LF)) {
content = content.replace(CR_LF, LF);
}
Document doc = XmlUtil.parseDocument(new ByteArrayInputStream(content.getBytes()));
Element rootElement = XmlUtil.getFirstElement(doc, "root");

assertThat(XmlUtil.nodeToString(rootElement, UTF8),
is("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LF
+ "<root>" + LF
+ " <element>SOME_DATA</element>" + LF
+ "</root>" + LF));
}

private String internalNodeToString(Element rootElement) throws TransformerException {
StringWriter writer = new StringWriter();
XmlUtil.nodeToWriter(rootElement, writer, UTF8);
Expand Down

0 comments on commit 4beaa2f

Please sign in to comment.