Class HTMLUtils
java.lang.Object
net.thevpc.more.shef.HTMLUtils
A collection of static convenience methods for working with HTML,
HTMLDocuments, AttributeSets and Elements from HTML documents.
- Author:
- Bob Tantlinger
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringcolorToHex(Color color) Converts a Color to a hex string in the format "#RRGGBB"static StringIncloses a chunk of HTML text in the specified tagstatic StringcreateTag(HTML.Tag enclTag, AttributeSet set, String innerHTML) Incloses a chunk of HTML text in the specified tag with the specified attribsstatic AttributeSetgetCharacterAttributes(JEditorPane editor) Gets the character attributes at theJEditorPane's caret positionstatic StringgetElementHTML(Element el, boolean includeEnclosingTags) Gets the html of the specifiedElementstatic StringgetFontFamily(JEditorPane editor) Gets the font family name at theJEditorPane's current caret positionstatic ElementgetListParent(Element elem) static ElementgetNextElement(HTMLDocument doc, Element el) Gets the element one position greater than the end of the specified elementstatic ListgetParagraphElements(JEditorPane editor) static ElementSearches upward for the specified parent for the element.static ElementgetPreviousElement(HTMLDocument doc, Element el) Gets the element one position less than the start of the specified elementstatic HTML.TaggetStartTag(String text) static ColorhexToColor(String value) Convert a "#FFFFFF" hex string to a Color.static voidinsertArbitraryHTML(String rawHtml, JEditorPane editor) Inserts an arbitrary chunk of HTML into the JEditorPane at the current caret position.static voidinsertHTML(String html, HTML.Tag tag, JEditorPane editor) Inserts a string of html into theJEditorPane'sHTMLDocumentat the current caret position.static booleanTests if the element is emptystatic booleanTests if an element is an implied paragraph (p-implied)static StringjEditorPaneizeHTML(String html) Removes self-closing tags from xhtml for the benefit ofJEditorPanestatic voidprintAttribs(AttributeSet attr) Helper method that prints out the contents of anAttributeSetto System.err for debuggingstatic voidremoveCharacterAttribute(JEditorPane editor, Object atr) Removes a single character attribute from the editor's current position/selection.static voidremoveCharacterAttribute(JEditorPane editor, CSS.Attribute atr, String val) Removes a CSS character attribute that has the specified value from theJEditorPane's current caret position or selection.static voidremoveElement(Element el) Removes an element from the document that contains itstatic StringremoveEnclosingTags(Element elem, String txt) Removes the enclosing tags from a chunk of HTML textstatic StringremoveEnclosingTags(HTML.Tag t, String txt) Removes the enclosing tags from a chunk of HTML textstatic voidsetCharacterAttributes(JEditorPane editor, AttributeSet attrs) Sets the character attributes for selection of the specified editorstatic voidsetCharacterAttributes(JEditorPane editor, AttributeSet attr, boolean replace) Sets the character attributes for selection of the specified editorstatic voidsetFontFamily(JEditorPane editor, String fontName) Set's the font family at theJEditorPane's current caret positon, or for the current selection (if there is one).static ColorstringToColor(String str) Convert a color string such as "RED" or "#NNNNNN" or "rgb(r, g, b)" to a Color.static MaptagAttribsToMap(String atts) Converts an html tag attribute list to aMap.
-
Constructor Details
-
HTMLUtils
public HTMLUtils()
-
-
Method Details
-
isImplied
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
-
createTag
Incloses a chunk of HTML text in the specified tag with the specified attribs- Parameters:
enclTag- enclTagset- setinnerHTML- innerHTML- Returns:
- tag
-
getParagraphElements
-
getParent
-
isElementEmpty
Tests if the element is empty- Parameters:
el- el- Returns:
- true if empty
-
getListParent
-
getPreviousElement
Gets the element one position less than the start of the specified element- Parameters:
doc- docel- el- Returns:
- previous elem
-
getNextElement
Gets the element one position greater than the end of the specified element- Parameters:
doc- docel- el- Returns:
- next elem
-
removeEnclosingTags
-
removeEnclosingTags
-
getElementHTML
-
removeElement
Removes an element from the document that contains it- Parameters:
el- el- Throws:
BadLocationException- when Bad Location
-
getStartTag
-
insertArbitraryHTML
Inserts an arbitrary chunk of HTML into the JEditorPane at the current caret position.- Parameters:
rawHtml- rawHtmleditor- editor
-
insertHTML
Inserts a string of html into theJEditorPane'sHTMLDocumentat the current caret position.- Parameters:
html- htmltag- tageditor- editor
-
getCharacterAttributes
Gets the character attributes at theJEditorPane's caret positionIf 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
AttributeSetor null, if the editor doesn't have aStyledDocument
-
getFontFamily
Gets the font family name at theJEditorPane's current caret position- Parameters:
editor- editor- Returns:
- The font family name, or null if no font is set
-
setFontFamily
Set's the font family at theJEditorPane'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- editorfontName- font
-
removeCharacterAttribute
Removes a CSS character attribute that has the specified value from theJEditorPane's current caret position or selection.The val parameter is a
Stringeven 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- editoratr- atrval- val
-
removeCharacterAttribute
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- editoratr- atr
-
setCharacterAttributes
Sets the character attributes for selection of the specified editor- Parameters:
editor- editorattr- attrreplace- if true, replaces the attrubutes
-
setCharacterAttributes
Sets the character attributes for selection of the specified editor- Parameters:
editor- editorattrs- attrs
-
tagAttribsToMap
-
colorToHex
-
hexToColor
-
stringToColor
-
jEditorPaneizeHTML
Removes self-closing tags from xhtml for the benefit ofJEditorPaneJEditorpane 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
Helper method that prints out the contents of anAttributeSetto System.err for debugging- Parameters:
attr- attr
-