Groovy 2.2.0

groovy.xml
[Java] Class MarkupBuilder

java.lang.Object
  groovy.lang.GroovyObjectSupport
      groovy.util.BuilderSupport
          groovy.xml.MarkupBuilder

public class MarkupBuilder
extends BuilderSupport

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:
Authors:
James Strachan
Stefan Matthias Aust
Scott Stirling
Paul King


Constructor Summary
MarkupBuilder()

Prints markup to System.out

MarkupBuilder(PrintWriter pw)

Sends markup to the given PrintWriter

MarkupBuilder(Writer writer)

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

MarkupBuilder(IndentPrinter out)

Sends markup to the given IndentPrinter.

 
Method Summary
protected Object createNode(Object name)

protected Object createNode(Object name, Object value)

protected Object createNode(Object name, Map attributes, Object value)

protected Object createNode(Object name, Map attributes)

boolean getDoubleQuotes()

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

Object getMkp()

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

protected Object getName(String methodName)

protected IndentPrinter getPrinter()

boolean isEscapeAttributes()

Returns the escapeAttributes property value.

boolean isExpandEmptyElements()

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

boolean isOmitEmptyAttributes()

Determine whether empty attributes will appear in the produced markup.

boolean isOmitNullAttributes()

Determine whether null attributes will appear in the produced markup.

protected void nodeCompleted(Object parent, Object node)

protected void print(Object node)

void setDoubleQuotes(boolean useDoubleQuotes)

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

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.

void setExpandEmptyElements(boolean expandEmptyElements)

Whether empty elements are expanded from to .

void setOmitEmptyAttributes(boolean omitEmptyAttributes)

Allows empty attributes to be removed from the generated markup.

void setOmitNullAttributes(boolean omitNullAttributes)

Allows null attributes to be removed from the generated markup.

protected void setParent(Object parent, Object child)

 
Methods inherited from class BuilderSupport
createNode, createNode, createNode, createNode, doInvokeMethod, getCurrent, getName, invokeMethod, invokeMethod, nodeCompleted, postNodeCompletion, setClosureDelegate, setCurrent, setParent
 
Methods inherited from class GroovyObjectSupport
getMetaClass, getProperty, invokeMethod, setMetaClass, setProperty
 
Methods inherited from class Object
wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll
 

Constructor Detail

MarkupBuilder

public MarkupBuilder()
Prints markup to System.out
See Also:
IndentPrinter.IndentPrinter


MarkupBuilder

public MarkupBuilder(PrintWriter pw)
Sends markup to the given PrintWriter
Parameters:
pw - the PrintWriter to use
See Also:
IndentPrinter.IndentPrinter


MarkupBuilder

public MarkupBuilder(Writer writer)
Sends markup to the given Writer but first wrapping it in a PrintWriter
Parameters:
writer - the writer to use
See Also:
IndentPrinter.IndentPrinter


MarkupBuilder

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 - the IndentPrinter to use


 
Method Detail

createNode

protected Object createNode(Object name)


createNode

protected Object createNode(Object name, Object value)


createNode

protected Object createNode(Object name, Map attributes, Object value)


createNode

protected Object createNode(Object name, Map attributes)


getDoubleQuotes

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:
true if double quotes are used for attributes


getMkp

public Object getMkp()
Property that may be called from within your builder closure to access helper methods, namely MarkupBuilderHelper.yield, MarkupBuilderHelper.yieldUnescaped, MarkupBuilderHelper.pi, MarkupBuilderHelper.xmlDeclaration and MarkupBuilderHelper.comment.
Returns:
this MarkupBuilder


getName

protected Object getName(String methodName)


getPrinter

protected IndentPrinter getPrinter()


isEscapeAttributes

public boolean isEscapeAttributes()
Returns the escapeAttributes property value.
Returns:
the escapeAttributes property value
See Also:
setEscapeAttributes(boolean)


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.


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.


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.


nodeCompleted

protected void nodeCompleted(Object parent, Object node)


print

protected void print(Object node)


setDoubleQuotes

public void setDoubleQuotes(boolean useDoubleQuotes)
Sets whether the builder outputs attribute values in double quotes or single quotes.
Parameters:
useDoubleQuotes - If this parameter is true, double quotes are used; otherwise, single quotes are.


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


setExpandEmptyElements

public void setExpandEmptyElements(boolean expandEmptyElements)
Whether empty elements are expanded from to .
Parameters:
expandEmptyElements - if true, empty elements will be represented by an opening tag followed immediately by a closing tag. Defaults to false.


setOmitEmptyAttributes

public void setOmitEmptyAttributes(boolean omitEmptyAttributes)
Allows empty attributes to be removed from the generated markup.
Parameters:
omitEmptyAttributes - if true, empty attributes will not be included in the resulting markup. Defaults to false.


setOmitNullAttributes

public void setOmitNullAttributes(boolean omitNullAttributes)
Allows null attributes to be removed from the generated markup.
Parameters:
omitNullAttributes - 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.


setParent

protected void setParent(Object parent, Object child)


 

Copyright &copy; 2003-2013 The Codehaus. All rights reserved.