| Nils Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.util; |
| 2 | |
| 3 | import java.io.*; |
| Eliza Margaretha | 805e27f | 2016-10-14 21:39:42 +0200 | [diff] [blame] | 4 | import java.net.URLDecoder; |
| Nils Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 5 | import java.nio.file.Files; |
| Eliza Margaretha | 805e27f | 2016-10-14 21:39:42 +0200 | [diff] [blame] | 6 | import java.nio.file.Path; |
| Nils Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 7 | import java.nio.file.Paths; |
| 8 | import java.nio.charset.Charset; |
| 9 | import java.nio.charset.StandardCharsets; |
| 10 | |
| 11 | /** |
| Nils Diewald | fe6a365 | 2015-02-05 20:34:27 +0000 | [diff] [blame] | 12 | * A collection of string related utility |
| 13 | * functions. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 14 | * |
| Nils Diewald | fe6a365 | 2015-02-05 20:34:27 +0000 | [diff] [blame] | 15 | * @author diewald |
| Nils Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 16 | */ |
| Nils Diewald | c383ed0 | 2015-02-26 21:35:22 +0000 | [diff] [blame] | 17 | public class KrillString { |
| Nils Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 18 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 19 | /** |
| Nils Diewald | fe6a365 | 2015-02-05 20:34:27 +0000 | [diff] [blame] | 20 | * Get String from file. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 21 | * |
| 22 | * @param path |
| 23 | * The path of the file represented as a string. |
| 24 | * @param path |
| 25 | * The expected {@link Charset}. |
| Nils Diewald | fe6a365 | 2015-02-05 20:34:27 +0000 | [diff] [blame] | 26 | * @return The content of the file |
| 27 | * @throws IOException |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 28 | */ |
| 29 | public static String StringfromFile (String path, Charset encoding) |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 30 | throws IOException { |
| Eliza Margaretha | 6f98920 | 2016-10-14 21:48:29 +0200 | [diff] [blame] | 31 | path = URLDecoder.decode(path, "UTF-8"); |
| Eliza Margaretha | 805e27f | 2016-10-14 21:39:42 +0200 | [diff] [blame] | 32 | path = path.replaceFirst("^/(.:/)", "$1"); |
| 33 | Path p = Paths.get(path); |
| 34 | byte[] encoded = Files.readAllBytes(p); |
| Nils Diewald | fe6a365 | 2015-02-05 20:34:27 +0000 | [diff] [blame] | 35 | return new String(encoded, encoding); |
| Nils Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 38 | |
| 39 | /** |
| Nils Diewald | fe6a365 | 2015-02-05 20:34:27 +0000 | [diff] [blame] | 40 | * Get String from file (expecting UTF-8). |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 41 | * |
| 42 | * @param path |
| 43 | * The path of the file represented as a string. |
| Nils Diewald | fe6a365 | 2015-02-05 20:34:27 +0000 | [diff] [blame] | 44 | * @return The content of the file |
| 45 | * @throws IOException |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 46 | */ |
| Nils Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 47 | public static String StringfromFile (String path) throws IOException { |
| Nils Diewald | fe6a365 | 2015-02-05 20:34:27 +0000 | [diff] [blame] | 48 | return StringfromFile(path, StandardCharsets.UTF_8); |
| 49 | }; |
| 50 | |
| 51 | |
| 52 | /** |
| 53 | * Escape HTML relevant characters as entities. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 54 | * |
| 55 | * @param text |
| 56 | * The string to escape. |
| Nils Diewald | fe6a365 | 2015-02-05 20:34:27 +0000 | [diff] [blame] | 57 | * @return The secured string. |
| 58 | */ |
| 59 | public static String escapeHTML (String text) { |
| Akron | 2caa2f9 | 2017-09-12 11:50:16 +0200 | [diff] [blame] | 60 | |
| 61 | if (text == null) |
| 62 | return ""; |
| 63 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 64 | return text.replace("&", "&").replace("<", "<") |
| 65 | .replace(">", ">").replace("\"", """); |
| Nils Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 66 | }; |
| Akron | 74748c6 | 2016-06-29 00:22:43 +0200 | [diff] [blame] | 67 | |
| 68 | |
| 69 | /** |
| Akron | fc2625e | 2016-07-27 01:52:28 +0200 | [diff] [blame] | 70 | * Escape Bracket relevant characters. |
| 71 | * |
| 72 | * @param text |
| 73 | * The string to escape. |
| 74 | * @return The secured string. |
| 75 | */ |
| 76 | public static String escapeBrackets (String text) { |
| Akron | 967ee7c | 2024-06-20 12:52:56 +0200 | [diff] [blame^] | 77 | if (text == null) |
| 78 | return ""; |
| Akron | fc2625e | 2016-07-27 01:52:28 +0200 | [diff] [blame] | 79 | return text.replaceAll("([\\{\\}\\[\\]\\\\])", "\\\\$1"); |
| 80 | }; |
| 81 | |
| Akron | 08f4ceb | 2016-08-03 23:53:32 +0200 | [diff] [blame] | 82 | |
| Akron | fc2625e | 2016-07-27 01:52:28 +0200 | [diff] [blame] | 83 | /** |
| Akron | 74748c6 | 2016-06-29 00:22:43 +0200 | [diff] [blame] | 84 | * Add surrounding double quotes. |
| 85 | * |
| 86 | * @param text |
| 87 | * The string to escape. |
| 88 | * @return The secured string. |
| 89 | */ |
| 90 | public static String quote (String text) { |
| 91 | return '"' + text.replaceAll("([\"\\\\])", "\\\\$1") + '"'; |
| 92 | }; |
| Akron | 906470f | 2023-12-19 11:13:32 +0100 | [diff] [blame] | 93 | |
| 94 | |
| 95 | /** |
| 96 | * Provide a substring method that works well with surrogate pairs. |
| 97 | * |
| 98 | * @param text |
| 99 | * The string to substring. |
| 100 | * @param start |
| 101 | * The start offset. |
| 102 | * @param end |
| 103 | * The end offset. |
| 104 | * @return The substring. |
| 105 | */ |
| 106 | public static String codePointSubstring(String text, int start, int end) { |
| 107 | int a = text.offsetByCodePoints(0, start); |
| 108 | return text.substring( |
| 109 | a, |
| 110 | text.offsetByCodePoints(a, end - start) |
| 111 | ); |
| 112 | }; |
| 113 | |
| 114 | /** |
| 115 | * Provide a substring method that works well with surrogate pairs. |
| 116 | * |
| 117 | * @param text |
| 118 | * The string to substring. |
| 119 | * @param start |
| 120 | * The start offset. |
| 121 | * @return The substring. |
| 122 | */ |
| 123 | public static String codePointSubstring(String text, int start) { |
| 124 | return text.substring(text.offsetByCodePoints(0, start)); |
| 125 | }; |
| Nils Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 126 | }; |