Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AP-5089 Change logic when normalize log file name during import #1430

Merged
merged 1 commit into from
Nov 4, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,42 @@
*/
package org.apromore.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

private static final String FILENAME_CONSTRAINT = "[a-zA-Z0-9 \\[\\]._+\\-()]+";

private StringUtil() {
throw new IllegalStateException("String Utility class");
}

/**
* Removes all illegal filename characters from a given String
*
* @param name user input filename
* @return normalized file name
* @see "http://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words"
*/
public static String normalizeFilename(String name) {
StringBuilder normalized = new StringBuilder();
try {
Pattern pattern = Pattern.compile(FILENAME_CONSTRAINT);
Matcher matcher = pattern.matcher(name);

while (matcher.find()) {
normalized.append(matcher.group());
}
} catch (Exception e) {
// ignore exception
} finally {
if (normalized.length() == 0) {
normalized = new StringBuilder("Untitled");
} else if (normalized.length() > 60) {
normalized = new StringBuilder(normalized.substring(0, 59));
}

String normalized = name.trim();

// remove illegal characters
normalized = normalized.replaceAll(
"[\\/|\\\\|\\*|\\:|\\||\"|\'|\\<|\\>|\\{|\\}|\\?|\\%|,]",
"");

// replace . dots with _ and remove the _ if at the end
normalized = normalized.replaceAll("\\.", "_");
if (normalized.endsWith("_")) {
normalized = normalized.substring(0, normalized.length() - 1);
}

// replace whitespace characters with _
normalized = normalized.replaceAll("\\s+", "_");

if (normalized.length() == 0) {
normalized = "Untitled";
} else if (normalized.length() > 60) {
normalized = normalized.substring(0, 59);
}
return normalized.toString();
return normalized;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ public void testNormalizeFilename() {
String fileName_1 = "abc*d?.=";
String fileName_2 = "abc*abcdzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz_______________more_than_60_characters";
String fileName_3 = "?";
String fileName_4 = "購買プロセス_2.";
String fileName_5 = "_تحياتي";

assertEquals("abcd.", StringUtil.normalizeFilename(fileName_1));
assertEquals("abcd_=", StringUtil.normalizeFilename(fileName_1));
assertEquals("abcabcdzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz_______________",
StringUtil.normalizeFilename(fileName_2));
assertEquals("Untitled", StringUtil.normalizeFilename(fileName_3));
assertEquals("購買プロセス_2", StringUtil.normalizeFilename(fileName_4));
assertEquals("_تحياتي", StringUtil.normalizeFilename(fileName_5));
}
}