Class RNCBuilder
XMLBuilder for XML content generation.
Wraps a StringBuilder and provides fluent methods for appending
RNC constructs (namespaces, defines, elements, attributes, operators,
keywords, comments) with correct formatting. All methods return
this to enable method chaining.
Use case: used by code actions and completions to generate properly
formatted RNC fragments that respect the user's indentation preferences
(tab vs spaces, line delimiter). Also supports LSP snippet placeholders
($1, ${2:default}) for interactive completions.
Example — generating a complete RNC schema:
RNCBuilder builder = new RNCBuilder(" ", "\n", false);
builder.addDefaultNamespace("http://example.com")
.blankLine()
.addStart("root")
.blankLine()
.startDefine("root")
.startElement("root", 0)
.linefeed().indent(1)
.appendText()
.linefeed()
.closeBrace();
// Produces:
// default namespace = "http://example.com"
//
// start = root
//
// root = element root {
// text
// }
Example — generating a snippet with placeholders for a completion item:
RNCBuilder builder = new RNCBuilder(" ", "\n", true);
builder.append("element ")
.addSnippetPlaceholder("name") // ${1:name}
.append(" {")
.linefeed().indent(1)
.addFinalTabstop() // $0
.linefeed()
.closeBrace();
// Produces: element ${1:name} {\n $0\n}
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionRNCBuilder(String whitespacesIndent, String lineDelimiter, boolean canSupportSnippets) Creates a new RNC builder. -
Method Summary
Modifier and TypeMethodDescriptionaddComment(String text) Appends a single-line comment:# text.addDatatypes(String prefix, String uri) Appends a datatypes declaration:datatypes prefix = "uri".Appends a default namespace declaration:default namespace = "uri".addDocComment(String text) Appends a documentation comment:## text.Appends the final tabstop ($0), which marks where the cursor rests after all other tabstops are visited.addNamespace(String prefix, String uri) Appends a namespace declaration:namespace prefix = "uri".addSnippetPlaceholder(String defaultValue) Appends a snippet placeholder (${N:defaultValue}) at the current position.Appends a snippet tabstop ($N) at the current position, auto-incrementing the index.Appends a start pattern:start = pattern.append(char c) Appends a single character to the buffer.Appends arbitrary text to the buffer.Appends the choice operator with surrounding spaces:|.Appends theemptykeyword (matches empty content).Appends the group (sequence) operator:,(comma + space).appendInclude(String file) Appends an include declaration:include "file".Appends the interleave operator with surrounding spaces:&.Appends the one-or-more postfix operator:+.Appends the optional postfix operator:?.Appends thetextkeyword (matches any text content).Appends the zero-or-more postfix operator:*.Appends a blank line (two consecutive line delimiters).Appends a closing brace:}.Closes an attribute pattern:}(with leading space).indent(int level) Appends indentation for the given nesting level.intlength()Returns the current length of the accumulated content.linefeed()Appends a line feed (line delimiter).Appends an opening brace:{.space()Appends a single space character.startAttribute(String name) Begins an attribute pattern:attribute name {.startDefine(String name) Begins a define statement:name =.startDefineChoice(String name) Begins a choice-combine define:name |=.startDefineInterleave(String name) Begins an interleave-combine define:name &=.startElement(String name, int indentLevel) Begins an element pattern:element name {.toString()Returns the accumulated RNC content as a string.
-
Constructor Details
-
RNCBuilder
Creates a new RNC builder.- Parameters:
whitespacesIndent- the string for one indent level (e.g. " " for 2-space indent, "\t" for tab indent).lineDelimiter- the line delimiter to use (e.g. "\n", "\r\n").canSupportSnippets-trueif the client supports LSP snippet syntax in completions and code actions.
-
-
Method Details
-
addNamespace
Appends a namespace declaration:namespace prefix = "uri".Use case: generating a namespace declaration in a "New RNC" file template or when a code action needs to add a namespace.
Example output:
namespace a = "http://example.com"- Parameters:
prefix- the namespace prefix (e.g. "a", "html").uri- the namespace URI.- Returns:
- this builder for chaining.
-
addDefaultNamespace
Appends a default namespace declaration:default namespace = "uri".Example output:
default namespace = "http://example.com"- Parameters:
uri- the default namespace URI.- Returns:
- this builder for chaining.
-
addDatatypes
Appends a datatypes declaration:datatypes prefix = "uri".Use case: adding an XSD datatypes declaration:
datatypes xsd = "http://www.w3.org/2001/XMLSchema-datatypes"- Parameters:
prefix- the datatypes prefix (e.g. "xsd").uri- the datatypes library URI.- Returns:
- this builder for chaining.
-
addStart
Appends a start pattern:start = pattern.Example output:
start = root- Parameters:
pattern- the start pattern name.- Returns:
- this builder for chaining.
-
startDefine
Begins a define statement:name =.Use case: start a define, then append the pattern body. Example:
builder.startDefine("root").startElement("root", 0)...producesroot = element root {...}.- Parameters:
name- the define name.- Returns:
- this builder for chaining.
-
startDefineChoice
Begins a choice-combine define:name |=.Use case: extending a pattern with additional choices:
content |= newElement.- Parameters:
name- the define name.- Returns:
- this builder for chaining.
-
startDefineInterleave
Begins an interleave-combine define:name &=.Use case: extending a pattern with interleaved content:
content &= optionalElement.- Parameters:
name- the define name.- Returns:
- this builder for chaining.
-
startElement
Begins an element pattern:element name {.Use case: generating element patterns in completions or code actions. The caller is responsible for adding content and closing with
closeBrace().Example:
builder.startElement("root", 0)produceselement root {.- Parameters:
name- the element name.indentLevel- the current indent level (reserved for future use).- Returns:
- this builder for chaining.
-
startAttribute
Begins an attribute pattern:attribute name {.Attributes use inline formatting with spaces inside braces:
attribute id { text }.- Parameters:
name- the attribute name.- Returns:
- this builder for chaining.
-
endAttribute
Closes an attribute pattern:}(with leading space).- Returns:
- this builder for chaining.
-
openBrace
Appends an opening brace:{.- Returns:
- this builder for chaining.
-
closeBrace
Appends a closing brace:}.- Returns:
- this builder for chaining.
-
appendChoice
Appends the choice operator with surrounding spaces:|.Use case: combining patterns with choice:
a | b | c.- Returns:
- this builder for chaining.
-
appendInterleave
Appends the interleave operator with surrounding spaces:&.Use case: combining patterns with interleave:
a & b.- Returns:
- this builder for chaining.
-
appendGroup
Appends the group (sequence) operator:,(comma + space).Use case: sequencing patterns:
a, b, c.- Returns:
- this builder for chaining.
-
appendOptional
Appends the optional postfix operator:?.Use case: marking a pattern as optional:
name?.- Returns:
- this builder for chaining.
-
appendZeroOrMore
Appends the zero-or-more postfix operator:*.Use case: marking a pattern as repeatable:
item*.- Returns:
- this builder for chaining.
-
appendOneOrMore
Appends the one-or-more postfix operator:+.Use case: marking a pattern as requiring at least one:
name+.- Returns:
- this builder for chaining.
-
appendText
Appends thetextkeyword (matches any text content).Example: inside an element —
element name { text }.- Returns:
- this builder for chaining.
-
appendEmpty
Appends theemptykeyword (matches empty content).Example:
element br { empty }.- Returns:
- this builder for chaining.
-
appendInclude
Appends an include declaration:include "file".Use case: including another RNC grammar:
include "common.rnc".- Parameters:
file- the file path to include.- Returns:
- this builder for chaining.
-
addSnippetTabstop
Appends a snippet tabstop ($N) at the current position, auto-incrementing the index. If snippets are not supported, appends nothing.Use case: placing cursor positions in a completion snippet:
element $1 { $2 }.- Returns:
- this builder for chaining.
-
addSnippetPlaceholder
Appends a snippet placeholder (${N:defaultValue}) at the current position. If snippets are not supported, appends the default value as plain text.Use case: a completion for
elementwith a placeholder for the name:element ${1:name} { $0 }.- Parameters:
defaultValue- the default text shown in the placeholder.- Returns:
- this builder for chaining.
-
addFinalTabstop
Appends the final tabstop ($0), which marks where the cursor rests after all other tabstops are visited. If snippets are not supported, appends nothing.- Returns:
- this builder for chaining.
-
linefeed
Appends a line feed (line delimiter).- Returns:
- this builder for chaining.
-
indent
Appends indentation for the given nesting level.Use case: indenting content inside a block:
builder.linefeed().indent(1).appendText()produces\n text(with 2-space indent).- Parameters:
level- the number of indent levels to append.- Returns:
- this builder for chaining.
-
space
Appends a single space character.- Returns:
- this builder for chaining.
-
blankLine
Appends a blank line (two consecutive line delimiters).Use case: separating top-level definitions with a visual gap.
- Returns:
- this builder for chaining.
-
append
Appends arbitrary text to the buffer.- Parameters:
text- the text to append.- Returns:
- this builder for chaining.
-
append
Appends a single character to the buffer.- Parameters:
c- the character to append.- Returns:
- this builder for chaining.
-
addComment
Appends a single-line comment:# text.Example output:
# This is a comment- Parameters:
text- the comment text (without the#prefix).- Returns:
- this builder for chaining.
-
addDocComment
Appends a documentation comment:## text.Documentation comments in RNC are attached to the following definition and can be used to generate documentation.
Example output:
## The root element of the document- Parameters:
text- the documentation comment text (without the##prefix).- Returns:
- this builder for chaining.
-
toString
Returns the accumulated RNC content as a string. -
length
public int length()Returns the current length of the accumulated content.- Returns:
- the number of characters in the buffer.
-