Groovy 1.7.0

groovy.xml
Class MarkupBuilder

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

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>
Note that mkp is 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 for #getMkp() for further details.
author:
James Strachan
author:
Stefan Matthias Aust
author:
Scott Stirling
author:
Paul King
version:
$Revision: 18226 $


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 { { { { {

protected Object getName(String methodName)

protected IndentPrinter getPrinter()

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)

void pi(Map args)

Produce an XML processing instruction in the output.

protected void print(Object node)

void setDoubleQuotes(boolean useDoubleQuotes)

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

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)

protected String transformValue(String value)

Returns a String with special XML characters escaped as entities so that output XML is valid.

void yield(String value, boolean escaping)

 
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, hashCode, getClass, equals, toString, notify, notifyAll
 

Constructor Detail

MarkupBuilder

public MarkupBuilder()
Prints markup to System.out
see:
IndentPrinter#IndentPrinter()


MarkupBuilder

public MarkupBuilder(PrintWriter pw)
Sends markup to the given PrintWriter
param:
pw the PrintWriter to use
see:
IndentPrinter#IndentPrinter(PrintWriter)


MarkupBuilder

public MarkupBuilder(Writer writer)
Sends markup to the given Writer but first wrapping it in a PrintWriter
param:
writer the writer to use
see:
IndentPrinter#IndentPrinter(PrintWriter)


MarkupBuilder

public MarkupBuilder(IndentPrinter out)
Sends markup to the given IndentPrinter. Use this option if you want to customize the indent used.
param:
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.
return:
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(String), MarkupBuilderHelper#yieldUnescaped(String), MarkupBuilderHelper#pi(Map), MarkupBuilderHelper#xmlDeclaration(Map) and MarkupBuilderHelper#comment(String).
return:
this MarkupBuilder


getName

protected Object getName(String methodName)


getPrinter

protected IndentPrinter getPrinter()


isOmitEmptyAttributes

public boolean isOmitEmptyAttributes()
Determine whether empty attributes will appear in the produced markup.
return:
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.
return:
true, if null attributes will be removed from the resulting markup.


nodeCompleted

protected void nodeCompleted(Object parent, Object node)


pi

public void pi(Map args)
Produce an XML processing instruction in the output. For example:
 mkp.pi("xml-stylesheet":[href:"mystyle.css", type:"text/css"])
 
param:
args 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.


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.
param:
useDoubleQuotes If this parameter is true, double quotes are used; otherwise, single quotes are.


setOmitEmptyAttributes

public void setOmitEmptyAttributes(boolean omitEmptyAttributes)
Allows empty attributes to be removed from the generated markup.
param:
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.
param:
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)


transformValue

protected String transformValue(String value)
Returns a String with special XML characters escaped as entities so that output XML is valid. Escapes the following characters as corresponding entities:
param:
value to be searched and replaced for XML special characters.
return:
value with XML characters escaped
see:
#escapeXmlValue(String, boolean)
deprecated:


yield

public void yield(String value, boolean escaping)


 

Copyright © 2003-2009 The Codehaus. All rights reserved.