1. import java.io.IOException;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. public class FormDebugger {
  5. private static final String DEBUG_MODE_KEY = "debug";
  6. private static final String SANITY_CHECKS_KEY = "sanity_checks";
  7. private static final Map<String, Object> formDebugConfig = new HashMap<>();
  8. public static boolean isDebuggingEnabled() {
  9. // Check if debug mode is enabled in the configuration.
  10. return formDebugConfig.containsKey(DEBUG_MODE_KEY) && (Boolean) formDebugConfig.get(DEBUG_MODE_KEY);
  11. }
  12. public static boolean areSanityChecksEnabled() {
  13. // Check if sanity checks are enabled.
  14. return formDebugConfig.containsKey(SANITY_CHECKS_KEY) && (Boolean) formDebugConfig.get(SANITY_CHECKS_KEY);
  15. }
  16. public static void configureFormDebug(Map<String, Object> config) {
  17. // Allow configuration of debug settings.
  18. formDebugConfig.putAll(config);
  19. }
  20. public static boolean validateForm(Map<String, String> formData) {
  21. // Perform sanity checks if enabled.
  22. if (!areSanityChecksEnabled()) {
  23. return true; // Skip validation if not enabled.
  24. }
  25. // Example sanity checks - customize as needed.
  26. if (formData.get("name") == null || formData.get("name").trim().isEmpty()) {
  27. System.err.println("Error: Name is required.");
  28. return false;
  29. }
  30. if (formData.get("email") == null || !formData.get("email").matches(".*@.*")) {
  31. System.err.println("Error: Invalid email format.");
  32. return false;
  33. }
  34. if (formData.get("age") == null) {
  35. System.err.println("Error: Age is required.");
  36. return false;
  37. }
  38. try {
  39. int age = Integer.parseInt(formData.get("age"));
  40. if (age < 0 || age > 150) {
  41. System.err.println("Error: Age must be between 0 and 150.");
  42. return false;
  43. }
  44. } catch (NumberFormatException e) {
  45. System.err.println("Error: Age must be a number.");
  46. return false;
  47. }
  48. return true; // All checks passed.
  49. }
  50. public static void main(String[] args) {
  51. //Example Usage
  52. Map<String,Object> debugConfig = new HashMap<>();
  53. debugConfig.put(DEBUG_MODE_KEY, true);
  54. debugConfig.put(SANITY_CHECKS_KEY, true);
  55. configureFormDebug(debugConfig);
  56. Map<String, String> sampleForm = new HashMap<>();
  57. sampleForm.put("name", "John Doe");
  58. sampleForm.put("email", "john.doe@example.com");
  59. sampleForm.put("age", "30");
  60. if (validateForm(sampleForm)) {
  61. System.out.println("Form is valid.");
  62. } else {
  63. System.out.println("Form is invalid.");
  64. }
  65. Map<String, String> invalidForm = new HashMap<>();
  66. invalidForm.put("name", "");
  67. invalidForm.put("email", "invalid");
  68. invalidForm.put("age", "abc");
  69. if (validateForm(invalidForm)) {
  70. System.out.println("Form is valid.");
  71. } else {
  72. System.out.println("Form is invalid.");
  73. }
  74. }
  75. }

Add your comment