View Javadoc

1   package org.restafarian.core.utils;
2   
3   import java.io.StringWriter;
4   import java.sql.Timestamp;
5   import java.util.Date;
6   
7   import org.apache.commons.beanutils.ConvertUtils;
8   import org.apache.commons.beanutils.Converter;
9   import org.apache.commons.betwixt.io.BeanWriter;
10  import org.apache.commons.betwixt.strategy.ConvertUtilsObjectStringConverter;
11  
12  /***
13   * <p>A collection of static methods for using Betwixt.</p>
14   */
15  public class BetwixtTool {
16  
17  	/***
18  	 * <p>Converts a single Java bean to XML.</p>
19  	 * 
20  	 * @param bean the bean to convert
21  	 * @return the bean in XML format
22  	 */
23  	public static String toXml(Object bean) {
24  		return toXml(bean, null, true);
25  	}
26  
27  	/***
28  	 * <p>Converts a single Java bean to XML.</p>
29  	 * 
30  	 * @param bean the bean to convert
31  	 * @param xsl the URL for the XSL stylesheet for this bean
32  	 * @return the bean in XML format
33  	 */
34  	public static String toXml(Object bean, String xsl) {
35  		return toXml(bean, xsl, true);
36  	}
37  
38  	/***
39  	 * <p>Converts a single Java bean to XML.</p>
40  	 * 
41  	 * @param bean the bean to convert
42  	 * @param includeDeclaration when true, adds the XML declaration to the output
43  	 * @return the bean in XML format
44  	 */
45  	public static String toXml(Object bean, boolean includeDeclaration) {
46  		return toXml(bean, null, includeDeclaration);
47  	}
48  
49  	/***
50  	 * <p>Converts a single Java bean to XML.</p>
51  	 * 
52  	 * @param bean the bean to convert
53  	 * @param xsl the URL for the XSL stylesheet for this bean
54  	 * @param includeDeclaration when true, adds the XML declaration to the output
55  	 * @return the bean in XML format
56  	 */
57  	public static String toXml(Object bean, String xsl, boolean includeDeclaration) {
58  		// set up ISO 8601 date convertion
59  		Converter dateConverter = new ISO8601DateConverter();
60  		ConvertUtils.register(dateConverter, Date.class);
61  		ConvertUtils.register(dateConverter, Timestamp.class);
62  		ConvertUtils.register(dateConverter, String.class);
63  
64  		// set up XML bean writer
65  		StringWriter out = new StringWriter();
66  		BeanWriter writer = new BeanWriter(out);
67  		writer.enablePrettyPrint();
68  		writer.setInitialIndentLevel(0);
69  		writer.getBindingConfiguration().setMapIDs(false);
70  		writer.getBindingConfiguration().setObjectStringConverter(new ConvertUtilsObjectStringConverter());
71  
72  		// create XML using bean writer
73  		String xml = "";
74  		try {
75  			if (includeDeclaration) {
76  				writer.writeXmlDeclaration("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
77  				if (xsl != null && !"".equals(xsl.trim())) {
78  					writer.writeXmlDeclaration("<?xml-stylesheet type=\"text/xsl\" href=\"" + xsl + "\"?>");
79  				}
80  			}
81  			writer.write(bean);
82  			out.flush();
83  			xml = out.toString();
84  		} catch (Exception e) {
85  			e.printStackTrace();
86  		}
87  
88  		return xml;
89  	}
90  }