1 package org.catacomb.serial; 2 3 import org.catacomb.interlish.structure.Element; 4 import org.catacomb.interlish.structure.ElementWriter; 5 import org.catacomb.interlish.structure.Elementizer; 6 import org.catacomb.report.E; 7 import org.catacomb.serial.om.OmElement; 8 9 10 import java.lang.reflect.Array; 11 import java.util.ArrayList; 12 import java.util.List; 13 import java.util.Map; 14 15 16 public class OmElementizer implements Elementizer { 17 18 SerializationContext ctxt; 19 20 21 public OmElementizer() { 22 ctxt = new SerializationContext(); 23 } 24 25 26 public OmElementizer(SerializationContext sc) { 27 ctxt = sc; 28 } 29 30 public SerializationContext getContext() { 31 return ctxt; 32 } 33 34 public Element getElement(Object obj) { 35 return makeElement(obj); 36 } 37 38 39 public Element getFlatElement(Object obj) { 40 ctxt.setPrimitivesOnly(); 41 return makeElement(obj); 42 } 43 44 45 public Element makeElement(Object objin) { 46 Object obj = objin; 47 48 Element retelt = null; 49 50 if (obj instanceof char[]) { 51 obj = new String((char[])obj); 52 } 53 54 if (obj instanceof String) { 55 retelt = makeStringElement((String)obj); 56 57 } else if (obj instanceof int[]) { 58 retelt = makeIntArrayElement((int[])obj); 59 60 } else if (obj instanceof double[]) { 61 retelt = makeDoubleArrayElement((double[])obj); 62 63 } else if (obj instanceof List) { 64 retelt = makeListElement((List)obj); 65 66 } else if (obj != null && obj.getClass().isArray()) { 67 retelt = makeArrayElement(obj); 68 69 } else if (obj instanceof Map) { 70 E.error("Elementizer asked to elementize a raw map - shouldn't happen"); 71 72 } else if (obj != null) { 73 retelt = makeObjectElement(obj); 74 } 75 return retelt; 76 } 77 78 79 80 private Element makeStringElement(String s) { 81 OmElement elt = new OmElement("String"); 82 elt.addAttribute("value", s); 83 return elt; 84 } 85 86 87 private Element makeIntArrayElement(int[] ia) { 88 OmElement elt = new OmElement("IntegerArray"); 89 elt.addAttribute("values", SerialUtil.stringify(ia)); 90 return elt; 91 } 92 93 94 private Element makeDoubleArrayElement(double[] da) { 95 OmElement elt = new OmElement("DoubleArray"); 96 elt.addAttribute("values", SerialUtil.stringify(da)); 97 return elt; 98 } 99 100 101 102 private Element makeListElement(List list) { 103 OmElement listelt = new OmElement(); 104 105 for (Object listobj : list) { 106 107 Element elt = makeElement(listobj); 108 109 listelt.addElement(elt); 110 } 111 return listelt; 112 } 113 114 115 116 117 private Element makeArrayElement(Object arr) { 118 ArrayList<Object> arl = new ArrayList<Object>(); 119 try { 120 int nel = Array.getLength(arr); 121 for (int i = 0; i < nel; i++) { 122 Object listobj = Array.get(arr, i); 123 if (listobj != null) { 124 arl.add(listobj); 125 } 126 } 127 } catch (Exception ex) { 128 System.out.println("ERROR - sand Reflector cant handle " + arr + " " + ex); 129 } 130 return makeListElement(arl); 131 } 132 133 134 135 private Element makeObjectElement(Object obj) { 136 Element ret = null; 137 if (obj instanceof ElementWriter) { 138 ret = ((ElementWriter)obj).makeElement(ctxt.getElementFactory(), this); 139 140 } else { 141 ret = Reflector.makeObjectElementByReflection(obj, this); 142 } 143 return ret; 144 145 } 146 147 public Element elementize(Object obj) { 148 return makeObjectElement(obj); 149 } 150 151 }