Class HTMLUtils

java.lang.Object
net.thevpc.more.shef.HTMLUtils

public class HTMLUtils extends Object
A collection of static convenience methods for working with HTML, HTMLDocuments, AttributeSets and Elements from HTML documents.
Author:
Bob Tantlinger
  • Constructor Details

    • HTMLUtils

      public HTMLUtils()
  • Method Details

    • isImplied

      public static boolean isImplied(Element el)
      Tests if an element is an implied paragraph (p-implied)
      Parameters:
      el - The element
      Returns:
      true if the elements name equals "p-implied", false otherwise
    • createTag

      public static String createTag(HTML.Tag enclTag, String innerHTML)
      Incloses a chunk of HTML text in the specified tag
      Parameters:
      enclTag - the tag to enclose the HTML in
      innerHTML - the HTML to be inclosed
      Returns:
      tag
    • createTag

      public static String createTag(HTML.Tag enclTag, AttributeSet set, String innerHTML)
      Incloses a chunk of HTML text in the specified tag with the specified attribs
      Parameters:
      enclTag - enclTag
      set - set
      innerHTML - innerHTML
      Returns:
      tag
    • getParagraphElements

      public static List getParagraphElements(JEditorPane editor)
    • getParent

      public static Element getParent(Element curElem, HTML.Tag parentTag)
      Searches upward for the specified parent for the element.
      Parameters:
      curElem - curElem
      parentTag - parentTag
      Returns:
      The parent element, or null if the parent wasnt found
    • isElementEmpty

      public static boolean isElementEmpty(Element el)
      Tests if the element is empty
      Parameters:
      el - el
      Returns:
      true if empty
    • getListParent

      public static Element getListParent(Element elem)
      Searches for a list Element that is the parent of the specified Element.
      Parameters:
      elem - elem
      Returns:
      A list element (UL, OL, DIR, MENU, or DL) if found, null otherwise
    • getPreviousElement

      public static Element getPreviousElement(HTMLDocument doc, Element el)
      Gets the element one position less than the start of the specified element
      Parameters:
      doc - doc
      el - el
      Returns:
      previous elem
    • getNextElement

      public static Element getNextElement(HTMLDocument doc, Element el)
      Gets the element one position greater than the end of the specified element
      Parameters:
      doc - doc
      el - el
      Returns:
      next elem
    • removeEnclosingTags

      public static String removeEnclosingTags(Element elem, String txt)
      Removes the enclosing tags from a chunk of HTML text
      Parameters:
      elem - elem
      txt - txt
      Returns:
      html
    • removeEnclosingTags

      public static String removeEnclosingTags(HTML.Tag t, String txt)
      Removes the enclosing tags from a chunk of HTML text
      Parameters:
      t - t
      txt - txt
      Returns:
      html
    • getElementHTML

      public static String getElementHTML(Element el, boolean includeEnclosingTags)
      Gets the html of the specified Element
      Parameters:
      el - el
      includeEnclosingTags - true, if the enclosing tags should be included
      Returns:
      html
    • removeElement

      public static void removeElement(Element el) throws BadLocationException
      Removes an element from the document that contains it
      Parameters:
      el - el
      Throws:
      BadLocationException - when Bad Location
    • getStartTag

      public static HTML.Tag getStartTag(String text)
    • insertArbitraryHTML

      public static void insertArbitraryHTML(String rawHtml, JEditorPane editor)
      Inserts an arbitrary chunk of HTML into the JEditorPane at the current caret position.
      Parameters:
      rawHtml - rawHtml
      editor - editor
    • insertHTML

      public static void insertHTML(String html, HTML.Tag tag, JEditorPane editor)
      Inserts a string of html into the JEditorPane's HTMLDocument at the current caret position.
      Parameters:
      html - html
      tag - tag
      editor - editor
    • getCharacterAttributes

      public static AttributeSet getCharacterAttributes(JEditorPane editor)
      Gets the character attributes at the JEditorPane's caret position

      If there is no selection, the character attributes at caretPos - 1 are retuned. If there is a selection, the attributes at selectionEnd - 1 are returned

      Parameters:
      editor - editor
      Returns:
      An AttributeSet or null, if the editor doesn't have a StyledDocument
    • getFontFamily

      public static String getFontFamily(JEditorPane editor)
      Gets the font family name at the JEditorPane's current caret position
      Parameters:
      editor - editor
      Returns:
      The font family name, or null if no font is set
    • setFontFamily

      public static void setFontFamily(JEditorPane editor, String fontName)
      Set's the font family at the JEditorPane's current caret positon, or for the current selection (if there is one).

      If the fontName parameter is null, any currently set font family is removed.

      Parameters:
      editor - editor
      fontName - font
    • removeCharacterAttribute

      public static void removeCharacterAttribute(JEditorPane editor, CSS.Attribute atr, String val)
      Removes a CSS character attribute that has the specified value from the JEditorPane's current caret position or selection.

      The val parameter is a String even though the actual attribute value is not. This is because the actual attribute values are not public. Thus, this method checks the value via the toString() method

      Parameters:
      editor - editor
      atr - atr
      val - val
    • removeCharacterAttribute

      public static void removeCharacterAttribute(JEditorPane editor, Object atr)
      Removes a single character attribute from the editor's current position/selection.

      Removes from the editor kit's input attribtues and/or document at the caret position. If there is a selction the attribute is removed from the selected text

      Parameters:
      editor - editor
      atr - atr
    • setCharacterAttributes

      public static void setCharacterAttributes(JEditorPane editor, AttributeSet attr, boolean replace)
      Sets the character attributes for selection of the specified editor
      Parameters:
      editor - editor
      attr - attr
      replace - if true, replaces the attrubutes
    • setCharacterAttributes

      public static void setCharacterAttributes(JEditorPane editor, AttributeSet attrs)
      Sets the character attributes for selection of the specified editor
      Parameters:
      editor - editor
      attrs - attrs
    • tagAttribsToMap

      public static Map tagAttribsToMap(String atts)
      Converts an html tag attribute list to a Map. For example, the String 'href="http://blah.com" target="_self"' becomes name-value pairs:
      href gt http://blah.com target gt _self
      Parameters:
      atts - atts
      Returns:
      map map
    • colorToHex

      public static String colorToHex(Color color)
      Converts a Color to a hex string in the format "#RRGGBB"
      Parameters:
      color - color
      Returns:
      hex
    • hexToColor

      public static Color hexToColor(String value)
      Convert a "#FFFFFF" hex string to a Color. If the color specification is bad, an attempt will be made to fix it up.
      Parameters:
      value - value
      Returns:
      color
    • stringToColor

      public static Color stringToColor(String str)
      Convert a color string such as "RED" or "#NNNNNN" or "rgb(r, g, b)" to a Color.
      Parameters:
      str - str
      Returns:
      color
    • jEditorPaneizeHTML

      public static String jEditorPaneizeHTML(String html)
      Removes self-closing tags from xhtml for the benefit of JEditorPane

      JEditorpane can't handle empty xhtml containers like <br /> or <img />, so this method replaces them without the "/" as in <br>

      Parameters:
      html - html
      Returns:
      JEditorpane friendly html
    • printAttribs

      public static void printAttribs(AttributeSet attr)
      Helper method that prints out the contents of an AttributeSet to System.err for debugging
      Parameters:
      attr - attr