1 22 package org.jboss.xb.binding; 23 24 import java.util.HashMap ; 25 import java.util.Map ; 26 import javax.xml.namespace.QName ; 27 import org.jboss.logging.Logger; 28 import org.jboss.util.Classes; 29 import org.jboss.xb.binding.introspection.FieldInfo; 30 31 35 public class MappingObjectModelProvider 36 implements GenericObjectModelProvider 37 { 38 private static final Logger log = Logger.getLogger(MappingObjectModelProvider.class); 39 40 private final Map classMappings = new HashMap (); 41 private final Map fieldMappings = new HashMap (); 42 private boolean ignoreLowLine = true; 43 private boolean ignoreNotFoundField = true; 44 45 public boolean isIgnoreNotFoundField() 46 { 47 return ignoreNotFoundField; 48 } 49 50 public void setIgnoreNotFoundField(boolean ignoreNotFoundField) 51 { 52 this.ignoreNotFoundField = ignoreNotFoundField; 53 } 54 55 public void mapClassToElement(Class cls, String namespaceURI, String localName, ObjectModelProvider provider) 56 { 57 ClassToElementMapping mapping = new ClassToElementMapping(cls, namespaceURI, localName, 58 provider instanceof GenericObjectModelProvider ? 59 (GenericObjectModelProvider)provider : new DelegatingObjectModelProvider(provider) 60 ); 61 classMappings.put(mapping.cls, mapping); 62 } 63 64 public void mapFieldToElement(Class cls, 65 String field, 66 String namespaceURI, 67 String localName, 68 TypeBinding converter) 69 { 70 FieldToElementMapping mapping = new FieldToElementMapping(cls, field, namespaceURI, localName, converter); 71 mapFieldToElement(mapping); 72 } 73 74 public boolean isIgnoreLowLine() 75 { 76 return ignoreLowLine; 77 } 78 79 public void setIgnoreLowLine(boolean ignoreLowLine) 80 { 81 this.ignoreLowLine = ignoreLowLine; 82 } 83 84 86 public Object getChildren(Object o, MarshallingContext ctx, String namespaceURI, String localName) 87 { 88 Object children = null; 89 if(!writeAsValue(o.getClass())) 90 { 91 children = getJavaValue(namespaceURI, localName, null, o, true, ignoreNotFoundField); 92 } 93 return children; 94 } 95 96 public Object getElementValue(Object o, MarshallingContext ctx, String namespaceURI, String localName) 97 { 98 Object value; 99 if(writeAsValue(o.getClass())) 100 { 101 value = o; 102 } 103 else 104 { 105 String fieldName = null; 106 if(ctx != null && ctx.isTypeComplex()) 107 { 108 fieldName = ctx.getSimpleContentProperty(); 110 } 111 112 try 114 { 115 value = getJavaValue(namespaceURI, localName, fieldName, o, false, false); 116 } 117 catch(JBossXBRuntimeException e) 118 { 119 value = getJavaValue(namespaceURI, localName, null, o, false, ignoreNotFoundField); 120 } 121 } 122 return value; 123 } 124 125 public Object getAttributeValue(Object o, MarshallingContext ctx, String namespaceURI, String localName) 126 { 127 boolean optional = ctx == null ? ignoreNotFoundField : !ctx.isAttributeRequired() || ignoreNotFoundField; 128 return getJavaValue(namespaceURI, localName, null, o, false, optional); 129 } 130 131 public Object getRoot(Object o, MarshallingContext ctx, String namespaceURI, String localName) 132 { 133 return o; 137 } 138 139 141 private void mapFieldToElement(FieldToElementMapping mapping) 142 { 143 String mappingKey = mapping.cls.getName() + ":" + mapping.localName; 144 fieldMappings.put(mappingKey, mapping); 145 } 146 147 private Object getJavaValue(String namespaceURI, String localName, String fieldName, Object o, boolean forComplexType, boolean optional) 148 { 149 String mappingKey = o.getClass().getName() + ":" + localName; 150 FieldToElementMapping mapping = (FieldToElementMapping)fieldMappings.get(mappingKey); 151 if(mapping == null) 152 { 153 if(fieldName == null) 154 { 155 fieldName = Util.xmlNameToFieldName(localName, ignoreLowLine); 156 } 157 158 try 160 { 161 mapping = new FieldToElementMapping(o.getClass(), fieldName, namespaceURI, localName, null); 162 mapFieldToElement(mapping); 163 } 164 catch(JBossXBRuntimeException e) 165 { 166 if(optional) 167 { 168 if(log.isTraceEnabled()) 169 { 170 log.trace(e.getMessage()); 171 } 172 } 173 else 174 { 175 throw e; 176 } 177 } 178 } 179 180 FieldInfo fieldInfo = null; 181 if(mapping != null) 182 { 183 fieldInfo = mapping.fieldInfo; 184 } 185 186 Object value = null; 187 if(fieldInfo != null && (!forComplexType || forComplexType && !writeAsValue(fieldInfo.getType()))) 188 { 189 value = fieldInfo.getValue(o); 190 } 191 192 if(value != null && mapping != null && mapping.converter != null) 193 { 194 value = mapping.converter.marshal(value); 195 } 196 197 return value; 198 } 199 200 private boolean writeAsValue(final Class type) 201 { 202 return Classes.isPrimitive(type) || 203 type == String .class || 204 type == java.util.Date .class || 205 type == java.math.BigDecimal .class || 206 type == java.math.BigInteger .class; 207 } 208 209 211 private class ClassToElementMapping 212 { 213 public final Class cls; 214 public final String namespaceURI; 215 public final String localName; 216 public final GenericObjectModelProvider provider; 217 218 public ClassToElementMapping(Class cls, 219 String namespaceURI, 220 String localName, 221 GenericObjectModelProvider provider) 222 { 223 this.cls = cls; 224 this.namespaceURI = namespaceURI; 225 this.localName = localName; 226 this.provider = provider; 227 228 if(log.isTraceEnabled()) 229 { 230 log.trace("new ClassToElementMapping: [cls=" + 231 cls.getName() + 232 ",qname=" + 233 new QName (namespaceURI, localName) + 234 "]" 235 ); 236 } 237 } 238 239 public boolean equals(Object o) 240 { 241 if(this == o) 242 { 243 return true; 244 } 245 if(!(o instanceof ClassToElementMapping)) 246 { 247 return false; 248 } 249 250 final ClassToElementMapping classToElementMapping = (ClassToElementMapping)o; 251 252 if(cls != null ? !cls.equals(classToElementMapping.cls) : classToElementMapping.cls != null) 253 { 254 return false; 255 } 256 if(localName != null ? 257 !localName.equals(classToElementMapping.localName) : 258 classToElementMapping.localName != null) 259 { 260 return false; 261 } 262 if(namespaceURI != null ? 263 !namespaceURI.equals(classToElementMapping.namespaceURI) : 264 classToElementMapping.namespaceURI != null) 265 { 266 return false; 267 } 268 269 return true; 270 } 271 272 public int hashCode() 273 { 274 int result; 275 result = (cls != null ? cls.hashCode() : 0); 276 result = 29 * result + (namespaceURI != null ? namespaceURI.hashCode() : 0); 277 result = 29 * result + (localName != null ? localName.hashCode() : 0); 278 return result; 279 } 280 } 281 282 private class FieldToElementMapping 283 { 284 public final Class cls; 285 public final String namespaceURI; 286 public final String localName; 287 public final TypeBinding converter; 288 public final FieldInfo fieldInfo; 289 290 public FieldToElementMapping(Class cls, 291 String field, 292 String namespaceURI, 293 String localName, 294 TypeBinding converter) 295 { 296 this.cls = cls; 297 this.namespaceURI = namespaceURI; 298 this.localName = localName; 299 this.converter = converter; 300 301 if(log.isTraceEnabled()) 302 { 303 log.trace("new FieldToElementMapping: [cls=" + 304 cls.getName() + 305 ",field=" + 306 field + 307 ",qname=" + 308 new QName (namespaceURI, localName) + 309 "]" 310 ); 311 } 312 313 fieldInfo = FieldInfo.getFieldInfo(cls, field, true); 314 } 315 316 public boolean equals(Object o) 317 { 318 if(this == o) 319 { 320 return true; 321 } 322 if(!(o instanceof FieldToElementMapping)) 323 { 324 return false; 325 } 326 327 final FieldToElementMapping fieldToElementMapping = (FieldToElementMapping)o; 328 329 if(cls != null ? !cls.equals(fieldToElementMapping.cls) : fieldToElementMapping.cls != null) 330 { 331 return false; 332 } 333 if(!fieldInfo.getName().equals(fieldToElementMapping.fieldInfo.getName())) 334 { 335 return false; 336 } 337 if(localName != null ? 338 !localName.equals(fieldToElementMapping.localName) : 339 fieldToElementMapping.localName != null) 340 { 341 return false; 342 } 343 if(namespaceURI != null ? 344 !namespaceURI.equals(fieldToElementMapping.namespaceURI) : 345 fieldToElementMapping.namespaceURI != null) 346 { 347 return false; 348 } 349 350 return true; 351 } 352 353 public int hashCode() 354 { 355 int result; 356 result = (cls != null ? cls.hashCode() : 0); 357 result = 29 * result + fieldInfo.getName().hashCode(); 358 result = 29 * result + (namespaceURI != null ? namespaceURI.hashCode() : 0); 359 result = 29 * result + (localName != null ? localName.hashCode() : 0); 360 return result; 361 } 362 } 363 } 364 | Popular Tags |