1 2 12 package com.versant.core.metadata.parser; 13 14 import org.xml.sax.Attributes ; 15 import org.xml.sax.helpers.AttributesImpl ; 16 import org.xml.sax.helpers.DefaultHandler ; 17 18 19 import org.xml.sax.InputSource ; 20 import org.xml.sax.SAXException ; 21 import com.versant.core.common.JaxpUtils; 22 23 import javax.xml.parsers.SAXParser ; 24 import javax.xml.parsers.SAXParserFactory ; 25 26 27 28 29 import com.versant.core.metadata.MDStatics; 30 31 import java.io.FileInputStream ; 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 import java.io.StringReader ; 35 import java.util.ArrayList ; 36 import java.util.Collection ; 37 import java.util.Iterator ; 38 39 import com.versant.core.common.BindingSupportImpl; 40 41 46 public class MetaDataParser extends DefaultHandler 47 { 48 49 public static final String VENDOR_NAME = "versant"; 50 public static final String VENDOR_NAME_JDOGENIE = "jdogenie"; 51 52 53 private SAXParserFactory parserFactory; 54 private SAXParser parser; 55 56 private int state; 57 private int extState; 58 private int extSkipState; 59 private String elementName; 60 private int extensionSkipDepth; 62 63 private JdoRoot jdoRoot; 65 private JdoPackage jdoPackage; 66 private JdoClass jdoClass; 67 private JdoField jdoField; 68 private JdoCollection jdoCollection; 69 private JdoMap jdoMap; 70 private JdoArray jdoArray; 71 private JdoQuery jdoQuery; 72 private int extStackTop; 73 private JdoExtension[] extStack = new JdoExtension[32]; 74 private ArrayList [] extStackList = new ArrayList [32]; 75 76 private ArrayList packageList = new ArrayList (); 77 private ArrayList packageExtList = new ArrayList (); 78 private ArrayList packageClassList = new ArrayList (); 79 private ArrayList classElementList = new ArrayList (); 80 private ArrayList queryExtList = new ArrayList (); 82 private ArrayList collectionExtList = new ArrayList (); 83 84 private boolean doneFilter; 85 private boolean doneSql; 86 private boolean doneDeclarations; 87 private boolean doneResult; 88 private StringBuffer text; 89 90 private static final int START = 1; 91 private static final int JDO = 2; 92 private static final int PACKAGE = 3; 93 private static final int CLASS = 4; 94 private static final int FIELD = 5; 95 private static final int COLLECTION = 6; 96 private static final int EXTENSION = 7; 97 private static final int EXTENSION_SKIP = 8; 98 private static final int QUERY = 9; 99 private static final int EMBEDDED = 10; 100 private static final int EMBEDDED_FIELD = 11; 101 102 private static String [] STATE_STRING = {"ERROR", "START", "JDO", "PACKAGE", "CLASS", 103 "FIELD", "COLLECTION", "EXTENSION", 104 "EXTENSION_SKIP", "QUERY", "EMBEDDED", 105 "EMBEDDED_FIELD"}; 106 107 public MetaDataParser() { 108 109 parserFactory = JaxpUtils.getSAXParserFactory(); 110 parserFactory.setValidating(false); 111 parserFactory.setNamespaceAware(false); 112 113 } 114 115 118 public JdoRoot[] parse(Collection jdoNames, ClassLoader loader) { 119 int n = jdoNames.size(); 120 JdoRoot[] roots = new JdoRoot[n]; 121 int c = 0; 122 for (Iterator i = jdoNames.iterator(); i.hasNext(); ) { 123 String name = (String )i.next(); 124 InputStream in = null; 125 try { 126 127 128 in = loader.getResourceAsStream( 129 name.startsWith("/") ? name.substring(1) : name); 130 if (in == null) { 131 throw BindingSupportImpl.getInstance().runtime("Unable to load resource: " + 132 name); 133 } 134 135 roots[c++] = parse(in, name); 136 } finally { 137 if (in != null) { 138 try { 139 in.close(); 140 } catch (IOException x) { 141 } 143 } 144 } 145 } 146 return roots; 147 } 148 149 152 public JdoRoot parse(InputStream in, String name) { 153 try { 154 init(name); 155 156 parser = JaxpUtils.createSAXParser(parserFactory); 157 parser.parse(in, this); 158 parser = null; 159 160 161 162 return jdoRoot; 163 } catch (Exception x) { 164 throw BindingSupportImpl.getInstance().runtime( 165 name + ": " + x.getMessage(), x); 166 } 167 } 168 169 170 171 172 177 178 public InputSource resolveEntity(String publicId, String systemId) 179 { 180 StringReader reader = new StringReader (""); 181 return new InputSource (reader); 182 } 183 184 185 190 public JdoRoot parse(String filename) throws IOException { 191 FileInputStream in = null; 192 try { 193 return parse(in = new FileInputStream (filename), filename); 194 } finally { 195 if (in != null) { 196 try { 197 in.close(); 198 } catch (IOException x) { 199 } 201 } 202 } 203 } 204 205 213 public void init(String name) { 214 jdoRoot = new JdoRoot(); 215 jdoRoot.name = name; 216 state = START; 217 jdoPackage = null; 218 jdoClass = null; 219 jdoField = null; 220 jdoCollection = null; 221 jdoMap = null; 222 jdoArray = null; 223 extStackTop = -1; 224 } 225 226 229 public JdoRoot getJdoRoot() { 230 return jdoRoot; 231 } 232 233 237 public void startElement(String uri, String localName, String name, 238 Attributes attr) throws SAXException { 239 try { 240 startElementImp(uri, localName, name, attr); 241 } catch (RuntimeException x) { 242 throw x; 243 } 244 } 245 246 250 public void startElementImp(String uri, String localName, String name, 251 Attributes attr) throws SAXException { 252 elementName = name; 271 switch (state) { 272 273 case START: 274 if (name.equals("jdo") || name.equals("mapping")) { 275 packageList.clear(); 276 state = JDO; 277 } else { 278 279 throwInvalidElement("<jdo>"); 280 281 282 } 283 break; 284 285 case JDO: 286 if (name.equals("package") || name.equals("namespace")) { 287 startPackage(attr); 288 } else { 289 290 throwInvalidElement("<package>"); 291 292 293 } 294 break; 295 296 case PACKAGE: 297 if (name.equals("class")) { 298 startClass(attr); 299 } else if (name.equals("extension")) { 300 startExtension(jdoPackage, packageExtList, attr); 301 } else { 302 throwInvalidElement("<class> or <extension>"); 303 } 304 break; 305 case EMBEDDED: 306 if (name.equals("field")) { 307 startEmbeddedField(attr); 308 } else { 309 throwInvalidElement("<field>"); 310 } 311 break; 312 case CLASS: 313 if (name.equals("field")) { 314 startField(attr); 315 } else if (name.equals("extension")) { 316 startExtension(jdoClass, classElementList, attr); 317 } else if (name.equals("query")) { 318 startQuery(attr); 319 } else { 320 throwInvalidElement("<field> or <extension>"); 321 } 322 break; 323 case EMBEDDED_FIELD: 324 case FIELD: 325 if (name.equals("extension")) { 326 startExtension(jdoField, jdoField.extensionList, attr); 327 } else if (name.equals("collection")) { 328 startCollection(attr); 329 } else if (name.equals("map")) { 330 startMap(attr); 331 } else if (name.equals("array")) { 332 startArray(attr); 333 } else if (name.equals("embedded")) { 334 startEmbedded(attr); 335 } else { 336 throwInvalidElement( 337 "<collection>, <map>, <array> or <extension>"); 338 } 339 break; 340 341 case COLLECTION: 342 if (name.equals("extension")) { 343 JdoElement p; 344 if (jdoCollection != null) { 345 p = jdoCollection; 346 } else if (jdoArray != null) { 347 p = jdoArray; 348 } else { 349 p = jdoMap; 350 } 351 startExtension(p, collectionExtList, attr); 352 } else { 353 throwInvalidElement("<extension>"); 354 } 355 break; 356 357 case EXTENSION: 358 if (name.equals("extension")) { 359 startExtension(extStack[extStackTop], 360 extStackList[extStackTop], attr); 361 } else { 362 throwInvalidElement("<extension>"); 363 } 364 break; 365 366 case EXTENSION_SKIP: 367 extensionSkipDepth++; 368 break; 369 370 case QUERY: 371 if (name.equals("filter")) { 372 if (doneFilter) throwDuplicateElement("filter"); 373 if (jdoQuery.filter != null) { 374 throw BindingSupportImpl.getInstance().runtime("You may not have a filter attribute and " + 375 "element in " + getContext()); 376 } 377 doneFilter = true; 378 text = new StringBuffer (); 379 } else if (name.equals("sql")) { 381 if (doneSql) throwDuplicateElement("sql"); 382 if (jdoQuery.sql != null) { 383 throw BindingSupportImpl.getInstance().runtime("You may not have a sql attribute and " + 384 "element in " + getContext()); 385 } 386 doneSql = true; 387 text = new StringBuffer (); 388 } else if (name.equals("declare")) { 390 if (doneDeclarations) throwDuplicateElement("declarations"); 391 doneDeclarations = true; 392 jdoQuery.imports = attr.getValue("imports"); 393 jdoQuery.parameters = attr.getValue("parameters"); 394 jdoQuery.variables = attr.getValue("variables"); 395 } else if (name.equals("result")) { 396 if (doneResult) throwDuplicateElement("result"); 397 doneResult = true; 398 jdoQuery.resultClass = attr.getValue("class"); 399 jdoQuery.unique = getTriState(attr, "unique"); 400 jdoQuery.grouping = attr.getValue("grouping"); 401 text = new StringBuffer (); 402 } else if (name.equals("extension")) { 403 startExtension(jdoQuery, queryExtList, attr); 404 } else { 405 throwInvalidElement("<filter>, <declarations>, <result> " + 406 "or <extension>"); 407 } 408 break; 409 } 410 } 411 412 private void startEmbedded(Attributes attr) { 413 jdoField.embedded = JdoField.TRUE; 414 state = EMBEDDED; 415 } 416 417 public void characters(char ch[], int start, int length) 418 throws SAXException { 419 if (text != null) text.append(ch, start, length); 420 } 421 422 private void throwDuplicateElement(String name) { 423 throw BindingSupportImpl.getInstance().runtime("Only one '" + elementName + 424 "' element is allowed in " + getContext()); 425 } 426 427 private void throwInvalidElement(String valid) { 428 throw BindingSupportImpl.getInstance().runtime("Invalid element '" + elementName + 429 "' in " + getContext() + ", expected " + valid); 430 } 431 432 private void throwInvalidAttribute(String attrName, String value) { 433 throw BindingSupportImpl.getInstance().runtime("Invalid " + attrName + " attribute '" + 434 value + "' in " + getContext()); 435 } 436 437 private void startPackage(Attributes attr) { 438 jdoPackage = new JdoPackage(); 439 jdoPackage.parent = jdoRoot; 440 packageList.add(jdoPackage); 441 jdoPackage.name = getReqAttr(attr, "name"); 442 packageClassList.clear(); 443 packageExtList.clear(); 444 state = PACKAGE; 445 } 446 447 private void startClass(Attributes attr) { 448 jdoClass = new JdoClass(); 449 jdoClass.parent = jdoPackage; 450 packageClassList.add(jdoClass); 451 jdoClass.name = getReqAttr(attr, "name"); 452 String idt = attr.getValue("identity-type"); 453 if (idt != null) { 454 if (idt.equals("datastore")) { 455 jdoClass.identityType = JdoClass.IDENTITY_TYPE_DATASTORE; 456 } else if (idt.equals("application")) { 457 jdoClass.identityType = JdoClass.IDENTITY_TYPE_APPLICATION; 458 } else if (idt.equals("nondurable")) { 459 jdoClass.identityType = JdoClass.IDENTITY_TYPE_NONDURABLE; 460 } else { 461 throwInvalidAttribute("indentity-type", idt); 462 } 463 } 464 jdoClass.objectIdClass = attr.getValue("objectid-class"); 465 String re = attr.getValue("requires-extent"); 466 if (re == null || re.equals("true")) { 467 jdoClass.requiresExtent = true; 468 } else if (re.equals("false")) { 469 jdoClass.requiresExtent = false; 470 } else { 471 throwInvalidAttribute("requires-extent", re); 472 } 473 jdoClass.pcSuperclass = attr.getValue("persistence-capable-superclass"); 474 classElementList.clear(); 475 state = CLASS; 476 } 477 478 private void startField(Attributes attr) { 479 jdoField = new JdoField(); 480 jdoField.parent = jdoClass; 481 classElementList.add(jdoField); 482 jdoField.name = getReqAttr(attr, "name"); 483 String pm = attr.getValue("persistence-modifier"); 484 if (pm != null) { 485 if (pm.equals("persistent")) { 486 jdoField.persistenceModifier = JdoField.PERSISTENCE_MODIFIER_PERSISTENT; 487 } else if (pm.equals("transactional")) { 488 jdoField.persistenceModifier = JdoField.PERSISTENCE_MODIFIER_TRANSACTIONAL; 489 } else if (pm.equals("none")) { 490 jdoField.persistenceModifier = JdoField.PERSISTENCE_MODIFIER_NONE; 491 } else { 492 throwInvalidAttribute("persistence-modifier", pm); 493 } 494 } 495 String pk = attr.getValue("primary-key"); 496 if (pk == null || pk.equals("false")) { 497 jdoField.primaryKey = false; 498 } else if (pk.equals("true")) { 499 jdoField.primaryKey = true; 500 } else { 501 throwInvalidAttribute("primary-key", pk); 502 } 503 String nv = attr.getValue("null-value"); 504 if (nv == null || nv.equals("none")) { 505 jdoField.nullValue = JdoField.NULL_VALUE_NONE; 506 } else if (nv.equals("exception")) { 507 jdoField.nullValue = JdoField.NULL_VALUE_EXCEPTION; 508 } else if (nv.equals("default")) { 509 jdoField.nullValue = JdoField.NULL_VALUE_DEFAULT; 510 } else { 511 throwInvalidAttribute("null-value", nv); 512 } 513 String df = attr.getValue("default-fetch-group"); 514 if (df != null) { 515 if (df.equals("true")) { 516 jdoField.defaultFetchGroup = JdoField.TRUE; 517 } else if (df.equals("false")) { 518 jdoField.defaultFetchGroup = JdoField.FALSE; 519 } else { 520 throwInvalidAttribute("default-fetch-group", df); 521 } 522 } 523 String em = attr.getValue("embedded"); 524 if (em != null) { 525 if (em.equals("true")) { 526 jdoField.embedded = JdoField.TRUE; 527 } else if (em.equals("false")) { 528 jdoField.embedded = JdoField.FALSE; 529 } else { 530 throwInvalidAttribute("embedded", em); 531 } 532 } 533 state = FIELD; 534 } 535 536 private void startEmbeddedField(Attributes attr) { 537 jdoField = new JdoField(jdoField); 538 jdoField.parent = jdoClass; 539 jdoField.name = getReqAttr(attr, "name"); 540 541 String pm = attr.getValue("persistence-modifier"); 542 if (pm != null) { 543 if (pm.equals("persistent")) { 544 jdoField.persistenceModifier = JdoField.PERSISTENCE_MODIFIER_PERSISTENT; 545 } else if (pm.equals("transactional")) { 546 jdoField.persistenceModifier = JdoField.PERSISTENCE_MODIFIER_TRANSACTIONAL; 547 } else if (pm.equals("none")) { 548 jdoField.persistenceModifier = JdoField.PERSISTENCE_MODIFIER_NONE; 549 } else { 550 throwInvalidAttribute("persistence-modifier", pm); 551 } 552 } 553 554 String nv = attr.getValue("null-value"); 555 if (nv == null || nv.equals("none")) { 556 jdoField.nullValue = JdoField.NULL_VALUE_NONE; 557 } else if (nv.equals("exception")) { 558 jdoField.nullValue = JdoField.NULL_VALUE_EXCEPTION; 559 } else if (nv.equals("default")) { 560 jdoField.nullValue = JdoField.NULL_VALUE_DEFAULT; 561 } else { 562 throwInvalidAttribute("null-value", nv); 563 } 564 String df = attr.getValue("default-fetch-group"); 565 if (df != null) { 566 if (df.equals("true")) { 567 jdoField.defaultFetchGroup = JdoField.TRUE; 568 } else if (df.equals("false")) { 569 jdoField.defaultFetchGroup = JdoField.FALSE; 570 } else { 571 throwInvalidAttribute("default-fetch-group", df); 572 } 573 } 574 String em = attr.getValue("embedded"); 575 if (em != null) { 576 if (em.equals("true")) { 577 jdoField.embedded = JdoField.TRUE; 578 } else if (em.equals("false")) { 579 jdoField.embedded = JdoField.FALSE; 580 } else { 581 throwInvalidAttribute("embedded", em); 582 } 583 } 584 state = EMBEDDED_FIELD; 585 } 586 587 private void startCollection(Attributes attr) { 588 jdoCollection = new JdoCollection(); 589 jdoCollection.parent = jdoField; 590 jdoField.collection = jdoCollection; 591 jdoCollection.elementType = attr.getValue("element-type"); 592 String em = attr.getValue("embedded-element"); 593 if (em != null) { 594 if (em.equals("true")) { 595 jdoCollection.embeddedElement = JdoCollection.TRUE; 596 } else if (em.equals("false")) { 597 jdoCollection.embeddedElement = JdoCollection.FALSE; 598 } else { 599 throwInvalidAttribute("embedded-element", em); 600 } 601 } 602 collectionExtList.clear(); 603 state = COLLECTION; 604 } 605 606 private void startMap(Attributes attr) { 607 jdoMap = new JdoMap(); 608 jdoMap.parent = jdoField; 609 jdoField.map = jdoMap; 610 jdoMap.keyType = attr.getValue("key-type"); 611 jdoMap.valueType = attr.getValue("value-type"); 612 String em = attr.getValue("embedded-key"); 613 if (em != null) { 614 if (em.equals("true")) { 615 jdoMap.embeddedKey = JdoMap.TRUE; 616 } else if (em.equals("false")) { 617 jdoMap.embeddedKey = JdoMap.FALSE; 618 } else { 619 throwInvalidAttribute("embedded-key", em); 620 } 621 } 622 em = attr.getValue("embedded-value"); 623 if (em != null) { 624 if (em.equals("true")) { 625 jdoMap.embeddedValue = JdoMap.TRUE; 626 } else if (em.equals("false")) { 627 jdoMap.embeddedValue = JdoMap.FALSE; 628 } else { 629 throwInvalidAttribute("embedded-value", em); 630 } 631 } 632 collectionExtList.clear(); 633 state = COLLECTION; 634 } 635 636 private void startArray(Attributes attr) { 637 jdoArray = new JdoArray(); 638 jdoArray.parent = jdoField; 639 jdoField.array = jdoArray; 640 String em = attr.getValue("embedded-element"); 641 if (em != null) { 642 if (em.equals("true")) { 643 jdoArray.embeddedElement = JdoCollection.TRUE; 644 } else if (em.equals("false")) { 645 jdoArray.embeddedElement = JdoCollection.FALSE; 646 } else { 647 throwInvalidAttribute("embedded-element", em); 648 } 649 } 650 collectionExtList.clear(); 651 state = COLLECTION; 652 } 653 654 private void startExtension(JdoElement parent, ArrayList list, 655 Attributes attr) { 656 657 658 String vn = getReqAttr(attr, "vendor-name"); 659 660 if (isOurVendorName(vn)) { 661 JdoExtension e = new JdoExtension(); 662 e.parent = parent; 663 String key = getReqAttr(attr, "key"); 664 int i = JdoExtension.parseKey(key); 665 if (i == Integer.MIN_VALUE) { 666 throw BindingSupportImpl.getInstance().runtime("Invalid key '" + key + "' in " + 667 getContext()); 668 } 669 e.key = i; 670 e.value = attr.getValue("value"); 671 extStack[++extStackTop] = e; 672 ArrayList a = extStackList[extStackTop]; 673 if (a == null) { 674 extStackList[extStackTop] = new ArrayList (); 675 } else { 676 a.clear(); 677 } 678 if (state != EXTENSION) { 679 extState = state; 680 state = EXTENSION; 681 } 682 list.add(e); 683 } else { 684 extSkipState = state; 685 state = EXTENSION_SKIP; 686 extensionSkipDepth = 0; 687 } 688 } 689 690 private void startQuery(Attributes attr) { 691 jdoQuery = new JdoQuery(); 692 jdoQuery.parent = jdoClass; 693 jdoClass.addJdoQuery(jdoQuery); 694 jdoQuery.name = getReqAttr(attr, "name"); 695 jdoQuery.language = attr.getValue("language"); 696 jdoQuery.ignoreCache = getTriState(attr, "ignore-cache"); 697 jdoQuery.includeSubclasses = getTriState(attr, "include-subclasses"); 698 jdoQuery.ordering = attr.getValue("ordering"); 699 jdoQuery.filter = attr.getValue("filter"); 700 jdoQuery.sql = attr.getValue("sql"); 701 String r = attr.getValue("range"); 702 if (r != null) { 703 try { 704 int i = r.indexOf(','); 705 jdoQuery.rangeStart = Integer.parseInt(r.substring(0, i)); 706 jdoQuery.rangeEnd = Integer.parseInt(r.substring(i + 1)); 707 } catch (Exception e) { 708 throwInvalidAttribute("range", r); 709 } 710 } 711 state = QUERY; 712 doneSql = false; 713 } 714 715 719 public void endElement(String uri, String localName, String name) 720 throws SAXException { 721 endElementImp(uri, localName, name); 722 } 723 724 728 public void endElementImp(String uri, String localName, String name) 729 throws SAXException { 730 elementName = name; 741 int n; 742 switch (state) { 743 744 case JDO: 745 jdoRoot.packages = new JdoPackage[packageList.size()]; 746 packageList.toArray(jdoRoot.packages); 747 state = START; 748 break; 749 750 case PACKAGE: 751 n = packageClassList.size(); 752 jdoPackage.classes = new JdoClass[n]; 753 packageClassList.toArray(jdoPackage.classes); 754 n = packageExtList.size(); 755 if (n > 0) { 756 jdoPackage.extensions = new JdoExtension[n]; 757 packageExtList.toArray(jdoPackage.extensions); 758 } 759 jdoPackage = null; 760 state = JDO; 761 break; 762 763 case CLASS: 764 jdoClass.elements = new JdoElement[classElementList.size()]; 765 classElementList.toArray(jdoClass.elements); 766 jdoClass = null; 767 state = PACKAGE; 768 break; 769 case EMBEDDED_FIELD: 770 n = jdoField.extensionList.size(); 771 if (n > 0) { 772 jdoField.extensions = new JdoExtension[n]; 773 jdoField.extensionList.toArray(jdoField.extensions); 774 } 775 jdoField = jdoField.parentField; 776 state = EMBEDDED; 777 break; 778 case FIELD: 779 n = jdoField.extensionList.size(); 780 if (n > 0) { 781 jdoField.extensions = new JdoExtension[n]; 782 jdoField.extensionList.toArray(jdoField.extensions); 783 } 784 jdoField = null; 785 jdoCollection = null; 786 jdoArray = null; 787 jdoMap = null; 788 state = CLASS; 789 break; 790 791 case COLLECTION: 792 n = collectionExtList.size(); 793 if (n > 0) { 794 JdoExtension[] e = new JdoExtension[n]; 795 collectionExtList.toArray(e); 796 if (jdoCollection != null) { 797 jdoCollection.extensions = e; 798 jdoCollection = null; 799 } else if (jdoArray != null) { 800 jdoArray.extensions = e; 801 jdoArray = null; 802 } else { 803 jdoMap.extensions = e; 804 jdoMap = null; 805 } 806 } 807 state = FIELD; 808 break; 809 810 case EXTENSION: 811 endExtension(); 812 break; 813 814 case EXTENSION_SKIP: 815 if (extensionSkipDepth == 0) { 816 state = extSkipState; 817 } else { 818 extensionSkipDepth--; 819 } 820 break; 821 822 case QUERY: 823 if (elementName.equals("query")) { 824 jdoQuery.extensions = new JdoExtension[queryExtList.size()]; 825 queryExtList.toArray(jdoQuery.extensions); 826 jdoQuery = null; 827 doneFilter = false; 828 doneDeclarations = false; 829 doneResult = false; 830 state = CLASS; 831 } else if (elementName.equals("filter")) { 832 if (text != null) jdoQuery.filter = text.toString().trim(); 833 text = null; 834 } else if (elementName.equals("result")) { 835 if (text != null) jdoQuery.result = text.toString().trim(); 836 text = null; 837 } else if (elementName.equals("sql")) { 838 if (text != null) jdoQuery.sql = text.toString().trim(); 839 text = null; 840 } 841 break; 842 case EMBEDDED: 843 if (jdoField.parentField == null) state = FIELD; 844 else state = EMBEDDED_FIELD; 845 break; 846 } 847 } 848 849 private void endExtension() { 850 JdoExtension top = extStack[extStackTop]; 851 ArrayList a = extStackList[extStackTop]; 852 int n = a.size(); 853 if (n > 0) { 854 top.nested = new JdoExtension[n]; 855 a.toArray(top.nested); 856 } 857 if (--extStackTop < 0) state = extState; 858 } 859 860 864 private String getContext() { 865 StringBuffer s = new StringBuffer (); 866 s.append(jdoRoot.name); 867 if (jdoPackage != null) { 868 s.append(":package["); 869 s.append(jdoPackage.name); 870 s.append(']'); 871 if (jdoClass != null) { 872 s.append("/class["); 873 s.append(jdoClass.name); 874 s.append(']'); 875 if (jdoField != null) { 876 s.append("/field["); 877 s.append(jdoField.name); 878 s.append(']'); 879 if (jdoCollection != null) { 880 s.append("/collection"); 881 } 882 if (jdoMap != null) { 883 s.append("/map"); 884 } 885 if (jdoArray != null) { 886 s.append("/array"); 887 } 888 } 889 if (jdoQuery != null) { 890 s.append("/query"); 891 } 892 } 893 } 894 for (int i = 0; i <= extStackTop; i++) { 895 s.append("/extension["); 896 JdoExtension e = extStack[i]; 897 s.append(JdoExtension.toKeyString(e.key)); 898 s.append('='); 899 s.append(e.value); 900 s.append(']'); 901 } 902 return s.toString(); 903 } 904 905 908 private String getReqAttr(Attributes attr, String name) 909 { 910 String v = attr.getValue(name); 911 if (v == null) { 912 StringBuffer s = new StringBuffer (); 913 s.append("Expected attribute '"); 914 s.append(name); 915 s.append("' in "); 916 s.append(getContext()); 917 s.append('.'); 918 s.append(elementName); 919 throw BindingSupportImpl.getInstance().runtime(s.toString()); 920 } 921 return v; 922 } 923 924 927 private int getIntAttr(Attributes attr, String name) { 928 String v = attr.getValue(name); 929 if (v == null) return -1; 930 try { 931 return Integer.parseInt(v); 932 } catch (NumberFormatException e) { 933 StringBuffer s = new StringBuffer (); 934 s.append("Expected int attribute '"); 935 s.append(name); 936 s.append("' found '"); 937 s.append(v); 938 s.append("' in "); 939 s.append(getContext()); 940 s.append('.'); 941 s.append(elementName); 942 throw BindingSupportImpl.getInstance().runtime(s.toString()); 943 } 944 } 945 946 953 private int getTriState(Attributes attr, String name) 954 { 955 String v = attr.getValue(name); 956 if (v == null) return MDStatics.NOT_SET; 957 if ("true".equals(v)) return MDStatics.TRUE; 958 if ("false".equals(v)) return MDStatics.FALSE; 959 StringBuffer s = new StringBuffer (); 960 s.append("Expected 'true' or 'false' for attribute '"); 961 s.append(name); 962 s.append("', got '"); 963 s.append(v); 964 s.append("' in "); 965 s.append(getContext()); 966 s.append('.'); 967 s.append(elementName); 968 throw BindingSupportImpl.getInstance().runtime(s.toString()); 969 } 970 971 974 public static boolean isOurVendorName(String s) { 975 return VENDOR_NAME.equals(s) || VENDOR_NAME_JDOGENIE.equals(s); 976 } 977 978 } 979 | Popular Tags |