1 22 package org.jboss.xb.binding; 23 24 25 import org.jboss.logging.Logger; 26 import org.jboss.util.Classes; 27 import org.xml.sax.Attributes ; 28 29 import java.lang.reflect.Method ; 30 import java.util.Collection ; 31 import java.util.List ; 32 import java.util.ArrayList ; 33 import java.util.Set ; 34 import java.util.HashSet ; 35 import java.util.Locale ; 36 import java.text.SimpleDateFormat ; 37 38 44 public class SchemalessObjectModelFactory 45 implements GenericObjectModelFactory 46 { 47 private static final Logger log = Logger.getLogger(SchemalessObjectModelFactory.class); 48 49 public Object newChild(Object parent, 50 UnmarshallingContext navigator, 51 String namespaceURI, 52 String localName, 53 Attributes attrs) 54 { 55 Object child = null; 56 try 57 { 58 if(parent instanceof Collection ) 59 { 60 if(!localName.equals(java.lang.String .class.getName())) 61 { 62 Class itemClass = Thread.currentThread().getContextClassLoader().loadClass(localName); 63 child = itemClass.newInstance(); 64 ((Collection )parent).add(child); 65 } 66 } 67 else 68 { 69 Method getter = parent.getClass().getMethod("get" + localName, null); 70 if(!SchemalessMarshaller.isAttributeType(getter.getReturnType())) 71 { 72 if(List .class.isAssignableFrom(getter.getReturnType())) 73 { 74 child = new ArrayList (); 75 } 76 else if(Set .class.isAssignableFrom(getter.getReturnType())) 77 { 78 child = new HashSet (); 79 } 80 else if(Collection .class.isAssignableFrom(getter.getReturnType())) 81 { 82 child = new ArrayList (); 83 } 84 else 85 { 86 child = getter.getReturnType().newInstance(); 87 } 88 } 89 90 if(child != null) 91 { 92 Method setter = Classes.getAttributeSetter(parent.getClass(), localName, getter.getReturnType()); 93 setter.invoke(parent, new Object []{child}); 94 } 95 } 96 } 97 catch(NoSuchMethodException e) 98 { 99 log.error("Failed to get getter/setter method for " + localName + " from " + parent.getClass(), e); 100 throw new IllegalStateException ("Failed to get getter/setter method for " + 101 localName + 102 " from " + 103 parent.getClass() + 104 ": " + 105 e.getMessage() 106 ); 107 } 108 catch(Exception e) 109 { 110 log.error("Failed to instantiate child", e); 111 throw new IllegalStateException ("Failed to instantiate child: " + e.getMessage()); 112 } 113 return child; 114 } 115 116 public void addChild(Object parent, 117 Object child, 118 UnmarshallingContext navigator, 119 String namespaceURI, 120 String localName) 121 { 122 } 123 124 public void setValue(Object o, UnmarshallingContext navigator, String namespaceURI, String localName, String value) 125 { 126 try 127 { 128 if(o instanceof Collection ) 129 { 130 if(localName.equals(java.lang.String .class.getName())) 131 { 132 ((Collection )o).add(value); 133 } 134 } 135 else 136 { 137 Method getter = Classes.getAttributeGetter(o.getClass(), localName); 138 Method setter = Classes.getAttributeSetter(o.getClass(), localName, getter.getReturnType()); 139 140 Object fieldValue; 141 if(java.util.Date .class.isAssignableFrom(getter.getReturnType())) 142 { 143 SimpleDateFormat formatter = new SimpleDateFormat ("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US); 144 fieldValue = formatter.parse(value); 145 } 146 else 147 { 148 fieldValue = value; 149 } 150 151 setter.invoke(o, new Object []{fieldValue}); 152 } 153 } 154 catch(NoSuchMethodException e) 155 { 156 throw new IllegalStateException ("Failed to discover getter/setter for " + localName + " in " + o); 157 } 158 catch(Exception e) 159 { 160 throw new IllegalStateException ("Failed to set value for " + localName + " in " + o); 161 } 162 } 163 164 public Object newRoot(Object root, 165 UnmarshallingContext navigator, 166 String namespaceURI, 167 String localName, 168 Attributes attrs) 169 { 170 Class rootClass; 171 try 172 { 173 rootClass = Thread.currentThread().getContextClassLoader().loadClass(localName); 174 } 175 catch(ClassNotFoundException e) 176 { 177 log.error("Faile to load root class " + localName, e); 178 throw new IllegalStateException ("Failed to load root class: " + localName + ": " + e.getMessage()); 179 } 180 181 try 182 { 183 root = rootClass.newInstance(); 184 } 185 catch(Exception e) 186 { 187 log.error("Failed to create an instance of root " + localName, e); 188 throw new IllegalStateException ("Failed to create an instance of root " + localName + ": " + e.getMessage()); 189 } 190 191 return root; 192 } 193 194 public Object completeRoot(Object root, UnmarshallingContext navigator, String namespaceURI, String localName) 195 { 196 return root; 197 } 198 } 199 | Popular Tags |