Package groovy.xml
Class MarkupBuilder
java.lang.Object
groovy.lang.GroovyObjectSupport
groovy.util.BuilderSupport
groovy.xml.MarkupBuilder
- All Implemented Interfaces:
GroovyObject
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 < 5</b>
<c a2='two'>blah</c>
</a>
</root>
Notes:
mkpis a special namespace used to escape away from the normal building mode of the builder and get access to helper markup methods such as 'yield' and 'yieldUnescaped'. See the javadoc forgetMkp()for further details.- Note that tab, newline and carriage return characters are escaped within attributes, i.e. will become 	, and respectively
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumIdentifiers for built-in character filtering strategies.static classDefault character replacement function used for XML content and attribute escaping. -
Constructor Summary
ConstructorsConstructorDescriptionPrints markup to System.outSends markup to the given IndentPrinter.Sends markup to the given PrintWriterMarkupBuilder(Writer writer) Sends markup to the given Writer but first wrapping it in a PrintWriter -
Method Summary
Modifier and TypeMethodDescriptionprotected ObjectcreateNode(Object name) Builder lifecycle callback that starts an element with no attributes or body text.protected ObjectcreateNode(Object name, Object value) Builder lifecycle callback that starts an element and writes an immediate text body.protected ObjectcreateNode(Object name, Map attributes) Builder lifecycle callback that starts an element and emits attributes without body text.protected ObjectcreateNode(Object name, Map attributes, Object value) Builder lifecycle callback that starts an element, emits attributes and optionally writes text.Returns extra character replacement filters consulted after the default XML escaping rules.booleanReturnstrueif attribute values are output with double quotes;falseif single quotes are used.getMkp()Property that may be called from within your builder closure to access helper methods, namelyMarkupBuilderHelper.yield(String),MarkupBuilderHelper.yieldUnescaped(String),MarkupBuilderHelper.pi(Map),MarkupBuilderHelper.xmlDeclaration(Map)andMarkupBuilderHelper.comment(String).protected ObjectResolves a builder method name to the element name that should be emitted.protected IndentPrinterReturns the printer used to emit markup.booleanReturns the escapeAttributes property value.booleanWhether empty elements are expanded from <tagName/> to <tagName></tagName>.booleanDetermine whether empty attributes will appear in the produced markup.booleanDetermine whether null attributes will appear in the produced markup.protected voidnodeCompleted(Object parent, Object node) Builder lifecycle callback invoked when the current node is complete.protected voidWrites raw markup fragments or names to the output stream.voidSets extra character replacement filters consulted after the default XML escaping rules.voidsetDoubleQuotes(boolean useDoubleQuotes) Sets whether the builder outputs attribute values in double quotes or single quotes.voidsetEscapeAttributes(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.voidsetExpandEmptyElements(boolean expandEmptyElements) Whether empty elements are expanded from <tagName/> to <tagName></tagName>.voidsetOmitEmptyAttributes(boolean omitEmptyAttributes) Allows empty attributes to be removed from the generated markup.voidsetOmitNullAttributes(boolean omitNullAttributes) Allows null attributes to be removed from the generated markup.protected voidBuilder lifecycle callback invoked after a child node has been created.Methods inherited from class groovy.util.BuilderSupport
doInvokeMethod, getCurrent, invokeMethod, invokeMethod, postNodeCompletion, setClosureDelegate, setCurrentMethods inherited from class groovy.lang.GroovyObjectSupport
getMetaClass, setMetaClassMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface groovy.lang.GroovyObject
getProperty, setProperty
-
Constructor Details
-
MarkupBuilder
public MarkupBuilder()Prints markup to System.out- See Also:
-
MarkupBuilder
Sends markup to the given PrintWriter- Parameters:
pw- the PrintWriter to use- See Also:
-
MarkupBuilder
Sends markup to the given Writer but first wrapping it in a PrintWriter- Parameters:
writer- the writer to use- See Also:
-
MarkupBuilder
Sends markup to the given IndentPrinter. Use this option if you want to customize the indent used or provide your own IndentPrinter.- Parameters:
out- the IndentPrinter to use
-
-
Method Details
-
getAdditionalFilters
Returns extra character replacement filters consulted after the default XML escaping rules.- Returns:
- the additional escaping filters, or
nullif none are configured
-
setAdditionalFilters
Sets extra character replacement filters consulted after the default XML escaping rules.- Parameters:
additionalFilters- the additional escaping filters to apply
-
isEscapeAttributes
public boolean isEscapeAttributes()Returns the escapeAttributes property value.- Returns:
- the escapeAttributes property value
- See Also:
-
setEscapeAttributes
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- the new value
-
getDoubleQuotes
public boolean getDoubleQuotes()Returnstrueif attribute values are output with double quotes;falseif single quotes are used. By default, single quotes are used.- Returns:
- true if double quotes are used for attributes
-
setDoubleQuotes
public void setDoubleQuotes(boolean useDoubleQuotes) Sets whether the builder outputs attribute values in double quotes or single quotes.- Parameters:
useDoubleQuotes- If this parameter istrue, double quotes are used; otherwise, single quotes are.
-
isOmitNullAttributes
public boolean isOmitNullAttributes()Determine whether null attributes will appear in the produced markup.- Returns:
true, if null attributes will be removed from the resulting markup.
-
setOmitNullAttributes
public void setOmitNullAttributes(boolean omitNullAttributes) Allows null attributes to be removed from the generated markup.- Parameters:
omitNullAttributes- iftrue, null attributes will not be included in the resulting markup. Iffalsenull attributes will be included in the markup as empty strings regardless of the omitEmptyAttribute setting. Defaults tofalse.
-
isOmitEmptyAttributes
public boolean isOmitEmptyAttributes()Determine whether empty attributes will appear in the produced markup.- Returns:
true, if empty attributes will be removed from the resulting markup.
-
setOmitEmptyAttributes
public void setOmitEmptyAttributes(boolean omitEmptyAttributes) Allows empty attributes to be removed from the generated markup.- Parameters:
omitEmptyAttributes- iftrue, empty attributes will not be included in the resulting markup. Defaults tofalse.
-
isExpandEmptyElements
public boolean isExpandEmptyElements()Whether empty elements are expanded from <tagName/> to <tagName></tagName>.- Returns:
true, if empty elements will be represented by an opening tag followed immediately by a closing tag.
-
setExpandEmptyElements
public void setExpandEmptyElements(boolean expandEmptyElements) Whether empty elements are expanded from <tagName/> to <tagName></tagName>.- Parameters:
expandEmptyElements- iftrue, empty elements will be represented by an opening tag followed immediately by a closing tag. Defaults tofalse.
-
getPrinter
Returns the printer used to emit markup. Subclasses may override to provide a custom output sink.- Returns:
- the printer backing this builder
-
setParent
Builder lifecycle callback invoked after a child node has been created. This implementation is a no-op because markup is streamed directly to the printer.- Specified by:
setParentin classBuilderSupport- Parameters:
parent- the current parent node markerchild- the newly created child node marker
-
getMkp
Property that may be called from within your builder closure to access helper methods, namelyMarkupBuilderHelper.yield(String),MarkupBuilderHelper.yieldUnescaped(String),MarkupBuilderHelper.pi(Map),MarkupBuilderHelper.xmlDeclaration(Map)andMarkupBuilderHelper.comment(String).- Returns:
- this MarkupBuilder
-
createNode
Builder lifecycle callback that starts an element with no attributes or body text.- Specified by:
createNodein classBuilderSupport- Parameters:
name- the node name requested by the builder- Returns:
- the normalized node name used for subsequent callbacks
-
createNode
Builder lifecycle callback that starts an element and writes an immediate text body.- Specified by:
createNodein classBuilderSupport- Parameters:
name- the node name requested by the buildervalue- the text body to emit, ornullto leave the element empty- Returns:
- the normalized node name used for subsequent callbacks
-
createNode
Builder lifecycle callback that starts an element, emits attributes and optionally writes text.- Specified by:
createNodein classBuilderSupport- Parameters:
name- the node name requested by the builderattributes- the attributes to emitvalue- the optional text body to emit- Returns:
- the normalized node name used for subsequent callbacks
-
createNode
Builder lifecycle callback that starts an element and emits attributes without body text.- Specified by:
createNodein classBuilderSupport- Parameters:
name- the node name requested by the builderattributes- the attributes to emit- Returns:
- the normalized node name used for subsequent callbacks
-
nodeCompleted
Builder lifecycle callback invoked when the current node is complete.- Overrides:
nodeCompletedin classBuilderSupport- Parameters:
parent- the parent node markernode- the completed node marker
-
print
Writes raw markup fragments or names to the output stream. Subclasses may override to customize low-level emission.- Parameters:
node- the value to print
-
getName
Resolves a builder method name to the element name that should be emitted. Subclasses may override to translate method names into alternate node names.- Overrides:
getNamein classBuilderSupport- Parameters:
methodName- the method name invoked on the builder- Returns:
- the node name to use for output
-