-
-
Save hdsdi3g/73c2f796d52c18beb875 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* "XXXXXXXX_MON FICHIER.mov" => "S0054321_MON_FICHIER.mov" | |
* "XXXXXXXXMon Autre fichier &à archiver .mov" => "00012345_MON_AUTRE_FICHIER_A_ARCHIVER.mov" | |
*/ | |
public static String sanitiseFilename(String filename) { | |
StringBuffer sb = new StringBuffer(); | |
String filename_normalized = combining_diacritical_marks.matcher(Normalizer.normalize(filename, Normalizer.Form.NFD)).replaceAll("").toUpperCase(); | |
char[] filename_chr = filename_normalized.trim().toCharArray(); | |
char chr; | |
for (int pos = 0; pos < filename_chr.length; pos++) { | |
chr = filename_chr[pos]; | |
if (pos > -1 & pos < 8) { | |
/** | |
* add id | |
*/ | |
sb.append(chr); | |
continue; | |
} | |
if ((pos == 8) & (pos != (filename_chr.length - 4))) { | |
/** | |
* first char after ID | |
*/ | |
if (chr == CHR_SPACE | chr == CHR_UNDERSCORE | chr == CHR_MINUS) { | |
sb.append("_"); | |
} else { | |
sb.append("_"); | |
sb.append(chr); | |
} | |
continue; | |
} | |
if (pos == (filename_chr.length - 5)) { | |
/** | |
* lastchar before ext | |
*/ | |
if (chr == CHR_SPACE | chr == CHR_UNDERSCORE | chr == CHR_MINUS) { | |
continue; | |
} | |
} | |
if (pos == (filename_chr.length - 4)) { | |
/** | |
* "." | |
*/ | |
sb.append(chr); | |
continue; | |
} | |
if (pos > (filename_chr.length - 4)) { | |
/** | |
* ext | |
*/ | |
sb.append((char) (chr + 32)); | |
continue; | |
} | |
if ((chr < 45) | (chr == CHR_DOT) | (chr == CHR_SLASH) | (chr > 57 & chr < 65) | (chr > 90 & chr < 97) | (chr > 122 & chr < 128)) { | |
/** | |
* Everything else 0-9 a-z A-Z "_" "-" | |
*/ | |
if ((sb.charAt(sb.length() - 1) != CHR_UNDERSCORE) & (sb.charAt(sb.length() - 1) != CHR_MINUS)) { | |
/** | |
* The last added char is not a "_" or "-" | |
*/ | |
sb.append("_"); | |
} | |
continue; | |
} | |
sb.append(chr); | |
} | |
return sb.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment