1 22 package org.jboss.xb.binding.metadata; 23 24 import java.io.StringReader ; 25 import javax.xml.namespace.QName ; 26 27 import org.jboss.xb.binding.Constants; 28 import org.jboss.xb.binding.GenericObjectModelFactory; 29 import org.jboss.xb.binding.JBossXBException; 30 import org.jboss.xb.binding.JBossXBRuntimeException; 31 import org.jboss.xb.binding.Unmarshaller; 32 import org.jboss.xb.binding.UnmarshallerFactory; 33 import org.jboss.xb.binding.UnmarshallingContext; 34 import org.jboss.logging.Logger; 35 import org.xml.sax.Attributes ; 36 37 41 public class XsdAnnotation 42 extends XsdElement 43 { 44 private static final Logger log = Logger.getLogger(XsdAnnotation.class); 45 46 public XsdAnnotation(QName qName) 47 { 48 super(qName); 49 } 50 51 public static final XsdAnnotation unmarshal(String annotation) 52 { 53 Unmarshaller unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller(); 54 unmarshaller.mapFactoryToNamespace(JaxbObjectModelFactory.INSTANCE, Constants.NS_JAXB); 55 unmarshaller.mapFactoryToNamespace(JbxbObjectModelFactory.INSTANCE, Constants.NS_JBXB); 56 57 try 58 { 59 return (XsdAnnotation)unmarshaller.unmarshal(new StringReader (annotation), 60 XsdObjectModelFactory.INSTANCE, 61 (Object )null 62 ); 63 } 64 catch(JBossXBException e) 65 { 66 throw new JBossXBRuntimeException("Failed to parse annotation string: " + annotation + ": " + e.getMessage(), 67 e 68 ); 69 } 70 } 71 72 public XsdAppInfo getAppInfo() 73 { 74 return (XsdAppInfo)getChild(XsdAppInfo.QNAME); 75 } 76 77 79 private static abstract class AbstractGOMF 80 implements GenericObjectModelFactory 81 { 82 public Object newChild(Object parent, 83 UnmarshallingContext ctx, 84 String namespaceURI, 85 String localName, 86 Attributes attrs) 87 { 88 return null; 89 } 90 91 public void addChild(Object parent, 92 Object child, 93 UnmarshallingContext ctx, 94 String namespaceURI, 95 String localName) 96 { 97 XsdElement p = (XsdElement)parent; 98 XsdElement c = (XsdElement)child; 99 p.addChild(c); 100 } 101 102 public void setValue(Object o, UnmarshallingContext ctx, String namespaceURI, String localName, String value) 103 { 104 XsdElement e = (XsdElement)o; 105 e.setData(value); 106 } 107 108 public Object completeRoot(Object root, UnmarshallingContext ctx, String namespaceURI, String localName) 109 { 110 return root; 111 } 112 113 } 114 115 private static final class XsdObjectModelFactory 116 extends AbstractGOMF 117 { 118 public static final GenericObjectModelFactory INSTANCE = new XsdObjectModelFactory(); 119 120 public Object newChild(Object parent, 121 UnmarshallingContext ctx, 122 String namespaceURI, 123 String localName, 124 Attributes attrs) 125 { 126 XsdElement element = null; 127 if("appinfo".equals(localName)) 128 { 129 element = new XsdAppInfo(); 130 for(int i = 0; i < attrs.getLength(); ++i) 131 { 132 element.addAttribute(new QName (attrs.getURI(i), attrs.getLocalName(i)), attrs.getValue(i)); 133 } 134 } 135 return element; 136 } 137 138 public void addChild(Object parent, 139 Object child, 140 UnmarshallingContext ctx, 141 String namespaceURI, 142 String localName) 143 { 144 if(parent instanceof XsdAppInfo) 145 { 146 XsdAppInfo appInfo = (XsdAppInfo)parent; 147 if(child instanceof ClassMetaData) 148 { 149 appInfo.setClassMetaData((ClassMetaData)child); 150 } 151 else if(child instanceof PropertyMetaData) 152 { 153 appInfo.setPropertyMetaData((PropertyMetaData)child); 154 } 155 else if(child instanceof SchemaMetaData) 156 { 157 appInfo.setSchemaMetaData((SchemaMetaData)child); 158 } 159 else if(child instanceof ValueMetaData) 160 { 161 appInfo.setValueMetaData((ValueMetaData)child); 162 } 163 else if(child instanceof CharactersMetaData) 164 { 165 appInfo.setCharactersMetaData((CharactersMetaData)child); 166 } 167 } 168 else 169 { 170 super.addChild(parent, child, ctx, namespaceURI, localName); 171 } 172 } 173 174 public Object newRoot(Object root, 175 UnmarshallingContext ctx, 176 String namespaceURI, 177 String localName, 178 Attributes attrs) 179 { 180 return new XsdAnnotation(new QName (namespaceURI, localName)); 181 } 182 } 183 184 private static final class JaxbObjectModelFactory 185 implements GenericObjectModelFactory 186 { 187 public static final GenericObjectModelFactory INSTANCE = new JaxbObjectModelFactory(); 188 189 public Object newChild(Object parent, 190 UnmarshallingContext ctx, 191 String namespaceURI, 192 String localName, 193 Attributes attrs) 194 { 195 Object element = null; 196 if("package".equals(localName)) 197 { 198 element = new PackageMetaData(); 199 setAttributes(element, attrs, new AttributeSetter() 200 { 201 public void setAttribute(Object o, String nsUri, String localName, String value) 202 { 203 if("name".equals(localName)) 204 { 205 ((PackageMetaData)o).setName(value); 206 } 207 } 208 } 209 ); 210 } 211 else if("javaType".equals(localName)) 212 { 213 ValueMetaData valueMetaData = new ValueMetaData(); 214 setAttributes(valueMetaData, attrs, new AttributeSetter() 215 { 216 public void setAttribute(Object o, String nsUri, String localName, String value) 217 { 218 if("parseMethod".equals(localName)) 219 { 220 ((ValueMetaData)o).setUnmarshalMethod(value); 221 } 222 else if("printMethod".equals(localName)) 223 { 224 ((ValueMetaData)o).setMarshalMethod(value); 225 } 226 } 227 } 228 ); 229 230 XsdAppInfo appInfo = (XsdAppInfo)parent; 232 appInfo.setValueMetaData(valueMetaData); 233 } 234 235 return element; 236 } 237 238 public void addChild(Object parent, 239 Object child, 240 UnmarshallingContext ctx, 241 String namespaceURI, 242 String localName) 243 { 244 if(parent instanceof SchemaMetaData) 245 { 246 SchemaMetaData schemaMetaData = (SchemaMetaData)parent; 247 if(child instanceof PackageMetaData) 248 { 249 schemaMetaData.setPackage((PackageMetaData)child); 250 } 251 else 252 { 253 schemaMetaData.addValue((ValueMetaData)child); 254 } 255 } 256 } 257 258 public void setValue(Object o, UnmarshallingContext ctx, String namespaceURI, String localName, String value) 259 { 260 } 261 262 public Object newRoot(Object root, 263 UnmarshallingContext ctx, 264 String namespaceURI, 265 String localName, 266 Attributes attrs) 267 { 268 Object element = null; 269 if("schemaBindings".equals(localName)) 270 { 271 element = new SchemaMetaData(); 272 } 273 else if("property".equals(localName)) 274 { 275 PropertyMetaData property = new PropertyMetaData(); 276 setAttributes(property, attrs, new AttributeSetter() 277 { 278 public void setAttribute(Object o, String nsUri, String localName, String value) 279 { 280 if("name".equals(localName)) 281 { 282 ((PropertyMetaData)o).setName(value); 283 } 284 else if("collectionType".equals(localName)) 285 { 286 ((PropertyMetaData)o).setCollectionType(value); 287 } 288 } 289 } 290 ); 291 XsdAppInfo appInfo = (XsdAppInfo)root; 293 appInfo.setPropertyMetaData(property); 294 } 296 else if("class".equals(localName)) 297 { 298 element = new ClassMetaData(); 299 setAttributes(element, attrs, new AttributeSetter() 300 { 301 public void setAttribute(Object o, String nsUri, String localName, String value) 302 { 303 if("implClass".equals(localName)) 304 { 305 ((ClassMetaData)o).setImpl(value); 306 } 307 } 308 } 309 ); 310 } 311 else if("javaType".equals(localName)) 312 { 313 element = new ValueMetaData(); 314 setAttributes(element, attrs, new AttributeSetter() 315 { 316 public void setAttribute(Object o, String nsUri, String localName, String value) 317 { 318 if("printMethod".equals(localName)) 319 { 320 ((ValueMetaData)o).setMarshalMethod(value); 321 } 322 else if("parseMethod".equals(localName)) 323 { 324 ((ValueMetaData)o).setUnmarshalMethod(value); 325 } 326 } 327 } 328 ); 329 } 330 331 return element; 332 } 333 334 public Object completeRoot(Object root, UnmarshallingContext ctx, String namespaceURI, String localName) 335 { 336 return root; 337 } 338 339 private void setAttributes(Object o, Attributes attrs, AttributeSetter attrSetter) 340 { 341 for(int i = 0; i < attrs.getLength(); ++i) 342 { 343 attrSetter.setAttribute(o, attrs.getURI(i), attrs.getLocalName(i), attrs.getValue(i)); 344 } 345 } 346 } 347 348 private static final class JbxbObjectModelFactory 349 implements GenericObjectModelFactory 350 { 351 public static final JbxbObjectModelFactory INSTANCE = new JbxbObjectModelFactory(); 352 353 public Object newChild(Object parent, 354 UnmarshallingContext ctx, 355 String namespaceURI, 356 String localName, 357 Attributes attrs) 358 { 359 Object child = null; 360 if("package".equals(localName)) 362 { 363 child = new PackageMetaData(); 364 setAttributes(child, attrs, new AttributeSetter() 365 { 366 public void setAttribute(Object o, String nsUri, String localName, String value) 367 { 368 if("name".equals(localName)) 369 { 370 ((PackageMetaData)o).setName(value); 371 } 372 } 373 } 374 ); 375 } 376 else if("value".equals(localName)) 377 { 378 child = new ValueMetaData(); 379 setAttributes(child, attrs, new AttributeSetter() 380 { 381 public void setAttribute(Object o, String nsUri, String localName, String value) 382 { 383 if("marshalMethod".equals(localName)) 384 { 385 ((ValueMetaData)o).setMarshalMethod(value); 386 } 387 else if("unmarshalMethod".equals(localName)) 388 { 389 ((ValueMetaData)o).setUnmarshalMethod(value); 390 } 391 } 392 } 393 ); 394 } 395 else if("property".equals(localName)) 396 { 397 PropertyMetaData property = new PropertyMetaData(); 398 setAttributes(property, attrs, new AttributeSetter() 399 { 400 public void setAttribute(Object o, String nsUri, String localName, String value) 401 { 402 if("name".equals(localName)) 403 { 404 ((PropertyMetaData)o).setName(value); 405 } 406 else if("collectionType".equals(localName)) 407 { 408 ((PropertyMetaData)o).setCollectionType(value); 409 } 410 } 411 } 412 ); 413 414 if(parent instanceof XsdAppInfo) 415 { 416 ((XsdAppInfo)parent).setPropertyMetaData(property); 417 } 418 else 419 { 420 ((CharactersMetaData)parent).setProperty(property); 421 } 422 } 423 else if("mapEntryKey".equals(localName)) 424 { 425 if(parent instanceof XsdAppInfo) 426 { 427 ((XsdAppInfo)parent).setMapEntryKey(true); 428 } 429 else 430 { 431 ((CharactersMetaData)parent).setMapEntryKey(true); 432 } 433 } 434 else if("mapEntryValue".equals(localName)) 435 { 436 if(parent instanceof XsdAppInfo) 437 { 438 ((XsdAppInfo)parent).setMapEntryValue(true); 439 } 440 else 441 { 442 ((CharactersMetaData)parent).setMapEntryValue(true); 443 } 444 } 445 else if("skip".equals(localName)) 446 { 447 XsdAppInfo appInfo = (XsdAppInfo)parent; 448 appInfo.setSkip(true); 449 } 450 else 451 { 452 if( "ignoreUnresolvedFieldOrClass".equals(localName) == false 454 && "replacePropertyRefs".equals(localName) == false ) 455 { 456 log.warn("newChild: " + localName); 457 } 458 } 459 return child; 460 } 461 462 public void addChild(Object parent, 463 Object child, 464 UnmarshallingContext ctx, 465 String namespaceURI, 466 String localName) 467 { 468 if(child instanceof PackageMetaData) 469 { 470 SchemaMetaData schema = (SchemaMetaData)parent; 471 schema.setPackage((PackageMetaData)child); 472 } 473 else if(child instanceof ValueMetaData) 474 { 475 ValueMetaData valueMetaData = (ValueMetaData)child; 476 if(parent instanceof XsdAppInfo) 477 { 478 ((XsdAppInfo)parent).setValueMetaData(valueMetaData); 479 } 480 else 481 { 482 ((CharactersMetaData)parent).setValue(valueMetaData); 483 } 484 } 485 else if(child instanceof CharactersMetaData) 486 { 487 CharactersMetaData charMD = (CharactersMetaData)child; 488 ((XsdAppInfo)parent).setCharactersMetaData(charMD); 489 } 490 else 491 { 492 log.warn("addChild: " + localName + "=" + child); 493 } 494 } 495 496 public void setValue(Object o, UnmarshallingContext ctx, String namespaceURI, String localName, String value) 497 { 498 if( "ignoreUnresolvedFieldOrClass".equals(localName) ) 500 { 501 SchemaMetaData schema = (SchemaMetaData) o; 502 Boolean flag = Boolean.valueOf(value); 503 schema.setIgnoreUnresolvedFieldOrClass(flag.booleanValue()); 504 } 505 else if( "replacePropertyRefs".equals(localName) ) 507 { 508 SchemaMetaData schema = (SchemaMetaData) o; 509 Boolean flag = Boolean.valueOf(value); 510 schema.setReplacePropertyRefs(flag.booleanValue()); 511 } 512 else 513 { 514 log.warn("setValue: " + localName + "=" + value); 515 } 516 } 517 518 public Object newRoot(Object root, 519 UnmarshallingContext ctx, 520 String namespaceURI, 521 String localName, 522 Attributes attrs) 523 { 524 Object element = null; 525 if("schemaBindings".equals(localName)) 526 { 527 element = new SchemaMetaData(); 528 } 529 else if("schema".equals(localName)) 531 { 532 element = new SchemaMetaData(); 533 } 534 else if("value".equals(localName)) 535 { 536 element = new ValueMetaData(); 537 setAttributes(element, attrs, new AttributeSetter() 538 { 539 public void setAttribute(Object o, String nsUri, String localName, String value) 540 { 541 if("marshalMethod".equals(localName)) 542 { 543 ((ValueMetaData)o).setMarshalMethod(value); 544 } 545 else if("unmarshalMethod".equals(localName)) 546 { 547 ((ValueMetaData)o).setUnmarshalMethod(value); 548 } 549 } 550 } 551 ); 552 } 553 else if("class".equals(localName)) 554 { 555 element = new ClassMetaData(); 556 setAttributes(element, attrs, new AttributeSetter() 557 { 558 public void setAttribute(Object o, String nsUri, String localName, String value) 559 { 560 if("impl".equals(localName)) 561 { 562 ((ClassMetaData)o).setImpl(value); 563 } 564 } 565 } 566 ); 567 } 568 else if("property".equals(localName)) 569 { 570 PropertyMetaData property = new PropertyMetaData(); 571 setAttributes(property, attrs, new AttributeSetter() 572 { 573 public void setAttribute(Object o, String nsUri, String localName, String value) 574 { 575 if("name".equals(localName)) 576 { 577 ((PropertyMetaData)o).setName(value); 578 } 579 else if("collectionType".equals(localName)) 580 { 581 ((PropertyMetaData)o).setCollectionType(value); 582 } 583 } 584 } 585 ); 586 XsdAppInfo appInfo = (XsdAppInfo)root; 587 appInfo.setPropertyMetaData(property); 588 } 589 else if("putMethod".equals(localName)) 590 { 591 PutMethodMetaData putMethod = new PutMethodMetaData(); 592 setAttributes(putMethod, attrs, new AttributeSetter() 593 { 594 public void setAttribute(Object o, String nsUri, String localName, String value) 595 { 596 if("name".equals(localName)) 597 { 598 ((PutMethodMetaData)o).setName(value); 599 } 600 else if("keyType".equals(localName)) 601 { 602 ((PutMethodMetaData)o).setKeyType(value); 603 } 604 else if("valueType".equals(localName)) 605 { 606 ((PutMethodMetaData)o).setValueType(value); 607 } 608 } 609 } 610 ); 611 XsdAppInfo appInfo = (XsdAppInfo)root; 612 appInfo.setPutMethodMetaData(putMethod); 613 } 614 else if("addMethod".equals(localName)) 615 { 616 AddMethodMetaData addMethod = new AddMethodMetaData(); 617 setAttributes(addMethod, attrs, new AttributeSetter() 618 { 619 public void setAttribute(Object o, String nsUri, String localName, String value) 620 { 621 if("name".equals(localName)) 622 { 623 ((AddMethodMetaData)o).setMethodName(value); 624 } 625 else if("valueType".equals(localName)) 626 { 627 if("child".equals(value)) 628 { 629 ((AddMethodMetaData)o).setChildType(true); 630 } 631 else 632 { 633 ((AddMethodMetaData)o).setValueType(value); 634 } 635 } 636 } 637 } 638 ); 639 XsdAppInfo appInfo = (XsdAppInfo)root; 640 appInfo.setAddMethodMetaData(addMethod); 641 } 642 else if("mapEntry".equals(localName)) 643 { 644 MapEntryMetaData mapEntry = new MapEntryMetaData(); 645 setAttributes(mapEntry, attrs, new AttributeSetter() 646 { 647 public void setAttribute(Object o, String nsUri, String localName, String value) 648 { 649 if("impl".equals(localName)) 650 { 651 ((MapEntryMetaData)o).setImpl(value); 652 } 653 else if("getKeyMethod".equals(localName)) 654 { 655 ((MapEntryMetaData)o).setGetKeyMethod(value); 656 } 657 else if("setKeyMethod".equals(localName)) 658 { 659 ((MapEntryMetaData)o).setSetKeyMethod(value); 660 } 661 else if("getValueMethod".equals(localName)) 662 { 663 ((MapEntryMetaData)o).setGetValueMethod(value); 664 } 665 else if("setValueMethod".equals(localName)) 666 { 667 ((MapEntryMetaData)o).setSetValueMethod(value); 668 } 669 else if("valueType".equals(localName)) 670 { 671 ((MapEntryMetaData)o).setValueType(value); 672 } 673 else if("nonNullValue".equals(localName)) 674 { 675 boolean b = Boolean.valueOf(value).booleanValue(); 676 ((MapEntryMetaData)o).setNonNullValue(b); 677 } 678 } 679 } 680 ); 681 XsdAppInfo appInfo = (XsdAppInfo)root; 682 appInfo.setMapEntryMetaData(mapEntry); 683 } 684 else if("mapEntryKey".equals(localName)) 685 { 686 XsdAppInfo appInfo = (XsdAppInfo)root; 687 appInfo.setMapEntryKey(true); 688 } 689 else if("mapEntryValue".equals(localName)) 690 { 691 XsdAppInfo appInfo = (XsdAppInfo)root; 692 appInfo.setMapEntryValue(true); 693 } 694 else if("characters".equals(localName)) 695 { 696 element = new CharactersMetaData(); 697 } 698 else if("skip".equals(localName)) 699 { 700 XsdAppInfo appInfo = (XsdAppInfo)root; 701 appInfo.setSkip(true); 702 } 703 else 704 { 705 log.warn("Unexpected jbxb annotation: ns=" + namespaceURI + ", localName=" + localName); 706 } 707 return element; 708 } 709 710 public Object completeRoot(Object root, UnmarshallingContext ctx, String namespaceURI, String localName) 711 { 712 return root; 713 } 714 715 717 private void setAttributes(Object o, Attributes attrs, AttributeSetter attrSetter) 718 { 719 for(int i = 0; i < attrs.getLength(); ++i) 720 { 721 attrSetter.setAttribute(o, attrs.getURI(i), attrs.getLocalName(i), attrs.getValue(i)); 722 } 723 } 724 } 725 726 interface AttributeSetter 727 { 728 void setAttribute(Object o, String nsUri, String localName, String value); 729 } 730 } 731 | Popular Tags |