1. import java.io.File;
  2. import java.io.IOException;
  3. import java.nio.file.Files;
  4. import java.nio.file.Path;
  5. import java.nio.file.Paths;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8. public class HtmlIntegrityChecker {
  9. private static final String MIN_HTML_VERSION = "<html>"; // Basic HTML tag for older versions
  10. private static final String MIN_HTML_VERSION_WITH_HEAD = "<html"; // HTML tag with head for slightly later versions
  11. private static final String MIN_HTML_VERSION_WITH_HEAD_AND_BODY = "<html body>"; // HTML tag with head and body for some older versions
  12. public static boolean verifyHtmlIntegrity(String filePath) throws IOException {
  13. // Check if the file exists
  14. Path path = Paths.get(filePath);
  15. if (!Files.exists(path)) {
  16. System.err.println("File not found: " + filePath);
  17. return false;
  18. }
  19. String htmlContent = new String(Files.readAllBytes(path));
  20. //Check for minimal HTML structure
  21. if (!htmlContent.contains(MIN_HTML_VERSION) && !htmlContent.contains(MIN_HTML_VERSION_WITH_HEAD) && !htmlContent.contains(MIN_HTML_VERSION_WITH_HEAD_AND_BODY)) {
  22. System.err.println("HTML file appears to be invalid or too minimal.");
  23. return false;
  24. }
  25. //Basic validation - Check for required tags
  26. if (!htmlContent.contains("<head>") || !htmlContent.contains("<body>")) {
  27. System.err.println("HTML file missing required head or body tags.");
  28. return false;
  29. }
  30. return true;
  31. }
  32. public static void main(String[] args) {
  33. // Example usage:
  34. if (args.length != 1) {
  35. System.out.println("Usage: java HtmlIntegrityChecker <html_file_path>");
  36. return;
  37. }
  38. String filePath = args[0];
  39. try {
  40. boolean isValid = verifyHtmlIntegrity(filePath);
  41. if (isValid) {
  42. System.out.println("HTML file is valid.");
  43. } else {
  44. System.out.println("HTML file is invalid.");
  45. }
  46. } catch (IOException e) {
  47. System.err.println("Error reading file: " + e.getMessage());
  48. }
  49. }
  50. }

Add your comment