Package org.ujorm.tools.web
Class AbstractHtmlElement
java.lang.Object
org.ujorm.tools.web.AbstractHtmlElement
- All Implemented Interfaces:
Closeable,AutoCloseable,Html,ApiElement<Element>
- Direct Known Subclasses:
HtmlElement
The root of HTML elements is independent on the Servlet API
Usage
ServletResponse response = new ServletResponse();
try (HtmlElement html = HtmlElement.of(response)) {
html.addBody().addHeading("Hello!");
}
assertTrue(response.toString().contains("<h1>Hello!</h1>"));
For more information, see the
next sample.-
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 -
Constructor Summary
ConstructorsConstructorDescriptionAbstractHtmlElement(@NotNull ApiElement root, @NotNull HtmlConfig config, @NotNull Appendable writer) Create new instance with empty HTML headersAbstractHtmlElement(@NotNull HtmlConfig config, @NotNull Appendable writer) Create new instance with empty HTML headers -
Method Summary
Modifier and TypeMethodDescription@NotNull ElementaddBody()Returns a body elementaddCDATA(CharSequence charData) Add a character data inCDATAformat to XML only.addComment(CharSequence comment) Add a comment text.addCssBodies(@NotNull CharSequence lineSeparator, @NotNull CharSequence... css) Create a new CSS element and return it.addCssBody(@NotNull CharSequence css) Create a new CSS element and return itaddCssLink(@NotNull CharSequence css) Create a new CSS element and return itvoidaddCssLinks(@NotNull CharSequence... css) Create a new CSS element and return itfinal @NotNull ElementaddElement(@NotNull String name) Create new ElementaddHead()Returns a head elementaddJavascriptBody(@Nullable CharSequence... javascript) Create a new Javascript element and return it.addJavascriptLink(boolean defer, @NotNull CharSequence javascriptLink) Create a new Javascript element and return itvoidaddJavascriptLinks(boolean defer, @NotNull CharSequence... javascriptLinks) Create a new Javascript element and return itaddRawText(Object value) Add an native text with no escaped characters, for example: XML code, JavaScript, CSS stylesAdd a text and escape special characteraddTextTemplated(@NotNull CharSequence template, @NotNull Object... values) Message templatevoidclose()Close the element@NotNull ElementgetBody()Returns a body element@NotNull HtmlConfigGet configgetHead()Returns a head element@NotNull StringgetName()Get an element namegetTitle()Get title of configuration@NotNull ExceptionProvidernext(@NotNull Consumer<AbstractHtmlElement> builder) Add nested elements to the element.static @NotNull HtmlElementniceOf(@NotNull CharSequence title, @NotNull Appendable response, @NotNull CharSequence... cssLinks) Create new instance with empty HTML headersstatic @NotNull HtmlElementniceOf(@NotNull CharSequence title, @NotNull Appendable response, @NotNull Charset charset, @NotNull CharSequence... cssLinks) Create new instance with empty HTML headersstatic @NotNull HtmlElementniceOf(@NotNull String title, @NotNull Appendable response, @NotNull CharSequence... cssLinks) Create new instance with empty HTML headersstatic @NotNull HtmlElementniceOf(@NotNull String title, @NotNull HttpContext context, @NotNull CharSequence... cssLinks) Create new instance with empty HTML headersstatic @NotNull HtmlElementniceOfResponse(@NotNull Object httpServletResponse, @NotNull CharSequence... cssLinks) Create new instance with empty HTML headersstatic @NotNull HtmlElementniceOfResponse(@NotNull String title, @NotNull Object httpServletResponse, @NotNull CharSequence... cssLinks) Create new instance with empty HTML headersstatic @NotNull HtmlElementof(@NotNull Appendable writer, @NotNull HtmlConfig myConfig) Create a root element for a required element name.static @NotNull HtmlElementof(@NotNull CharSequence title, @NotNull Appendable response, @NotNull CharSequence... cssLinks) Create new instance with empty HTML headersstatic @NotNull HtmlElementof(@NotNull CharSequence title, @NotNull Appendable response, @NotNull Charset charset, @NotNull CharSequence... cssLinks) Create new instance with empty HTML headersstatic @NotNull HtmlElementof(@NotNull HttpContext context, @NotNull HtmlConfig myConfig) Create root element for a required element name.static @NotNull HtmlElementof(@Nullable HtmlConfig config) Create new instance with empty HTML headersstatic @NotNull HtmlElementofServlet(@NotNull Object httpServletResponse, @Nullable HtmlConfig config) Create a new instance with empty HTML headers.static @NotNull HtmlElementofServlet(@NotNull String title, @NotNull Object httpServletResponse, @NotNull CharSequence... cssLinks) Create new instance with empty HTML headers@NotNull Elementoriginal()Get an original root elementsetAttribute(String name, Object value) Set an attributefinal @NotNull ExceptionProviderthen(@NotNull Consumer<AbstractHtmlElement> builder) Deprecated.@NotNull StringtoString()Returns an Render the HTML code including header.Methods 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
-
Constructor Details
-
AbstractHtmlElement
public AbstractHtmlElement(@NotNull @NotNull HtmlConfig config, @NotNull @NotNull Appendable writer) Create new instance with empty HTML headers -
AbstractHtmlElement
public AbstractHtmlElement(@NotNull @NotNull ApiElement root, @NotNull @NotNull HtmlConfig config, @NotNull @NotNull Appendable writer) Create new instance with empty HTML headers
-
-
Method Details
-
getName
Description copied from interface:ApiElementGet an element name- Specified by:
getNamein interfaceApiElement<Element>
-
setAttribute
Description copied from interface:ApiElementSet 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
-
addText
Description copied from interface:ApiElementAdd a text and escape special character- Specified by:
addTextin interfaceApiElement<Element>- Parameters:
value- Thenullvalue is allowed. Formatting is performed by theAbstractWriter.writeValue(Object, ApiElement, String)} method, where the default implementation calls atoString()only.- Returns:
- This instance
-
addTextTemplated
public Element addTextTemplated(@NotNull @NotNull CharSequence template, @NotNull @NotNull Object... values) Description copied from interface:ApiElementMessage template- Specified by:
addTextTemplatedin interfaceApiElement<Element>- Parameters:
template- Message template where parameters are marked by the{}symbolvalues- argument values- Returns:
- The original builder
-
addRawText
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:
value- Thenullvalue is ignored.- Returns:
- This instance
-
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
-
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
-
addElement
@NotNull public final @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
-
getHead
Returns a head element -
addHead
Returns a head element -
getBody
Returns a body element -
addBody
Returns a body element -
addJavascriptLinks
Create a new Javascript element and return it- Parameters:
javascriptLinks- URL list to Javascriptdefer- A script that will not run until after the page has loaded
-
addJavascriptLink
Create a new Javascript element and return it- Parameters:
javascriptLink- URL to Javascriptdefer- A script that will not run until after the page has loaded- Returns:
-
addJavascriptBody
Create a new Javascript element and return it. Each item is separated by a new line.- Parameters:
javascript- Add a javascriptLink link- Returns:
- New CSS element
-
addCssLinks
Create a new CSS element and return it- Parameters:
css- Add a CSS link
-
addCssLink
Create a new CSS element and return it- Parameters:
css- Add a CSS link- Returns:
- New CSS element
-
addCssBody
Create a new CSS element and return it- Parameters:
css- CSS content- Returns:
- New CSS element
-
addCssBodies
public Element addCssBodies(@NotNull @NotNull CharSequence lineSeparator, @NotNull @NotNull CharSequence... css) Create a new CSS element and return it. Each item is separated by a new line.- Parameters:
lineSeparator- Row separatorcss- CSS content rows- Returns:
- New CSS element
-
original
Get an original root element -
toString
Returns an Render the HTML code including header. Call the close() method before view- Overrides:
toStringin classObject- 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
-
getConfig
Get config -
getTitle
Get title of configuration -
then
@NotNull public final @NotNull ExceptionProvider then(@NotNull @NotNull Consumer<AbstractHtmlElement> builder) Deprecated.Use the methodnext(Consumer)rather.Apply body of element by a lambda expression. -
next
@NotNull public @NotNull ExceptionProvider next(@NotNull @NotNull Consumer<AbstractHtmlElement> builder) 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); }); -
of
@NotNull public static @NotNull HtmlElement of(@NotNull @NotNull Appendable writer, @NotNull @NotNull HtmlConfig myConfig) throws IllegalStateException Create a root element for a required element name. The MAIN factory method.- Throws:
IllegalStateException
-
of
@NotNull public static @NotNull HtmlElement of(@NotNull @NotNull HttpContext context, @NotNull @NotNull HtmlConfig myConfig) Create root element for a required element name. The MAIN factory method. -
ofServlet
@NotNull public static @NotNull HtmlElement ofServlet(@NotNull @NotNull Object httpServletResponse, @Nullable @Nullable HtmlConfig config) Create a new instance with empty HTML headers. The MAIN servlet factory method.- Throws:
IllegalStateException- IO exceptions- See Also:
-
ofServlet
@NotNull public static @NotNull HtmlElement ofServlet(@NotNull @NotNull String title, @NotNull @NotNull Object httpServletResponse, @NotNull @NotNull CharSequence... cssLinks) Create new instance with empty HTML headers- Throws:
IllegalStateException- IO exceptions- See Also:
-
of
@NotNull public static @NotNull HtmlElement of(@NotNull @NotNull CharSequence title, @NotNull @NotNull Appendable response, @NotNull @NotNull CharSequence... cssLinks) Create new instance with empty HTML headers- Throws:
IllegalStateException- IO exceptions- See Also:
-
of
@NotNull public static @NotNull HtmlElement of(@NotNull @NotNull CharSequence title, @NotNull @NotNull Appendable response, @NotNull @NotNull Charset charset, @NotNull @NotNull CharSequence... cssLinks) Create new instance with empty HTML headers- Throws:
IllegalStateException- IO exceptions- See Also:
-
niceOf
@NotNull public static @NotNull HtmlElement niceOf(@NotNull @NotNull CharSequence title, @NotNull @NotNull Appendable response, @NotNull @NotNull CharSequence... cssLinks) Create new instance with empty HTML headers- Throws:
IllegalStateException- IO exceptions- See Also:
-
niceOf
@NotNull public static @NotNull HtmlElement niceOf(@NotNull @NotNull CharSequence title, @NotNull @NotNull Appendable response, @NotNull @NotNull Charset charset, @NotNull @NotNull CharSequence... cssLinks) Create new instance with empty HTML headers- Throws:
IllegalStateException- IO exceptions- See Also:
-
niceOfResponse
@NotNull public static @NotNull HtmlElement niceOfResponse(@NotNull @NotNull String title, @NotNull @NotNull Object httpServletResponse, @NotNull @NotNull CharSequence... cssLinks) Create new instance with empty HTML headers- Throws:
IllegalStateException- IO exceptions- See Also:
-
niceOfResponse
@NotNull public static @NotNull HtmlElement niceOfResponse(@NotNull @NotNull Object httpServletResponse, @NotNull @NotNull CharSequence... cssLinks) Create new instance with empty HTML headers- Throws:
IllegalStateException- IO exceptions- See Also:
-
niceOf
@NotNull public static @NotNull HtmlElement niceOf(@NotNull @NotNull String title, @NotNull @NotNull HttpContext context, @NotNull @NotNull CharSequence... cssLinks) Create new instance with empty HTML headers- Throws:
IllegalStateException- IO exceptions- See Also:
-
niceOf
@NotNull public static @NotNull HtmlElement niceOf(@NotNull @NotNull String title, @NotNull @NotNull Appendable response, @NotNull @NotNull CharSequence... cssLinks) Create new instance with empty HTML headers- Throws:
IllegalStateException- IO exceptions- See Also:
-
of
@NotNull public static @NotNull HtmlElement of(@Nullable @Nullable HtmlConfig config) throws IllegalStateException Create new instance with empty HTML headers- Parameters:
config- Html configuration- Returns:
- An instance of the HtmlPage
- Throws:
IllegalStateException- IO exceptions
-
next(Consumer)rather.