Upload Your Videos

Friday, February 6, 2009

Betwixt Multi Mapping Example

Betwixt Multi Mapping Example
Betwixt is a dynamic, start-from-java XML-object binder. For those unfamiliar with these terms, this means that it maps XML <-> objects defining a binding from an existing group of java objects into xml (and back again) without the use of code generation.

Below is a simple betwixt configuration file. The code sniplet is self descriptive, the code in myClass.java shows how a simple java object is converted in to xml document based on the specification provided in the configuration file.

Betwixt-config.xml


<?xml version="1.0"?>
<betwixt-config>

<class name="com.syn.soft.DescBean">
<element name="addContent">
<element name="author" property="author" />
<element name="description" property="description"/>
<element name="title" property="title"/>
</element>
<element name="Bean" property="summaryBean"/>
</class>

<class name=" com.syn.soft.SummaryBean">
<element name="media">
<element name="summary" property="summary"/>
<element name="description" value="I am static breadcrumb"/>
</element>
</class>
</betwixt-config>


Test code:
Public class myClass {
public... main(String[] arg) {
myDescBean = new DescBean();
myDescBean.setTitle("testing my title");
....
...

XmlTransformer xmlTransformer = new XmlTransformer();
String xml = xmlTransformer.transform(myDescBean);
System.out.println(xml);
}
}

Bean Code:
Public class SummaryBean {
Private String summary, description;
// create all setter & getters..
}

Public class DescBean {
Private String author, description, title;
// create all setter & getters..
}

Transformation Code

XmlTransformer.java

public class XmlTransformer {
public static final String betwixtMappingFileName = "betwixt-config.xml";

public String transform(Object bean)throws Exception{
XMLSingleMappingFileIntrospector xmlIntrospector = new XMLSingleMappingFileIntrospector();
xmlIntrospector.setMappingFile(betwixtMappingFileName);
StringWriter stringWriter = new StringWriter();
BeanWriter beanWriter = BeanWriterFactory.getBeanWriter(stringWriter,xmlIntrospector);
beanWriter.writeXmlDeclaration(BeanWriterFactory.XML_DECLARATION);
beanWriter.write(bean);
String transformationResult = stringWriter.toString();
return transformationResult;
}
}

BeanWriterFactory.java
public class BeanWriterFactory {
public static final String XML_DECLARATION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

public static BeanWriter getBeanWriter(Writer writer, XMLIntrospector introSpector) {
BeanWriter beanWriter = new BeanWriter(writer);
if(introSpector != null) {
beanWriter.setXMLIntrospector(introSpector);
}
beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
beanWriter.setMixedContentEncodingStrategy(new WWEMixedContentEncodingStrategy());
BindingConfiguration bindingConfiguration = new BindingConfiguration();
bindingConfiguration.setMapIDs(false);
beanWriter.setBindingConfiguration(bindingConfiguration);
beanWriter.enablePrettyPrint();
beanWriter.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(false);
beanWriter.getXMLIntrospector().getConfiguration().setElementNameMapper(
new DecapitalizeNameMapper());
return beanWriter;
}

public static BeanWriter getBeanWriter(Writer writer) {
return getBeanWriter(writer, null);
}

}

References
http://commons.apache.org/betwixt/guide/tutorial.html
http://commons.apache.org/betwixt/index.html