Package org.ujorm.tools.web
Class Element
java.lang.Object
org.ujorm.tools.web.Element
- All Implemented Interfaces:
Closeable,AutoCloseable,Html,ApiElement<Element>
A HTML Element implements some methods for frequently used elements and attributes
Usage
ServletResponse response = new ServletResponse();
try (HtmlElement html = HtmlElement.of(response)) {
try (Element body = html.getBody()) {
body.addHeading("Hello!");
}
}
assertTrue(response.toString().contains("<h1>Hello!</h1>"));
-
Field Summary
Fields inherited from interface org.ujorm.tools.web.Html
A, A_ACTION, A_ALT, A_CELLPADDING, A_CELLSPACING, A_CLASS, A_COLS, A_COLSPAN, A_CONTENT, A_FOR, A_HREF, A_HTTP_EQUIV, A_CHARSET, A_CHECKED, A_ID, A_LANG, A_LANGUAGE, A_MAXLENGTH, A_MEDIA, A_METHOD, A_NAME, A_ONCLICK, A_PLACEHOLDER, A_READONLY, A_REL, A_ROWS, A_ROWSPAN, A_SELECTED, A_SRC, A_STYLE, A_TARGET, A_TITLE, A_TYPE, A_VALUE, BODY, BR, BUTTON, DIV, FIELDSET, FORM, H1, H2, H3, HEAD, HEADING_PREFIX, HR, HTML, IMAGE, INPUT, LABEL, LEGEND, LI, LINK, META, OL, OPTION, P, PRE, SCRIPT, SELECT, SPAN, STYLE, TABLE, TBODY, TD, TEXT_AREA, TH, THEAD, TITLE, TR, UL, V_BLANK, V_GET, V_HIDDEN, V_CHECKBOX, V_PASSWORD, V_POST, V_RESET, V_STYLESHEET, V_SUBMIT, V_TEXT, V_TEXT_CSS -
Method Summary
Modifier and TypeMethodDescription@NotNull ElementaddAnchor(@NotNull String url, @NotNull CharSequence... cssClasses) Add an anchor element with URL and CSS classes@NotNull ElementaddBody(@NotNull CharSequence... cssClasses) Add new body element@NotNull ElementaddBreak(@NotNull CharSequence... cssClasses) Add a line break@NotNull ElementaddButton(@NotNull CharSequence... cssClasses) Add new button element@NotNull ElementaddCDATA(CharSequence charData) Add a character data inCDATAformat to XML only.@NotNull ElementaddComment(CharSequence comment) Add a comment text.@NotNull ElementaddDiv(@NotNull CharSequence... cssClasses) Add new div element@NotNull ElementaddElement(@NotNull String name) Create new Element@NotNull ElementaddElement(@NotNull String name, @NotNull CharSequence... cssClasses) Add a new Element with optional CSS classes@NotNull ElementaddElementIf(boolean enabled, @NotNull String name, @NotNull CharSequence... cssClasses) Add an element according to a condition.@NotNull ElementaddFieldset(@Nullable String title, @NotNull CharSequence... cssClasses) Add new fieldset element including a title@NotNull ElementaddForm(@NotNull CharSequence... cssClasses) Add new form element@NotNull ElementaddHeading(int level, @NotNull CharSequence title, @NotNull CharSequence... cssClasses) Add new heading with the required level where the first level is the one,@NotNull ElementaddHeading(@NotNull CharSequence title, @NotNull CharSequence... cssClasses) Add a top heading (level one)@NotNull ElementaddHiddenInput(@Nullable CharSequence name, @Nullable Object value) Add a new hidden input element with a name & value@NotNull ElementaddImage(@NotNull InputStream imageStream, @NotNull CharSequence alt, @NotNull CharSequence... cssClasses) Add an embeded image@NotNull ElementaddImage(@NotNull CharSequence imageLink, @NotNull CharSequence alt, @NotNull CharSequence... cssClasses) Add a link to an image@NotNull ElementaddInput(@NotNull CharSequence... cssClasses) Add new input element@NotNull ElementaddLabel(@NotNull CharSequence... cssClasses) Add new label element@NotNull ElementaddLink(@NotNull CharSequence... cssClasses) Add new link element@NotNull ElementaddLinkedText(@NotNull String url, @NotNull Object... text) Add a@NotNull ElementaddListItem(@NotNull CharSequence... cssClasses) @NotNull ElementaddOption(@NotNull CharSequence... cssClasses) Add new option element@NotNull ElementaddOrderedList(@NotNull CharSequence... cssClasses) Add new ordered list element@NotNull ElementaddParagraph(@NotNull CharSequence... cssClasses) Add new paragram element@NotNull ElementaddPasswordInput(@NotNull CharSequence... cssClasses) Add a new password input element@NotNull ElementaddPreformatted(@NotNull CharSequence... cssClasses) Add new pre element@NotNull ElementaddRawText(@NotNull Object... data) @NotNull ElementaddRawText(@Nullable Object data) Add an native text with no escaped characters, for example: XML code, JavaScript, CSS stylesaddRawTexts(@NotNull CharSequence separator, @NotNull Object... data) Add many words separated by a delimeter@NotNull ElementaddScript(@NotNull CharSequence... cssClasses) Add new script element@NotNull ElementaddSelect(@NotNull CharSequence... cssClasses) Add new select element@NotNull ElementaddSelectOptions(@NotNull Object value, @NotNull Map<?, ?> options, @NotNull CharSequence... cssClasses) Add options from map to current select element@NotNull ElementaddSpan(@NotNull CharSequence... cssClasses) Add new span element@NotNull ElementaddStyle(@NotNull CharSequence... cssClasses) Add new style element@NotNull ElementaddSubmitButton(@NotNull CharSequence... cssClasses) Add a submit button@NotNull ElementaddTable(@NotNull CharSequence... cssClasses) Add new Table@NotNull ElementaddTable(@NotNull Object[][] data, @NotNull CharSequence... cssClass) Create a HTML table according to data@NotNull ElementaddTable(@NotNull Collection<Object[]> data, @NotNull CharSequence... cssClass) Create a HTML table according to data<D,V> @NotNull Element addTable(@NotNull Stream<D> domains, @Nullable CharSequence[] cssClass, @Nullable Object[] headers, @NotNull Function<D, V>... attributes) Create a HTML table according to data@NotNull ElementaddTableDetail(@NotNull CharSequence... cssClasses) Add new detail of table element@NotNull ElementaddTableHead(@NotNull CharSequence... cssClasses) Add new head of table element@NotNull ElementaddTableRow(@NotNull CharSequence... cssClasses) Add new table row element@NotNull ElementAdd many texts with no separator@NotNull ElementAdd simple text@NotNull ElementaddTextArea(@NotNull CharSequence... cssClasses) Add new text area element<V> @NotNull ElementaddTextInp(@NotNull HttpParameter param, V value, @NotNull CharSequence title, @NotNull CharSequence... cssClasses) Add new input element type of text including attributes: name, value, placeholder and title@NotNull ElementaddTextInput(@NotNull CharSequence... cssClasses) Add new input element type of textaddTexts(@NotNull CharSequence separator, @NotNull Object... data) Add many words separated by a delimeter@NotNull ElementaddTextTemplated(CharSequence template, Object... values) Add a template based text with parameters with hight performance.@NotNull ElementaddTitle(@NotNull CharSequence... cssClasses) Add new title element@NotNull ElementaddUnorderedlist(@NotNull CharSequence... cssClasses) Add new unordered list elementvoidclose()Close the elementstatic @NotNull ElementcreateHtmlRoot(@NotNull Object title, @Nullable CharSequence... cssLinks) Crate a root elementstatic @NotNull ElementcreateHtmlRoot(@NotNull Object title, @Nullable Charset charset, @Nullable CharSequence... cssLinks) Crate a root element@NotNull StringgetName()Get an element name@NotNull ExceptionProviderAdd nested elements to the element.static @NotNull Elementof(@NotNull ApiElement original) New element for an API element@NotNull ElementSet an action type of from@NotNull ElementA shortcut for the methodsetAttribute(java.lang.String, java.lang.Object).@NotNull ElementsetAttribute(@NotNull String name) Set an attribute with no value@NotNull ElementsetAttribute(@NotNull String name, @Nullable Object value) Set an attribute@NotNull ElementsetAttributes(@NotNull String name, @NotNull CharSequence separator, @NotNull Object... value) Set an attribute@NotNull ElementsetClass(@NotNull CharSequence... cssClasses) Set a CSS class attribute optionally, the empty attribute is ignored.@NotNull ElementColumn count of a text area@NotNull ElementsetColSpan(@org.jetbrains.annotations.Nullable int value) Column span inside the table@NotNull ElementsetFor(@Nullable CharSequence value) Set an value of input element@NotNull ElementsetHint(@Nullable CharSequence value) Set a placeholder name@NotNull ElementsetHref(@Nullable CharSequence value) Set hyperlink reference@NotNull ElementsetId(@Nullable CharSequence value) Set an identifier of the element@NotNull ElementSet a method of form@NotNull ElementsetName(@Nullable CharSequence value) Set an name of input element@NotNull ElementsetNameValue(@Nullable CharSequence name, @Nullable Object value) Set name & value to the input element@NotNull ElementsetRows(@org.jetbrains.annotations.Nullable int value) Row count of a text area@NotNull ElementsetRowSpan(@org.jetbrains.annotations.Nullable int value) Row span inside the table@NotNull ElementSet a type of input element@NotNull ElementSet an value of input element@NotNull ExceptionProviderDeprecated.@NotNull StringtoString()String valueMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.ujorm.tools.xml.ApiElement
setAttrib
-
Method Details
-
getName
Description copied from interface:ApiElementGet an element name- Specified by:
getNamein interfaceApiElement<Element>
-
setAttribute
@NotNull public @NotNull Element setAttribute(@NotNull @NotNull String name, @Nullable @Nullable Object value) Set an attribute- Specified by:
setAttributein interfaceApiElement<Element>- Parameters:
name- Required element namevalue- Thenullvalue is silently ignored. Formatting is performed by theAbstractWriter.writeValue(Object, ApiElement, String)method, where the default implementation calls atoString()only.- Returns:
- The original element
-
setAttributes
@NotNull public @NotNull Element setAttributes(@NotNull @NotNull String name, @NotNull @NotNull CharSequence separator, @NotNull @NotNull Object... value) Set an attribute- Parameters:
name- Required element namevalue- Thenullvalue is silently ignored. Formatting is performed by theAbstractWriter.writeValue(Object, ApiElement, String)method, where the default implementation calls atoString()only.- Returns:
- The original element
-
setAttribute
Set an attribute with no value- Parameters:
name- Required element name- Returns:
- The original element
-
setAttr
@NotNull public @NotNull Element setAttr(@NotNull @NotNull String name, @Nullable @Nullable Object value) A shortcut for the methodsetAttribute(java.lang.String, java.lang.Object).- Parameters:
name- Required element namevalue- Thenullvalue is silently ignored. Formatting is performed by theAbstractWriter.writeValue(Object, ApiElement, String)method, where the default implementation calls atoString()only.- Returns:
- The original element
-
addText
Add simple text- Specified by:
addTextin interfaceApiElement<Element>- Parameters:
data- Text item- Returns:
- A parent element.
- Throws:
IllegalStateException- See Also:
-
addText
@NotNull public @NotNull Element addText(@NotNull @NotNull Object... data) throws IllegalStateException Add many texts with no separator- Parameters:
data- Text items- Returns:
- A parent element.
- Throws:
IllegalStateException- See Also:
-
addTextTemplated
Add a template based text with parameters with hight performance.- Specified by:
addTextTemplatedin interfaceApiElement<Element>- Parameters:
template- A message template with an ENGLISH locale. SeeString.format(String, Object...)) for more parameters.values- A template parameters- Returns:
- A parent element.
-
addTexts
public Element addTexts(@NotNull @NotNull CharSequence separator, @NotNull @NotNull Object... data) throws IllegalStateException Add many words separated by a delimeter- Parameters:
separator- The delimiter must contain no special HTML character.data- Data to print- Returns:
- The current element
- Throws:
IllegalStateException
-
addRawText
@NotNull public @NotNull Element addRawText(@Nullable @Nullable Object data) throws IllegalStateException Description copied from interface:ApiElementAdd an native text with no escaped characters, for example: XML code, JavaScript, CSS styles- Specified by:
addRawTextin interfaceApiElement<Element>- Parameters:
data- Thenullvalue is ignored.- Returns:
- This instance
- Throws:
IllegalStateException
-
addRawText
@NotNull public @NotNull Element addRawText(@NotNull @NotNull Object... data) throws IllegalStateException - Throws:
IllegalStateException
-
addRawTexts
public Element addRawTexts(@NotNull @NotNull CharSequence separator, @NotNull @NotNull Object... data) throws IllegalStateException Add many words separated by a delimeter- Parameters:
separator- The delimiter must contain no special HTML character.data- Data to print- Returns:
- The current element
- Throws:
IllegalStateException
-
addComment
Description copied from interface:ApiElementAdd a comment text. The CDATA structure isn't really for HTML at all.- Specified by:
addCommentin interfaceApiElement<Element>- Parameters:
comment- A comment text must not contain a string-->.- Returns:
- This instance
- Throws:
IllegalStateException
-
addCDATA
Description copied from interface:ApiElementAdd a character data inCDATAformat to XML only. The CDATA structure isn't really for HTML at all.- Specified by:
addCDATAin interfaceApiElement<Element>- Parameters:
charData- A text including the final DATA sequence. An empty argument is ignored.- Returns:
- This instance
- Throws:
IllegalStateException
-
close
Description copied from interface:ApiElementClose the element- Specified by:
closein interfaceApiElement<Element>- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IllegalStateException
-
addElement
@NotNull public @NotNull Element addElement(@NotNull @NotNull String name) throws IllegalStateException Create new Element- Specified by:
addElementin interfaceApiElement<Element>- Parameters:
name- The element name- Returns:
- New instance of the Element
- Throws:
IllegalStateException- An envelope for IO exceptions
-
addElement
@NotNull public @NotNull Element addElement(@NotNull @NotNull String name, @NotNull @NotNull CharSequence... cssClasses) Add a new Element with optional CSS classes- Parameters:
name- A required name of the elementcssClasses- Optional CSS classes.- Returns:
- New instance of the Element
-
addElementIf
@NotNull public @NotNull Element addElementIf(boolean enabled, @NotNull @NotNull String name, @NotNull @NotNull CharSequence... cssClasses) Add an element according to a condition.- Parameters:
enabled- A condition for rendering the element.name- An element namecssClasses- CSS classes- Returns:
- New instance of the Element
-
addTable
Add new Table -
addTable
@NotNull public @NotNull Element addTable(@NotNull @NotNull Object[][] data, @NotNull @NotNull CharSequence... cssClass) Create a HTML table according to data -
addTable
@NotNull public @NotNull Element addTable(@NotNull @NotNull Collection<Object[]> data, @NotNull @NotNull CharSequence... cssClass) Create a HTML table according to data -
addTable
@NotNull public <D,V> @NotNull Element addTable(@NotNull @NotNull Stream<D> domains, @Nullable @Nullable CharSequence[] cssClass, @Nullable @Nullable Object[] headers, @NotNull @NotNull Function<D, V>... attributes) Create a HTML table according to dataUsage
element.addTable(getCars(), cssClasses, titles, Car::getId, Car::getName, Car::getEnabled); -
addImage
@NotNull public @NotNull Element addImage(@NotNull @NotNull CharSequence imageLink, @NotNull @NotNull CharSequence alt, @NotNull @NotNull CharSequence... cssClasses) throws IllegalStateException Add a link to an image- Parameters:
imageLink- A link to imagealt- An alternate textcssClasses- Optional CSS classes- Returns:
- Throws:
IllegalStateException
-
addImage
@NotNull public @NotNull Element addImage(@NotNull @NotNull InputStream imageStream, @NotNull @NotNull CharSequence alt, @NotNull @NotNull CharSequence... cssClasses) throws IllegalStateException Add an embeded image- Parameters:
imageStream- Stream provides a PNG image and it will be closed after reading.alt- An alternate textcssClasses- Optional CSS classes- Returns:
- Throws:
IllegalStateException
-
addBody
Add new body element -
addTitle
Add new title element -
addLink
Add new link element -
addStyle
Add new style element -
addScript
Add new script element -
addDiv
Add new div element -
addFieldset
@NotNull public @NotNull Element addFieldset(@Nullable @Nullable String title, @NotNull @NotNull CharSequence... cssClasses) Add new fieldset element including a title- Parameters:
title- An optional titlecssClasses- CSS classes- Returns:
- An instance of FieldSet
- See Also:
-
addPreformatted
Add new pre element -
addSpan
Add new span element -
addParagraph
Add new paragram element -
addForm
Add new form element -
addHeading
@NotNull public @NotNull Element addHeading(@NotNull @NotNull CharSequence title, @NotNull @NotNull CharSequence... cssClasses) Add a top heading (level one) -
addHeading
@NotNull public @NotNull Element addHeading(int level, @NotNull @NotNull CharSequence title, @NotNull @NotNull CharSequence... cssClasses) Add new heading with the required level where the first level is the one, -
addTableHead
Add new head of table element -
addTableRow
Add new table row element -
addTableDetail
Add new detail of table element -
addLabel
Add new label element -
addInput
Add new input element -
addTextInput
Add new input element type of text -
addTextInp
@NotNull public <V> @NotNull Element addTextInp(@NotNull @NotNull HttpParameter param, @Nullable V value, @NotNull @NotNull CharSequence title, @NotNull @NotNull CharSequence... cssClasses) Add new input element type of text including attributes: name, value, placeholder and title -
addPasswordInput
Add a new password input element -
addHiddenInput
@NotNull public @NotNull Element addHiddenInput(@Nullable @Nullable CharSequence name, @Nullable @Nullable Object value) Add a new hidden input element with a name & value -
addTextArea
Add new text area element -
addSelect
Add new select element -
addSelectOptions
@NotNull public @NotNull Element addSelectOptions(@NotNull @NotNull Object value, @NotNull @NotNull Map<?, ?> options, @NotNull @NotNull CharSequence... cssClasses) Add options from map to current select element- Parameters:
value- Value of a select elementoptions- Consider an instance of theLinkedHashMapclass predictable iteration order of options.cssClasses-- Returns:
- Return
this - See Also:
-
addOption
Add new option element -
addButton
Add new button element -
addSubmitButton
Add a submit button -
addAnchor
@NotNull public @NotNull Element addAnchor(@NotNull @NotNull String url, @NotNull @NotNull CharSequence... cssClasses) Add an anchor element with URL and CSS classes -
addLinkedText
@NotNull public @NotNull Element addLinkedText(@NotNull @NotNull String url, @NotNull @NotNull Object... text) Add a- Parameters:
url-text-- Returns:
- The original element!
-
addUnorderedlist
Add new unordered list element -
addOrderedList
Add new ordered list element -
addListItem
-
setClass
Set a CSS class attribute optionally, the empty attribute is ignored.- Parameters:
cssClasses- Optional CSS classes. The css item is ignored when the value is empty ornull.- Returns:
- The current instanlce
-
addBreak
Add a line break -
createHtmlRoot
@NotNull public static @NotNull Element createHtmlRoot(@NotNull @NotNull Object title, @Nullable @Nullable CharSequence... cssLinks) Crate a root element- Parameters:
cssLinks- Nullable CSS link array
-
createHtmlRoot
@NotNull public static @NotNull Element createHtmlRoot(@NotNull @NotNull Object title, @Nullable @Nullable Charset charset, @Nullable @Nullable CharSequence... cssLinks) Crate a root element- Parameters:
title- A HTML titlecharset- A charsetcssLinks- Nullable CSS link array
-
setId
Set an identifier of the element -
setMethod
Set a method of form -
setAction
Set an action type of from -
setType
Set a type of input element -
setName
Set an name of input element -
setValue
Set an value of input element -
setNameValue
@NotNull public @NotNull Element setNameValue(@Nullable @Nullable CharSequence name, @Nullable @Nullable Object value) Set name & value to the input element -
setFor
Set an value of input element -
setRows
Row count of a text area -
setCols
Column count of a text area -
setColSpan
@NotNull public @NotNull Element setColSpan(@Nullable @org.jetbrains.annotations.Nullable int value) Column span inside the table -
setRowSpan
@NotNull public @NotNull Element setRowSpan(@Nullable @org.jetbrains.annotations.Nullable int value) Row span inside the table -
setHref
Set hyperlink reference -
setHint
Set a placeholder name -
then
Deprecated.Use the methodnext(Consumer)rather.Apply body of element by a lambda expression. -
next
Add nested elements to the element.Usage
HtmlElement.of(config, writer).addBody() .next(body -> { body.addHeading(config.getTitle()); }) .catche(e -> { logger.log(Level.SEVERE, "An error", e); }); -
toString
String value -
of
New element for an API element
-
next(Consumer)rather.