1 23 24 package org.objectweb.jorm.generator.lib; 25 26 import org.objectweb.jorm.api.PException; 27 import org.objectweb.jorm.metainfo.api.Class; 28 import org.objectweb.jorm.metainfo.api.Package; 29 import org.objectweb.jorm.metainfo.api.ClassRef; 30 import org.objectweb.jorm.metainfo.api.CompositeName; 31 import org.objectweb.jorm.metainfo.api.FieldName; 32 import org.objectweb.jorm.metainfo.api.GenClassRef; 33 import org.objectweb.jorm.metainfo.api.Manager; 34 import org.objectweb.jorm.metainfo.api.MetaObject; 35 import org.objectweb.jorm.metainfo.api.NameDef; 36 import org.objectweb.jorm.metainfo.api.NameRef; 37 import org.objectweb.jorm.metainfo.api.PrimitiveElement; 38 import org.objectweb.jorm.metainfo.api.Reference; 39 import org.objectweb.jorm.metainfo.api.TypedElement; 40 import org.objectweb.jorm.metainfo.api.Mapping; 41 import org.objectweb.jorm.metainfo.api.ReferenceMapping; 42 import org.objectweb.jorm.metainfo.api.PrimitiveElementMapping; 43 import org.objectweb.jorm.type.api.PExceptionTyping; 44 import org.objectweb.jorm.type.api.PType; 45 import org.objectweb.jorm.type.api.PTypeSpace; 46 import org.objectweb.jorm.util.api.Loggable; 47 import org.objectweb.jorm.compiler.api.PExceptionCompiler; 48 import org.objectweb.util.monolog.api.BasicLevel; 49 import org.objectweb.util.monolog.api.Logger; 50 import org.objectweb.util.monolog.api.LoggerFactory; 51 52 import java.util.ArrayList ; 53 import java.util.Iterator ; 54 import java.util.Map ; 55 import java.util.Collection ; 56 import java.util.HashMap ; 57 58 63 public class CommonHelper implements Loggable { 64 65 protected LoggerFactory loggerFactory = null; 66 protected Logger logger = null; 67 protected boolean debug = false; 68 69 public final static PType[] PN_GET_TYPES = { 70 PTypeSpace.BYTE, 71 PTypeSpace.CHAR, 72 PTypeSpace.DATE, 73 PTypeSpace.INT, 74 PTypeSpace.LONG, 75 PTypeSpace.OBJBYTE, 76 PTypeSpace.OBJCHAR, 77 PTypeSpace.OBJINT, 78 PTypeSpace.OBJLONG, 79 PTypeSpace.OBJSHORT, 80 PTypeSpace.SHORT, 81 PTypeSpace.STRING, 82 PTypeSpace.BYTEARRAY, 83 PTypeSpace.CHARARRAY, 84 PTypeSpace.BIGDECIMAL, 85 PTypeSpace.BIGINTEGER 86 }; 87 88 96 public String upperFL(String word) { 97 if (word != null && word.length() > 0) 98 return word.substring(0, 1).toUpperCase() + word.substring(1); 99 else 100 return word; 101 } 102 103 112 113 public PrimitiveElement getPrimitiveElement(Object mo, 114 Class c, 115 boolean canBeFieldName) 116 throws PException { 117 if (mo instanceof PrimitiveElement) 118 return (PrimitiveElement) mo; 119 else if (mo instanceof NameDef) { 120 NameDef nd = ((NameDef) mo); 121 if (nd.isFieldName()) 122 return (PrimitiveElement) (c.getTypedElement(nd.getFieldName())); 123 else 124 throw new PException("This is composite name !" + mo); 125 } else 126 throw new PException( 127 "impossible to obtain a PrimitiveElement with " + mo); 128 } 129 130 136 public boolean containsReference(Class co) { 137 Iterator it = co.getAllFields().iterator(); 138 while (it.hasNext()) { 139 if (it.next() instanceof Reference) 140 return true; 141 } 142 return false; 143 } 144 145 162 163 168 169 public ArrayList getCompositeReferences(Mapping mo) throws Exception { 170 ArrayList res = new ArrayList (); 171 Iterator it = mo.getClassMapping().getReferenceMappings().iterator(); 172 while (it.hasNext()) { 173 ReferenceMapping rm = (ReferenceMapping) it.next(); 174 if (((NameDef) rm.getLinkedMO()).isNameRef()) { 175 res.add(((NameDef) rm.getLinkedMO()).getNameRef()); 176 } 177 } 178 return res; 179 } 180 181 212 213 219 public String getCNPackageName(MetaObject mo) { 220 if (mo instanceof Manager) return null; 221 while (!(mo instanceof Package )) { 222 mo = mo.getParent(); 223 } 224 if (mo instanceof Package ) 225 return ((Package ) mo).getName(); 226 else 227 return null; 228 } 229 230 235 public boolean containsSerializedField(Class co) { 236 Iterator it = co.getAllFields().iterator(); 237 while (it.hasNext()) { 238 if (((TypedElement) it.next()).getType().getTypeCode() 239 == PType.TYPECODE_SERIALIZED) 240 return true; 241 } 242 return false; 243 } 244 245 250 public boolean containsCharArrayField(Mapping m) { 251 Class co = (Class ) m.getClassMapping().getLinkedMO(); 252 Iterator it = co.getAllFields().iterator(); 253 while (it.hasNext()) { 254 TypedElement te = (TypedElement) it.next(); 255 if (te == null) { 256 throw new NullPointerException ( 257 "A field of the class '" + co.getFQName() + "' is null."); 258 } 259 if (te.getType() == null) { 260 throw new NullPointerException ( 261 "the type of field '" + te.getName() 262 + "' of the class '" + co.getFQName() + "' is null."); 263 } 264 if (te.getType().getTypeCode() == PType.TYPECODE_CHARARRAY) 265 return true; 266 } 267 it = co.getAllHiddenFields().iterator(); 268 while (it.hasNext()) { 269 if (((TypedElement) it.next()).getType().getTypeCode() 270 == PType.TYPECODE_CHARARRAY) 271 return true; 272 } 273 NameDef nd = (NameDef) 274 m.getClassMapping().getIdentifierMapping().getLinkedMO(); 275 if (nd.isNameRef()) { 276 it = nd.getNameRef().getCompositeName().getAllFields().iterator(); 277 while (it.hasNext()) { 278 if (((TypedElement) it.next()).getType().getTypeCode() 279 == PType.TYPECODE_CHARARRAY) 280 return true; 281 } 282 } 283 return false; 284 } 285 286 291 public boolean containsFieldWhichCanBeNull(Class co) throws PException { 292 for (Iterator it = co.getAllFields().iterator(); it.hasNext();) { 293 if (canBeNullValue(((TypedElement) it.next()).getType())) 294 return true; 295 } 296 for (Iterator it = co.getAllHiddenFields().iterator(); it.hasNext();) { 297 if (canBeNullValue(((TypedElement) it.next()).getType())) 298 return true; 299 } 300 return false; 301 } 302 303 309 public boolean isValidString(String str) { 310 return str != null && str.length() > 0; 311 } 312 313 318 public boolean containsGenClassRef(Class co) { 319 Iterator it = co.getAllFields().iterator(); 320 while (it.hasNext()) { 321 if (it.next() instanceof GenClassRef) 322 return true; 323 } 324 return false; 325 } 326 327 332 public boolean isCompositeName(NameDef nd) { 333 return nd.isNameRef(); 334 } 335 336 343 344 public Collection getCompositeNameDefEntries(NameDef mo) throws Exception { 345 if (mo == null) 346 throw new PException("Null NameDef Parameter"); 347 else if (mo.isNameRef()) 348 return mo.getNameRef().getProjection().entrySet(); 349 else if (mo.isFieldName()) 350 throw new PException("The NameDef is a FieldName (" 351 + mo.getFieldName() + ")"); 352 else if (mo.isSystem()) 353 throw new PException("System identifier unmanaged"); 354 else 355 throw new PException("Unknown NameDef: " + mo); 356 } 357 358 public String getFQNOfCompositeName(Object o) { 359 CompositeName cn = null; 360 if (o instanceof CompositeName) 361 cn = (CompositeName) o; 362 else if (o instanceof NameDef) 363 cn = ((NameDef) o).getNameRef().getCompositeName(); 364 else 365 return ""; 366 367 String packName = ((Package ) cn.getParent()).getName(); 368 if (packName == null || packName.length() == 0) { 369 return cn.getName(); 370 } else { 371 return packName + "." + cn.getName(); 372 } 373 } 374 375 381 public boolean containsCompositeName(Mapping mo) { 382 if (mo == null) { 383 return false; 384 } 385 NameDef nd = (NameDef) 386 mo.getClassMapping().getIdentifierMapping().getLinkedMO(); 387 if (nd.isNameRef()) 388 return true; 389 if (logger != null) 390 logger.log(BasicLevel.DEBUG, "PName class has a single field"); 391 Iterator it = mo.getClassMapping().getReferenceMappings().iterator(); 392 while (it.hasNext()) { 393 ReferenceMapping rm = (ReferenceMapping) it.next(); 394 nd = (NameDef) rm.getLinkedMO(); 395 if (nd.isNameRef()) { 396 return true; 397 } 398 } 399 return false; 400 } 401 402 408 public boolean isClass(Object o) { 409 return o instanceof Class ; 410 } 411 412 418 public boolean isReference(TypedElement te) { 419 return te instanceof Reference; 420 } 421 422 428 public boolean isClassRef(TypedElement te) { 429 return te instanceof ClassRef; 430 } 431 432 438 public boolean isGenClassRef(TypedElement te) { 439 return te instanceof GenClassRef; 440 } 441 442 448 public boolean isPrimitiveElement(Object o) { 449 return o instanceof PrimitiveElement; 450 } 451 452 458 public String comparePE(String val1, String val2, PType type) { 459 if (isObjectType(type)) { 460 return "((" + val1 + " == null && " + val2 + " == null) || (" 461 + val1 + " !=null && " + val1 + ".equals(" + val2 + ")))"; 462 } else { 463 return val1 + " == " + val2; 464 } 465 } 466 467 public boolean isObjectType(PType type) { 468 switch(type.getTypeCode()) { 469 case PType.TYPECODE_OBJBOOLEAN: 470 case PType.TYPECODE_OBJBYTE: 471 case PType.TYPECODE_OBJCHAR: 472 case PType.TYPECODE_DATE: 473 case PType.TYPECODE_CHARARRAY: 474 case PType.TYPECODE_BYTEARRAY: 475 case PType.TYPECODE_OBJSHORT: 476 case PType.TYPECODE_OBJINT: 477 case PType.TYPECODE_OBJLONG: 478 case PType.TYPECODE_OBJFLOAT: 479 case PType.TYPECODE_OBJDOUBLE: 480 case PType.TYPECODE_STRING: 481 case PType.TYPECODE_SERIALIZED: 482 case PType.TYPECODE_BIGDECIMAL: 483 case PType.TYPECODE_BIGINTEGER: 484 return true; 485 case PType.TYPECODE_BOOLEAN: 486 case PType.TYPECODE_BYTE: 487 case PType.TYPECODE_CHAR: 488 case PType.TYPECODE_SHORT: 489 case PType.TYPECODE_INT: 490 case PType.TYPECODE_LONG: 491 case PType.TYPECODE_FLOAT: 492 case PType.TYPECODE_DOUBLE: 493 default: 494 return false; 495 } 496 } 497 498 public boolean isNull(Object o) { 499 return o == null; 500 } 501 502 508 public boolean isHiddenField(PrimitiveElement pe) { 509 return ((Class ) pe.getParent()).getHiddenField(pe.getName()) != null; 510 } 511 512 518 public boolean isFieldName(Object o) { 519 return o instanceof FieldName; 520 } 521 522 528 public boolean isSerializedField(Object o) { 529 return (o instanceof PrimitiveElement) 530 && (((PrimitiveElement) o).getType().getTypeCode() 531 == PType.TYPECODE_SERIALIZED); 532 } 533 534 539 public boolean isSerializedType(PType t) { 540 return t.getTypeCode() == PType.TYPECODE_SERIALIZED; 541 } 542 543 548 public boolean isCharArrayType(PType t) { 549 return t.getTypeCode() == PType.TYPECODE_CHARARRAY; 550 } 551 552 public PrimitiveElementMapping[] buildPemList(Collection pemcoll) { 553 return (PrimitiveElementMapping[]) pemcoll.toArray(new PrimitiveElementMapping[0]); 554 } 555 556 public int getPemPos(PrimitiveElementMapping[] pemlist, PrimitiveElement pe) throws PExceptionCompiler { 557 for (int i = 0; i < pemlist.length; i++) { 558 if (pemlist[i].getLinkedMO() == pe) { 559 return i + 1; 560 } 561 } 562 throw new PExceptionCompiler("Cannot find a mapping for field: " + pe.getName()); 563 } 564 565 public PrimitiveElement getPrimitiveElement(String fn, Class cl) throws PExceptionCompiler { 566 TypedElement res = cl.getTypedElement(fn); 567 if (res == null) { 568 throw new PExceptionCompiler("NameDef with field name does not correspond to any field - searched field: " + fn); 569 } 570 if (!(res instanceof PrimitiveElement)) { 571 throw new PExceptionCompiler("NameDef with field name does not correspond to any primitive field - searched field: " + fn); 572 } 573 return (PrimitiveElement) res; 574 } 575 576 public NameDef getRefNameDef(Mapping mo, Reference field) throws PException { 577 if (field == null) { 578 return null; 579 } 580 if (mo == null) { 581 return null; 582 } 583 ReferenceMapping rm = mo.getClassMapping().getReferenceMapping(field.getName()); 584 NameDef nd = null; 585 if (rm != null) { 586 nd = (NameDef) rm.getLinkedMO(); 587 } 588 if (nd == null) { 589 throw new PException("No NameDef found for the reference field '" 590 + field.getName() +"' of the persistent class '" 591 + ((Class ) mo.getClassMapping().getLinkedMO()).getFQName() 592 + "'."); 593 } 594 return nd; 595 } 596 597 public String getTupleDecoding(String fn, PType pt, String value) 598 throws PException { 599 if (pt == null) { 600 throw new PException("Unauthorized null PType"); 601 } 602 StringBuffer sb = new StringBuffer (); 603 sb.append(fn); 604 sb.append("Pnc.decode"); 605 sb.append(pt.getCodingName()); 606 sb.append("(("); 607 sb.append(pt.getJavaName()); 608 sb.append(") "); 609 sb.append(value); 610 sb.append(")"); 611 return sb.toString(); 612 } 613 614 624 public String getTupleDecoding(String tmpv, String fn, String tuplevn, PType pt, int pos) 625 throws PException { 626 if (pt == null) 627 throw new PException("Unauthorized null PType"); 628 StringBuffer sb = new StringBuffer (); 629 switch (pt.getTypeCode()) { 630 case PType.TYPECODE_BYTE: 631 case PType.TYPECODE_CHAR: 632 case PType.TYPECODE_SHORT: 633 case PType.TYPECODE_INT: 634 case PType.TYPECODE_LONG: 635 sb.append(fn); 636 sb.append("Pnc.decode"); 637 sb.append(pt.getCodingName()); 638 sb.append("("); 639 sb.append(getTupleGetter(tuplevn, pt, pos) ); 640 sb.append(")"); 641 return sb.toString(); 642 case PType.TYPECODE_OBJBYTE: 643 case PType.TYPECODE_OBJCHAR: 644 case PType.TYPECODE_OBJSHORT: 645 case PType.TYPECODE_OBJINT: 646 case PType.TYPECODE_OBJLONG: 647 case PType.TYPECODE_STRING: 648 case PType.TYPECODE_DATE: 649 case PType.TYPECODE_BIGDECIMAL: 650 case PType.TYPECODE_BIGINTEGER: 651 sb.append("((("); 652 sb.append(tmpv); 653 sb.append(" = "); 654 sb.append(getTupleGetter(tuplevn, pt, pos) ); 655 sb.append(") == null) ? "); 656 sb.append(fn); 657 sb.append("Pnc.getNull() : "); 658 sb.append(fn); 659 sb.append("Pnc.decode"); 660 sb.append(pt.getCodingName()); 661 sb.append("(("); 662 sb.append(pt.getJavaName()); 663 sb.append(") "); 664 sb.append(tmpv); 665 sb.append("))"); 666 return sb.toString(); 667 } 668 throw new PExceptionTyping("Unauthorized PType " + pt); 669 } 670 671 679 public String getTupleGetter(String tuplevn, PType pt, int pos) 680 throws PException { 681 if (pt == null) 682 throw new PException("Unauthorized null PType"); 683 switch (pt.getTypeCode()) { 684 case PType.TYPECODE_BOOLEAN: 685 return tuplevn + ".getBoolean(" + pos + ")"; 686 case PType.TYPECODE_OBJBOOLEAN: 687 return "(new Boolean(" + tuplevn + ".getBoolean(" + pos + ")))"; 688 case PType.TYPECODE_BYTE: 689 return tuplevn + ".getByte(" + pos + ")"; 690 case PType.TYPECODE_OBJBYTE: 691 return "(new Byte(" + tuplevn + ".getByte(" + pos + ")))"; 692 case PType.TYPECODE_CHAR: 693 return tuplevn + ".getChar(" + pos + ")"; 694 case PType.TYPECODE_OBJCHAR: 695 return "(new Character(" + tuplevn + ".getChar(" + pos + ")))"; 696 case PType.TYPECODE_DATE: 697 return tuplevn + ".getDate(" + pos + ")"; 698 case PType.TYPECODE_CHARARRAY: 699 return tuplevn + ".getCharArray(" + pos + ")"; 700 case PType.TYPECODE_BYTEARRAY: 701 return tuplevn + ".getByteArray(" + pos + ")"; 702 case PType.TYPECODE_SHORT: 703 return tuplevn + ".getShort(" + pos + ")"; 704 case PType.TYPECODE_OBJSHORT: 705 return "(new Short(" + tuplevn + ".getShort(" + pos + ")))"; 706 case PType.TYPECODE_INT: 707 return tuplevn + ".getInt(" + pos + ")"; 708 case PType.TYPECODE_OBJINT: 709 return "(new Integer( " + tuplevn + ".getInt(" + pos + ")))"; 710 case PType.TYPECODE_LONG: 711 return tuplevn + ".getLong(" + pos + ")"; 712 case PType.TYPECODE_OBJLONG: 713 return "(new Long(" + tuplevn + ".getLong(" + pos + ")))"; 714 case PType.TYPECODE_FLOAT: 715 return tuplevn + ".getFloat(" + pos + ")"; 716 case PType.TYPECODE_OBJFLOAT: 717 return "(new Float(" + tuplevn + ".getFloat(" + pos + ")))"; 718 case PType.TYPECODE_DOUBLE: 719 return tuplevn + ".getDouble(" + pos + ")"; 720 case PType.TYPECODE_OBJDOUBLE: 721 return "(new Double(" + tuplevn + ".getDouble(" + pos + ")))"; 722 case PType.TYPECODE_STRING: 723 return tuplevn + ".getString(" + pos + ")"; 724 case PType.TYPECODE_SERIALIZED: 725 return "(Serializable) " + tuplevn + ".getObject(" + pos + ")"; 726 case PType.TYPECODE_BIGDECIMAL: 727 return tuplevn + ".getBigDecimal(" + pos + ")"; 728 case PType.TYPECODE_BIGINTEGER: 729 return tuplevn + ".getBigInteger(" + pos + ")"; 730 } 731 throw new PExceptionTyping("Unauthorized PType " + pt); 732 } 733 734 741 public String getPaGetMethod(PType pt) throws PException { 742 if (pt == null) 743 throw new PException("Unauthorized null PType"); 744 switch (pt.getTypeCode()) { 745 case PType.TYPECODE_BOOLEAN: 746 return "paGetBooleanField"; 747 case PType.TYPECODE_OBJBOOLEAN: 748 return "paGetObooleanField"; 749 case PType.TYPECODE_BYTE: 750 return "paGetByteField"; 751 case PType.TYPECODE_OBJBYTE: 752 return "paGetObyteField"; 753 case PType.TYPECODE_CHAR: 754 return "paGetCharField"; 755 case PType.TYPECODE_OBJCHAR: 756 return "paGetOcharField"; 757 case PType.TYPECODE_DATE: 758 return "paGetDateField"; 759 case PType.TYPECODE_CHARARRAY: 760 return "paGetCharArrayField"; 761 case PType.TYPECODE_BYTEARRAY: 762 return "paGetByteArrayField"; 763 case PType.TYPECODE_SHORT: 764 return "paGetShortField"; 765 case PType.TYPECODE_OBJSHORT: 766 return "paGetOshortField"; 767 case PType.TYPECODE_INT: 768 return "paGetIntField"; 769 case PType.TYPECODE_OBJINT: 770 return "paGetOintField"; 771 case PType.TYPECODE_LONG: 772 return "paGetLongField"; 773 case PType.TYPECODE_OBJLONG: 774 return "paGetOlongField"; 775 case PType.TYPECODE_FLOAT: 776 return "paGetFloatField"; 777 case PType.TYPECODE_OBJFLOAT: 778 return "paGetOfloatField"; 779 case PType.TYPECODE_DOUBLE: 780 return "paGetDoubleField"; 781 case PType.TYPECODE_OBJDOUBLE: 782 return "paGetOdoubleField"; 783 case PType.TYPECODE_STRING: 784 return "paGetStringField"; 785 case PType.TYPECODE_SERIALIZED: 786 return "paGetSerializedField"; 787 case PType.TYPECODE_BIGINTEGER: 788 return "paGetBigIntegerField"; 789 case PType.TYPECODE_BIGDECIMAL: 790 return "paGetBigDecimalField"; 791 case PType.TYPECODE_REFERENCE: 792 return "paGetRefField"; 793 } 794 throw new PExceptionTyping("Unauthorized PType " + pt); 795 } 796 797 804 public String getPaSetMethod(PType pt) throws PException { 805 if (pt == null) 806 throw new PException("Unauthorized null PType"); 807 808 switch (pt.getTypeCode()) { 809 case PType.TYPECODE_BOOLEAN: 810 return "paSetBooleanField"; 811 case PType.TYPECODE_OBJBOOLEAN: 812 return "paSetObooleanField"; 813 case PType.TYPECODE_BYTE: 814 return "paSetByteField"; 815 case PType.TYPECODE_OBJBYTE: 816 return "paSetObyteField"; 817 case PType.TYPECODE_CHAR: 818 return "paSetCharField"; 819 case PType.TYPECODE_OBJCHAR: 820 return "paSetOcharField"; 821 case PType.TYPECODE_DATE: 822 return "paSetDateField"; 823 case PType.TYPECODE_CHARARRAY: 824 return "paSetCharArrayField"; 825 case PType.TYPECODE_BYTEARRAY: 826 return "paSetByteArrayField"; 827 case PType.TYPECODE_SHORT: 828 return "paSetShortField"; 829 case PType.TYPECODE_OBJSHORT: 830 return "paSetOshortField"; 831 case PType.TYPECODE_INT: 832 return "paSetIntField"; 833 case PType.TYPECODE_OBJINT: 834 return "paSetOintField"; 835 case PType.TYPECODE_LONG: 836 return "paSetLongField"; 837 case PType.TYPECODE_OBJLONG: 838 return "paSetOlongField"; 839 case PType.TYPECODE_FLOAT: 840 return "paSetFloatField"; 841 case PType.TYPECODE_OBJFLOAT: 842 return "paSetOfloatField"; 843 case PType.TYPECODE_DOUBLE: 844 return "paSetDoubleField"; 845 case PType.TYPECODE_OBJDOUBLE: 846 return "paSetOdoubleField"; 847 case PType.TYPECODE_STRING: 848 return "paSetStringField"; 849 case PType.TYPECODE_SERIALIZED: 850 return "paSetSerializedField"; 851 case PType.TYPECODE_BIGINTEGER: 852 return "paSetBigIntegerField"; 853 case PType.TYPECODE_BIGDECIMAL: 854 return "paSetBigDecimalField"; 855 case PType.TYPECODE_REFERENCE: 856 return "paSetRefField"; 857 } 858 throw new PExceptionTyping("Unauthorized PType " + pt); 859 } 860 861 871 public String getPNameDecodeFunction(PType pt) throws PException { 872 if (pt == null) { 873 throw new PExceptionTyping("The parameter must be not null"); 874 } 875 switch (pt.getTypeCode()) { 876 case PType.TYPECODE_CHAR: 877 return "decodeChar"; 878 case PType.TYPECODE_OBJCHAR: 879 return "decodeOchar"; 880 case PType.TYPECODE_BYTE: 881 return "decodeByte"; 882 case PType.TYPECODE_OBJBYTE: 883 return "decodeObyte"; 884 case PType.TYPECODE_SHORT: 885 return "decodeShort"; 886 case PType.TYPECODE_OBJSHORT: 887 return "decodeOshort"; 888 case PType.TYPECODE_INT: 889 return "decodeInt"; 890 case PType.TYPECODE_OBJINT: 891 return "decodeOint"; 892 case PType.TYPECODE_LONG: 893 return "decodeLong"; 894 case PType.TYPECODE_OBJLONG: 895 return "decodeOlong"; 896 case PType.TYPECODE_STRING: 897 return "decodeString"; 898 case PType.TYPECODE_DATE: 899 return "decodeDate"; 900 case PType.TYPECODE_BYTEARRAY: 901 return "decode"; 902 case PType.TYPECODE_CHARARRAY: 903 return "decodeCharArray"; 904 case PType.TYPECODE_BIGINTEGER: 905 return "decodeBigInteger"; 906 case PType.TYPECODE_BIGDECIMAL: 907 return "decodeBigDecimal"; 908 default : 909 throw new PExceptionTyping( 910 "Jorm does not support naming with the following PType:" 911 + pt.getTypeCode()); 912 } 913 } 914 915 public String getCoderName(PType t) { 916 switch (t.getTypeCode()) { 917 case PType.TYPECODE_BYTE: 918 return "Byte"; 919 case PType.TYPECODE_OBJBYTE: 920 return "Obyte"; 921 case PType.TYPECODE_CHAR: 922 return "Char"; 923 case PType.TYPECODE_OBJCHAR: 924 return "Ochar"; 925 case PType.TYPECODE_DATE: 926 return "Date"; 927 case PType.TYPECODE_BIGDECIMAL: 928 return "BigDecimal"; 929 case PType.TYPECODE_BIGINTEGER: 930 return "BigInteger"; 931 case PType.TYPECODE_SHORT: 932 return "Short"; 933 case PType.TYPECODE_OBJSHORT: 934 return "Oshort"; 935 case PType.TYPECODE_INT: 936 return "Int"; 937 case PType.TYPECODE_OBJINT: 938 return "Oint"; 939 case PType.TYPECODE_LONG: 940 return "Long"; 941 case PType.TYPECODE_OBJLONG: 942 return "Olong"; 943 case PType.TYPECODE_STRING: 944 return "String"; 945 case PType.TYPECODE_BYTEARRAY: 946 return ""; 947 case PType.TYPECODE_CHARARRAY: 948 return "CharArray"; 949 } 950 return null; 951 } 952 953 963 public String getPNameGetterGetFunction(PType pt) throws PException { 964 if (pt == null) { 965 throw new PExceptionTyping("The parameter must be not null"); 966 } 967 switch (pt.getTypeCode()) { 968 case PType.TYPECODE_CHAR: 969 return "pngetCharField"; 970 case PType.TYPECODE_OBJCHAR: 971 return "pngetOcharField"; 972 case PType.TYPECODE_BYTE: 973 return "pngetByteField"; 974 case PType.TYPECODE_OBJBYTE: 975 return "pngetObyteField"; 976 case PType.TYPECODE_SHORT: 977 return "pngetShortField"; 978 case PType.TYPECODE_OBJSHORT: 979 return "pngetOshortField"; 980 case PType.TYPECODE_INT: 981 return "pngetIntField"; 982 case PType.TYPECODE_OBJINT: 983 return "pngetOintField"; 984 case PType.TYPECODE_LONG: 985 return "pngetLongField"; 986 case PType.TYPECODE_OBJLONG: 987 return "pngetOlongField"; 988 case PType.TYPECODE_STRING: 989 return "pngetStringField"; 990 case PType.TYPECODE_DATE: 991 return "pngetDateField"; 992 case PType.TYPECODE_CHARARRAY: 993 return "pngetCharArrayField"; 994 case PType.TYPECODE_BYTEARRAY: 995 return "pngetByteArrayField"; 996 case PType.TYPECODE_BIGINTEGER: 997 return "pngetBigIntegerField"; 998 case PType.TYPECODE_BIGDECIMAL: 999 return "pngetBigDecimalField"; 1000 default : 1001 throw new PExceptionTyping( 1002 "Jorm does not support naming with the following PType:" 1003 + pt.getTypeCode()); 1004 } 1005 1006 } 1007 1008 1018 public String getPNameEncodeFunction(PType pt) throws PException { 1019 if (pt == null) { 1020 throw new PExceptionTyping("The parameter must be not null"); 1021 } 1022 switch (pt.getTypeCode()) { 1023 case PType.TYPECODE_CHAR: 1024 return "encodeChar"; 1025 case PType.TYPECODE_OBJCHAR: 1026 return "encodeOchar"; 1027 case PType.TYPECODE_BYTE: 1028 return "encodeByte"; 1029 case PType.TYPECODE_OBJBYTE: 1030 return "encodeObyte"; 1031 case PType.TYPECODE_SHORT: 1032 return "encodeShort"; 1033 case PType.TYPECODE_OBJSHORT: 1034 return "encodeOshort"; 1035 case PType.TYPECODE_INT: 1036 return "encodeInt"; 1037 case PType.TYPECODE_OBJINT: 1038 return "encodeOint"; 1039 case PType.TYPECODE_LONG: 1040 return "encodeLong"; 1041 case PType.TYPECODE_OBJLONG: 1042 return "encodeOlong"; 1043 case PType.TYPECODE_STRING: 1044 return "encodeString"; 1045 case PType.TYPECODE_DATE: 1046 return "encodeDate"; 1047 case PType.TYPECODE_BYTEARRAY: 1048 return "encode"; 1049 case PType.TYPECODE_CHARARRAY: 1050 return "encodeCharArray"; 1051 case PType.TYPECODE_BIGINTEGER: 1052 return "encodeBigInteger"; 1053 case PType.TYPECODE_BIGDECIMAL: 1054 return "encodeBigDecimal"; 1055 default : 1056 throw new PExceptionTyping( 1057 "Jorm does not support naming with the following PType:" 1058 + pt.getTypeCode()); 1059 } 1060 } 1061 1062 public String getCTDeclaration(PType t) { 1063 return getCTDeclaration(t.getTypeCode()); 1064 } 1065 1066 public String getCTDeclaration(int typecode) { 1067 switch (typecode) { 1068 case PType.TYPECODE_CHAR: 1069 return "PNameCoder.CTCHAR"; 1070 case PType.TYPECODE_OBJCHAR: 1071 return "PNameCoder.CTOCHAR"; 1072 1073 case PType.TYPECODE_BYTE: 1074 return "PNameCoder.CTBYTE"; 1075 case PType.TYPECODE_OBJBYTE: 1076 return "PNameCoder.CTOBYTE"; 1077 1078 case PType.TYPECODE_SHORT: 1079 return "PNameCoder.CTSHORT"; 1080 case PType.TYPECODE_OBJSHORT: 1081 return "PNameCoder.CTOSHORT"; 1082 1083 case PType.TYPECODE_INT: 1084 return "PNameCoder.CTINT"; 1085 case PType.TYPECODE_OBJINT: 1086 return "PNameCoder.CTOINT"; 1087 1088 case PType.TYPECODE_LONG: 1089 return "PNameCoder.CTLONG"; 1090 case PType.TYPECODE_OBJLONG: 1091 return "PNameCoder.CTOLONG"; 1092 1093 case PType.TYPECODE_STRING: 1094 return "PNameCoder.CTSTRING"; 1095 1096 case PType.TYPECODE_BYTEARRAY: 1097 return "PNameCoder.CTBYTEARRAY"; 1098 case PType.TYPECODE_CHARARRAY: 1099 return "PNameCoder.CTCHARARRAY"; 1100 case PType.TYPECODE_DATE: 1101 return "PNameCoder.CTDATE"; 1102 case PType.TYPECODE_BIGDECIMAL: 1103 return "PNameCoder.CTBIGDECIMAL"; 1104 case PType.TYPECODE_BIGINTEGER: 1105 return "PNameCoder.CTBIGINTEGER"; 1106 } 1107 return null; 1108 } 1109 1110 1117 1118 public String getProjectionField(NameDef nd, String cfn) throws Exception { 1119 if (nd.isNameRef()) { 1120 NameRef nr = nd.getNameRef(); 1121 Iterator it = nr.getProjection().entrySet().iterator(); 1125 while (it.hasNext()) { 1126 Map.Entry me = (Map.Entry ) (it.next()); 1127 if (me.getValue().equals(cfn)) 1128 return (String ) (me.getKey()); 1129 } 1130 } 1131 throw new PException("impossible to obtain field " + cfn + " of " + nd); 1132 } 1133 1134 public PType getPType(String ptn) { 1135 if (ptn.equals("boolean")) return PTypeSpace.BOOLEAN; 1136 if (ptn.equals("Boolean")) return PTypeSpace.OBJBOOLEAN; 1137 if (ptn.equals("char")) return PTypeSpace.CHAR; 1138 if (ptn.equals("Character")) return PTypeSpace.OBJCHAR; 1139 if (ptn.equals("byte")) return PTypeSpace.BYTE; 1140 if (ptn.equals("Byte")) return PTypeSpace.OBJBYTE; 1141 if (ptn.equals("short")) return PTypeSpace.SHORT; 1142 if (ptn.equals("Short")) return PTypeSpace.OBJSHORT; 1143 if (ptn.equals("int")) return PTypeSpace.INT; 1144 if (ptn.equals("Integer")) return PTypeSpace.OBJINT; 1145 if (ptn.equals("long")) return PTypeSpace.LONG; 1146 if (ptn.equals("Long")) return PTypeSpace.OBJLONG; 1147 if (ptn.equals("float")) return PTypeSpace.FLOAT; 1148 if (ptn.equals("Float")) return PTypeSpace.OBJFLOAT; 1149 if (ptn.equals("double")) return PTypeSpace.DOUBLE; 1150 if (ptn.equals("Double")) return PTypeSpace.OBJDOUBLE; 1151 if (ptn.equals("string")) return PTypeSpace.STRING; 1152 if (ptn.equals("date")) return PTypeSpace.DATE; 1153 if (ptn.equals("serialized")) return PTypeSpace.SERIALIZED; 1154 if (ptn.equals("BigInteger")) return PTypeSpace.BIGINTEGER; 1155 if (ptn.equals("BigDecimal")) return PTypeSpace.BIGDECIMAL; 1156 if (ptn.equals("byte[]")) return PTypeSpace.BYTEARRAY; 1157 if (ptn.equals("char[]")) return PTypeSpace.CHARARRAY; 1158 if (ptn.equals("reference")) return PTypeSpace.REFTOP; 1159 return null; 1160 } 1161 1162 1165 public PType getPTypeBoolean() { 1166 return PTypeSpace.BOOLEAN; 1167 } 1168 1169 1172 public PType getPTypeOboolean() { 1173 return PTypeSpace.OBJBOOLEAN; 1174 } 1175 1176 1179 public PType getPTypeChar() { 1180 return PTypeSpace.CHAR; 1181 } 1182 1183 1186 public PType getPTypeOchar() { 1187 return PTypeSpace.OBJCHAR; 1188 } 1189 1190 1193 public PType getPTypeByte() { 1194 return PTypeSpace.BYTE; 1195 } 1196 1197 1200 public PType getPTypeObyte() { 1201 return PTypeSpace.OBJBYTE; 1202 } 1203 1204 1207 public PType getPTypeShort() { 1208 return PTypeSpace.SHORT; 1209 } 1210 1211 1214 public PType getPTypeOshort() { 1215 return PTypeSpace.OBJSHORT; 1216 } 1217 1218 1221 public PType getPTypeInt() { 1222 return PTypeSpace.INT; 1223 } 1224 1225 1228 public PType getPTypeOint() { 1229 return PTypeSpace.OBJINT; 1230 } 1231 1232 1235 public PType getPTypeLong() { 1236 return PTypeSpace.LONG; 1237 } 1238 1239 1242 public PType getPTypeOlong() { 1243 return PTypeSpace.OBJLONG; 1244 } 1245 1246 1249 public PType getPTypeFloat() { 1250 return PTypeSpace.FLOAT; 1251 } 1252 1253 1256 public PType getPTypeOfloat() { 1257 return PTypeSpace.OBJFLOAT; 1258 } 1259 1260 1263 public PType getPTypeDouble() { 1264 return PTypeSpace.DOUBLE; 1265 } 1266 1267 1270 public PType getPTypeOdouble() { 1271 return PTypeSpace.OBJDOUBLE; 1272 } 1273 1274 1277 public PType getPTypeString() { 1278 return PTypeSpace.STRING; 1279 } 1280 1281 1284 public PType getPTypeDate() { 1285 return PTypeSpace.DATE; 1286 } 1287 1288 1291 public PType getPTypeCharArray() { 1292 return PTypeSpace.CHARARRAY; 1293 } 1294 1295 1298 public PType getPTypeByteArray() { 1299 return PTypeSpace.BYTEARRAY; 1300 } 1301 1302 1305 public PType getPTypeSerialized() { 1306 return PTypeSpace.SERIALIZED; 1307 } 1308 1309 1312 public PType getPTypeBigInteger() { 1313 return PTypeSpace.BIGINTEGER; 1314 } 1315 1316 1319 public PType getPTypeBigDecimal() { 1320 return PTypeSpace.BIGDECIMAL; 1321 } 1322 1323 1326 public PType getPTypeReference() { 1327 return PTypeSpace.REFTOP; 1328 } 1329 1330 public PType[] getPNGTypes() { 1331 return PN_GET_TYPES; 1332 } 1333 1334 public boolean not(boolean b) { 1335 return !b; 1336 } 1337 1338 public boolean canBeNullValue(PType type) throws PException { 1339 if (type == null) { 1340 throw new PException("canBeNullValue: Unauthorized null PType parameter"); 1341 } 1342 switch (type.getTypeCode()) { 1343 case PType.TYPECODE_BOOLEAN: 1344 1345 case PType.TYPECODE_CHAR: 1346 case PType.TYPECODE_BYTE: 1347 case PType.TYPECODE_SHORT: 1348 case PType.TYPECODE_INT: 1349 case PType.TYPECODE_LONG: 1350 case PType.TYPECODE_FLOAT: 1351 case PType.TYPECODE_DOUBLE: 1352 case PType.TYPECODE_REFERENCE: 1353 return false; 1354 case PType.TYPECODE_OBJBOOLEAN: 1355 case PType.TYPECODE_OBJCHAR: 1356 case PType.TYPECODE_OBJBYTE: 1357 case PType.TYPECODE_OBJSHORT: 1358 case PType.TYPECODE_OBJINT: 1359 case PType.TYPECODE_OBJLONG: 1360 case PType.TYPECODE_OBJFLOAT: 1361 case PType.TYPECODE_OBJDOUBLE: 1362 case PType.TYPECODE_STRING: 1363 case PType.TYPECODE_DATE: 1364 case PType.TYPECODE_CHARARRAY: 1365 case PType.TYPECODE_BYTEARRAY: 1366 case PType.TYPECODE_SERIALIZED: 1367 case PType.TYPECODE_BIGINTEGER: 1368 case PType.TYPECODE_BIGDECIMAL: 1369 return true; 1370 default: 1371 throw new PExceptionTyping("Unknown type " + type.getJormName()); 1372 } 1373 } 1374 1375 1380 public String getInheritedClasses(Class c) { 1381 Map m = getInheritedClasses(c, new HashMap (), false); 1382 StringBuffer sb = new StringBuffer (); 1383 sb.append('{'); 1384 Iterator it = m.entrySet().iterator(); 1385 String sep = ""; 1386 while(it.hasNext()) { 1387 Map.Entry me = (Map.Entry ) it.next(); 1388 String child = (String ) me.getKey(); 1389 String [] parents = (String []) me.getValue(); 1390 for(int i=0; i<parents.length; i++) { 1391 sb.append(sep); 1392 sb.append("{\""); 1393 sb.append(child); 1394 sb.append("\", \""); 1395 sb.append(parents[i]); 1396 sb.append("\"}"); 1397 sep =", "; 1398 } 1399 } 1400 sb.append('}'); 1401 return sb.toString(); 1402 } 1403 1404 1410 private HashMap getInheritedClasses(Class current, 1411 HashMap calculated, 1412 boolean addCurrent) { 1413 int parentNumber = current.getInheritedClassNumber(); 1414 if (parentNumber == 0) 1416 return calculated; 1417 1418 int addcurrent = (addCurrent ? 1 : 0); 1419 String [] list = new String [parentNumber + addcurrent]; 1420 if (addCurrent) { 1421 list[0] = current.getFQName(); 1423 } 1424 calculated.put(current.getFQName(), list); 1426 Iterator it = current.getSuperClasses().iterator(); 1428 int i; 1429 for (i = addcurrent; it.hasNext() && i < list.length; i++) { 1430 Class parent = (Class ) it.next(); 1431 list[i] = parent.getFQName(); 1432 if (!calculated.containsKey(parent.getFQName())) 1433 getInheritedClasses(parent, calculated, false); 1434 } 1435 if (it.hasNext() || i < list.length) { 1436 throw new RuntimeException ("MalFormed JMI"); 1437 } 1438 return calculated; 1439 } 1440 1441 public String log(String msg) { 1442 System.out.println(msg); 1443 return msg; 1444 } 1445 1446 public String debug(String msg) { 1447 if (logger != null && logger.isLoggable(BasicLevel.DEBUG)) { 1448 logger.log(BasicLevel.DEBUG, msg); 1449 return "//" + msg; 1450 } 1451 return ""; 1452 } 1453 1454 1458 1461 public void setLoggerFactory(LoggerFactory lf) { 1462 if (logger == null && lf != null) 1463 setLogger(lf.getLogger(getClass().getName())); 1464 loggerFactory = lf; 1465 } 1466 1467 1470 public void setLogger(Logger l) { 1471 logger = l; 1472 debug = logger != null && logger.isLoggable(BasicLevel.DEBUG); 1473 } 1474 1475 1478 public Logger getLogger() { 1479 return logger; 1480 } 1481 1482 1485 public LoggerFactory getLoggerFactory() { 1486 return loggerFactory; 1487 } 1488} 1489 | Popular Tags |