1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5. public class UrlDecoder {
  6. /**
  7. * Decodes a list of URLs from a string, supporting older versions of Java.
  8. *
  9. * @param urlListString A string containing a list of URLs, separated by commas or semicolons.
  10. * @return A list of decoded URLs. Returns an empty list if the input is null or empty.
  11. */
  12. public static List<String> decodeUrls(String urlListString) {
  13. List<String> urls = new ArrayList<>();
  14. if (urlListString == null || urlListString.isEmpty()) {
  15. return urls;
  16. }
  17. // Split the string by commas or semicolons
  18. String[] urlStrings = urlListString.split("[,;]");
  19. for (String urlString : urlStrings) {
  20. // Trim whitespace
  21. String trimmedUrl = urlString.trim();
  22. if (!trimmedUrl.isEmpty()) {
  23. // Basic URL validation (can be expanded)
  24. if (trimmedUrl.startsWith("http://") || trimmedUrl.startsWith("https://")) {
  25. urls.add(trimmedUrl);
  26. } else {
  27. //Handle relative URLs or other formats as needed. Add logic for more complex scenarios.
  28. urls.add(trimmedUrl);
  29. }
  30. }
  31. }
  32. return urls;
  33. }
  34. public static void main(String[] args) {
  35. //Example Usage
  36. String urlList = "http://example.com,https://google.com;ftp://oldserver.com";
  37. List<String> decodedUrls = decodeUrls(urlList);
  38. for (String url : decodedUrls) {
  39. System.out.println(url);
  40. }
  41. String emptyList = "";
  42. List<String> emptyResult = decodeUrls(emptyList);
  43. System.out.println("Empty List Result: " + emptyResult);
  44. String nullList = null;
  45. List<String> nullResult = decodeUrls(nullList);
  46. System.out.println("Null List Result: " + nullResult);
  47. }
  48. }

Add your comment