net.atlanticbb.tantlinger.ui.text
Class HTMLUtils

java.lang.Object
  extended by net.atlanticbb.tantlinger.ui.text.HTMLUtils

public class HTMLUtils
extends java.lang.Object

A collection of static convenience methods for working with HTML, HTMLDocuments, AttributeSets and Elements from HTML documents.

Author:
Bob Tantlinger

Constructor Summary
HTMLUtils()
           
 
Method Summary
static java.lang.String colorToHex(java.awt.Color color)
          Converts a Color to a hex string in the format "#RRGGBB"
static java.lang.String createTag(javax.swing.text.html.HTML.Tag enclTag, javax.swing.text.AttributeSet set, java.lang.String innerHTML)
          Incloses a chunk of HTML text in the specified tag with the specified attribs
static java.lang.String createTag(javax.swing.text.html.HTML.Tag enclTag, java.lang.String innerHTML)
          Incloses a chunk of HTML text in the specified tag
static javax.swing.text.AttributeSet getCharacterAttributes(javax.swing.JEditorPane editor)
          Gets the character attributes at the JEditorPane's caret position
static java.lang.String getElementHTML(javax.swing.text.Element el, boolean includeEnclosingTags)
          Gets the html of the specified Element
static java.lang.String getFontFamily(javax.swing.JEditorPane editor)
          Gets the font family name at the JEditorPane's current caret position
static javax.swing.text.Element getListParent(javax.swing.text.Element elem)
          Searches for a list Element that is the parent of the specified Element.
static javax.swing.text.Element getNextElement(javax.swing.text.html.HTMLDocument doc, javax.swing.text.Element el)
          Gets the element one position greater than the end of the specified element
static java.util.List getParagraphElements(javax.swing.JEditorPane editor)
           
static javax.swing.text.Element getParent(javax.swing.text.Element curElem, javax.swing.text.html.HTML.Tag parentTag)
          Searches upward for the specified parent for the element.
static javax.swing.text.Element getPreviousElement(javax.swing.text.html.HTMLDocument doc, javax.swing.text.Element el)
          Gets the element one position less than the start of the specified element
static javax.swing.text.html.HTML.Tag getStartTag(java.lang.String text)
           
static java.awt.Color hexToColor(java.lang.String value)
          Convert a "#FFFFFF" hex string to a Color.
static void insertArbitraryHTML(java.lang.String rawHtml, javax.swing.JEditorPane editor)
          Inserts an arbitrary chunk of HTML into the JEditorPane at the current caret position.
static void insertHTML(java.lang.String html, javax.swing.text.html.HTML.Tag tag, javax.swing.JEditorPane editor)
          Inserts a string of html into the JEditorPane's HTMLDocument at the current caret position.
static boolean isElementEmpty(javax.swing.text.Element el)
          Tests if the element is empty
static boolean isImplied(javax.swing.text.Element el)
          Tests if an element is an implied paragraph (p-implied)
static java.lang.String jEditorPaneizeHTML(java.lang.String html)
          Removes self-closing tags from xhtml for the benifit of JEditorPane
static void printAttribs(javax.swing.text.AttributeSet attr)
          Helper method that prints out the contents of an AttributeSet to System.err for debugging
static void removeCharacterAttribute(javax.swing.JEditorPane editor, javax.swing.text.html.CSS.Attribute atr, java.lang.String val)
          Removes a CSS character attribute that has the specified value from the JEditorPane's current caret position or selection.
static void removeCharacterAttribute(javax.swing.JEditorPane editor, java.lang.Object atr)
          Removes a single character attribute from the editor's current position/selection.
static void removeElement(javax.swing.text.Element el)
          Removes an element from the document that contains it
static java.lang.String removeEnclosingTags(javax.swing.text.Element elem, java.lang.String txt)
          Removes the enclosing tags from a chunk of HTML text
static java.lang.String removeEnclosingTags(javax.swing.text.html.HTML.Tag t, java.lang.String txt)
          Removes the enclosing tags from a chunk of HTML text
static void setCharacterAttributes(javax.swing.JEditorPane editor, javax.swing.text.AttributeSet attrs)
          Sets the character attributes for selection of the specified editor
static void setCharacterAttributes(javax.swing.JEditorPane editor, javax.swing.text.AttributeSet attr, boolean replace)
          Sets the character attributes for selection of the specified editor
static void setFontFamily(javax.swing.JEditorPane editor, java.lang.String fontName)
          Set's the font family at the JEditorPane's current caret positon, or for the current selection (if there is one).
static java.awt.Color stringToColor(java.lang.String str)
          Convert a color string such as "RED" or "#NNNNNN" or "rgb(r, g, b)" to a Color.
static java.util.Map tagAttribsToMap(java.lang.String atts)
          Converts an html tag attribute list to a Map.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HTMLUtils

public HTMLUtils()
Method Detail

isImplied

public static boolean isImplied(javax.swing.text.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 java.lang.String createTag(javax.swing.text.html.HTML.Tag enclTag,
                                         java.lang.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:

createTag

public static java.lang.String createTag(javax.swing.text.html.HTML.Tag enclTag,
                                         javax.swing.text.AttributeSet set,
                                         java.lang.String innerHTML)
Incloses a chunk of HTML text in the specified tag with the specified attribs

Parameters:
enclTag -
set -
innerHTML -
Returns:

getParagraphElements

public static java.util.List getParagraphElements(javax.swing.JEditorPane editor)

getParent

public static javax.swing.text.Element getParent(javax.swing.text.Element curElem,
                                                 javax.swing.text.html.HTML.Tag parentTag)
Searches upward for the specified parent for the element.

Parameters:
curElem -
parentTag -
Returns:
The parent element, or null if the parent wasnt found

isElementEmpty

public static boolean isElementEmpty(javax.swing.text.Element el)
Tests if the element is empty

Parameters:
el -
Returns:

getListParent

public static javax.swing.text.Element getListParent(javax.swing.text.Element elem)
Searches for a list Element that is the parent of the specified Element.

Parameters:
elem -
Returns:
A list element (UL, OL, DIR, MENU, or DL) if found, null otherwise

getPreviousElement

public static javax.swing.text.Element getPreviousElement(javax.swing.text.html.HTMLDocument doc,
                                                          javax.swing.text.Element el)
Gets the element one position less than the start of the specified element

Parameters:
doc -
el -
Returns:

getNextElement

public static javax.swing.text.Element getNextElement(javax.swing.text.html.HTMLDocument doc,
                                                      javax.swing.text.Element el)
Gets the element one position greater than the end of the specified element

Parameters:
doc -
el -
Returns:

removeEnclosingTags

public static java.lang.String removeEnclosingTags(javax.swing.text.Element elem,
                                                   java.lang.String txt)
Removes the enclosing tags from a chunk of HTML text

Parameters:
elem -
txt -
Returns:

removeEnclosingTags

public static java.lang.String removeEnclosingTags(javax.swing.text.html.HTML.Tag t,
                                                   java.lang.String txt)
Removes the enclosing tags from a chunk of HTML text

Parameters:
t -
txt -
Returns:

getElementHTML

public static java.lang.String getElementHTML(javax.swing.text.Element el,
                                              boolean includeEnclosingTags)
Gets the html of the specified Element

Parameters:
el -
includeEnclosingTags - true, if the enclosing tags should be included
Returns:

removeElement

public static void removeElement(javax.swing.text.Element el)
                          throws javax.swing.text.BadLocationException
Removes an element from the document that contains it

Parameters:
el -
Throws:
javax.swing.text.BadLocationException

getStartTag

public static javax.swing.text.html.HTML.Tag getStartTag(java.lang.String text)

insertArbitraryHTML

public static void insertArbitraryHTML(java.lang.String rawHtml,
                                       javax.swing.JEditorPane editor)
Inserts an arbitrary chunk of HTML into the JEditorPane at the current caret position.

Parameters:
rawHtml -
editor -

insertHTML

public static void insertHTML(java.lang.String html,
                              javax.swing.text.html.HTML.Tag tag,
                              javax.swing.JEditorPane editor)
Inserts a string of html into the JEditorPane's HTMLDocument at the current caret position.

Parameters:
html -
tag -
editor -

getCharacterAttributes

public static javax.swing.text.AttributeSet getCharacterAttributes(javax.swing.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 slection, the attributes at selectionEnd - 1 are returned

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

getFontFamily

public static java.lang.String getFontFamily(javax.swing.JEditorPane editor)
Gets the font family name at the JEditorPane's current caret position

Parameters:
editor -
Returns:
The font family name, or null if no font is set

setFontFamily

public static void setFontFamily(javax.swing.JEditorPane editor,
                                 java.lang.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 -
fontName -

removeCharacterAttribute

public static void removeCharacterAttribute(javax.swing.JEditorPane editor,
                                            javax.swing.text.html.CSS.Attribute atr,
                                            java.lang.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 -
atr -
val -

removeCharacterAttribute

public static void removeCharacterAttribute(javax.swing.JEditorPane editor,
                                            java.lang.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 -
atr -

setCharacterAttributes

public static void setCharacterAttributes(javax.swing.JEditorPane editor,
                                          javax.swing.text.AttributeSet attr,
                                          boolean replace)
Sets the character attributes for selection of the specified editor

Parameters:
editor -
attrs -
replace - if true, replaces the attrubutes

setCharacterAttributes

public static void setCharacterAttributes(javax.swing.JEditorPane editor,
                                          javax.swing.text.AttributeSet attrs)
Sets the character attributes for selection of the specified editor

Parameters:
editor -
attrs -

tagAttribsToMap

public static java.util.Map tagAttribsToMap(java.lang.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 > http://blah.com
target > _self

Parameters:
atts -
Returns:

colorToHex

public static java.lang.String colorToHex(java.awt.Color color)
Converts a Color to a hex string in the format "#RRGGBB"


hexToColor

public static java.awt.Color hexToColor(java.lang.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.


stringToColor

public static java.awt.Color stringToColor(java.lang.String str)
Convert a color string such as "RED" or "#NNNNNN" or "rgb(r, g, b)" to a Color.


jEditorPaneizeHTML

public static java.lang.String jEditorPaneizeHTML(java.lang.String html)
Removes self-closing tags from xhtml for the benifit 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 -
Returns:
JEditorpane friendly html

printAttribs

public static void printAttribs(javax.swing.text.AttributeSet attr)
Helper method that prints out the contents of an AttributeSet to System.err for debugging

Parameters:
attr -