From 9dc9cbd8212829026c2b0507e30f4f9db2381fe1 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sun, 13 Oct 2019 21:52:53 +0200 Subject: [PATCH 01/10] Remove the unused toISO8601 method --- .../org/airsonic/player/util/StringUtil.java | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java b/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java index ebb0f725..b4d4b401 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java +++ b/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java @@ -42,7 +42,6 @@ import java.util.regex.Pattern; public final class StringUtil { public static final String ENCODING_UTF8 = "UTF-8"; - private static final DateFormat ISO_8601_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); private static final String[][] HTML_SUBSTITUTIONS = { {"&", "&"}, @@ -120,24 +119,6 @@ public final class StringUtil { } - /** - * Formats the given date to a ISO-8601 date/time format, and UTC timezone. - *

- * The returned date uses the following format: 2007-12-17T14:57:17 - * - * @param date The date to format - * @return The corresponding ISO-8601 formatted string. - */ - public static String toISO8601(Date date) { - if (date == null) { - return null; - } - - synchronized (ISO_8601_DATE_FORMAT) { - return ISO_8601_DATE_FORMAT.format(date); - } - } - /** * Removes the suffix (the substring after the last dot) of the given string. The dot is * also removed. From e1583691d87bd99e4f41c5903f02e3d29e0c8f59 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sun, 13 Oct 2019 22:19:53 +0200 Subject: [PATCH 02/10] Replace StringUtil.toHtml with StringEscapeUtils.escapeHtml Apache commons is providing Html-escaping, no need to reinvent the wheel: > It supports all known HTML 4.0 entities, including funky accents. Note that the > commonly used apostrophe escape character (') is not a legal entity and so > is not supported). So I manually checked that nothing is relying on escaped single-quotes, and didn't manage to find anything that does. --- .../airsonic/player/ajax/CoverArtService.java | 4 +- .../player/ajax/NowPlayingService.java | 9 ++-- .../SetMusicFileInfoController.java | 4 +- .../player/controller/UploadController.java | 8 ++-- .../org/airsonic/player/util/StringUtil.java | 43 +------------------ .../player/util/StringUtilTestCase.java | 20 +++------ 6 files changed, 20 insertions(+), 68 deletions(-) diff --git a/airsonic-main/src/main/java/org/airsonic/player/ajax/CoverArtService.java b/airsonic-main/src/main/java/org/airsonic/player/ajax/CoverArtService.java index 9d8d3407..498936eb 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/ajax/CoverArtService.java +++ b/airsonic-main/src/main/java/org/airsonic/player/ajax/CoverArtService.java @@ -25,8 +25,8 @@ import org.airsonic.player.service.LastFmService; import org.airsonic.player.service.MediaFileService; import org.airsonic.player.service.SecurityService; import org.airsonic.player.util.FileUtil; -import org.airsonic.player.util.StringUtil; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.StringEscapeUtils; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; @@ -109,7 +109,7 @@ public class CoverArtService { // Check permissions. File newCoverFile = new File(path, "cover." + suffix); if (!securityService.isWriteAllowed(newCoverFile)) { - throw new Exception("Permission denied: " + StringUtil.toHtml(newCoverFile.getPath())); + throw new Exception("Permission denied: " + StringEscapeUtils.escapeHtml(newCoverFile.getPath())); } // If file exists, create a backup. diff --git a/airsonic-main/src/main/java/org/airsonic/player/ajax/NowPlayingService.java b/airsonic-main/src/main/java/org/airsonic/player/ajax/NowPlayingService.java index 302b1aa4..63e0a28a 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/ajax/NowPlayingService.java +++ b/airsonic-main/src/main/java/org/airsonic/player/ajax/NowPlayingService.java @@ -22,6 +22,7 @@ package org.airsonic.player.ajax; import org.airsonic.player.domain.*; import org.airsonic.player.service.*; import org.airsonic.player.util.StringUtil; +import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.lang.StringUtils; import org.directwebremoting.WebContext; import org.directwebremoting.WebContextFactory; @@ -129,14 +130,14 @@ public class NowPlayingService { avatarUrl = url + "avatar.view?usernameUtf8Hex=" + StringUtil.utf8HexEncode(username); } - String tooltip = StringUtil.toHtml(artist) + " – " + StringUtil.toHtml(title); + String tooltip = StringEscapeUtils.escapeHtml(artist) + " – " + StringEscapeUtils.escapeHtml(title); if (StringUtils.isNotBlank(player.getName())) { username += "@" + player.getName(); } - artist = StringUtil.toHtml(StringUtils.abbreviate(artist, 25)); - title = StringUtil.toHtml(StringUtils.abbreviate(title, 25)); - username = StringUtil.toHtml(StringUtils.abbreviate(username, 25)); + artist = StringEscapeUtils.escapeHtml(StringUtils.abbreviate(artist, 25)); + title = StringEscapeUtils.escapeHtml(StringUtils.abbreviate(title, 25)); + username = StringEscapeUtils.escapeHtml(StringUtils.abbreviate(username, 25)); long minutesAgo = status.getMinutesAgo(); diff --git a/airsonic-main/src/main/java/org/airsonic/player/controller/SetMusicFileInfoController.java b/airsonic-main/src/main/java/org/airsonic/player/controller/SetMusicFileInfoController.java index 92018ecd..8a37d5ee 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/controller/SetMusicFileInfoController.java +++ b/airsonic-main/src/main/java/org/airsonic/player/controller/SetMusicFileInfoController.java @@ -21,7 +21,7 @@ package org.airsonic.player.controller; import org.airsonic.player.domain.MediaFile; import org.airsonic.player.service.MediaFileService; -import org.airsonic.player.util.StringUtil; +import org.apache.commons.lang.StringEscapeUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.ServletRequestUtils; @@ -52,7 +52,7 @@ public class SetMusicFileInfoController { MediaFile mediaFile = mediaFileService.getMediaFile(id); if ("comment".equals(action)) { - mediaFile.setComment(StringUtil.toHtml(request.getParameter("comment"))); + mediaFile.setComment(StringEscapeUtils.escapeHtml(request.getParameter("comment"))); mediaFileService.updateMediaFile(mediaFile); } diff --git a/airsonic-main/src/main/java/org/airsonic/player/controller/UploadController.java b/airsonic-main/src/main/java/org/airsonic/player/controller/UploadController.java index 5256fb7d..6f6936ab 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/controller/UploadController.java +++ b/airsonic-main/src/main/java/org/airsonic/player/controller/UploadController.java @@ -28,10 +28,10 @@ import org.airsonic.player.service.StatusService; import org.airsonic.player.upload.MonitoredDiskFileItemFactory; import org.airsonic.player.upload.UploadListener; import org.airsonic.player.util.FileUtil; -import org.airsonic.player.util.StringUtil; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; +import org.apache.commons.lang.StringEscapeUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -127,7 +127,7 @@ public class UploadController { File targetFile = new File(dir, new File(fileName).getName()); if (!securityService.isUploadAllowed(targetFile)) { - throw new Exception("Permission denied: " + StringUtil.toHtml(targetFile.getPath())); + throw new Exception("Permission denied: " + StringEscapeUtils.escapeHtml(targetFile.getPath())); } if (!dir.exists()) { @@ -174,13 +174,13 @@ public class UploadController { ZipEntry entry = (ZipEntry) entries.nextElement(); File entryFile = new File(file.getParentFile(), entry.getName()); if (!entryFile.toPath().normalize().startsWith(file.getParentFile().toPath())) { - throw new Exception("Bad zip filename: " + StringUtil.toHtml(entryFile.getPath())); + throw new Exception("Bad zip filename: " + StringEscapeUtils.escapeHtml(entryFile.getPath())); } if (!entry.isDirectory()) { if (!securityService.isUploadAllowed(entryFile)) { - throw new Exception("Permission denied: " + StringUtil.toHtml(entryFile.getPath())); + throw new Exception("Permission denied: " + StringEscapeUtils.escapeHtml(entryFile.getPath())); } entryFile.getParentFile().mkdirs(); diff --git a/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java b/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java index b4d4b401..2b459379 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java +++ b/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java @@ -20,6 +20,7 @@ package org.airsonic.player.util; import org.apache.commons.codec.binary.Hex; +import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.lang.StringUtils; import java.io.*; @@ -43,14 +44,6 @@ public final class StringUtil { public static final String ENCODING_UTF8 = "UTF-8"; - private static final String[][] HTML_SUBSTITUTIONS = { - {"&", "&"}, - {"<", "<"}, - {">", ">"}, - {"'", "'"}, - {"\"", """}, - }; - private static final String[][] MIME_TYPES = { {"mp3", "audio/mpeg"}, {"ogg", "audio/ogg"}, @@ -97,40 +90,6 @@ public final class StringUtil { private StringUtil() { } - /** - * Returns the specified string converted to a format suitable for - * HTML. All single-quote, double-quote, greater-than, less-than and - * ampersand characters are replaces with their corresponding HTML - * Character Entity code. - * - * @param s the string to convert - * @return the converted string - */ - public static String toHtml(String s) { - if (s == null) { - return null; - } - for (String[] substitution : HTML_SUBSTITUTIONS) { - if (s.contains(substitution[0])) { - s = s.replaceAll(substitution[0], substitution[1]); - } - } - return s; - } - - - /** - * Removes the suffix (the substring after the last dot) of the given string. The dot is - * also removed. - * - * @param s The string in question, e.g., "foo.mp3". - * @return The string without the suffix, e.g., "foo". - */ - public static String removeSuffix(String s) { - int index = s.lastIndexOf('.'); - return index == -1 ? s : s.substring(0, index); - } - /** * Returns the proper MIME type for the given suffix. * diff --git a/airsonic-main/src/test/java/org/airsonic/player/util/StringUtilTestCase.java b/airsonic-main/src/test/java/org/airsonic/player/util/StringUtilTestCase.java index c45cd14b..b187acb8 100644 --- a/airsonic-main/src/test/java/org/airsonic/player/util/StringUtilTestCase.java +++ b/airsonic-main/src/test/java/org/airsonic/player/util/StringUtilTestCase.java @@ -20,6 +20,7 @@ package org.airsonic.player.util; import junit.framework.TestCase; +import org.apache.commons.lang.StringEscapeUtils; import java.util.Arrays; import java.util.Locale; @@ -32,20 +33,11 @@ import java.util.Locale; public class StringUtilTestCase extends TestCase { public void testToHtml() { - assertEquals(null, StringUtil.toHtml(null)); - assertEquals("", StringUtil.toHtml("")); - assertEquals(" ", StringUtil.toHtml(" ")); - assertEquals("q & a", StringUtil.toHtml("q & a")); - assertEquals("q & a <> b", StringUtil.toHtml("q & a <> b")); - } - - public void testRemoveSuffix() { - assertEquals("Error in removeSuffix().", "foo", StringUtil.removeSuffix("foo.mp3")); - assertEquals("Error in removeSuffix().", "", StringUtil.removeSuffix(".mp3")); - assertEquals("Error in removeSuffix().", "foo.bar", StringUtil.removeSuffix("foo.bar.mp3")); - assertEquals("Error in removeSuffix().", "foo.", StringUtil.removeSuffix("foo..mp3")); - assertEquals("Error in removeSuffix().", "foo", StringUtil.removeSuffix("foo")); - assertEquals("Error in removeSuffix().", "", StringUtil.removeSuffix("")); + assertEquals(null, StringEscapeUtils.escapeHtml(null)); + assertEquals("", StringEscapeUtils.escapeHtml("")); + assertEquals(" ", StringEscapeUtils.escapeHtml(" ")); + assertEquals("q & a", StringEscapeUtils.escapeHtml("q & a")); + assertEquals("q & a <> b", StringEscapeUtils.escapeHtml("q & a <> b")); } public void testGetMimeType() { From 29b083ddac7b7c42ae3a024ca13ca79d248c458e Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sun, 13 Oct 2019 22:40:59 +0200 Subject: [PATCH 03/10] Simplify the split method Use a regexp with two groups, instead of doing some black magic manually. --- .../org/airsonic/player/util/StringUtil.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java b/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java index 2b459379..b0a157d9 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java +++ b/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java @@ -20,7 +20,6 @@ package org.airsonic.player.util; import org.apache.commons.codec.binary.Hex; -import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.lang.StringUtils; import java.io.*; @@ -44,6 +43,8 @@ public final class StringUtil { public static final String ENCODING_UTF8 = "UTF-8"; + private static final Pattern SPLIT_PATTERN = Pattern.compile("\"([^\"]*)\"|(\\S+)"); + private static final String[][] MIME_TYPES = { {"mp3", "audio/mpeg"}, {"ogg", "audio/ogg"}, @@ -195,16 +196,14 @@ public final class StringUtil { return new String[0]; } - Pattern pattern = Pattern.compile("\".*?\"|\\S+"); - Matcher matcher = pattern.matcher(input); - - List result = new ArrayList(); - while (matcher.find()) { - String element = matcher.group(); - if (element.startsWith("\"") && element.endsWith("\"") && element.length() > 1) { - element = element.substring(1, element.length() - 1); + List result = new ArrayList<>(); + Matcher m = SPLIT_PATTERN.matcher(input); + while (m.find()) { + if (m.group(1) != null) { + result.add(m.group(1)); // quoted string + } else { + result.add(m.group(2)); // unquoted string } - result.add(element); } return result.toArray(new String[result.size()]); From 332718386b21a21b908588a58d05952b37966218 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sun, 13 Oct 2019 22:43:28 +0200 Subject: [PATCH 04/10] Simplify the parseInts method --- .../main/java/org/airsonic/player/util/StringUtil.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java b/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java index b0a157d9..ba9c599e 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java +++ b/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java @@ -33,6 +33,7 @@ import java.text.*; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Stream; /** * Miscellaneous string utility methods. @@ -249,12 +250,9 @@ public final class StringUtil { return new int[0]; } - String[] strings = StringUtils.split(s); - int[] ints = new int[strings.length]; - for (int i = 0; i < strings.length; i++) { - ints[i] = Integer.parseInt(strings[i]); - } - return ints; + return Stream.of(StringUtils.split(s)) + .mapToInt(Integer::parseInt) + .toArray(); } /** From 6b540865902062427b1c4d34755bf595bb26a1e3 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sun, 13 Oct 2019 22:43:51 +0200 Subject: [PATCH 05/10] Remove an unused method --- .../main/java/org/airsonic/player/util/StringUtil.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java b/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java index ba9c599e..987e2b62 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java +++ b/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java @@ -255,15 +255,6 @@ public final class StringUtil { .toArray(); } - /** - * Determines whether a is equal to b, taking null into account. - * - * @return Whether a and b are equal, or both null. - */ - public static boolean isEqual(Object a, Object b) { - return Objects.equals(a, b); - } - /** * Parses a locale from the given string. * From e1a8a3b752b82c8ffd3a3bd3412ad7e903217647 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sun, 13 Oct 2019 22:45:00 +0200 Subject: [PATCH 06/10] Use a switch-case instead of if-spaghettis --- .../org/airsonic/player/util/StringUtil.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java b/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java index 987e2b62..ed796630 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java +++ b/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java @@ -267,17 +267,16 @@ public final class StringUtil { } String[] elements = s.split("_"); - - if (elements.length == 0) { - return new Locale(s, "", ""); - } - if (elements.length == 1) { - return new Locale(elements[0], "", ""); - } - if (elements.length == 2) { - return new Locale(elements[0], elements[1], ""); + switch (elements.length) { + case 0: + return new Locale(s, "", ""); + case 1: + return new Locale(elements[0], "", ""); + case 2: + return new Locale(elements[0], elements[1], ""); + default: + return new Locale(elements[0], elements[1], elements[2]); } - return new Locale(elements[0], elements[1], elements[2]); } /** From d07d0ad6873a0fb38698ad33e9469c4504fd3375 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sun, 13 Oct 2019 22:46:08 +0200 Subject: [PATCH 07/10] Tighten en exception --- .../src/main/java/org/airsonic/player/util/StringUtil.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java b/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java index ed796630..0080b8cd 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java +++ b/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java @@ -19,6 +19,7 @@ */ package org.airsonic.player.util; +import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.binary.Hex; import org.apache.commons.lang.StringUtils; @@ -321,9 +322,9 @@ public final class StringUtil { * * @param s The string to decode. * @return The decoded string. - * @throws Exception If an error occurs. + * @throws DecoderException If an error occurs. */ - public static String utf8HexDecode(String s) throws Exception { + public static String utf8HexDecode(String s) throws DecoderException { if (s == null) { return null; } From d2ad85447b6cf6ad99a43e2ef0b4fea59411c5f8 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sun, 13 Oct 2019 22:55:52 +0200 Subject: [PATCH 08/10] Remove the unused md5Hex method --- .../org/airsonic/player/util/StringUtil.java | 20 ------------------- .../player/util/StringUtilTestCase.java | 6 ------ 2 files changed, 26 deletions(-) diff --git a/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java b/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java index 0080b8cd..9114d484 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java +++ b/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java @@ -29,7 +29,6 @@ import java.net.URL; import java.net.URLDecoder; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; -import java.security.MessageDigest; import java.text.*; import java.util.*; import java.util.regex.Matcher; @@ -331,25 +330,6 @@ public final class StringUtil { return new String(Hex.decodeHex(s.toCharArray()), StandardCharsets.UTF_8); } - /** - * Calculates the MD5 digest and returns the value as a 32 character hex string. - * - * @param s Data to digest. - * @return MD5 digest as a hex string. - */ - public static String md5Hex(String s) { - if (s == null) { - return null; - } - - try { - MessageDigest md5 = MessageDigest.getInstance("MD5"); - return new String(Hex.encodeHex(md5.digest(s.getBytes(StandardCharsets.UTF_8)))); - } catch (Exception x) { - throw new RuntimeException(x.getMessage(), x); - } - } - /** * Returns the file part of an URL. For instance: *

diff --git a/airsonic-main/src/test/java/org/airsonic/player/util/StringUtilTestCase.java b/airsonic-main/src/test/java/org/airsonic/player/util/StringUtilTestCase.java index b187acb8..d161a108 100644 --- a/airsonic-main/src/test/java/org/airsonic/player/util/StringUtilTestCase.java +++ b/airsonic-main/src/test/java/org/airsonic/player/util/StringUtilTestCase.java @@ -147,12 +147,6 @@ public class StringUtilTestCase extends TestCase { assertEquals("Error in utf8hex.", s, StringUtil.utf8HexDecode(StringUtil.utf8HexEncode(s))); } - public void testMd5Hex() { - assertNull("Error in md5Hex().", StringUtil.md5Hex(null)); - assertEquals("Error in md5Hex().", "d41d8cd98f00b204e9800998ecf8427e", StringUtil.md5Hex("")); - assertEquals("Error in md5Hex().", "308ed0af23d48f6d2fd4717e77a23e0c", StringUtil.md5Hex("sindre@activeobjects.no")); - } - public void testGetUrlFile() { assertEquals("Error in getUrlFile().", "foo.mp3", StringUtil.getUrlFile("http://www.asdf.com/foo.mp3")); assertEquals("Error in getUrlFile().", "foo.mp3", StringUtil.getUrlFile("http://www.asdf.com/bar/foo.mp3")); From 1bc5729cfb520b4f874bc766b1f6568e601bc9da Mon Sep 17 00:00:00 2001 From: jvoisin Date: Thu, 17 Oct 2019 23:24:53 +0200 Subject: [PATCH 09/10] Remove the parseLocale function in SettingsService --- .../player/service/SettingsService.java | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/airsonic-main/src/main/java/org/airsonic/player/service/SettingsService.java b/airsonic-main/src/main/java/org/airsonic/player/service/SettingsService.java index 5ee92d33..4f59ae06 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/service/SettingsService.java +++ b/airsonic-main/src/main/java/org/airsonic/player/service/SettingsService.java @@ -901,7 +901,7 @@ public class SettingsService { String[] lines = StringUtil.readLines(in); for (String line : lines) { - locales.add(parseLocale(line)); + locales.add(StringUtil.parseLocale(line)); } } catch (IOException x) { @@ -912,21 +912,6 @@ public class SettingsService { return locales.toArray(new Locale[locales.size()]); } - private Locale parseLocale(String line) { - String[] s = line.split("_"); - String language = s[0]; - String country = ""; - String variant = ""; - - if (s.length > 1) { - country = s[1]; - } - if (s.length > 2) { - variant = s[2]; - } - return new Locale(language, country, variant); - } - /** * Returns the "brand" name. Normally, this is just "Airsonic". * From fd1b154a3c9ae6b1378aed29c9e5f07b3505afe0 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Thu, 24 Oct 2019 21:42:25 +0200 Subject: [PATCH 10/10] Simplify a bit a function Credit goes to eharris@ --- .../java/org/airsonic/player/util/StringUtil.java | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java b/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java index 9114d484..df7db5ff 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java +++ b/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java @@ -266,17 +266,11 @@ public final class StringUtil { return null; } - String[] elements = s.split("_"); - switch (elements.length) { - case 0: - return new Locale(s, "", ""); - case 1: - return new Locale(elements[0], "", ""); - case 2: - return new Locale(elements[0], elements[1], ""); - default: - return new Locale(elements[0], elements[1], elements[2]); + List elements = new ArrayList<>(Arrays.asList(s.split("_", 3))); + while (elements.size() < 3) { + elements.add(""); } + return new Locale(elements.get(0), elements.get(1), elements.get(2)); } /**