1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. public class UrlMapper {
  6. /**
  7. * Maps fields of URL lists for batch processing with manual overrides.
  8. *
  9. * @param urlList The list of URLs to process.
  10. * @param overrideMap A map containing field overrides for specific URLs.
  11. * Key: URL, Value: Map of field names to override values.
  12. * @return A list of maps, where each map represents a processed URL.
  13. */
  14. public static List<Map<String, String>> mapUrls(List<String> urlList, Map<String, Map<String, String>> overrideMap) {
  15. List<Map<String, String>> processedUrls = new ArrayList<>();
  16. for (String url : urlList) {
  17. Map<String, String> urlData = new HashMap<>();
  18. // Basic URL parsing (can be expanded for more complex parsing)
  19. String[] parts = url.split("/");
  20. if (parts.length >= 3) {
  21. urlData.put("protocol", parts[0]);
  22. urlData.put("host", parts[1]);
  23. urlData.put("path", parts[2]);
  24. } else {
  25. // Handle cases with insufficient URL components
  26. urlData.put("protocol", "http"); // Default protocol
  27. urlData.put("host", "unknown"); // Default host
  28. urlData.put("path", ""); // Default path
  29. }
  30. // Apply overrides if available
  31. if (overrideMap != null && overrideMap.containsKey(url)) {
  32. Map<String, String> overrides = overrideMap.get(url);
  33. if (overrides != null) {
  34. for (Map.Entry<String, String> entry : overrides.entrySet()) {
  35. urlData.put(entry.getKey(), entry.getValue());
  36. }
  37. }
  38. }
  39. processedUrls.add(urlData);
  40. }
  41. return processedUrls;
  42. }
  43. public static void main(String[] args) {
  44. //Example Usage
  45. List<String> urls = new ArrayList<>();
  46. urls.add("https://example.com/page1");
  47. urls.add("http://example.com/page2/subpage");
  48. urls.add("https://example.com/page3");
  49. Map<String, Map<String, String>> overrides = new HashMap<>();
  50. Map<String, String> page1Overrides = new HashMap<>();
  51. page1Overrides.put("protocol", "https");
  52. page1Overrides.put("path", "index.html");
  53. overrides.put("https://example.com/page1", page1Overrides);
  54. List<Map<String, String>> processed = mapUrls(urls, overrides);
  55. for (Map<String, String> urlData : processed) {
  56. System.out.println(urlData);
  57. }
  58. }
  59. }

Add your comment