Outputing Results (Of Bean Writing)

Betwixt is flexible when it comes to outputing the results of bean writing. The abstract AbstractBeanWriter class provides a basic framework with an implementation API inspired by SAX . Betwixt ships with writers that support SAX and textual streams but others can be created with relative ease.

Pushing Results to a Textual Stream

BeanWriter push results to a textual stream. This can be used to efficiently push content through a socket or just to create a simple String. Note that only document fragments are created so you may need to append any prologs before writing the bean.

Content Encoding

When writing character data (the content between markup tags) to a textual stream, sections of character data can be processed in various ways by Betwixt with the aim of easily producing valid xml from beans. In particular, Betwixt assumes that bean's property values are plain java rather than pre-processed xml and so may contain characters that should be escaped.

There are two primary use cases for this processing:

  • Escaping the character data
  • Wrapping the entire section as CDATA

Betwixt provides a plug-in MixedCharacterEncodingStrategy and a property on BeanWriter which allows the processing to be varied. Factory constants are provided on MixedCharacterEncodingStrategy for the two common use cases above.

For example, to have all content wrapped in CDATA sections:

        BeanWriter writer = ... 
        writer.setMixedContentEncodingStrategy(MixedContentEncodingStrategy.CDATA);

By default, Betwixt uses character escaping only. However, the default strategy also supports per-property specification through setting the appropriate option . Setting the org.apache.commons.betwixt.mixed-content-encoding option to CDATA will instruct the default strategy to wrap the element's body text in a CDATA section. For example, the following betwixt file fragment encodes the some-property element's body text as CDATA (when the default strategy is used):


<?xml version='1.0'?>
<info primitiveTypes="attribute">
	<element name='some-bean'>
        ...
		<element name='some-property' property='someProperty'>
			<option>
				<name>org.apache.commons.betwixt.mixed-content-encoding</name>
				<value>CDATA</value>
			</option>
		</element>
        ...
	</element>
</info>

Pushing Results to SAX

SAXBeanWriter push results to a SAX content handler. This allows Betwixt to efficiently participate as a content generator in SAX-based pipelines such as cocoon .