Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mxMarketplace {
moduleLicense = 'Apache V2'
appDirectory = "src/CommunityCommons"
versionPathPrefix = "_Version " // the path prefix within the module to the version folder
includeFiles = ["$mprDir/License.txt", "$mprDir/SiemensMendixCommunityCommons__11.0.0__READMEOSS.html"]
includeFiles = ["$mprDir/License.txt", "$mprDir/SiemensMendixCommunityCommons__11.0.1__READMEOSS.html"]
syncJavaDependenciesBeforeBuild = true
}

Expand Down
1 change: 1 addition & 0 deletions marketplace/release-notes/11.0.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- The StringFromFile Java action now removes BOM from strings. (Ticket #242904)
Binary file modified src/CommunityCommons/CommunityCommons.mpr
Binary file not shown.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import javax.swing.text.html.parser.ParserDelegator;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.BOMInputStream;
import org.apache.commons.text.StringEscapeUtils;
import org.owasp.html.PolicyFactory;
import org.owasp.html.Sanitizers;
Expand Down Expand Up @@ -222,10 +223,14 @@ public static String stringFromFile(IContext context, FileDocument source, Chars
return null;
}
try (InputStream f = Core.getFileDocumentContent(context, source.getMendixObject())) {
return IOUtils.toString(f, charset);
return stringFromInputStream(f, charset);
}
}

public static String stringFromInputStream(InputStream inputStream, Charset charset) throws IOException {
return IOUtils.toString(BOMInputStream.builder().setInputStream(inputStream).get(), charset);
}

public static void stringToFile(IContext context, String value, FileDocument destination) throws IOException {
stringToFile(context, value, destination, StandardCharsets.UTF_8);
}
Expand Down
32 changes: 32 additions & 0 deletions src/test/communitycommons/StringUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
import org.owasp.html.PolicyFactory;
import org.owasp.html.Sanitizers;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.security.DigestException;
import java.security.NoSuchAlgorithmException;

Expand Down Expand Up @@ -170,4 +174,32 @@ public void testHash() throws DigestException, NoSuchAlgorithmException {
assertEquals(StringUtils.hash(originalString), StringUtils.hash(originalString, length));
assertEquals(StringUtils.hash(originalString), hashedString);
}

@Test
public void testStringFromInputStream() throws IOException {
Charset utf8 = Charset.forName("UTF-8");
Charset utf16 = Charset.forName("UTF-16");
Charset utf16be = Charset.forName("UTF-16BE");
Charset utf16le = Charset.forName("UTF-16LE");

String text = "hello";

assertEquals(text, testStringFromInputStream(text, utf8));
assertEquals(text, testStringFromInputStream(text, utf16));
assertEquals(text, testStringFromInputStream(text, utf16be));
assertEquals(text, testStringFromInputStream(text, utf16le));

// BOM should be removed (UTF-8)
byte[] UTF8BOM = { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF };
String textUTF8BOM = new String(UTF8BOM) + text;
assertEquals(text, testStringFromInputStream(textUTF8BOM, utf8));
}

private String testStringFromInputStream(String text, Charset charset) throws IOException {
return StringUtils.stringFromInputStream(stringToInputStream(text, charset), charset);
}

private InputStream stringToInputStream(String str, Charset charset) {
return new ByteArrayInputStream(str.getBytes(charset));
}
}