public class XmlBuilder extends Object implements ApiElement<XmlBuilder>
XmlPriter writer = XmlPriter.forXml(); try (XmlBuilder html = new XmlBuilder(Html.HTML, writer)) { try (XmlBuilder head = html.addElement(Html.HEAD)) { head.addElement(Html.META, Html.A_CHARSET, UTF_8); head.addElement(Html.TITLE).addText("Test"); } ry (XmlBuilder body = html.addElement(Html.BODY)) { body.addElement(Html.H1).addText("Hello word!"); body.addElement(Html.DIV).addText(null); } }; String result = writer.toString();The XmlElement class implements the
Closeable
implementation
for an optional highlighting the tree structure in the source code.HtmlElement
Modifier and Type | Field and Description |
---|---|
protected static String |
HTML
The HTML tag name
|
protected String |
name
Element name
|
protected static String |
REQUIRED_MSG
Assertion message template
|
Modifier | Constructor and Description |
---|---|
|
XmlBuilder(CharSequence name,
XmlPrinter writer)
New element with a parent
|
|
XmlBuilder(CharSequence name,
XmlPrinter writer,
int level)
The new element constructor
|
protected |
XmlBuilder(CharSequence name,
XmlPrinter writer,
int level,
boolean printName)
The new element constructor
|
Modifier and Type | Method and Description |
---|---|
XmlBuilder |
addCDATA(CharSequence charData)
Deprecated.
|
XmlBuilder |
addComment(CharSequence comment)
Deprecated.
|
XmlBuilder |
addElement(String name)
Create a new
XmlBuilder for a required name and add it to children. |
XmlBuilder |
addRawText(Object value)
Add an native text with no escaped characters, for example: XML code, JavaScript, CSS styles
|
XmlBuilder |
addText(Object value)
Add a text and escape special character
|
void |
close()
Close the Node
|
static XmlBuilder |
forHtml(Appendable response)
Create builder for HTML
|
static XmlBuilder |
forNiceHtml(Appendable response) |
int |
getLevel() |
String |
getName()
Get an element name
|
XmlPrinter |
getWriter()
Writer
|
boolean |
isClosed()
Is the node closed?
|
boolean |
isFilled() |
boolean |
isLastText()
The last child was a text
|
protected XmlBuilder |
nextChild(XmlBuilder element)
Setup states
|
XmlBuilder |
setAttribute(String name,
Object value)
Add an attribute
|
String |
toString()
Render the XML code including header
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
setAttrib
protected static final String HTML
protected static final String REQUIRED_MSG
public XmlBuilder(@Nonnull CharSequence name, @Nonnull XmlPrinter writer, int level)
name
- The element name must not be empty nor special HTML characters.protected XmlBuilder(@Nonnull CharSequence name, @Nonnull XmlPrinter writer, int level, boolean printName)
name
- The element name must not be empty nor special HTML characters.writer
- A XmlPrinterlevel
- Level of the ElementprintName
- Print the element name immediately.public XmlBuilder(@Nonnull CharSequence name, @Nonnull XmlPrinter writer)
@Nonnull public String getName()
ApiElement
getName
in interface ApiElement<XmlBuilder>
@Nonnull protected XmlBuilder nextChild(@Nullable XmlBuilder element)
element
- A child Node or null
value for a text data@Nonnull public final XmlBuilder addElement(@Nonnull String name)
XmlBuilder
for a required name and add it to children.addElement
in interface ApiElement<XmlBuilder>
name
- A name of the new XmlElement is required.@Nonnull public final XmlBuilder setAttribute(@Nonnull String name, @Nullable Object value)
setAttribute
in interface ApiElement<XmlBuilder>
name
- Required element namevalue
- The null
value is ignored. Formatting is performed by the
XmlPrinter#writeValue(java.lang.Object, org.ujorm.tools.dom.XmlElement, java.lang.String, java.io.Writer)
method, where the default implementation calls a toString()
only.@Nonnull public final XmlBuilder addText(@Nullable Object value)
addText
in interface ApiElement<XmlBuilder>
value
- The null
value is allowed. Formatting is performed by the
XmlPrinter#writeValue(java.lang.Object, org.ujorm.tools.dom.XmlElement, java.lang.String, java.io.Writer)
method, where the default implementation calls a toString()
only.@Nonnull public final XmlBuilder addRawText(@Nullable Object value)
addRawText
in interface ApiElement<XmlBuilder>
value
- The null
value is ignored.@Nonnull @Deprecated public final XmlBuilder addComment(@Nullable CharSequence comment)
addComment
in interface ApiElement<XmlBuilder>
comment
- A comment text must not contain a string -->
.@Nonnull @Deprecated public final XmlBuilder addCDATA(@Nullable CharSequence charData)
CDATA
format to XML only.
The CDATA structure isn't really for HTML at all.addCDATA
in interface ApiElement<XmlBuilder>
charData
- A text including the final DATA sequence. An empty argument is ignored.public final void close()
close
in interface Closeable
close
in interface AutoCloseable
close
in interface ApiElement<XmlBuilder>
public boolean isClosed()
public int getLevel()
public boolean isFilled()
public boolean isLastText()
public XmlPrinter getWriter()
@Nonnull public static XmlBuilder forHtml(@Nonnull Appendable response)
@Nonnull public static XmlBuilder forNiceHtml(@Nonnull Appendable response)
Copyright 2019-2022, Pavel Ponec