Top Description Inners Fields Constructors Methods
groovy.xml

public Class MarkupBuilder

extends BuilderSupport
Class Inheritance
Imports
groovy.namespace.QName, groovy.util.BuilderSupport, .IndentPrinter, groovy.xml.markupsupport.DoubleQuoteFilter, .SingleQuoteFilter, .StandardXmlAttributeFilter, .StandardXmlFilter, org.codehaus.groovy.runtime.StringGroovyMethods, java.io.PrintWriter, .Writer, java.util.ArrayList, .Iterator, .List, .Map, .Optional, java.util.function.Function

A helper class for creating XML or HTML markup. The builder supports various 'pretty printed' formats.

Example:

new MarkupBuilder().root {
  a( a1:'one' ) {
    b { mkp.yield( '3 < 5' ) }
    c( a2:'two', 'blah' )
  }
}
Will print the following to System.out:
<root>
  <a a1='one'>
    <b>3 &lt; 5</b>
    <c a2='two'>blah</c>
  </a>
</root>
Notes:

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
public static class

Field Summary

Modifier and TypeField and Description
private List<Function<Character, Optional<String>>>
private boolean
private boolean
private boolean
private boolean
private boolean
private boolean
private IndentPrinter
private int
private boolean

Constructor Summary

AccessConstructor and Description
public
MarkupBuilder()

Prints markup to System.out

public
MarkupBuilder(PrintWriter
the PrintWriter to use
pw
)

Sends markup to the given PrintWriter

public
MarkupBuilder(Writer
the writer to use
writer
)

Sends markup to the given Writer but first wrapping it in a PrintWriter

public
MarkupBuilder(IndentPrinter
the IndentPrinter to use
out
)

Sends markup to the given IndentPrinter.

Method Summary

Modifier and TypeMethod and Description
protected Object
protected Object
protected Object
createNode(Object name, Map<K, V> attributes, Object value)

Implements abstract groovy.util.BuilderSupport.createNode.

protected Object
createNode(Object name, Map<K, V> attributes)

Implements abstract groovy.util.BuilderSupport.createNode.

private String

Returns:

A new string in which all characters that require escaping have been replaced with the corresponding XML entities.
escapeAttributeValue
(String
The string to escape.
value
)

Escapes a string so that it can be used directly as an XML attribute value.

private String

Returns:

A new string in which all characters that require escaping have been replaced with the corresponding XML entities.
escapeElementContent
(String
The string to escape.
value
)

Escapes a string so that it can be used directly in XML element content.

private String

Returns:

A new string in which all characters that require escaping have been replaced with the corresponding XML entities.
escapeXmlValue
(String
The string to escape.
value
,
boolean
true if the string is to be used as an attribute value, otherwise false.
isAttrValue
)

Escapes a string so that it can be used in XML text successfully.

public List<Function<Character, Optional<String>>>
public boolean

Returns:

true if double quotes are used for attributes
getDoubleQuotes
()

Returns true if attribute values are output with double quotes; false if single quotes are used.

public MarkupBuilderHelper

Returns:

this MarkupBuilder
getMkp
()

Property that may be called from within your builder closure to access helper methods, namely MarkupBuilderHelper#yield(String), MarkupBuilderHelper#yieldUnescaped(String), MarkupBuilderHelper#pi(Map), MarkupBuilderHelper#xmlDeclaration(Map) and MarkupBuilderHelper#comment(String).

protected Object
getName(String
the name of the desired method
methodName
)

Overrides groovy.util.BuilderSupport.getName.

A hook to allow names to be converted into some other object such as a QName in XML or ObjectName in JMX.
private static Object
protected IndentPrinter
public boolean

Returns:

the escapeAttributes property value
isEscapeAttributes
()

Returns the escapeAttributes property value.

public boolean

Returns:

true, if empty elements will be represented by an opening tag followed immediately by a closing tag.
isExpandEmptyElements
()

Whether empty elements are expanded from <tagName/> to <tagName></tagName>.

public boolean

Returns:

true, if empty attributes will be removed from the resulting markup.
isOmitEmptyAttributes
()

Determine whether empty attributes will appear in the produced markup.

public boolean

Returns:

true, if null attributes will be removed from the resulting markup.
isOmitNullAttributes
()

Determine whether null attributes will appear in the produced markup.

protected void
nodeCompleted(Object
the parent of the node being processed
parent
,
Object
the current node being processed
node
)

Overrides groovy.util.BuilderSupport.nodeCompleted.

A hook to allow nodes to be processed once they have had all of their children applied.
pack-priv void
pi(Map<String, Map<String, Object>>
a map with a single entry whose key is the name of the processing instruction and whose value is the attributes for the processing instruction.
args
)

Produce an XML processing instruction in the output.

public void
public void
setDoubleQuotes(boolean
If this parameter is true, double quotes are used; otherwise, single quotes are.
useDoubleQuotes
)

Sets whether the builder outputs attribute values in double quotes or single quotes.

public void
setEscapeAttributes(boolean
the new value
escapeAttributes
)

Defaults to true.

public void
setExpandEmptyElements(boolean
if true, empty elements will be represented by an opening tag followed immediately by a closing tag. Defaults to false.
expandEmptyElements
)

Whether empty elements are expanded from <tagName/> to <tagName></tagName>.

public void
setOmitEmptyAttributes(boolean
if true, empty attributes will not be included in the resulting markup. Defaults to false.
omitEmptyAttributes
)

Allows empty attributes to be removed from the generated markup.

public void
setOmitNullAttributes(boolean
if true, null attributes will not be included in the resulting markup. If false null attributes will be included in the markup as empty strings regardless of the omitEmptyAttribute setting. Defaults to false.
omitNullAttributes
)

Allows null attributes to be removed from the generated markup.

protected void
setParent(Object parent, Object child)

Implements abstract groovy.util.BuilderSupport.setParent.

private void
toState(int next, Object name)

pack-priv void
yield(String value, boolean escaping)

Inherited from groovy.util.BuilderSupport:
doInvokeMethodgetCurrentinvokeMethodinvokeMethodpostNodeCompletionsetClosureDelegatesetCurrent

Field Detail

additionalFiltersback to summary
private List<Function<Character, Optional<String>>> additionalFilters
escapeAttributesback to summary
private boolean escapeAttributes
expandEmptyElementsback to summary
private boolean expandEmptyElements
nodeIsEmptyback to summary
private boolean nodeIsEmpty
nospaceback to summary
private boolean nospace
omitEmptyAttributesback to summary
private boolean omitEmptyAttributes
omitNullAttributesback to summary
private boolean omitNullAttributes
outback to summary
private IndentPrinter out
stateback to summary
private int state
useDoubleQuotesback to summary
private boolean useDoubleQuotes

Constructor Detail

MarkupBuilderback to summary
public MarkupBuilder()

Prints markup to System.out

See Also
IndentPrinter#IndentPrinter()
MarkupBuilderback to summary
public MarkupBuilder(PrintWriter pw)

Sends markup to the given PrintWriter

Parameters
pw:PrintWriter

the PrintWriter to use

See Also
IndentPrinter#IndentPrinter(Writer)
MarkupBuilderback to summary
public MarkupBuilder(Writer writer)

Sends markup to the given Writer but first wrapping it in a PrintWriter

Parameters
writer:Writer

the writer to use

See Also
IndentPrinter#IndentPrinter(Writer)
MarkupBuilderback to summary
public MarkupBuilder(IndentPrinter out)

Sends markup to the given IndentPrinter. Use this option if you want to customize the indent used or provide your own IndentPrinter.

Parameters
out:IndentPrinter

the IndentPrinter to use

Method Detail

createNodeback to summary
protected Object createNode(Object name)

Implements abstract groovy.util.BuilderSupport.createNode.

createNodeback to summary
protected Object createNode(Object name, Object value)

Implements abstract groovy.util.BuilderSupport.createNode.

createNodeback to summary
protected Object createNode(Object name, Map<K, V> attributes, Object value)

Implements abstract groovy.util.BuilderSupport.createNode.

createNodeback to summary
protected Object createNode(Object name, Map<K, V> attributes)

Implements abstract groovy.util.BuilderSupport.createNode.

escapeAttributeValueback to summary
private String escapeAttributeValue(String value)

Escapes a string so that it can be used directly as an XML attribute value.

Parameters
value:String

The string to escape.

Returns:String

A new string in which all characters that require escaping have been replaced with the corresponding XML entities.

See Also
escapeXmlValue(String, boolean)
escapeElementContentback to summary
private String escapeElementContent(String value)

Escapes a string so that it can be used directly in XML element content.

Parameters
value:String

The string to escape.

Returns:String

A new string in which all characters that require escaping have been replaced with the corresponding XML entities.

See Also
escapeXmlValue(String, boolean)
escapeXmlValueback to summary
private String escapeXmlValue(String value, boolean isAttrValue)

Escapes a string so that it can be used in XML text successfully. It replaces the following characters with the corresponding XML entities:

  • & as &amp;
  • < as &lt;
  • > as &gt;
If the string is to be added as an attribute value, these characters are also escaped:
  • ' as &apos;
Parameters
value:String

The string to escape.

isAttrValue:boolean

true if the string is to be used as an attribute value, otherwise false.

Returns:String

A new string in which all characters that require escaping have been replaced with the corresponding XML entities.

getAdditionalFiltersback to summary
public List<Function<Character, Optional<String>>> getAdditionalFilters()
getDoubleQuotesback to summary
public boolean getDoubleQuotes()

Returns true if attribute values are output with double quotes; false if single quotes are used. By default, single quotes are used.

Returns:boolean

true if double quotes are used for attributes

getMkpback to summary
public MarkupBuilderHelper getMkp()

Property that may be called from within your builder closure to access helper methods, namely MarkupBuilderHelper#yield(String), MarkupBuilderHelper#yieldUnescaped(String), MarkupBuilderHelper#pi(Map), MarkupBuilderHelper#xmlDeclaration(Map) and MarkupBuilderHelper#comment(String).

Returns:MarkupBuilderHelper

this MarkupBuilder

getNameback to summary
protected Object getName(String methodName)

Overrides groovy.util.BuilderSupport.getName.

Doc from groovy.util.BuilderSupport.getName.

A hook to allow names to be converted into some other object such as a QName in XML or ObjectName in JMX.

Parameters
methodName:String

the name of the desired method

Returns:Object

the object representing the name

getNameback to summary
private static Object getName(Object name)
getPrinterback to summary
protected IndentPrinter getPrinter()
isEscapeAttributesback to summary
public boolean isEscapeAttributes()

Returns the escapeAttributes property value.

Returns:boolean

the escapeAttributes property value

See Also
setEscapeAttributes(boolean)
isExpandEmptyElementsback to summary
public boolean isExpandEmptyElements()

Whether empty elements are expanded from <tagName/> to <tagName></tagName>.

Returns:boolean

true, if empty elements will be represented by an opening tag followed immediately by a closing tag.

isOmitEmptyAttributesback to summary
public boolean isOmitEmptyAttributes()

Determine whether empty attributes will appear in the produced markup.

Returns:boolean

true, if empty attributes will be removed from the resulting markup.

isOmitNullAttributesback to summary
public boolean isOmitNullAttributes()

Determine whether null attributes will appear in the produced markup.

Returns:boolean

true, if null attributes will be removed from the resulting markup.

nodeCompletedback to summary
protected void nodeCompleted(Object parent, Object node)

Overrides groovy.util.BuilderSupport.nodeCompleted.

Doc from groovy.util.BuilderSupport.nodeCompleted.

A hook to allow nodes to be processed once they have had all of their children applied.

Parameters
parent:Object

the parent of the node being processed

node:Object

the current node being processed

piback to summary
pack-priv void pi(Map<String, Map<String, Object>> args)

Produce an XML processing instruction in the output. For example:

mkp.pi("xml-stylesheet":[href:"mystyle.css", type:"text/css"])
Parameters
args:Map<String, Map<String, Object>>

a map with a single entry whose key is the name of the processing instruction and whose value is the attributes for the processing instruction.

setAdditionalFiltersback to summary
public void setAdditionalFilters(List<Function<Character, Optional<String>>> additionalFilters)
setDoubleQuotesback to summary
public void setDoubleQuotes(boolean useDoubleQuotes)

Sets whether the builder outputs attribute values in double quotes or single quotes.

Parameters
useDoubleQuotes:boolean

If this parameter is true, double quotes are used; otherwise, single quotes are.

setEscapeAttributesback to summary
public void setEscapeAttributes(boolean escapeAttributes)

Defaults to true. If set to false then you must escape any special characters within attribute values such as '&', '<', CR/LF, single and double quotes etc. manually as needed. The builder will not guard against producing invalid XML when in this mode and the output may not be able to be parsed/round-tripped but it does give you full control when producing for instance HTML output.

Parameters
escapeAttributes:boolean

the new value

setExpandEmptyElementsback to summary
public void setExpandEmptyElements(boolean expandEmptyElements)

Whether empty elements are expanded from <tagName/> to <tagName></tagName>.

Parameters
expandEmptyElements:boolean

if true, empty elements will be represented by an opening tag followed immediately by a closing tag. Defaults to false.

setOmitEmptyAttributesback to summary
public void setOmitEmptyAttributes(boolean omitEmptyAttributes)

Allows empty attributes to be removed from the generated markup.

Parameters
omitEmptyAttributes:boolean

if true, empty attributes will not be included in the resulting markup. Defaults to false.

setOmitNullAttributesback to summary
public void setOmitNullAttributes(boolean omitNullAttributes)

Allows null attributes to be removed from the generated markup.

Parameters
omitNullAttributes:boolean

if true, null attributes will not be included in the resulting markup. If false null attributes will be included in the markup as empty strings regardless of the omitEmptyAttribute setting. Defaults to false.

setParentback to summary
protected void setParent(Object parent, Object child)

Implements abstract groovy.util.BuilderSupport.setParent.

toStateback to summary
private void toState(int next, Object name)
yieldback to summary
pack-priv void yield(String value, boolean escaping)
groovy.xml back to summary

public final Enum MarkupBuilder.CharFilter

extends Enum<MarkupBuilder.CharFilter>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final MarkupBuilder.CharFilter
public static final MarkupBuilder.CharFilter
public static final MarkupBuilder.CharFilter

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static MarkupBuilder.CharFilter
public static MarkupBuilder.CharFilter[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

NONEback to summary
public static final MarkupBuilder.CharFilter NONE
XML_ALLback to summary
public static final MarkupBuilder.CharFilter XML_ALL
XML_STRICTback to summary
public static final MarkupBuilder.CharFilter XML_STRICT

Constructor Detail

CharFilterback to summary
private CharFilter()

Method Detail

valueOfback to summary
public static MarkupBuilder.CharFilter valueOf(String name)
valuesback to summary
public static MarkupBuilder.CharFilter[] values()
groovy.xml back to summary

public Class MarkupBuilder.DefaultXmlEscapingFunction

extends Object
implements Function<Character, Optional<String>>
Class Inheritance
All Implemented Interfaces
java.util.function.Function

Field Summary

Modifier and TypeField and Description
private final Function<Character, Optional<String>>
private final boolean
private final Function<Character, Optional<String>>
private final Function<Character, Optional<String>>

Constructor Summary

AccessConstructor and Description
public
DefaultXmlEscapingFunction(boolean isAttrValue, boolean useDoubleQuotes)

Method Summary

Modifier and TypeMethod and Description
public Optional<String>
apply(Character
the function argument
ch
)

Implements java.util.function.Function.apply.

Applies this function to the given argument.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

attrFilterback to summary
private final Function<Character, Optional<String>> attrFilter
isAttrValueback to summary
private final boolean isAttrValue
quoteFilterback to summary
private final Function<Character, Optional<String>> quoteFilter
stdFilterback to summary
private final Function<Character, Optional<String>> stdFilter

Constructor Detail

DefaultXmlEscapingFunctionback to summary
public DefaultXmlEscapingFunction(boolean isAttrValue, boolean useDoubleQuotes)

Method Detail

applyback to summary
public Optional<String> apply(Character ch)

Implements java.util.function.Function.apply.

Doc from java.util.function.Function.apply.

Applies this function to the given argument.

Parameters
ch:Character

the function argument

Returns:Optional<String>

the function result