01: package com.jeta.forms.store.jml;
02:
03: import java.beans.BeanInfo;
04: import java.beans.Introspector;
05: import java.beans.PropertyDescriptor;
06: import java.lang.reflect.Method;
07:
08: import com.jeta.forms.store.jml.dom.JMLDocument;
09: import com.jeta.forms.store.jml.dom.JMLNode;
10:
11: public class JMLBeanSerializer implements JMLSerializer {
12:
13: /**
14: * XMLDeserializer implementation <object classname="classname">
15: * <properties> <property name="propname"> </property> </properties>
16: * </object>
17: */
18: public JMLNode serialize(JMLDocument document, Object obj)
19: throws JMLException {
20:
21: try {
22: // XMLNode objnode = XMLUtils.createObjectNode2(document,
23: // obj.getClass().getName() );
24: JMLNode objnode = JMLUtils.createObjectNode(document, obj
25: .getClass().getName());
26: if (obj != null) {
27: JMLNode propsnode = JMLUtils
28: .createPropertiesNode(document);
29: objnode.appendChild(propsnode);
30:
31: BeanInfo info = Introspector
32: .getBeanInfo(obj.getClass());
33: PropertyDescriptor[] props = info
34: .getPropertyDescriptors();
35: Object[] params = new Object[0];
36: for (int index = 0; index < props.length; index++) {
37: String pname = "";
38: try {
39: PropertyDescriptor prop = props[index];
40: pname = prop.getName();
41: Method m = prop.getReadMethod();
42: Object pvalue = m.invoke(obj, params);
43: System.out
44: .println("XMLBeanSerializer: pname: "
45: + pname + " pvalue: "
46: + pvalue);
47: JMLNode pnode = JMLUtils.createPropertyNode(
48: document, pname, pvalue);
49: if (pnode != null)
50: propsnode.appendChild(pnode);
51: } catch (Exception e) {
52: System.out.println(e.getMessage()
53: + ". Unable to get property: " + pname
54: + " for bean: " + obj.getClass());
55: }
56: }
57: }
58: return objnode;
59: } catch (Exception e) {
60: throw new JMLException(e.getMessage());
61: }
62: }
63:
64: }
|