alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (StreamingMarkupBuilder.groovy)

This example Groovy source code file (StreamingMarkupBuilder.groovy) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

abstractstreamingbuilder, basemarkupbuilder, buildable, buildable, closure, closure, groovyruntimeexception, groovyruntimeexception, map, namespace, streamingmarkupbuilder, streamingmarkupbuilder, streamingmarkupwriter, uri

The Groovy StreamingMarkupBuilder.groovy source code

/*
 * Copyright 2003-2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package groovy.xml

import groovy.xml.streamingmarkupsupport.AbstractStreamingBuilder
import groovy.xml.streamingmarkupsupport.StreamingMarkupWriter
import groovy.xml.streamingmarkupsupport.BaseMarkupBuilder

/**
 * <p>A builder class for creating XML markup.  This implementation uses a 
 * {@link StreamingMarkupWriter} to handle output.</p>
 * 
 * <p>Example:

* <pre>System.out << new StreamingMarkupBuilder().bind { * root { * a( a1:'one' ) { * b { mkp.yield( '3 < 5' ) } * c( a2:'two', 'blah' ) * } * } * }</pre> * Will output the following String, without newlines or indentation: * <pre><root> * <a a1='one'> * <b>3 &lt; 5</b> * <c a2='two'>blah</c> * </a> * </root></pre> * Notes: * <ul> * <li>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 'yield', 'pi', 'comment', 'out', * 'namespaces', 'xmlDeclaration' and 'yieldUnescaped'. * </li> * <li>Note that tab, newline and carriage return characters are escaped within attributes, i.e. will become , and respectively * </ul> */ class StreamingMarkupBuilder extends AbstractStreamingBuilder { boolean useDoubleQuotes = false boolean expandEmptyElements = false def getQt() { useDoubleQuotes ? '"' : "'" } def pendingStack = [] /** * Invoked by calling <code>mkp.comment */ def commentClosure = {doc, pendingNamespaces, namespaces, namespaceSpecificTags, prefix, attrs, body, out -> out.unescaped() << "