Package org.ujorm.tools.xml.builder
Class XmlBuilder
java.lang.Object
org.ujorm.tools.xml.builder.XmlBuilder
- All Implemented Interfaces:
Closeable,AutoCloseable,ApiElement<XmlBuilder>
A XML builder.
The main benefits are:
- secure building well-formed XML documents by the Java code
- a simple API built on a single XmlElement class
- creating XML components by a subclass is possible
- great performance and small memory footprint
How to use the class:
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.- Since:
- 1.86
- Author:
- Pavel Ponec
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionXmlBuilder(@NotNull String name, @NotNull XmlPrinter writer) New element with a parentXmlBuilder(@NotNull String name, @NotNull XmlPrinter writer, int level) The new element constructorprotectedXmlBuilder(@NotNull String name, @NotNull XmlPrinter writer, int level, boolean printName) The new element constructor -
Method Summary
Modifier and TypeMethodDescriptionfinal @NotNull XmlBuilderaddCDATA(@Nullable CharSequence charData) Deprecated.final @NotNull XmlBuilderaddComment(@Nullable CharSequence comment) Deprecated.final @NotNull XmlBuilderaddElement(@NotNull String name) Create a newXmlBuilderfor a required name and add it to children.final @NotNull XmlBuilderaddRawText(@Nullable Object value) Add an native text with no escaped characters, for example: XML code, JavaScript, CSS stylesfinal @NotNull XmlBuilderAdd a text and escape special characterfinal @NotNull XmlBuilderaddTextTemplated(@Nullable CharSequence template, @NotNull Object... values) Message template with hight performance.final voidclose()Close the Nodestatic @NotNull XmlBuilderforHtml(@NotNull Appendable response) Create builder for HTMLstatic @NotNull XmlBuilderforNiceHtml(@NotNull Appendable response) intgetLevel()@NotNull StringgetName()Get an element nameWriterbooleanisClosed()Is the node closed?booleanisFilled()booleanThe last child was a textprotected @Nullable XmlBuildernextChild(@Nullable XmlBuilder element) Setup statesfinal @NotNull XmlBuildersetAttribute(@Nullable String name, @Nullable Object value) Add an attribute@NotNull StringtoString()Render the XML code including headerMethods 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
-
Field Details
-
HIDDEN_NAME
A name of a hidden element must be a unique instance -
HTML
The HTML tag name- See Also:
-
REQUIRED_MSG
Assertion message template- See Also:
-
name
Element name
-
-
Constructor Details
-
XmlBuilder
The new element constructor- Parameters:
name- The element name must not be special HTML characters. Thenullvalue is intended to build a root of AJAX queries.
-
XmlBuilder
protected XmlBuilder(@NotNull @NotNull String name, @NotNull @NotNull XmlPrinter writer, int level, boolean printName) The new element constructor- Parameters:
name- The element name must not be special HTML characters. Thenullvalue is intended to build a root of AJAX queries.writer- A XmlPrinterlevel- Level of the ElementprintName- Print the element name immediately.
-
XmlBuilder
New element with a parent
-
-
Method Details
-
getName
Description copied from interface:ApiElementGet an element name- Specified by:
getNamein interfaceApiElement<XmlBuilder>
-
nextChild
Setup states- Parameters:
element- A child Node ornullvalue for a text data
-
addElement
Create a newXmlBuilderfor a required name and add it to children.- Specified by:
addElementin interfaceApiElement<XmlBuilder>- Parameters:
name- A name of the new XmlElement is required.- Returns:
- The new XmlElement!
-
setAttribute
@NotNull public final @NotNull XmlBuilder setAttribute(@Nullable @Nullable String name, @Nullable @Nullable Object value) Add an attribute- Specified by:
setAttributein interfaceApiElement<XmlBuilder>- Parameters:
name- Required element namevalue- Thenullvalue is ignored. Formatting is performed by theAbstractWriter.writeValue(Object, ApiElement, String)method, where the default implementation calls atoString()only.- Returns:
- The original element
-
addText
Add a text and escape special character- Specified by:
addTextin interfaceApiElement<XmlBuilder>- 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
@NotNull public final @NotNull XmlBuilder addTextTemplated(@Nullable @Nullable CharSequence template, @NotNull @NotNull Object... values) Message template with hight performance.- Specified by:
addTextTemplatedin interfaceApiElement<XmlBuilder>- Parameters:
template- Message template where parameters are marked by the{}symbolvalues- argument values- Returns:
- The original builder
-
addRawText
Add an native text with no escaped characters, for example: XML code, JavaScript, CSS styles- Specified by:
addRawTextin interfaceApiElement<XmlBuilder>- Parameters:
value- Thenullvalue is ignored.- Returns:
- This instance
-
addComment
@NotNull @Deprecated public final @NotNull XmlBuilder addComment(@Nullable @Nullable CharSequence comment) Deprecated.Add a comment text. The CDATA structure isn't really for HTML at all.- Specified by:
addCommentin interfaceApiElement<XmlBuilder>- Parameters:
comment- A comment text must not contain a string-->.- Returns:
- This instance
-
addCDATA
@NotNull @Deprecated public final @NotNull XmlBuilder addCDATA(@Nullable @Nullable CharSequence charData) Deprecated.Add a character data inCDATAformat to XML only. The CDATA structure isn't really for HTML at all.- Specified by:
addCDATAin interfaceApiElement<XmlBuilder>- Parameters:
charData- A text including the final DATA sequence. An empty argument is ignored.- Returns:
- This instance
-
close
public final void close()Close the Node- Specified by:
closein interfaceApiElement<XmlBuilder>- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
isClosed
public boolean isClosed()Is the node closed? -
getLevel
public int getLevel() -
isFilled
public boolean isFilled() -
isLastText
public boolean isLastText()The last child was a text -
getWriter
Writer -
toString
Render the XML code including header -
forHtml
Create builder for HTML -
forNiceHtml
-