1 package org.sapia.util.xml.idefix.serializer; 2 3 4 import org.sapia.util.xml.Namespace; 6 import org.sapia.util.xml.idefix.SerializationContext; 7 import org.sapia.util.xml.idefix.SerializationException; 8 import org.sapia.util.xml.idefix.SerializerIF; 9 import org.sapia.util.xml.idefix.SerializerNotFoundException; 10 11 import java.lang.reflect.InvocationTargetException ; 12 import java.lang.reflect.Method ; 13 import java.lang.reflect.Modifier ; 14 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 20 31 public class DynamicSerializer implements SerializerIF { 32 33 private static final List EMPTY_LIST = new ArrayList (0); 34 35 36 private static final Class [] EMPTY_CLASS_ARRAY = new Class [0]; 37 38 39 40 42 43 46 public DynamicSerializer() { 47 super(); 48 } 49 50 57 public void serialize(Object anObject, SerializationContext aContext) 58 throws SerializationException { 59 String aRootElementName = getObjectName(anObject.getClass()); 61 62 Namespace aRootNamespace = aContext.getNamespaceFactory().getNamespaceFor(anObject.getClass()); 64 65 serialize(anObject, aRootNamespace, aRootElementName, aContext); 67 } 68 69 79 public void serialize(Object anObject, Namespace aNamespace, 80 String anObjectName, SerializationContext aContext) 81 throws SerializationException { 82 aContext.getXmlBuffer().addNamespace(aNamespace.getURI(), 84 aNamespace.getPrefix()); 85 aContext.getXmlBuffer().startElement(aNamespace.getURI(), anObjectName); 86 87 Method aMethod = null; 88 89 try { 90 List someMethods = getMatchingMethods(anObject); 92 93 for (Iterator it = someMethods.iterator(); it.hasNext();) { 95 aMethod = (Method ) it.next(); 96 97 Object aMethodValue = aMethod.invoke(anObject, EMPTY_CLASS_ARRAY); 99 100 if (aMethodValue != null) { 101 String aMethodName = getObjectName(aMethod); 103 104 Namespace aMethodNamespace = aContext.getNamespaceFactory() 106 .getNamespaceFor(aMethodValue.getClass()); 107 108 SerializerIF aSerializer = aContext.getSerializerFactory() 110 .getSerializer(aMethodValue.getClass()); 111 112 aSerializer.serialize(aMethodValue, aMethodNamespace, aMethodName, 114 aContext); 115 } 116 } 117 } catch (IllegalAccessException iae) { 118 String aMessage = "Unable to extract the value of the method: " + 119 aMethod; 120 121 throw new SerializationException(aMessage, iae); 123 } catch (InvocationTargetException ite) { 124 String aMessage = "Error calling the method: " + aMethod; 125 126 throw new SerializationException(aMessage, ite); 128 } catch (SerializerNotFoundException snfe) { 129 String aMessage = "Unable to found a serializer for the method: " + 130 aMethod; 131 132 throw new SerializationException(aMessage, snfe); 134 } 135 136 aContext.getXmlBuffer().endElement(aNamespace.getURI(), anObjectName); 138 aContext.getXmlBuffer().removeNamespace(aNamespace.getURI()); 139 } 140 141 148 private List getMatchingMethods(Object anObject) { 149 if (anObject == null) { 150 return EMPTY_LIST; 151 } 152 153 Class aClass = anObject.getClass(); 154 Method [] allMethods = aClass.getMethods(); ArrayList someMatchingMethods = new ArrayList (allMethods.length); 156 157 for (int i = 0; i < allMethods.length; i++) { 158 Method aMethod = allMethods[i]; 159 160 if ((aMethod.getName().startsWith("get") || aMethod.getName().startsWith("is")) && !aMethod.getName().equals("getClass") && !Modifier.isStatic(aMethod.getModifiers()) && (aMethod.getParameterTypes().length == 0) && (aMethod.getReturnType() != Void.TYPE)) { someMatchingMethods.add(aMethod); 167 } 168 } 169 170 return someMatchingMethods; 171 } 172 173 178 private String getObjectName(Class aClass) { 179 String aQualifiedClassName = aClass.getName(); 180 String aPackageName = aClass.getPackage().getName(); 181 String aLocalClassName = aQualifiedClassName.substring(aPackageName.length() + 182 1); 183 184 return aLocalClassName; 185 } 186 187 192 private String getObjectName(Method aMethod) { 193 int aPrefixLength = 0; 194 String aMethodName = aMethod.getName(); 195 196 if (aMethodName.startsWith("get")) { 197 aPrefixLength = 3; 198 } else if (aMethodName.startsWith("is")) { 199 aPrefixLength = 2; 200 } else { 201 throw new IllegalArgumentException ( 202 "The method name does not starts with \'get\' or \'is\'"); 203 } 204 205 String anObjectName = SerializerHelper.firstToUpperFromIndex(aMethodName, 206 aPrefixLength); 207 208 return anObjectName; 209 } 210 } 211 | Popular Tags |