1 55 56 package org.jboss.axis.description; 57 58 import org.jboss.axis.utils.BeanPropertyDescriptor; 59 import org.jboss.axis.utils.BeanUtils; 60 import org.jboss.axis.utils.ClassUtils; 61 import org.jboss.axis.utils.Messages; 62 import org.jboss.logging.Logger; 63 64 import javax.xml.namespace.QName ; 65 import java.lang.reflect.Method ; 66 import java.util.HashMap ; 67 import java.util.Hashtable ; 68 import java.util.Iterator ; 69 import java.util.Map ; 70 71 78 public class TypeDesc 79 { 80 private final Logger log = Logger.getLogger(TypeDesc.class); 82 83 public static final Class [] noClasses = new Class []{}; 84 public static final Object [] noObjects = new Object []{}; 85 86 89 private static Map classMap = new Hashtable (); 90 91 94 private boolean lookedForAny; 95 96 99 private Class javaClass; 100 101 104 private QName xmlType; 105 106 109 private FieldDesc[] fields; 110 111 114 private HashMap fieldNameMap = new HashMap (); 115 116 119 private HashMap fieldElementMap; 120 121 124 private boolean hasAttributes; 125 126 129 private BeanPropertyDescriptor[] propertyDescriptors; 130 131 134 private Map propertyMap; 135 136 139 private BeanPropertyDescriptor anyDesc; 140 141 public TypeDesc(Class javaClass) 142 { 143 this.javaClass = javaClass; 144 } 145 146 153 public static void registerTypeDescForClass(Class cls, TypeDesc td) 154 { 155 classMap.put(cls, td); 156 } 157 158 169 public static TypeDesc getTypeDescForClass(Class cls) 170 { 171 TypeDesc result = (TypeDesc)classMap.get(cls); 173 if (result != null) 174 { 175 return result; 176 } 177 178 try 179 { 180 Method getTypeDesc = null; 181 try 182 { 183 getTypeDesc = 184 cls.getMethod("getTypeDesc", noClasses); 185 } 186 catch (NoSuchMethodException e) 187 { 188 } 189 if (getTypeDesc == null) 190 { 191 Class helper = ClassUtils.forName(cls.getName() + "_Helper"); 193 try 194 { 195 getTypeDesc = 196 helper.getMethod("getTypeDesc", noClasses); 197 } 198 catch (NoSuchMethodException e) 199 { 200 } 201 } 202 if (getTypeDesc != null) 203 { 204 return (TypeDesc)getTypeDesc.invoke(null, 205 noObjects); 206 } 207 } 208 catch (Exception e) 209 { 210 } 211 return null; 212 } 213 214 public BeanPropertyDescriptor getAnyDesc() 215 { 216 return anyDesc; 217 } 218 219 222 public FieldDesc[] getFields() 223 { 224 return fields; 225 } 226 227 public FieldDesc[] getFields(boolean searchParents) 228 { 229 if (searchParents) 230 { 231 Class cls = javaClass.getSuperclass(); 233 if (cls != null && !cls.getName().startsWith("java.")) 234 { 235 TypeDesc superDesc = getTypeDescForClass(cls); 236 if (superDesc != null) 237 { 238 FieldDesc[] parentFields = superDesc.getFields(true); 239 if (parentFields != null) 241 { 242 if (fields != null) 243 { 244 FieldDesc[] ret = new FieldDesc[parentFields.length + fields.length]; 245 System.arraycopy(parentFields, 0, ret, 0, parentFields.length); 246 System.arraycopy(fields, 0, ret, parentFields.length, fields.length); 247 fields = ret; 248 } 249 else 250 { 251 FieldDesc[] ret = new FieldDesc[parentFields.length]; 252 System.arraycopy(parentFields, 0, ret, 0, parentFields.length); 253 fields = ret; 254 } 255 } 256 } 258 } 259 } 260 261 return fields; 262 } 263 264 268 public void setFields(FieldDesc[] newFields) 269 { 270 fieldNameMap = new HashMap (); 271 fields = newFields; 272 hasAttributes = false; 273 fieldElementMap = null; 274 275 for (int i = 0; i < newFields.length; i++) 276 { 277 FieldDesc field = newFields[i]; 278 if (field.isElement()) 279 { 280 fieldNameMap.put(field.getFieldName(), field); 281 } 282 else 283 { 284 hasAttributes = true; 285 } 286 } 287 } 288 289 292 public void addFieldDesc(FieldDesc field) 293 { 294 if (field == null) 295 { 296 throw new IllegalArgumentException (Messages.getMessage("nullFieldDesc")); 297 } 298 299 int numFields = 0; 300 if (fields != null) 301 { 302 numFields = fields.length; 303 } 304 FieldDesc[] newFields = new FieldDesc[numFields + 1]; 305 if (fields != null) 306 { 307 System.arraycopy(fields, 0, newFields, 0, numFields); 308 } 309 newFields[numFields] = field; 310 fields = newFields; 311 312 fieldNameMap.put(field.getFieldName(), field); 314 315 if (!hasAttributes && !field.isElement()) 316 hasAttributes = true; 317 } 318 319 323 public QName getElementNameForField(String fieldName) 324 { 325 FieldDesc desc = (FieldDesc)fieldNameMap.get(fieldName); 326 if (desc == null) 327 { 328 Class cls = javaClass.getSuperclass(); 330 if (cls != null && !cls.getName().startsWith("java.")) 331 { 332 TypeDesc superDesc = getTypeDescForClass(cls); 333 if (superDesc != null) 334 { 335 return superDesc.getElementNameForField(fieldName); 336 } 337 } 338 } 339 else if (!desc.isElement()) 340 { 341 return null; 342 } 343 return desc.getXmlName(); 344 } 345 346 350 public QName getAttributeNameForField(String fieldName) 351 { 352 FieldDesc desc = (FieldDesc)fieldNameMap.get(fieldName); 353 if (desc == null) 354 { 355 Class cls = javaClass.getSuperclass(); 357 if (cls != null && !cls.getName().startsWith("java.")) 358 { 359 TypeDesc superDesc = getTypeDescForClass(cls); 360 if (superDesc != null) 361 { 362 return superDesc.getAttributeNameForField(fieldName); 363 } 364 } 365 } 366 else if (desc.isElement()) 367 { 368 return null; 369 } 370 QName ret = desc.getXmlName(); 371 if (ret == null) 372 { 373 ret = new QName ("", fieldName); 374 } 375 return ret; 376 } 377 378 382 public String getFieldNameForElement(QName qname) 383 { 384 if (fieldElementMap != null) 386 { 387 String cached = (String )fieldElementMap.get(qname); 388 if (cached != null) return cached; 389 } 390 391 String result = null; 392 393 String localPart = qname.getLocalPart(); 394 395 for (int i = 0; fields != null && i < fields.length; i++) 397 { 398 FieldDesc field = fields[i]; 399 if (field.isElement()) 400 { 401 QName xmlName = field.getXmlName(); 402 if (localPart.equals(xmlName.getLocalPart())) 403 { 404 if (qname.getNamespaceURI().equals(xmlName.getNamespaceURI())) 405 { 406 result = field.getFieldName(); 407 break; 408 } 409 } 410 } 411 } 412 413 if (result == null) 415 { 416 Class cls = javaClass.getSuperclass(); 417 if (cls != null && !cls.getName().startsWith("java.")) 418 { 419 TypeDesc superDesc = getTypeDescForClass(cls); 420 if (superDesc != null) 421 { 422 result = superDesc.getFieldNameForElement(qname); 423 } 424 } 425 } 426 427 if (result != null) 429 { 430 if (fieldElementMap == null) fieldElementMap = new HashMap (); 431 fieldElementMap.put(qname, result); 432 } 433 434 return result; 435 } 436 437 441 public String getFieldNameForAttribute(QName qname) 442 { 443 String possibleMatch = null; 444 445 for (int i = 0; fields != null && i < fields.length; i++) 446 { 447 FieldDesc field = fields[i]; 448 if (!field.isElement()) 449 { 450 if (qname.equals(field.getXmlName())) 453 { 454 return field.getFieldName(); 455 } 456 if (qname.getNamespaceURI().equals("") && 459 qname.getLocalPart().equals(field.getFieldName())) 460 { 461 possibleMatch = field.getFieldName(); 462 } 463 } 464 } 465 466 if (possibleMatch == null) 467 { 468 Class cls = javaClass.getSuperclass(); 470 if (cls != null && !cls.getName().startsWith("java.")) 471 { 472 TypeDesc superDesc = getTypeDescForClass(cls); 473 if (superDesc != null) 474 { 475 possibleMatch = superDesc.getFieldNameForAttribute(qname); 476 } 477 } 478 } 479 480 return possibleMatch; 481 } 482 483 486 public FieldDesc getFieldByName(String name) 487 { 488 FieldDesc ret = (FieldDesc)fieldNameMap.get(name); 489 if (ret == null) 490 { 491 Class cls = javaClass.getSuperclass(); 492 if (cls != null && !cls.getName().startsWith("java.")) 493 { 494 TypeDesc superDesc = getTypeDescForClass(cls); 495 if (superDesc != null) 496 { 497 ret = superDesc.getFieldByName(name); 498 } 499 } 500 } 501 return ret; 502 } 503 504 507 public boolean hasAttributes() 508 { 509 return hasAttributes; 510 } 511 512 public QName getXmlType() 513 { 514 return xmlType; 515 } 516 517 public void setXmlType(QName xmlType) 518 { 519 this.xmlType = xmlType; 520 } 521 522 527 public BeanPropertyDescriptor[] getPropertyDescriptors() 528 { 529 if (propertyDescriptors == null) 536 { 537 propertyDescriptors = BeanUtils.getPd(javaClass, this); 538 if (!lookedForAny) 539 { 540 anyDesc = BeanUtils.getAnyContentPD(javaClass); 541 lookedForAny = true; 542 } 543 } 544 return propertyDescriptors; 545 } 546 547 550 public void setPropertyDescriptors(BeanPropertyDescriptor[] propertyDescriptors) 551 { 552 this.propertyDescriptors = propertyDescriptors; 553 this.propertyMap = null; 554 } 555 556 public BeanPropertyDescriptor getAnyContentDescriptor() 557 { 558 if (!lookedForAny) 559 { 560 anyDesc = BeanUtils.getAnyContentPD(javaClass); 561 lookedForAny = true; 562 } 563 return anyDesc; 564 } 565 566 571 public Map getPropertyDescriptorMap() 572 { 573 if (propertyMap != null) 575 { 576 return propertyMap; 577 } 578 579 if (propertyDescriptors == null) 581 { 582 getPropertyDescriptors(); 583 } 584 585 propertyMap = new HashMap (); 587 for (int i = 0; i < propertyDescriptors.length; i++) 588 { 589 BeanPropertyDescriptor bpd = propertyDescriptors[i]; 590 String bpName = bpd.getName(); 591 592 propertyMap.put(bpName, bpd); 593 } 594 595 return propertyMap; 596 } 597 } 598 | Popular Tags |