1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.net.URL;
  5. import java.net.URLConnection;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import java.util.logging.Logger;
  9. public class HTMLResourceSync {
  10. private static final Logger logger = Logger.getLogger(HTMLResourceSync.class.getName());
  11. public static void main(String[] args) throws IOException {
  12. // Define the URLs to sync
  13. String url1 = "http://example.com/page1.html"; // Replace with your first URL
  14. String url2 = "http://example.com/page2.html"; // Replace with your second URL
  15. // Perform synchronization
  16. syncResources(url1, url2);
  17. }
  18. public static void syncResources(String url1, String url2) throws IOException {
  19. logger.info("Starting resource synchronization...");
  20. // Get resources from the first URL
  21. Map<String, String> resources1 = getResources(url1);
  22. logger.info("Resources retrieved from " + url1 + ": " + resources1.size());
  23. // Get resources from the second URL
  24. Map<String, String> resources2 = getResources(url2);
  25. logger.info("Resources retrieved from " + url2 + ": " + resources2.size());
  26. // Compare and update resources in the second URL based on the first
  27. updateResources(url2, resources1, resources2);
  28. logger.info("Synchronization completed.");
  29. }
  30. private static Map<String, String> getResources(String url) throws IOException {
  31. logger.info("Fetching resources from: " + url);
  32. URL urlObj = new URL(url);
  33. URLConnection connection = urlObj.openConnection();
  34. connection.setConnectTimeout(5000); // Set a timeout for connection
  35. connection.setReadTimeout(10000); // Set a timeout for reading
  36. try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
  37. StringBuilder html = new StringBuilder();
  38. String line;
  39. while ((line = reader.readLine()) != null) {
  40. html.append(line).append("\n");
  41. }
  42. } catch (IOException e) {
  43. logger.error("Error fetching resources from " + url + ": " + e.getMessage());
  44. return new HashMap<>(); // Return empty map on error
  45. }
  46. Map<String, String> resources = new HashMap<>();
  47. // Simple resource extraction (can be improved with regex)
  48. for (String line : html.toString().split("\\s+")) {
  49. if (line.startsWith("href=")) {
  50. String resource = line.substring(5).trim();
  51. resources.put(resource, "present"); // Indicate resource is present
  52. } else if (line.startsWith("src=")) {
  53. String resource = line.substring(5).trim();
  54. resources.put(resource, "present");
  55. }
  56. }
  57. return resources;
  58. }
  59. private static void updateResources(String url2, Map<String, String> resources1, Map<String, String> resources2) throws IOException {
  60. logger.info("Updating resources in: " + url2);
  61. // Iterate through resources from the first URL and update the second URL
  62. for (Map.Entry<String, String> entry : resources1.entrySet()) {
  63. String resource = entry.getKey();
  64. if (!resources2.containsKey(resource)) {
  65. // Resource is missing in the second URL, so add it
  66. logger.info("Adding resource: " + resource + " to " + url2);
  67. // In a real application, you would update the HTML content of url2 here
  68. // For this example, we just log the addition
  69. } else {
  70. // Resource exists in the second URL, check if content are different
  71. // In a real application, you would compare the content of the resource
  72. // and update it if necessary
  73. logger.info("Resource " + resource + " already exists in " + url2);
  74. }
  75. }
  76. }
  77. }

Add your comment