import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class UrlDecoder {
/**
* Decodes a list of URLs from a string, supporting older versions of Java.
*
* @param urlListString A string containing a list of URLs, separated by commas or semicolons.
* @return A list of decoded URLs. Returns an empty list if the input is null or empty.
*/
public static List<String> decodeUrls(String urlListString) {
List<String> urls = new ArrayList<>();
if (urlListString == null || urlListString.isEmpty()) {
return urls;
}
// Split the string by commas or semicolons
String[] urlStrings = urlListString.split("[,;]");
for (String urlString : urlStrings) {
// Trim whitespace
String trimmedUrl = urlString.trim();
if (!trimmedUrl.isEmpty()) {
// Basic URL validation (can be expanded)
if (trimmedUrl.startsWith("http://") || trimmedUrl.startsWith("https://")) {
urls.add(trimmedUrl);
} else {
//Handle relative URLs or other formats as needed. Add logic for more complex scenarios.
urls.add(trimmedUrl);
}
}
}
return urls;
}
public static void main(String[] args) {
//Example Usage
String urlList = "http://example.com,https://google.com;ftp://oldserver.com";
List<String> decodedUrls = decodeUrls(urlList);
for (String url : decodedUrls) {
System.out.println(url);
}
String emptyList = "";
List<String> emptyResult = decodeUrls(emptyList);
System.out.println("Empty List Result: " + emptyResult);
String nullList = null;
List<String> nullResult = decodeUrls(nullList);
System.out.println("Null List Result: " + nullResult);
}
}
Add your comment