1 56 package org.objectstyle.cayenne.map; 57 58 import java.io.IOException ; 59 import java.io.InputStream ; 60 import java.util.Map ; 61 import java.util.TreeMap ; 62 63 import org.apache.log4j.Logger; 64 import org.objectstyle.cayenne.conf.Configuration; 65 import org.objectstyle.cayenne.dba.TypesMapping; 66 import org.objectstyle.cayenne.exp.Expression; 67 import org.objectstyle.cayenne.project.DataMapFile; 68 import org.objectstyle.cayenne.util.ResourceLocator; 69 import org.objectstyle.cayenne.util.Util; 70 import org.xml.sax.Attributes ; 71 import org.xml.sax.InputSource ; 72 import org.xml.sax.SAXException ; 73 import org.xml.sax.SAXParseException ; 74 import org.xml.sax.XMLReader ; 75 import org.xml.sax.helpers.DefaultHandler ; 76 77 85 public class MapLoader extends DefaultHandler { 86 private static final Logger logObj = Logger.getLogger(MapLoader.class); 87 88 public static final String DATA_MAP_TAG = "data-map"; 89 public static final String PROPERTY_TAG = "property"; 90 public static final String DB_ENTITY_TAG = "db-entity"; 91 public static final String OBJ_ENTITY_TAG = "obj-entity"; 92 public static final String DB_ATTRIBUTE_TAG = "db-attribute"; 93 public static final String DB_ATTRIBUTE_DERIVED_TAG = "db-attribute-derived"; 94 public static final String DB_ATTRIBUTE_REF_TAG = "db-attribute-ref"; 95 public static final String OBJ_ATTRIBUTE_TAG = "obj-attribute"; 96 public static final String OBJ_RELATIONSHIP_TAG = "obj-relationship"; 97 public static final String DB_RELATIONSHIP_TAG = "db-relationship"; 98 public static final String DB_RELATIONSHIP_REF_TAG = "db-relationship-ref"; 99 public static final String DB_ATTRIBUTE_PAIR_TAG = "db-attribute-pair"; 100 public static final String PROCEDURE_TAG = "procedure"; 101 public static final String PROCEDURE_PARAMETER_TAG = "procedure-parameter"; 102 103 public static final String QUERY_TAG = "query"; 105 public static final String QUERY_RESULT_COLUMN_TAG = "result-column"; 106 public static final String QUERY_SQL_TAG = "sql"; 107 public static final String QUERY_QUALIFIER_TAG = "qualifier"; 108 public static final String QUERY_ORDERING_TAG = "ordering"; 109 public static final String QUERY_PREFETCH_TAG = "prefetch"; 110 111 public static final String TRUE = "true"; 112 public static final String FALSE = "false"; 113 114 public static final String DB_KEY_GENERATOR_TAG = "db-key-generator"; 115 public static final String DB_GENERATOR_TYPE_TAG = "db-generator-type"; 116 public static final String DB_GENERATOR_NAME_TAG = "db-generator-name"; 117 public static final String DB_KEY_CACHE_SIZE_TAG = "db-key-cache-size"; 118 119 private DataMap dataMap; 121 private DbEntity dbEntity; 122 private ObjEntity objEntity; 123 private DbRelationship dbRelationship; 124 private ObjRelationship objRelationship; 125 private DbAttribute attrib; 126 private Procedure procedure; 127 private QueryBuilder queryBuilder; 128 private String sqlKey; 129 private String descending; 130 private String ignoreCase; 131 132 private String currentTag; 133 private StringBuffer charactersBuffer; 134 private Map mapProperties; 135 136 139 public synchronized DataMap loadDataMap(InputSource src) throws DataMapException { 140 if (src == null) { 141 throw new NullPointerException ("Null InputSource."); 142 } 143 144 try { 145 String mapName = mapNameFromLocation(src.getSystemId()); 146 dataMap = new DataMap(mapName); 147 XMLReader parser = Util.createXmlReader(); 148 149 parser.setContentHandler(this); 150 parser.setErrorHandler(this); 151 parser.parse(src); 152 } 153 catch (SAXException e) { 154 dataMap = null; 155 throw new DataMapException( 156 "Wrong DataMap format, last processed tag: <" + currentTag, 157 Util.unwindException(e)); 158 } 159 catch (Exception e) { 160 dataMap = null; 161 throw new DataMapException( 162 "Error loading DataMap, last processed tag: <" + currentTag, 163 Util.unwindException(e)); 164 } 165 return dataMap; 166 } 167 168 173 public DataMap loadDataMap(String uri) throws DataMapException { 174 ResourceLocator locator = configLocator(); 176 InputStream in = locator.findResourceStream(uri); 177 if (in == null) { 178 throw new DataMapException("Can't find data map " + uri); 179 } 180 181 try { 182 InputSource inSrc = new InputSource (in); 183 inSrc.setSystemId(uri); 184 return loadDataMap(inSrc); 185 } 186 finally { 187 try { 188 in.close(); 189 } 190 catch (IOException ioex) { 191 } 192 } 193 } 194 195 198 protected String mapNameFromLocation(String location) { 199 if (location == null) { 200 return "Untitled"; 201 } 202 203 int lastSlash = location.lastIndexOf('/'); 204 if (lastSlash < 0) { 205 lastSlash = location.lastIndexOf('\\'); 206 } 207 208 if (lastSlash >= 0 && lastSlash + 1 < location.length()) { 209 location = location.substring(lastSlash + 1); 210 } 211 212 if (location.endsWith(DataMapFile.LOCATION_SUFFIX)) { 213 location = 214 location.substring( 215 0, 216 location.length() - DataMapFile.LOCATION_SUFFIX.length()); 217 } 218 219 return location; 220 } 221 222 226 protected ResourceLocator configLocator() { 227 ResourceLocator locator = new ResourceLocator(); 228 locator.setSkipAbsolutePath(true); 229 locator.setSkipClasspath(false); 230 locator.setSkipCurrentDirectory(false); 231 locator.setSkipHomeDirectory(false); 232 233 locator.setClassLoader(Configuration.getResourceLoader()); 238 239 return locator; 240 } 241 242 public void startElement( 243 String namespaceUri, 244 String localName, 245 String qName, 246 Attributes attributes) 247 throws SAXException { 248 249 rememberCurrentTag(localName); 250 if (localName.equals(DATA_MAP_TAG)) { 251 } 252 else if (localName.equals(DB_ENTITY_TAG)) { 253 processStartDbEntity(attributes); 254 } 255 else if (localName.equals(DB_ATTRIBUTE_TAG)) { 256 processStartDbAttribute(attributes); 257 } 258 else if (localName.equals(DB_ATTRIBUTE_DERIVED_TAG)) { 259 processStartDerivedDbAttribute(attributes); 260 } 261 else if (localName.equals(DB_ATTRIBUTE_REF_TAG)) { 262 processStartDbAttributeRef(attributes); 263 } 264 else if (localName.equals(OBJ_ENTITY_TAG)) { 265 processStartObjEntity(attributes); 266 } 267 else if (localName.equals(OBJ_ATTRIBUTE_TAG)) { 268 processStartObjAttribute(attributes); 269 } 270 else if (localName.equals(DB_RELATIONSHIP_TAG)) { 271 processStartDbRelationship(attributes); 272 } 273 else if (localName.equals(DB_ATTRIBUTE_PAIR_TAG)) { 274 processStartDbAttributePair(attributes); 275 } 276 else if (localName.equals(OBJ_RELATIONSHIP_TAG)) { 277 processStartObjRelationship(attributes); 278 } 279 else if (localName.equals(DB_RELATIONSHIP_REF_TAG)) { 280 processStartDbRelationshipRef(attributes); 281 } 282 else if (localName.equals(PROCEDURE_PARAMETER_TAG)) { 283 processStartProcedureParameter(attributes); 284 } 285 else if (localName.equals(PROCEDURE_TAG)) { 286 processStartProcedure(attributes); 287 } 288 else if (localName.equals(QUERY_TAG)) { 289 processStartQuery(attributes); 290 } 291 else if (localName.equals(QUERY_RESULT_COLUMN_TAG)) { 292 processStartQueryResultColumn(attributes); 293 } 294 else if (localName.equals(QUERY_SQL_TAG)) { 295 charactersBuffer = new StringBuffer (); 296 processStartQuerySQL(attributes); 297 } 298 else if (localName.equals(QUERY_ORDERING_TAG)) { 299 charactersBuffer = new StringBuffer (); 300 processStartQueryOrdering(attributes); 301 } 302 else if (localName.equals(QUERY_PREFETCH_TAG)) { 303 charactersBuffer = new StringBuffer (); 304 } 305 else if (localName.equals(QUERY_QUALIFIER_TAG)) { 306 charactersBuffer = new StringBuffer (); 307 } 308 else if (localName.equals(DB_KEY_GENERATOR_TAG)) { 309 processStartDbKeyGenerator(attributes); 310 } 311 else if (localName.equals(DB_GENERATOR_TYPE_TAG)) { 312 charactersBuffer = new StringBuffer (); 313 } 314 else if (localName.equals(DB_GENERATOR_NAME_TAG)) { 315 charactersBuffer = new StringBuffer (); 316 } 317 else if (localName.equals(DB_KEY_CACHE_SIZE_TAG)) { 318 charactersBuffer = new StringBuffer (); 319 } 320 else if (localName.equals(PROPERTY_TAG)) { 322 if (queryBuilder != null) { 323 processStartQueryProperty(attributes); 324 } 325 else { 326 processStartDataMapProperty(attributes); 327 } 328 } 329 } 330 331 public void endElement(String namespaceURI, String local_name, String qName) 332 throws SAXException { 333 if (local_name.equals(DATA_MAP_TAG)) { 334 processEndDataMap(); 335 } 336 else if (local_name.equals(DB_ENTITY_TAG)) { 337 processEndDbEntity(); 338 } 339 else if (local_name.equals(OBJ_ENTITY_TAG)) { 340 processEndObjEntity(); 341 } 342 else if (local_name.equals(DB_ATTRIBUTE_TAG)) { 343 processEndDbAttribute(); 344 } 345 else if (local_name.equals(DB_ATTRIBUTE_DERIVED_TAG)) { 346 processEndDbAttribute(); 347 } 348 else if (local_name.equals(DB_RELATIONSHIP_TAG)) { 349 processEndDbRelationship(); 350 } 351 else if (local_name.equals(OBJ_RELATIONSHIP_TAG)) { 352 processEndObjRelationship(); 353 } 354 else if (local_name.equals(DB_KEY_GENERATOR_TAG)) { 355 } 356 else if (local_name.equals(DB_GENERATOR_TYPE_TAG)) { 357 processEndDbGeneratorType(); 358 } 359 else if (local_name.equals(DB_GENERATOR_NAME_TAG)) { 360 processEndDbGeneratorName(); 361 } 362 else if (local_name.equals(DB_KEY_CACHE_SIZE_TAG)) { 363 processEndDbKeyCacheSize(); 364 } 365 else if (local_name.equals(PROCEDURE_PARAMETER_TAG)) { 366 processEndProcedureParameter(); 367 } 368 else if (local_name.equals(PROCEDURE_TAG)) { 369 processEndProcedure(); 370 } 371 else if (local_name.equals(QUERY_TAG)) { 372 processEndQuery(); 373 } 374 else if (local_name.equals(QUERY_SQL_TAG)) { 375 processEndQuerySQL(); 376 } 377 else if (local_name.equals(QUERY_QUALIFIER_TAG)) { 378 processEndQualifier(); 379 } 380 else if (local_name.equals(QUERY_ORDERING_TAG)) { 381 processEndQueryOrdering(); 382 } 383 else if (local_name.equals(QUERY_PREFETCH_TAG)) { 384 processEndQueryPrefetch(); 385 } 386 387 resetCurrentTag(); 388 charactersBuffer = null; 389 } 390 391 public void warning(SAXParseException e) throws SAXException { 392 logObj.warn( 393 "**Parsing warning**\n" 394 + "Line:" 395 + e.getLineNumber() 396 + "\nMessage:" 397 + e.getMessage()); 398 throw new SAXException ("Warning!"); 399 } 400 401 public void error(SAXParseException e) throws SAXException { 402 logObj.error( 403 "**Parsing error**\n" 404 + "Line:" 405 + e.getLineNumber() 406 + "\nMessage:" 407 + e.getMessage()); 408 throw new SAXException ("Warning!"); 409 } 410 411 public void fatalError(SAXParseException e) throws SAXException { 412 logObj.fatal( 413 "**Parsing fatal error**\n" 414 + "Line:" 415 + e.getLineNumber() 416 + "\nMessage:" 417 + e.getMessage()); 418 throw new SAXException ("Warning!"); 419 } 420 421 private void processStartDbEntity(Attributes atts) throws SAXException { 422 String name = atts.getValue("", "name"); 423 String parentName = atts.getValue("", "parentName"); 424 425 if (parentName != null) { 426 dbEntity = new DerivedDbEntity(name); 427 ((DerivedDbEntity) dbEntity).setParentEntityName(parentName); 428 } 429 else { 430 dbEntity = new DbEntity(name); 431 } 432 433 if (!(dbEntity instanceof DerivedDbEntity)) { 434 dbEntity.setSchema(atts.getValue("", "schema")); 435 dbEntity.setCatalog(atts.getValue("", "catalog")); 436 } 437 438 dataMap.addDbEntity(dbEntity); 439 } 440 441 private void processStartDbAttributeRef(Attributes atts) throws SAXException { 442 String name = atts.getValue("", "name"); 443 if ((attrib instanceof DerivedDbAttribute) 444 && (dbEntity instanceof DerivedDbEntity)) { 445 DbEntity parent = ((DerivedDbEntity) dbEntity).getParentEntity(); 446 DbAttribute ref = (DbAttribute) parent.getAttribute(name); 447 ((DerivedDbAttribute) attrib).addParam(ref); 448 } 449 else { 450 throw new SAXException ( 451 "Referenced attributes are not supported by regular DbAttributes. " 452 + " Offending attribute name '" 453 + attrib.getName() 454 + "'."); 455 } 456 } 457 458 private void processStartDbAttribute(Attributes atts) throws SAXException { 459 String name = atts.getValue("", "name"); 460 String type = atts.getValue("", "type"); 461 462 attrib = new DbAttribute(name); 463 attrib.setType(TypesMapping.getSqlTypeByName(type)); 464 dbEntity.addAttribute(attrib); 465 466 String length = atts.getValue("", "length"); 467 if (length != null) { 468 attrib.setMaxLength(Integer.parseInt(length)); 469 } 470 471 String precision = atts.getValue("", "precision"); 472 if (precision != null) { 473 attrib.setPrecision(Integer.parseInt(precision)); 474 } 475 476 attrib.setPrimaryKey(TRUE.equalsIgnoreCase(atts.getValue("", "isPrimaryKey"))); 477 attrib.setMandatory(TRUE.equalsIgnoreCase(atts.getValue("", "isMandatory"))); 478 attrib.setGenerated(TRUE.equalsIgnoreCase(atts.getValue("", "isGenerated"))); 479 } 480 481 private void processStartDerivedDbAttribute(Attributes atts) throws SAXException { 482 String name = atts.getValue("", "name"); 483 String type = atts.getValue("", "type"); 484 String spec = atts.getValue("", "spec"); 485 486 attrib = new DerivedDbAttribute(name); 487 attrib.setType(TypesMapping.getSqlTypeByName(type)); 488 ((DerivedDbAttribute) attrib).setExpressionSpec(spec); 489 dbEntity.addAttribute(attrib); 490 491 String temp = atts.getValue("", "length"); 492 if (temp != null) { 493 attrib.setMaxLength(Integer.parseInt(temp)); 494 } 495 temp = atts.getValue("", "precision"); 496 if (temp != null) { 497 attrib.setPrecision(Integer.parseInt(temp)); 498 } 499 temp = atts.getValue("", "isPrimaryKey"); 500 if (temp != null && temp.equalsIgnoreCase(TRUE)) { 501 attrib.setPrimaryKey(true); 502 } 503 temp = atts.getValue("", "isMandatory"); 504 if (temp != null && temp.equalsIgnoreCase(TRUE)) { 505 attrib.setMandatory(true); 506 } 507 508 temp = atts.getValue("", "isGroupBy"); 509 if (temp != null && temp.equalsIgnoreCase(TRUE)) { 510 ((DerivedDbAttribute) attrib).setGroupBy(true); 511 } 512 } 513 514 private void processStartDbKeyGenerator(Attributes atts) throws SAXException { 515 DbKeyGenerator pkGenerator = new DbKeyGenerator(); 516 dbEntity.setPrimaryKeyGenerator(pkGenerator); 517 } 518 519 private void processStartQuerySQL(Attributes atts) throws SAXException { 520 this.sqlKey = atts.getValue("", "adapter-class"); 521 } 522 523 private void processStartObjEntity(Attributes atts) { 524 objEntity = new ObjEntity(atts.getValue("", "name")); 525 objEntity.setClassName(atts.getValue("", "className")); 526 objEntity.setClientClassName(atts.getValue("", "clientClassName")); 527 528 String readOnly = atts.getValue("", "readOnly"); 529 objEntity.setReadOnly(TRUE.equalsIgnoreCase(readOnly)); 530 531 String lockType = atts.getValue("", "lock-type"); 532 if ("optimistic".equals(lockType)) { 533 objEntity.setDeclaredLockType(ObjEntity.LOCK_TYPE_OPTIMISTIC); 534 } 535 536 String superEntityName = atts.getValue("", "superEntityName"); 537 if (superEntityName != null) { 538 objEntity.setSuperEntityName(superEntityName); 539 } 540 else { 541 objEntity.setDbEntityName(atts.getValue("", "dbEntityName")); 542 objEntity.setSuperClassName(atts.getValue("", "superClassName")); 543 objEntity.setClientSuperClassName(atts.getValue("", "clientSuperClassName")); 544 } 545 546 dataMap.addObjEntity(objEntity); 547 } 548 549 private void processStartObjAttribute(Attributes atts) { 550 String name = atts.getValue("", "name"); 551 String type = atts.getValue("", "type"); 552 553 String lock = atts.getValue("", "lock"); 554 555 ObjAttribute oa = new ObjAttribute(name); 556 oa.setType(type); 557 oa.setUsedForLocking(TRUE.equalsIgnoreCase(lock)); 558 objEntity.addAttribute(oa); 559 String dbPath = atts.getValue("", "db-attribute-path"); 560 if (dbPath == null) { 561 dbPath = atts.getValue("", "db-attribute-name"); 562 } 563 oa.setDbAttributePath(dbPath); 564 } 565 566 private void processStartDbRelationship(Attributes atts) throws SAXException { 567 String name = atts.getValue("", "name"); 568 if (name == null) { 569 throw new SAXException ( 570 "MapLoaderImpl::processStartDbRelationship()," 571 + " Unable to parse name. Attributes:\n" 572 + printAttributes(atts).toString()); 573 } 574 575 String sourceName = atts.getValue("", "source"); 576 if (sourceName == null) { 577 throw new SAXException ("MapLoaderImpl::processStartDbRelationship() - null source entity"); 578 } 579 580 DbEntity source = dataMap.getDbEntity(sourceName); 581 if (source == null) { 582 logObj.debug( 583 "MapLoaderImpl::processStartDbRelationship() - Unable to find source " 584 + sourceName); 585 return; 586 } 587 588 String toManyString = atts.getValue("", "toMany"); 589 boolean toMany = toManyString != null && toManyString.equalsIgnoreCase(TRUE); 590 591 String toDependPkString = atts.getValue("", "toDependentPK"); 592 boolean toDependentPK = 593 toDependPkString != null && toDependPkString.equalsIgnoreCase(TRUE); 594 595 dbRelationship = new DbRelationship(name); 596 dbRelationship.setSourceEntity(source); 597 dbRelationship.setTargetEntityName(atts.getValue("", "target")); 598 dbRelationship.setToMany(toMany); 599 dbRelationship.setToDependentPK(toDependentPK); 600 601 source.addRelationship(dbRelationship); 602 } 603 604 private void processStartDbRelationshipRef(Attributes atts) throws SAXException { 605 608 String name = atts.getValue("", "name"); 609 if (name == null) { 610 throw new SAXException ( 611 "MapLoaderImpl::processStartDbRelationshipRef()" 612 + ", Null DbRelationship name for " 613 + objRelationship.getName()); 614 } 615 616 String path = objRelationship.getDbRelationshipPath(); 617 path = (path != null) ? path + "." + name : name; 618 objRelationship.setDbRelationshipPath(path); 619 } 620 621 private void processStartDbAttributePair(Attributes atts) throws SAXException { 622 DbJoin join = new DbJoin(dbRelationship); 623 join.setSourceName(atts.getValue("", "source")); 624 join.setTargetName(atts.getValue("", "target")); 625 dbRelationship.addJoin(join); 626 } 627 628 private void processStartObjRelationship(Attributes atts) throws SAXException { 629 String name = atts.getValue("", "name"); 630 if (null == name) { 631 throw new SAXException ( 632 "MapLoaderImpl::processStartObjRelationship()," 633 + " Unable to parse target. Attributes:\n" 634 + printAttributes(atts).toString()); 635 } 636 637 String sourceName = atts.getValue("", "source"); 638 if (sourceName == null) { 639 throw new SAXException ( 640 "MapLoaderImpl::processStartObjRelationship()," 641 + " Unable to parse source. Attributes:\n" 642 + printAttributes(atts).toString()); 643 } 644 645 ObjEntity source = dataMap.getObjEntity(sourceName); 646 if (source == null) { 647 throw new SAXException ( 648 "MapLoaderImpl::processStartObjRelationship()," 649 + " Unable to find source " 650 + sourceName); 651 } 652 653 String deleteRuleName = atts.getValue("", "deleteRule"); 654 int deleteRule = 655 (deleteRuleName != null) 656 ? DeleteRule.deleteRuleForName(deleteRuleName) 657 : DeleteRule.NO_ACTION; 658 659 objRelationship = new ObjRelationship(name); 660 objRelationship.setSourceEntity(source); 661 objRelationship.setTargetEntityName(atts.getValue("", "target")); 662 objRelationship.setDeleteRule(deleteRule); 663 objRelationship.setUsedForLocking( 664 TRUE.equalsIgnoreCase(atts.getValue("", "lock"))); 665 objRelationship.setDbRelationshipPath((atts.getValue("", "db-relationship-path"))); 666 source.addRelationship(objRelationship); 667 } 668 669 private void processStartProcedure(Attributes attributes) throws SAXException { 670 671 String name = attributes.getValue("", "name"); 672 if (null == name) { 673 throw new SAXException ( 674 "MapLoaderImpl::processStartProcedure()," + " no procedure name."); 675 } 676 677 String schema = attributes.getValue("", "schema"); 678 String catalog = attributes.getValue("", "catalog"); 679 String returningValue = attributes.getValue("", "returningValue"); 680 681 procedure = new Procedure(name); 682 procedure.setReturningValue( 683 returningValue != null && returningValue.equalsIgnoreCase(TRUE)); 684 procedure.setSchema(schema); 685 procedure.setCatalog(catalog); 686 dataMap.addProcedure(procedure); 687 } 688 689 private void processStartProcedureParameter(Attributes attributes) 690 throws SAXException { 691 692 String name = attributes.getValue("", "name"); 693 if (name == null) { 694 throw new SAXException ( 695 "MapLoaderImpl::processStartProcedureParameter()," 696 + " no procedure parameter name."); 697 } 698 699 ProcedureParameter parameter = new ProcedureParameter(name); 700 701 String type = attributes.getValue("", "type"); 702 if (type != null) { 703 parameter.setType(TypesMapping.getSqlTypeByName(type)); 704 } 705 706 String length = attributes.getValue("", "length"); 707 if (length != null) { 708 parameter.setMaxLength(Integer.parseInt(length)); 709 } 710 711 String precision = attributes.getValue("", "precision"); 712 if (precision != null) { 713 parameter.setPrecision(Integer.parseInt(precision)); 714 } 715 716 String direction = attributes.getValue("", "direction"); 717 if ("in".equals(direction)) { 718 parameter.setDirection(ProcedureParameter.IN_PARAMETER); 719 } 720 else if ("out".equals(direction)) { 721 parameter.setDirection(ProcedureParameter.OUT_PARAMETER); 722 } 723 else if ("in_out".equals(direction)) { 724 parameter.setDirection(ProcedureParameter.IN_OUT_PARAMETER); 725 } 726 727 procedure.addCallParameter(parameter); 728 } 729 730 private void processStartQuery(Attributes attributes) throws SAXException { 731 String name = attributes.getValue("", "name"); 732 if (null == name) { 733 throw new SAXException ("MapLoader::processStartQuery(), no query name."); 734 } 735 736 String builder = attributes.getValue("", "factory"); 737 738 if (builder == null) { 741 builder = SelectQueryBuilder.class.getName(); 742 } 743 else if (builder.equals("org.objectstyle.cayenne.query.SelectQueryBuilder")) { 744 builder = SelectQueryBuilder.class.getName(); 745 } 746 747 try { 748 queryBuilder = (QueryBuilder) Class.forName(builder).newInstance(); 749 } 750 catch (Exception ex) { 751 throw new SAXException ( 752 "MapLoader::processStartQuery(), invalid query builder: " + builder); 753 } 754 755 String rootType = attributes.getValue("", "root"); 756 String rootName = attributes.getValue("", "root-name"); 757 String resultType = attributes.getValue("", "result-type"); 758 String selecting = attributes.getValue("", "selecting"); 759 760 queryBuilder.setName(name); 761 queryBuilder.setRoot(dataMap, rootType, rootName); 762 queryBuilder.setSelecting(selecting); 763 queryBuilder.setResultType(resultType); 764 } 765 766 private void processStartQueryProperty(Attributes attributes) throws SAXException { 767 String name = attributes.getValue("", "name"); 768 if (null == name) { 769 throw new SAXException ("MapLoader::processStartQueryProperty(), no property name."); 770 } 771 772 String value = attributes.getValue("", "value"); 773 if (null == value) { 774 throw new SAXException ("MapLoader::processStartQueryProperty(), no property value."); 775 } 776 777 queryBuilder.addProperty(name, value); 778 } 779 780 private void processStartDataMapProperty(Attributes attributes) throws SAXException { 781 String name = attributes.getValue("", "name"); 782 if (null == name) { 783 throw new SAXException ("MapLoader::processStartDataMapProperty(), no property name."); 784 } 785 786 String value = attributes.getValue("", "value"); 787 if (null == value) { 788 throw new SAXException ("MapLoader::processStartDataMapProperty(), no property value."); 789 } 790 791 if(mapProperties == null) { 792 mapProperties = new TreeMap (); 793 } 794 795 mapProperties.put(name, value); 796 } 797 798 private void processStartQueryResultColumn(Attributes attributes) 799 throws SAXException { 800 String label = attributes.getValue("", "label"); 801 if (label == null) { 802 throw new SAXException ("MapLoader::processStartQueryResultColumn(), no label."); 803 } 804 805 String dbType = attributes.getValue("", "db-type"); 806 if (dbType == null) { 807 throw new SAXException ("MapLoader::processStartQueryResultColumn(), no db-type."); 808 } 809 810 String javaType = attributes.getValue("", "java-type"); 811 if (javaType == null) { 812 throw new SAXException ("MapLoader::processStartQueryResultColumn(), no java-type."); 813 } 814 815 queryBuilder.addResultColumn(label, dbType, javaType); 816 } 817 818 private void processEndQueryPrefetch() throws SAXException { 819 queryBuilder.addPrefetch(charactersBuffer.toString()); 820 } 821 822 private void processStartQueryOrdering(Attributes attributes) throws SAXException { 823 descending = attributes.getValue("", "descending"); 824 ignoreCase = attributes.getValue("", "ignore-case"); 825 } 826 827 private void processEndQuery() throws SAXException { 828 dataMap.addQuery(queryBuilder.getQuery()); 829 queryBuilder = null; 830 } 831 832 private void processEndQuerySQL() throws SAXException { 833 queryBuilder.addSql(charactersBuffer.toString(), sqlKey); 834 sqlKey = null; 835 } 836 837 private void processEndQualifier() throws SAXException { 838 String qualifier = charactersBuffer.toString(); 839 if (qualifier.trim().length() == 0) { 840 return; 841 } 842 843 if (objEntity != null) { 845 objEntity.setDeclaredQualifier(Expression.fromString(qualifier)); 846 } 847 else { 848 queryBuilder.setQualifier(qualifier); 849 } 850 } 851 852 private void processEndQueryOrdering() throws SAXException { 853 String path = charactersBuffer.toString(); 854 queryBuilder.addOrdering(path, descending, ignoreCase); 855 } 856 857 private void processEndDbAttribute() throws SAXException { 858 attrib = null; 859 } 860 861 private void processEndDbEntity() { 862 dbEntity = null; 863 } 864 865 private void processEndProcedure() { 866 procedure = null; 867 } 868 869 private void processEndProcedureParameter() { 870 } 871 872 private void processEndDbGeneratorType() { 873 if (dbEntity == null) 874 return; 875 DbKeyGenerator pkGenerator = dbEntity.getPrimaryKeyGenerator(); 876 if (pkGenerator == null) 877 return; 878 pkGenerator.setGeneratorType(charactersBuffer.toString()); 879 if (pkGenerator.getGeneratorType() == null) { 880 dbEntity.setPrimaryKeyGenerator(null); 881 } 882 } 883 884 private void processEndDbGeneratorName() { 885 if (dbEntity == null) 886 return; 887 DbKeyGenerator pkGenerator = dbEntity.getPrimaryKeyGenerator(); 888 if (pkGenerator == null) 889 return; 890 pkGenerator.setGeneratorName(charactersBuffer.toString()); 891 } 892 893 private void processEndDbKeyCacheSize() { 894 if (dbEntity == null) 895 return; 896 DbKeyGenerator pkGenerator = dbEntity.getPrimaryKeyGenerator(); 897 if (pkGenerator == null) 898 return; 899 try { 900 pkGenerator.setKeyCacheSize(new Integer (charactersBuffer.toString().trim())); 901 } 902 catch (Exception ex) { 903 pkGenerator.setKeyCacheSize(null); 904 } 905 } 906 907 private void processEndDataMap() { 908 if(mapProperties != null) { 909 dataMap.initWithProperties(mapProperties); 910 } 911 912 mapProperties = null; 913 } 914 915 private void processEndObjEntity() { 916 objEntity = null; 917 } 918 919 private void processEndDbRelationship() { 920 dbRelationship = null; 921 } 922 923 private void processEndObjRelationship() { 924 objRelationship = null; 925 } 926 927 928 private StringBuffer printAttributes(Attributes atts) { 929 StringBuffer sb = new StringBuffer (); 930 String name, value; 931 for (int i = 0; i < atts.getLength(); i++) { 932 value = atts.getQName(i); 933 name = atts.getValue(i); 934 sb.append("Name: " + name + "\tValue: " + value + "\n"); 935 } 936 return sb; 937 } 938 939 public void characters(char[] text, int start, int length) 940 throws org.xml.sax.SAXException { 941 if (charactersBuffer != null) { 942 charactersBuffer.append(text, start, length); 943 } 944 } 945 946 private void rememberCurrentTag(String tag) { 947 currentTag = tag; 948 } 949 950 private void resetCurrentTag() { 951 currentTag = null; 952 } 953 } 954 | Popular Tags |