1 23 24 31 package org.enhydra.dods.trans; 32 33 import java.io.File ; 34 import java.io.FileOutputStream ; 35 import java.util.HashMap ; 36 import java.util.Iterator ; 37 import java.util.Map ; 38 import javax.xml.parsers.DocumentBuilder ; 39 import javax.xml.parsers.DocumentBuilderFactory ; 40 import org.enhydra.dods.wizard.TraceDialog; 41 import org.enhydra.dods.xslt.XSLTUtil; 42 import org.w3c.dom.Document ; 43 import org.w3c.dom.Element ; 44 import org.w3c.dom.NamedNodeMap ; 45 import org.w3c.dom.Node ; 46 import org.w3c.dom.NodeList ; 47 import org.enhydra.dods.Common; 48 import org.enhydra.dods.generator.DODSGenerator; 49 50 53 public class TransientXMLBuilderFactory { 54 public static final String ERROR_NO_PARSED_DOCUMENT = "ERROR_NO_PARSED_DOCUMENT"; 56 public static final String ERROR_NO_DATABASE_TAG = "ERROR_NO_DATABASE_TAG"; 57 public static final String ERROR_NO_DATABASE_ATTRIBUTES = "ERROR_NO_DATABASE_ATTRIBUTES"; 58 public static final String ERROR_NO_DATABASE_ATTRIBUTE = "ERROR_NO_DATABASE_ATTRIBUTE"; 59 public static final String ERROR_NO_TABLE_TAG = "ERROR_NO_TABLE_TAG"; 60 public static final String ERROR_NO_COLUMN_TAG = "ERROR_NO_COLUMN_TAG"; 61 public static final String TABLE_ATTR_NAMES[] = { 62 "id", "dbTableName", "isView", "generateSecure", "generateInsecure", 63 "multidb", "dirtyDOs","massUpdates","massDeletes" 64 }; 65 public static final String DIRTY_DO_DEFAULT = "Compatible"; 66 static final int TABLE_ID = 0; 67 static final int TABLE_DB_TABLE_NAME = 1; 68 static final int TABLE_IS_VIEW = 2; 69 static final int TABLE_SECURITY = 3; 70 static final int TABLE_NON_SECURITY = 4; 71 static final int TABLE_MULTIDB = 5; 72 static final int TABLE_DIRTY_DOS = 6; 73 static final int TABLE_MASS_UPDATES = 7; 74 static final int TABLE_MASS_DELETES = 8; 75 public static final String COLUMN_ATT_NAMES[] = { 76 "id", "usedForQuery", "isConstant", "generateSecure", "generateInsecure", 77 }; 78 static final int COLUMN_ID = 0; 79 static final int COLUMN_USED_FOR_QUERY = 1; 80 static final int COLUMN_IS_CONSTANT = 2; 81 static final int COLUMN_SECURITY = 3; 82 static final int COLUMN_NON_SECURITY = 4; 83 public static final String REF_OBJECT_ATTR_NAMES[] = { 84 "constraint", "reference" 85 }; 86 static final int COLUMN_CONSTRAINT = 0; 87 static final int COLUMN_REFERENCE = 1; 88 public static final String TYPE_ATTR_NAMES[] = { 90 "size", "canBeNull", "dbType", "javaType" 91 }; 92 static final int COLUMN_SIZE = 0; 93 static final int COLUMN_CAN_BE_NULL = 1; 94 static final int COLUMN_DB_TYPE = 2; 95 static final int COLUMN_JAVA_TYPE = 3; 96 97 public static final String INDEX_ATTR_NAMES[] = { 99 "id", "unique", "clustered" 100 }; 101 static final int INDEX_ID = 0; 102 static final int INDEX_UNIQUE = 1; 103 static final int INDEX_CLUSTERED = 2; 104 105 108 protected HashMap tables; 109 110 113 protected String project = DefaultTagValues.TABLE_PROJECT_NAME; 114 115 118 protected String author = DefaultTagValues.TABLE_AUTHOR; 119 120 123 protected String database = null; 124 125 128 protected String templateSet = DefaultTagValues.TABLE_TEMPLATE_SET; 129 130 133 protected TraceDialog td = null; 134 135 138 public TransientXMLBuilderFactory() { 139 tables = new HashMap (); 140 } 141 142 147 public TransientXMLBuilderFactory(TraceDialog td) { 148 this.td = td; 149 tables = new HashMap (); 150 } 151 152 155 public String readDoml() throws InvalidDomlException { 156 String projectRoot = Common.getProjectRoot(); 157 String domlFile = projectRoot + File.separator 158 + Common.getDomlFileName(); Document doc = null; 160 String nodeValue = ""; 161 162 try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 164 DocumentBuilder db = dbf.newDocumentBuilder(); 165 166 doc = db.parse(domlFile); 167 } catch (RuntimeException e) { 168 e.printStackTrace(); 169 } catch (Exception e1) { 170 e1.printStackTrace(); 171 } 172 173 if (doc == null) { 174 return ERROR_NO_PARSED_DOCUMENT; 175 } 176 try { 178 NodeList nodeListTagAuthor = doc.getElementsByTagName("author"); 179 180 if (nodeListTagAuthor != null) { 181 NodeList nodeText = nodeListTagAuthor.item(0).getChildNodes(); 182 183 if (nodeText.item(0) != null) { 184 author = nodeText.item(0).getNodeValue(); 185 } 186 } 187 } catch (Exception e) {} 188 try { 190 NodeList nodeListTagProject = doc.getElementsByTagName("projectname"); 191 192 if (nodeListTagProject != null) { 193 NodeList nodeText = nodeListTagProject.item(0).getChildNodes(); 194 195 if (nodeText.item(0) != null) { 196 project = nodeText.item(0).getNodeValue(); 197 } 198 } 199 } catch (Exception e) {} 200 201 NodeList nodeListTagDatabase = doc.getElementsByTagName("database"); 203 204 if (nodeListTagDatabase == null) { 205 return ERROR_NO_DATABASE_TAG; 206 } 207 NamedNodeMap databaseAttrs = nodeListTagDatabase.item(0).getAttributes(); 208 209 if (database == null || database.equals(DODSGenerator.DATABASE_NOT_SET)) { 210 if (databaseAttrs == null) { 211 return ERROR_NO_DATABASE_ATTRIBUTES; 212 } 213 Node nodeDatabase = databaseAttrs.getNamedItem("database"); 214 215 if (nodeDatabase == null) { 216 return ERROR_NO_DATABASE_ATTRIBUTE; 217 } 218 database = nodeDatabase.getNodeValue(); 219 if(System.getProperty("DATABASE_VENDOR")==null || System.getProperty("DATABASE_VENDOR").equals(DODSGenerator.DATABASE_NOT_SET)) 220 System.setProperty("DATABASE_VENDOR",database); 221 } 222 DefaultTagValues.loadDatabaseValues(database); 223 Node nodeTemplateSet = databaseAttrs.getNamedItem("templateset"); 224 225 if (nodeTemplateSet != null) { 226 templateSet = nodeTemplateSet.getNodeValue(); 227 } 228 229 Node dirtyTag = databaseAttrs.getNamedItem("dirtyDOs"); 230 String dirty = null; 231 232 if (dirtyTag != null) { 233 dirty = dirtyTag.getNodeValue(); 234 } else { 235 dirty = DIRTY_DO_DEFAULT; 236 } 237 boolean dbMassUpdate = DefaultTagValues.DATABASE_MASS_UPDATE; 238 Node dbMassUpdateTag = databaseAttrs.getNamedItem("massUpdates"); 239 240 if (dbMassUpdateTag != null) { 241 dbMassUpdate = dbMassUpdateTag.getNodeValue().equalsIgnoreCase("true"); 242 } 243 244 boolean dbMassDelete = DefaultTagValues.DATABASE_MASS_DELETE; 245 Node dbMassDeleteTag = databaseAttrs.getNamedItem("massDeletes"); 246 247 if (dbMassDeleteTag != null) { 248 dbMassDelete = dbMassDeleteTag.getNodeValue().equalsIgnoreCase("true"); 249 } 250 251 boolean dbGenerateSecure = DefaultTagValues.DATABASE_SECURITY; 252 Node dbGenerateSecureTag = databaseAttrs.getNamedItem("generateSecure"); 253 254 if (dbGenerateSecureTag != null) { 255 dbGenerateSecure = dbGenerateSecureTag.getNodeValue().equalsIgnoreCase("true"); 256 } 257 258 boolean dbGenerateInsecure = DefaultTagValues.DATABASE_NON_SECURITY; 259 Node dbGenerateInsecureTag = databaseAttrs.getNamedItem("generateInsecure"); 260 261 if (dbGenerateInsecureTag != null) { 262 dbGenerateInsecure = dbGenerateInsecureTag.getNodeValue().equalsIgnoreCase("true"); 263 } 264 265 NodeList nodeListTagTable = doc.getElementsByTagName("table"); 267 268 if (nodeListTagTable == null) { 269 return ERROR_NO_TABLE_TAG; 270 } 271 272 for (int i = 0; i < nodeListTagTable.getLength(); i++) { 274 NamedNodeMap tableAttrs = nodeListTagTable.item(i).getAttributes(); 275 276 if (tableAttrs == null) { 277 continue; 278 } Table table = null; 280 String tableID = null; 281 Referrer referrer = null; 282 String dirtyTable = null; 284 boolean md = dbMassDelete; 285 boolean mu = dbMassUpdate; 286 287 for (int j = 0; j < TABLE_ATTR_NAMES.length; j++) { 288 Node nodeTable = tableAttrs.getNamedItem(TABLE_ATTR_NAMES[j]); 289 290 if (nodeTable != null) { 291 nodeValue = nodeTable.getNodeValue(); 292 switch (j) { case TABLE_ID: { 294 tableID = nodeValue; 295 table = (Table) tables.get(tableID); 296 if (table == null) { 297 table = new Table(dbGenerateSecure, 298 dbGenerateInsecure, dirty); 299 tables.put(tableID, table); 300 } 301 String className = Common.capitalizeName(tableID.substring(tableID.lastIndexOf('.') 302 + 1)); 303 304 table.pckg(tableID.substring(0, 305 tableID.lastIndexOf('.'))); 306 table.projectName(tableID.substring(0, 307 tableID.indexOf('.'))); 308 table.tableName(className); 309 table.className(className); 310 } 311 break; 312 313 case TABLE_DB_TABLE_NAME: { 314 table.tableName(nodeValue); 315 } 316 break; 317 318 case TABLE_IS_VIEW: {} 319 break; 320 321 case TABLE_SECURITY: { 322 table.doSecure(nodeValue.equalsIgnoreCase("true")); 323 } 324 break; 325 326 case TABLE_NON_SECURITY: { 327 table.doInSecure(nodeValue.equalsIgnoreCase("true")); 328 } 329 break; 330 331 case TABLE_MULTIDB: { 332 table.doMultidb(nodeValue.equalsIgnoreCase("true")); 333 } 334 break; 335 336 case TABLE_DIRTY_DOS: { 337 table.setDirtyDOs(nodeValue); 338 } 339 break; 340 case TABLE_MASS_UPDATES: { 341 mu = nodeValue.equalsIgnoreCase("true"); 342 if(!dbMassUpdate) 343 mu = mu || dbMassUpdate; 344 } 345 break; 346 case TABLE_MASS_DELETES: { 347 md = nodeValue.equalsIgnoreCase("true"); 348 if(!dbMassDelete) 349 md = md || dbMassDelete; 350 } 352 break; 353 354 } } 357 } 359 Element tagTable = (Element ) nodeListTagTable.item(i); 360 NodeList nodeListTagColumn = tagTable.getElementsByTagName("column"); 362 363 if (nodeListTagColumn == null) { 364 return ERROR_NO_COLUMN_TAG; 365 } 366 for (int j = 0; j < nodeListTagColumn.getLength(); j++) { 368 Column column = new Column(table.doSecure(), table.doInSecure()); 369 NamedNodeMap columnAttrs = nodeListTagColumn.item(j).getAttributes(); 370 371 if (columnAttrs == null) { 372 continue; 373 } 374 for (int k = 0; k < COLUMN_ATT_NAMES.length; k++) { 376 Node nodeColumn = columnAttrs.getNamedItem(COLUMN_ATT_NAMES[k]); 377 378 if (nodeColumn != null) { 379 nodeValue = nodeColumn.getNodeValue(); 380 switch (k) { case COLUMN_ID: { 382 column.name(nodeValue.substring(nodeValue.lastIndexOf('.') 383 + 1)); 384 } 385 break; 386 387 case COLUMN_USED_FOR_QUERY: { 388 column.usedForQuery(nodeValue.equalsIgnoreCase("true")); 389 } 390 break; 391 392 case COLUMN_IS_CONSTANT: { 393 column.isConstant(nodeValue.equalsIgnoreCase("true")); 394 } 395 break; 396 397 case COLUMN_SECURITY: { 398 column.isSecure(nodeValue.equalsIgnoreCase("true")); 399 if (nodeValue.equalsIgnoreCase("true")) { 400 table.anyColumnSecure(true); 401 } 402 } 403 break; 404 405 case COLUMN_NON_SECURITY: { 406 column.isInSecure(nodeValue.equalsIgnoreCase("true")); 407 } 408 break; 409 } } } 412 Element columnTag = (Element ) nodeListTagColumn.item(j); 413 NodeList nodeListTagError = columnTag.getElementsByTagName("error"); 415 416 if (nodeListTagError != null) { 417 for (int k = 0; k < nodeListTagError.getLength(); k++) { 418 NodeList nodeText = nodeListTagError.item(k).getChildNodes(); 419 420 if (nodeText.item(0) != null) { } 422 } 423 } 424 NodeList nodeListTagJavadoc = columnTag.getElementsByTagName("javadoc"); 426 427 if (nodeListTagJavadoc != null) { 428 for (int k = 0; k < nodeListTagJavadoc.getLength(); k++) { 429 NodeList nodeText = nodeListTagJavadoc.item(k).getChildNodes(); 430 431 if (nodeText.item(0) != null) { 432 column.javadoc(nodeText.item(0).getNodeValue()); 433 } 434 } 435 } 436 NodeList nodeListTagInitialValue = columnTag.getElementsByTagName("initialValue"); 438 439 if (nodeListTagInitialValue != null) { 440 for (int k = 0; k < nodeListTagInitialValue.getLength(); k++) { 441 NodeList nodeText = nodeListTagInitialValue.item(k).getChildNodes(); 442 443 if (nodeText.item(0) != null) { 444 column.javaDefaultValue(nodeText.item(0).getNodeValue()); 445 } 446 } 447 } 448 NodeList nodeListTagReferenceObject = columnTag.getElementsByTagName("referenceObject"); 450 451 if (nodeListTagReferenceObject != null) { 452 for (int k = 0; k < nodeListTagReferenceObject.getLength(); k++) { 453 NamedNodeMap referenceObjectAttrs = nodeListTagReferenceObject.item(k).getAttributes(); 454 455 if (referenceObjectAttrs == null) { 456 break; 457 } 458 for (int l = 0; l < REF_OBJECT_ATTR_NAMES.length; l++) { 460 Node nodeReferenceObject = referenceObjectAttrs.getNamedItem(REF_OBJECT_ATTR_NAMES[l]); 462 if (nodeReferenceObject != null) { 463 nodeValue = nodeReferenceObject.getNodeValue(); 464 switch (l) { case COLUMN_REFERENCE: { 466 column.refPackage(nodeValue.substring(0, 467 nodeValue.lastIndexOf('.'))); 468 column.refName(Common.capitalizeName(nodeValue.substring(nodeValue.lastIndexOf('.') 469 + 1))); 470 } 471 break; 472 473 case COLUMN_CONSTRAINT: { 474 column.refConstarint(nodeValue.equalsIgnoreCase("true")); 475 } 476 break; 477 } } } } } NodeList nodeListTagType = columnTag.getElementsByTagName("type"); 484 485 if (nodeListTagType != null) { 486 for (int k = 0; k < nodeListTagType.getLength(); k++) { 487 if (nodeListTagType.item(k) != null) { 488 NamedNodeMap typeAttrs = nodeListTagType.item(k).getAttributes(); 489 490 if (typeAttrs == null) { 491 break; 492 } 493 494 for (int l = 0; l < TYPE_ATTR_NAMES.length; l++) { 496 Node nodeType = typeAttrs.getNamedItem(TYPE_ATTR_NAMES[l]); 498 if (nodeType != null) { 499 nodeValue = nodeType.getNodeValue(); 500 switch (l) { case COLUMN_SIZE: { 502 column.size(nodeValue); 503 } 504 break; 505 506 case COLUMN_CAN_BE_NULL: { 507 column.canBeNull(nodeValue.equalsIgnoreCase("true")); 508 } 509 break; 510 511 case COLUMN_DB_TYPE: { 512 column.dbType(nodeValue); 513 } 514 break; 515 516 case COLUMN_JAVA_TYPE: { 517 column.javaType(nodeValue); 518 } 519 break; 520 } } } } } } table.addColumn(column); 527 if (column.isReference()) { 529 if (column.refPackage() != null && column.refName() != null) { 530 String refTableID = column.refPackage() + "." 531 + column.refName(); 532 533 if (!refTableID.equals(".")) { 535 if (column.usedForQuery() && !column.refIsAbstarct()) { 536 if (!column.refIsForeignKey()) { 537 if (referrer == null) { 538 referrer = new Referrer(table.pckg(), 539 table.className()); 540 } 541 referrer.secure(table.doSecure()); 542 referrer.addAttribute(column.name(), 543 refTableID, column.isSecure()); 544 } 545 } 546 } 547 } 548 } 549 } 551 if (referrer != null) { 553 for (int j = 0; j < referrer.size(); j++) { 554 String refTableID = referrer.attributeDoName(j); 555 Table refTable = (Table) tables.get(refTableID); 556 557 if (refTable == null) { 558 refTable = new Table(dbGenerateSecure, 559 dbGenerateInsecure, dirty); 560 tables.put(refTableID, refTable); 561 } 562 refTable.addReferrer(referrer); 563 } 564 } 565 NodeList nodeListTagIndex = tagTable.getElementsByTagName("index"); 567 568 if (nodeListTagIndex != null) { 569 for (int j = 0; j < nodeListTagIndex.getLength(); j++) { 570 if (nodeListTagIndex.item(j) != null) { 571 NamedNodeMap indexAttrs = nodeListTagIndex.item(j).getAttributes(); 572 573 if (indexAttrs == null) { 574 break; 575 } 576 Index index = new Index(); 577 578 for (int k = 0; k < INDEX_ATTR_NAMES.length; k++) { 580 Node nodeType = indexAttrs.getNamedItem(INDEX_ATTR_NAMES[k]); 582 if (nodeType != null) { 583 nodeValue = nodeType.getNodeValue(); 584 switch (k) { case INDEX_ID: { 586 index.id(nodeValue); 587 } 588 break; 589 590 case INDEX_UNIQUE: { 591 index.isUnique(nodeValue.equalsIgnoreCase("true")); 592 } 593 break; 594 595 case INDEX_CLUSTERED: { 596 index.isClustered(nodeValue.equalsIgnoreCase("true")); 597 } 598 break; 599 } } } Element indexTag = (Element ) nodeListTagIndex.item(j); 604 NodeList nodeListTagIndexColumn = indexTag.getElementsByTagName("indexColumn"); 605 606 if (nodeListTagIndexColumn != null) { 607 for (int k = 0; k 608 < nodeListTagIndexColumn.getLength(); k++) { 609 NamedNodeMap indexColumnAttrs = nodeListTagIndexColumn.item(k).getAttributes(); 610 611 if (indexColumnAttrs == null) { 612 break; 613 } 614 Node nodeIndexColumnID = indexColumnAttrs.getNamedItem("id"); 617 if (nodeIndexColumnID != null) { 618 index.addIndexColumn(new String (nodeIndexColumnID.getNodeValue())); 619 } 620 } } table.addIndex(index); 623 } } } table.doMassUpdates(mu); 627 table.doMassDeletes(md); 628 } 630 return ""; 631 } 632 633 636 public void generateTransientXML() throws InvalidDomlException { 637 if (td == null) { 638 System.out.println("Creating TransientXML"); 639 } else { 640 td.appendLine("Creating TransientXML\n"); 641 } 642 Iterator iter = tables.values().iterator(); 643 StringBuffer tablesBuff = new StringBuffer ("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\n"); 646 647 tablesBuff.append("<DATABASE>\n"); 648 while (iter.hasNext()) { 649 Table table = (Table) iter.next(); 650 651 if (table.pckg() == null) { 653 throw new InvalidDomlException("Invalid table package"); 654 } 655 if (table.tableName() == null) { 656 throw new InvalidDomlException("Invalid table name"); 657 } 658 if (table.className() == null) { 659 throw new InvalidDomlException("Invalid class name"); 660 } 661 if (table.pckg() == null) { 662 throw new InvalidDomlException("Invalid table package"); 663 } 664 tablesBuff.append(" <TABLE name=\"" + table.tableName() + "\" "); 665 tablesBuff.append("id=\"" + table.pckg() + "." + table.className() 666 + "\" "); 667 tablesBuff.append("path=\"" + table.pckg().replace('.', '/') + "\" "); 668 tablesBuff.append("class=\"" + table.className() + "\"/>\n"); 669 670 StringBuffer xmlBuff = new StringBuffer ("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\n"); 673 674 xmlBuff.append("\n<TABLE>\n"); 675 xmlBuff.append(" <PACKAGE>" 676 + XSLTUtil.getAdjustedPackageName(table.pckg()) 677 + "</PACKAGE>\n"); 678 xmlBuff.append(" <AUTHOR>" + author + "</AUTHOR>\n"); 679 xmlBuff.append(" <PROJECT_NAME>" + project + "</PROJECT_NAME>\n"); 680 xmlBuff.append(" <TABLE_NAME>" + table.tableName() 681 + "</TABLE_NAME>\n"); 682 xmlBuff.append(" <CLASS_NAME>" + table.className() 683 + "</CLASS_NAME>\n"); 684 xmlBuff.append(" <DB_VENDOR>" + table.dbVendor() 685 + "</DB_VENDOR>\n"); 686 xmlBuff.append(" <TEMPLATE_SET>" + templateSet 687 + "</TEMPLATE_SET>\n"); 688 xmlBuff.append(" <GENERATE_SECURE>" + table.doSecure() 689 + "</GENERATE_SECURE>\n"); 690 xmlBuff.append(" <GENERATE_INSECURE>" + table.doInSecure() 691 + "</GENERATE_INSECURE>\n"); 692 xmlBuff.append(" <MASS_UPDATES>" + table.doMassUpdates() 693 + "</MASS_UPDATES>\n"); 694 xmlBuff.append(" <MASS_DELETES>" + table.doMassDeletes() 695 + "</MASS_DELETES>\n"); 696 xmlBuff.append(" <DO_IS_OID_BASED>" + table.doIsOidBased() 697 + "</DO_IS_OID_BASED>\n"); 698 xmlBuff.append(" <IS_ABSTRACT>" + table.isAbstract() 699 + "</IS_ABSTRACT>\n"); 700 xmlBuff.append(" <DELETE_CASCADES>" + table.deleteCascade() 701 + "</DELETE_CASCADES>\n"); 702 xmlBuff.append(" <DO_IS_MULTIDB_BASED>" + table.doMultidb() 703 + "</DO_IS_MULTIDB_BASED>\n"); 704 xmlBuff.append(" <IS_ANY_COLUMN_SECURE>" 705 + table.isAnyColumnSecure() + "</IS_ANY_COLUMN_SECURE>\n"); 706 xmlBuff.append(" <GENERATE_DIRTY>" + table.getDirtyDOs() 707 + "</GENERATE_DIRTY>\n\n"); 708 709 for (int i = 0; i < table.columnsSize(); i++) { 711 Column column = table.column(i); 712 713 if (column.name() == null) { 715 throw new InvalidDomlException("Invalid column name"); 716 } 717 if (column.dbType() == null) { 718 throw new InvalidDomlException("Invalid column database type"); 719 } 720 if (column.javaType() == null) { 721 throw new InvalidDomlException("Invalid column java type"); 722 } 723 if (column.size() == null) { 724 column.size(DefaultTagValues.getDefaultSize(column.dbType(), 725 column.javaType())); 726 } 727 if (column.isSecure() == false && column.isInSecure() == false) { 728 column.isInSecure(true); 729 } 730 xmlBuff.append(" <COLUMN name=\"" + column.name() + "\">\n"); 732 if (column.isReference()) { 733 String refTableName = column.refName(); 734 Table refTable = (Table) tables.get(column.refPackage() 735 + "." + column.refName()); 736 737 if (refTable != null) { 738 refTableName = refTable.tableName(); 739 } 740 741 if (column.refName() == null) { 742 throw new InvalidDomlException("Invalid column reference name"); 743 } 744 if (column.refPackage() == null) { 745 throw new InvalidDomlException("Invalid column reference package"); 746 } 747 xmlBuff.append(" <REFERENCE_OBJECT name=\"" 748 + column.refName() + "\">\n"); 749 xmlBuff.append(" <CONSTRAINT>" 750 + column.refConstarint() + "</CONSTRAINT>\n"); 751 xmlBuff.append(" <IS_ABSTRACT>" 752 + column.refIsAbstarct() + "</IS_ABSTRACT>\n"); 753 xmlBuff.append(" <IS_FOREIGN_KEY>" 754 + column.refIsForeignKey() + "</IS_FOREIGN_KEY>\n"); 755 xmlBuff.append(" <PACKAGE>" 756 + XSLTUtil.getAdjustedPackageName(column.refPackage()) 757 + "</PACKAGE>\n"); 758 xmlBuff.append(" <TABLE_NAME>" + refTableName 759 + "</TABLE_NAME>\n"); 760 if (column.refForeignKeyColumnName() != null) { 761 xmlBuff.append(" <FOREIGN_KEY_COLUMN>" 762 + column.refForeignKeyColumnName() 763 + "</FOREIGN_KEY_COLUMN>\n"); 764 } 765 if (column.refForeignKeyGroup() != null) { 766 xmlBuff.append(" <FOREIGN_KEY_GROUP>" 767 + column.refForeignKeyGroup() 768 + "</FOREIGN_KEY_GROUP>\n"); 769 } 770 xmlBuff.append(" </REFERENCE_OBJECT>\n"); 771 } 772 xmlBuff.append(" <IS_CONSTANT>" + column.isConstant() 773 + "</IS_CONSTANT>\n"); 774 if (column.javadoc() != null) { 775 xmlBuff.append(" <JAVADOC>" + column.javadoc() 776 + "</JAVADOC>\n"); 777 } 778 xmlBuff.append(" <DB_TYPE>" + column.dbType() 779 + "</DB_TYPE>\n"); 780 xmlBuff.append(" <JAVA_TYPE>" 781 + XSLTUtil.adjustJavaType(column.javaType()) 782 + "</JAVA_TYPE>\n"); 783 if (column.javaDefaultValue() != null) { 784 xmlBuff.append(" <JAVA_DEFAULT_VALUE>" 785 + column.javaDefaultValue() 786 + "</JAVA_DEFAULT_VALUE>\n"); 787 } 788 xmlBuff.append(" <USED_FOR_QUERY>" 789 + column.usedForQuery() + "</USED_FOR_QUERY>\n"); 790 xmlBuff.append(" <CAN_BE_NULL>" + column.canBeNull() 791 + "</CAN_BE_NULL>\n"); 792 xmlBuff.append(" <IS_PRIMARY_KEY>" 793 + column.isPrimaryKey() + "</IS_PRIMARY_KEY>\n"); 794 if (column.size() != null) { 795 xmlBuff.append(" <SIZE>" + column.size() 796 + "</SIZE>\n"); 797 } 798 xmlBuff.append(" <IS_ARRAY>" + column.isArray() 799 + "</IS_ARRAY>\n"); 800 xmlBuff.append(" <GENERATE_SECURE>" + column.isSecure() 801 + "</GENERATE_SECURE>\n"); 802 xmlBuff.append(" <GENERATE_INSECURE>" 803 + column.isInSecure() + "</GENERATE_INSECURE>\n"); 804 xmlBuff.append(" </COLUMN>\n\n\n"); 805 } 806 for (int i = 0; i < table.indexesSize(); i++) { 808 Index index = table.index(i); 809 810 if (index.id() == null) { 812 throw new InvalidDomlException("Invalid index id"); 813 } 814 xmlBuff.append(" <INDEX id=\"" 816 + XSLTUtil.getAdjustedPackageName(index.id()) 817 + "\" unique=\"" + index.isUnique() + "\" clustered=\"" 818 + index.isClustered() + "\">\n"); 819 for (int j = 0; j < index.size(); j++) { 820 xmlBuff.append(" <INDEX_COLUMN id=\"" 821 + index.indexColumn(j) + "\"/>\n"); 822 } 823 xmlBuff.append(" </INDEX>\n\n"); 824 } 825 for (Iterator refIter = table.referrersValueIterator(); refIter.hasNext();) { 827 Referrer referrer = (Referrer) refIter.next(); 828 829 xmlBuff.append(" <REFERRER name=\"" + referrer.name() 830 + "\" package=\"" 831 + XSLTUtil.getAdjustedPackageName(referrer.pckg()) 832 + "\" generateSecure=\"" + referrer.isSecure() + "\">\n"); 833 for (int i = 0; i < referrer.size(); i++) { 834 boolean n2n = false; 835 String another = ""; 836 837 if (referrer.size() == 2) { 838 if (!(table.pckg() + "." + table.className()).equals(referrer.attributeDoName(i))) { 839 n2n = true; 840 Table doTable = (Table) tables.get(referrer.attributeDoName(i)); 841 842 if (doTable.isAbstract()) { 843 n2n = false; 844 } 845 if (doTable.referrers().containsKey(table.pckg() 846 + "." + table.className())) { 847 n2n = false; 848 } 849 if (table.referrers().containsKey(referrer.attributeDoName(i))) { 850 n2n = false; 851 } 852 if (n2n) { 853 int j = (0 == i ? 1 : 0); 855 another = "\" another=\"" 856 + referrer.attributeName(j); 857 } 858 } 859 } 860 xmlBuff.append(" <REFATTR name=\"" 861 + referrer.attributeName(i) + "\" do_name=\"" 862 + XSLTUtil.getAdjustedPackageName(referrer.attributeDoName(i)) 863 + "\" n2n=\"" + n2n + another 864 + "\" generateSecure=\"" 865 + referrer.attributeSecurity(i) + "\"/>\n"); 866 } 867 xmlBuff.append(" </REFERRER>\n"); 868 } 869 xmlBuff.append("\n</TABLE>\n"); 870 String filename = Common.getProjectRoot() + File.separator 872 + (XSLTUtil.getAdjustedPackageName(table.pckg()) + "." + table.className()).replace('.', 873 File.separatorChar) + ".xml"; 874 String dirname = Common.getProjectRoot() + File.separator 875 + XSLTUtil.getAdjustedPackageName(table.pckg()).replace('.', 876 File.separatorChar); 877 878 if (td == null) { 879 System.out.println("Creating " + filename); 880 } else { 881 td.appendLine("Creating " + filename + "\n"); 882 } 883 File file = new File (filename); 884 File dir = new File (dirname); 885 886 dir.mkdirs(); 887 try { 888 FileOutputStream outStream = new FileOutputStream (file); 889 890 outStream.write(xmlBuff.toString().getBytes("UTF-8")); 892 outStream.close(); 893 } catch (Exception e) { 894 e.printStackTrace(); 895 } 896 } tablesBuff.append("</DATABASE>\n"); 898 String tablesname = Common.getProjectRoot() + File.separator 899 + "tables.xml"; 900 File file = new File (tablesname); 901 902 try { 903 FileOutputStream outStream = new FileOutputStream (file); 904 905 outStream.write(tablesBuff.toString().getBytes("UTF-8")); 907 outStream.close(); 908 } catch (Exception e) { 909 e.printStackTrace(); 910 } 911 } 912 913 916 public void generateTablesXML() throws InvalidDomlException { 917 Iterator iter = tables.values().iterator(); 918 StringBuffer tablesBuff = new StringBuffer ("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\n"); 920 921 tablesBuff.append("<DATABASE>\n"); 922 while (iter.hasNext()) { 923 Table table = (Table) iter.next(); 924 925 if (table.pckg() == null) { 927 throw new InvalidDomlException("Invalid table package"); 928 } 929 if (table.tableName() == null) { 930 throw new InvalidDomlException("Invalid table name"); 931 } 932 if (table.className() == null) { 933 throw new InvalidDomlException("Invalid class name"); 934 } 935 if (table.pckg() == null) { 936 throw new InvalidDomlException("Invalid table package"); 937 } 938 tablesBuff.append(" <TABLE name=\"" + table.tableName() + "\" "); 939 tablesBuff.append("id=\"" 940 + XSLTUtil.getAdjustedPackageName(table.pckg()) + "." 941 + table.className() + "\" "); 942 tablesBuff.append("path=\"" 943 + XSLTUtil.getAdjustedPackageName(table.pckg()).replace('.', 944 '/') + "\" "); 945 tablesBuff.append("massUpdates=\"" + table.doMassUpdates() + "\" "); 946 tablesBuff.append("massDeletes=\"" + table.doMassDeletes() + "\" "); 947 tablesBuff.append("class=\"" + table.className() + "\"/>\n"); 948 } 949 tablesBuff.append("</DATABASE>\n"); 950 String tablesname = Common.getProjectRoot() + File.separator 951 + "tables.xml"; 952 File file = new File (tablesname); 953 954 try { 955 FileOutputStream outStream = new FileOutputStream (file); 956 957 outStream.write(tablesBuff.toString().getBytes("UTF-8")); 959 outStream.close(); 960 } catch (Exception e) { 961 e.printStackTrace(); 962 } 963 } 964 965 968 public void generateClassList() throws InvalidDomlException { 969 Iterator iter = tables.values().iterator(); 970 StringBuffer tablesBuff = new StringBuffer ("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\n"); 972 tablesBuff.append("<CLASSES>\n"); 973 974 975 while (iter.hasNext()) { 976 Table table = (Table) iter.next(); 977 978 if (table.pckg() == null) { 980 throw new InvalidDomlException("Invalid table package"); 981 } 982 if (table.tableName() == null) { 983 throw new InvalidDomlException("Invalid table name"); 984 } 985 if (table.className() == null) { 986 throw new InvalidDomlException("Invalid class name"); 987 } 988 if (table.pckg() == null) { 989 throw new InvalidDomlException("Invalid table package"); 990 } 991 tablesBuff.append(" <CLASS name=\"" + XSLTUtil.getAdjustedPackageName(table.pckg()) + "."+ table.className() + "DO"+ "\"/>\n" ); 992 } 993 tablesBuff.append("</CLASSES>\n"); 994 String dirname = Common.getProjectRoot() +File.separator+ "org"+File.separator+"enhydra"+File.separator+"dods"; 995 996 String filename = Common.getProjectRoot() +File.separator+ "org"+File.separator+"enhydra"+File.separator+"dods"+ File.separator+"DODSClassList.xml"; 997 998 File file = new File (filename); 999 File dir = new File (dirname); 1000 1001 dir.mkdirs(); 1002 1003 try { 1004 FileOutputStream outStream = new FileOutputStream (file); 1005 1006 outStream.write(tablesBuff.toString().getBytes("UTF-8")); 1008 outStream.close(); 1009 } catch (Exception e) { 1010 e.printStackTrace(); 1011 } 1012 } 1013 1014 1015 1018 protected void showAllTables() { 1019 Iterator iter = tables.entrySet().iterator(); 1020 1021 while (iter.hasNext()) { 1022 Map.Entry map = (Map.Entry ) iter.next(); 1023 String tableID = (String ) map.getKey(); 1024 Table table = (Table) map.getValue(); 1025 1026 System.out.println("Table: " + tableID + "\n" + table); 1027 1028 } 1029 } 1030 1031 1036 public static void main(String [] args) throws Exception { 1037 try { 1038 TransientXMLBuilderFactory transientXMLBuilder = new TransientXMLBuilderFactory(); 1039 1040 if (args.length > 1) { 1041 transientXMLBuilder.database = args[1]; 1042 } 1043 transientXMLBuilder.readDoml(); 1044 1045 if ((args[0]).equals("ClassList")) 1046 transientXMLBuilder.generateClassList(); 1047 else if ((new Boolean (args[0])).booleanValue()) 1048 transientXMLBuilder.generateTablesXML(); 1049 else 1050 transientXMLBuilder.generateTransientXML(); 1051 } catch (InvalidDomlException ide) { 1052 ide.printStackTrace(); 1053 } catch (Exception e) { 1054 e.printStackTrace(); 1055 } 1056 } 1057} 1058 | Popular Tags |