Package com.pnfsoftware.jeb.util.format
Class XmlBuilder
java.lang.Object
com.pnfsoftware.jeb.util.format.TextBuilder
com.pnfsoftware.jeb.util.format.XmlBuilder
- All Implemented Interfaces:
Appendable
A specialized text builder used to craft XML documents sequentially.
-
Constructor Summary
ConstructorsConstructorDescriptionCreate an XML builder with default indentation settings.XmlBuilder(int indentationSize) Create an XML builder.XmlBuilder(int indentationSize, int initialCapacity) Create an XML builder. -
Method Summary
Modifier and TypeMethodDescriptionvoidappendHeader(int versionMajor, int versionMinor, String encoding, boolean standalone) Append an XML header.voidAppend a standard 1.0 header specifying a UTF-8 encoding and standalone=no.voidappendText(String s) Append text data, enclosed in tags.voidClose the last opened element.voidcloseElement(boolean appendLine) Close the last opened element.static CharSequencePerform blanket escape of the input string: the character set(&, <, >, ", ')to their five predefined XML entities.static Stringstatic StringvoidopenElement(String name) Output an XML element.voidopenElement(String name, Map<String, String> attributes) Output an XML element with attributes.voidopenElement(String name, Map<String, String> attributes, boolean close) Output an XML element with attributes.static StringsanitizeAttributeName(String name) static Stringstatic StringsanitizeElementName(String name) static StringvoidsetAutoModifiers(int forNames, int forAttributeNames, int forAttributeValues, int forTextData) Set automatic modifiers for XML tokens.Methods inherited from class com.pnfsoftware.jeb.util.format.TextBuilder
append, append, append, append, append, append, append, append, append, append, append, appendLine, appendLine, appendLine, clear, eol, indent, indent, length, removeChar, removeChars, removeLastChar, removeLastLine, space, toString, unindent, updateIndentation
-
Constructor Details
-
XmlBuilder
public XmlBuilder()Create an XML builder with default indentation settings. -
XmlBuilder
public XmlBuilder(int indentationSize) Create an XML builder.- Parameters:
indentationSize- number of spaces per indentation level
-
XmlBuilder
public XmlBuilder(int indentationSize, int initialCapacity) Create an XML builder.- Parameters:
indentationSize- number of spaces per indentation levelinitialCapacity- initial capacity of the underlying character buffer
-
-
Method Details
-
setAutoModifiers
public void setAutoModifiers(int forNames, int forAttributeNames, int forAttributeValues, int forTextData) Set automatic modifiers for XML tokens.- 0 means leave as-is
- 1 means escape
- 2 means sanitize an unsafe token (that should be escaped already, but may not be, even if escaping is relevant.)
- Parameters:
forNames- 0 (no-op) or 2 (sanitize)forAttributeNames- 0 (no-op) or 2 (sanitize)forAttributeValues- 0 (no-op), 1 (escape), or 2 (sanitize)forTextData- 0 (no-op), 1 (escape), or 2 (sanitize)
- 0 means leave as-is
-
appendStandardHeader
public void appendStandardHeader()Append a standard 1.0 header specifying a UTF-8 encoding and standalone=no. -
appendHeader
Append an XML header.- Parameters:
versionMajor- XML major versionversionMinor- XML minor versionencoding- declared character encodingstandalone- true to mark the document as standalone
-
openElement
Output an XML element. The closing tag is not output.- Parameters:
name- the element name
-
openElement
Output an XML element with attributes. The closing tag is not output.- Parameters:
name-attributes- optional attributes
-
openElement
Output an XML element with attributes.- Parameters:
name- the element nameattributes- optional map of attributes name->value, which must be escaped (this method will append the attributes' names and values as-is)close- if true, the tag is immediately closed using "/>"
-
closeElement
public void closeElement()Close the last opened element. -
closeElement
public void closeElement(boolean appendLine) Close the last opened element.- Parameters:
appendLine- if true, a new line is appended after the closing tag
-
appendText
Append text data, enclosed in tags.- Parameters:
s-
-
escapeAttributeValue
-
escapeTextValue
-
sanitizeElementName
-
sanitizeAttributeName
-
sanitizeAttributeValue
-
sanitizeTextValue
-
escapeAll
Perform blanket escape of the input string: the character set(&, <, >, ", ')to their five predefined XML entities.- Parameters:
s- input string- Returns:
- escaped string
-