1. import java.util.HashMap;
  2. import java.util.Map;
  3. public class HtmlHelper {
  4. private static final String DEFAULT_VALUE = ""; // Default value if not found
  5. /**
  6. * Safely retrieves an attribute value from an HTML element's attributes.
  7. * Handles cases where the attribute is missing or the element is null.
  8. *
  9. * @param elementAttributes A map of attribute names to values.
  10. * @param attributeName The name of the attribute to retrieve.
  11. * @return The value of the attribute, or DEFAULT_VALUE if not found.
  12. */
  13. public static String getAttributeValue(Map<String, String> elementAttributes, String attributeName) {
  14. if (elementAttributes == null || attributeName == null || attributeName.isEmpty()) {
  15. return DEFAULT_VALUE; // Handle null/empty input
  16. }
  17. return elementAttributes.getOrDefault(attributeName, DEFAULT_VALUE);
  18. }
  19. /**
  20. * Safely sets an attribute on an HTML element.
  21. * Handles cases where the element is null or the attribute name is invalid.
  22. *
  23. * @param elementAttributes A map of attribute names to values.
  24. * @param attributeName The name of the attribute to set.
  25. * @param attributeValue The value of the attribute.
  26. */
  27. public static void setAttribute(Map<String, String> elementAttributes, String attributeName, String attributeValue) {
  28. if (elementAttributes == null || attributeName == null || attributeName.isEmpty()) {
  29. return; // Do nothing if input is invalid
  30. }
  31. elementAttributes.put(attributeName, attributeValue);
  32. }
  33. /**
  34. * Checks if an element contains a specific attribute.
  35. * @param elementAttributes A map of attribute names to values.
  36. * @param attributeName The name of the attribute to check.
  37. * @return True if the attribute exists, false otherwise.
  38. */
  39. public static boolean hasAttribute(Map<String, String> elementAttributes, String attributeName) {
  40. if (elementAttributes == null || attributeName == null || attributeName.isEmpty()) {
  41. return false;
  42. }
  43. return elementAttributes.containsKey(attributeName);
  44. }
  45. /**
  46. * Converts a string to a safe HTML attribute value, escaping special characters.
  47. *
  48. * @param value The string to convert.
  49. * @return The HTML-safe attribute value.
  50. */
  51. public static String escapeHtml(String value) {
  52. if (value == null) {
  53. return "";
  54. }
  55. return value.replace("&", "&amp;")
  56. .replace("<", "&lt;")
  57. .replace(">", "&gt;")
  58. .replace("\"", "&quot;")
  59. .replace("'", "&#39;");
  60. }
  61. /**
  62. * Create a map of attributes.
  63. * @param attributes A string representation of attributes, where key=value pairs are separated by '&'.
  64. * @return A map containing the attributes.
  65. */
  66. public static Map<String, String> parseAttributes(String attributes) {
  67. Map<String, String> attributeMap = new HashMap<>();
  68. if (attributes != null && !attributes.isEmpty()) {
  69. String[] attributeArray = attributes.split("&");
  70. for (String attribute : attributeArray) {
  71. String[] parts = attribute.split("=");
  72. if (parts.length == 2) {
  73. attributeMap.put(parts[0], parts[1]);
  74. }
  75. }
  76. }
  77. return attributeMap;
  78. }
  79. }

Add your comment