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