import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
public class HTMLResourceSync {
private static final Logger logger = Logger.getLogger(HTMLResourceSync.class.getName());
public static void main(String[] args) throws IOException {
// Define the URLs to sync
String url1 = "http://example.com/page1.html"; // Replace with your first URL
String url2 = "http://example.com/page2.html"; // Replace with your second URL
// Perform synchronization
syncResources(url1, url2);
}
public static void syncResources(String url1, String url2) throws IOException {
logger.info("Starting resource synchronization...");
// Get resources from the first URL
Map<String, String> resources1 = getResources(url1);
logger.info("Resources retrieved from " + url1 + ": " + resources1.size());
// Get resources from the second URL
Map<String, String> resources2 = getResources(url2);
logger.info("Resources retrieved from " + url2 + ": " + resources2.size());
// Compare and update resources in the second URL based on the first
updateResources(url2, resources1, resources2);
logger.info("Synchronization completed.");
}
private static Map<String, String> getResources(String url) throws IOException {
logger.info("Fetching resources from: " + url);
URL urlObj = new URL(url);
URLConnection connection = urlObj.openConnection();
connection.setConnectTimeout(5000); // Set a timeout for connection
connection.setReadTimeout(10000); // Set a timeout for reading
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
StringBuilder html = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
html.append(line).append("\n");
}
} catch (IOException e) {
logger.error("Error fetching resources from " + url + ": " + e.getMessage());
return new HashMap<>(); // Return empty map on error
}
Map<String, String> resources = new HashMap<>();
// Simple resource extraction (can be improved with regex)
for (String line : html.toString().split("\\s+")) {
if (line.startsWith("href=")) {
String resource = line.substring(5).trim();
resources.put(resource, "present"); // Indicate resource is present
} else if (line.startsWith("src=")) {
String resource = line.substring(5).trim();
resources.put(resource, "present");
}
}
return resources;
}
private static void updateResources(String url2, Map<String, String> resources1, Map<String, String> resources2) throws IOException {
logger.info("Updating resources in: " + url2);
// Iterate through resources from the first URL and update the second URL
for (Map.Entry<String, String> entry : resources1.entrySet()) {
String resource = entry.getKey();
if (!resources2.containsKey(resource)) {
// Resource is missing in the second URL, so add it
logger.info("Adding resource: " + resource + " to " + url2);
// In a real application, you would update the HTML content of url2 here
// For this example, we just log the addition
} else {
// Resource exists in the second URL, check if content are different
// In a real application, you would compare the content of the resource
// and update it if necessary
logger.info("Resource " + resource + " already exists in " + url2);
}
}
}
}
Add your comment