KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > dods > trans > TransientXMLBuilderFactory


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: TransientXMLBuilderFactory.java,v 1.2 2004/12/16 12:15:04 predrag Exp $
22  */

23
24 /*
25  *
26  * @author Dragan Radeka & Nenad Vico
27  * @since LBS1.8
28  * @version $Revision: 1.2 $
29  *
30  */

31 package org.enhydra.dods.trans;
32
33 import java.io.File JavaDoc;
34 import java.io.FileOutputStream JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.Map JavaDoc;
38 import javax.xml.parsers.DocumentBuilder JavaDoc;
39 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
40 import org.enhydra.dods.wizard.TraceDialog;
41 import org.enhydra.dods.xslt.XSLTUtil;
42 import org.w3c.dom.Document JavaDoc;
43 import org.w3c.dom.Element JavaDoc;
44 import org.w3c.dom.NamedNodeMap JavaDoc;
45 import org.w3c.dom.Node JavaDoc;
46 import org.w3c.dom.NodeList JavaDoc;
47 import org.enhydra.dods.Common;
48 import org.enhydra.dods.generator.DODSGenerator;
49
50 /**
51  * This class is factory class for generating transient XML files into given project root by reading input doml file.
52  */

53 public class TransientXMLBuilderFactory {
54     // errors messages
55
public static final String JavaDoc ERROR_NO_PARSED_DOCUMENT = "ERROR_NO_PARSED_DOCUMENT";
56     public static final String JavaDoc ERROR_NO_DATABASE_TAG = "ERROR_NO_DATABASE_TAG";
57     public static final String JavaDoc ERROR_NO_DATABASE_ATTRIBUTES = "ERROR_NO_DATABASE_ATTRIBUTES";
58     public static final String JavaDoc ERROR_NO_DATABASE_ATTRIBUTE = "ERROR_NO_DATABASE_ATTRIBUTE";
59     public static final String JavaDoc ERROR_NO_TABLE_TAG = "ERROR_NO_TABLE_TAG";
60     public static final String JavaDoc ERROR_NO_COLUMN_TAG = "ERROR_NO_COLUMN_TAG";
61     public static final String JavaDoc TABLE_ATTR_NAMES[] = {
62         "id", "dbTableName", "isView", "generateSecure", "generateInsecure",
63         "multidb", "dirtyDOs","massUpdates","massDeletes"
64     };
65     public static final String JavaDoc 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 JavaDoc 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 JavaDoc REF_OBJECT_ATTR_NAMES[] = {
84         "constraint", "reference"
85     };
86     static final int COLUMN_CONSTRAINT = 0;
87     static final int COLUMN_REFERENCE = 1;
88     // attributes of type tag in column tag
89
public static final String JavaDoc 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     // attributes of index tag in table tag
98
public static final String JavaDoc 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     /**
106      * Store all tables read from doml file.
107      */

108     protected HashMap JavaDoc tables;
109
110     /**
111      * used project
112      */

113     protected String JavaDoc project = DefaultTagValues.TABLE_PROJECT_NAME;
114
115     /**
116      * used author
117      */

118     protected String JavaDoc author = DefaultTagValues.TABLE_AUTHOR;
119
120     /**
121      * used database
122      */

123     protected String JavaDoc database = null;
124
125     /**
126      * used database
127      */

128     protected String JavaDoc templateSet = DefaultTagValues.TABLE_TEMPLATE_SET;
129
130     /**
131      * trace dialog used for showing output
132      */

133     protected TraceDialog td = null;
134
135     /**
136      * Constructor.
137      */

138     public TransientXMLBuilderFactory() {
139         tables = new HashMap JavaDoc();
140     }
141
142     /**
143      * Constructor with trace dialog.
144      *
145      * @param td Trace dialog used for showing output.
146      */

147     public TransientXMLBuilderFactory(TraceDialog td) {
148         this.td = td;
149         tables = new HashMap JavaDoc();
150     }
151
152     /**
153      * Load doml file in memory structure.
154      */

155     public String JavaDoc readDoml() throws InvalidDomlException {
156         String JavaDoc projectRoot = Common.getProjectRoot();
157         String JavaDoc domlFile = projectRoot + File.separator
158                 + Common.getDomlFileName(); // read doml file project root
159
Document JavaDoc doc = null;
160         String JavaDoc nodeValue = "";
161
162         try { // read DOM parser
163
DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
164             DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
165
166             doc = db.parse(domlFile);
167         } catch (RuntimeException JavaDoc e) {
168             e.printStackTrace();
169         } catch (Exception JavaDoc e1) {
170             e1.printStackTrace();
171         }
172     
173         if (doc == null) {
174             return ERROR_NO_PARSED_DOCUMENT;
175         }
176         // read author tag
177
try {
178             NodeList JavaDoc nodeListTagAuthor = doc.getElementsByTagName("author");
179
180             if (nodeListTagAuthor != null) {
181                 NodeList JavaDoc nodeText = nodeListTagAuthor.item(0).getChildNodes();
182
183                 if (nodeText.item(0) != null) {
184                     author = nodeText.item(0).getNodeValue();
185                 }
186             }
187         } catch (Exception JavaDoc e) {}
188         // read project tag
189
try {
190             NodeList JavaDoc nodeListTagProject = doc.getElementsByTagName("projectname");
191
192             if (nodeListTagProject != null) {
193                 NodeList JavaDoc nodeText = nodeListTagProject.item(0).getChildNodes();
194
195                 if (nodeText.item(0) != null) {
196                     project = nodeText.item(0).getNodeValue();
197                 }
198             }
199         } catch (Exception JavaDoc e) {}
200            
201         // read database tag
202
NodeList JavaDoc nodeListTagDatabase = doc.getElementsByTagName("database");
203
204         if (nodeListTagDatabase == null) {
205             return ERROR_NO_DATABASE_TAG;
206         }
207         NamedNodeMap JavaDoc 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 JavaDoc 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 JavaDoc nodeTemplateSet = databaseAttrs.getNamedItem("templateset");
224
225         if (nodeTemplateSet != null) {
226             templateSet = nodeTemplateSet.getNodeValue();
227         }
228                 
229         Node JavaDoc dirtyTag = databaseAttrs.getNamedItem("dirtyDOs");
230         String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc dbGenerateInsecureTag = databaseAttrs.getNamedItem("generateInsecure");
260
261         if (dbGenerateInsecureTag != null) {
262             dbGenerateInsecure = dbGenerateInsecureTag.getNodeValue().equalsIgnoreCase("true");
263         }
264             
265         // read all table tags
266
NodeList JavaDoc nodeListTagTable = doc.getElementsByTagName("table");
267
268         if (nodeListTagTable == null) {
269             return ERROR_NO_TABLE_TAG;
270         }
271   
272         // loop trough all tables
273
for (int i = 0; i < nodeListTagTable.getLength(); i++) {
274             NamedNodeMap JavaDoc tableAttrs = nodeListTagTable.item(i).getAttributes();
275
276             if (tableAttrs == null) {
277                 continue;
278             } // attributes don't exist
279
Table table = null;
280             String JavaDoc tableID = null;
281             Referrer referrer = null;
282             // read all table attributes
283
String JavaDoc dirtyTable = null;
284             boolean md = dbMassDelete;
285             boolean mu = dbMassUpdate;
286
287             for (int j = 0; j < TABLE_ATTR_NAMES.length; j++) {
288                 Node JavaDoc nodeTable = tableAttrs.getNamedItem(TABLE_ATTR_NAMES[j]);
289
290                 if (nodeTable != null) {
291                     nodeValue = nodeTable.getNodeValue();
292                     switch (j) { // depend on table attributes
293
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 JavaDoc 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 // table.doMassDeletes(md || dbMassDelete);
351
}
352                         break;
353                             
354                     } // switch
355
} // if (nodeTable != null)
356

357             } // for TABLE_ATTR_NAMES.length
358

359             Element JavaDoc tagTable = (Element JavaDoc) nodeListTagTable.item(i);
360             // read all column tags
361
NodeList JavaDoc nodeListTagColumn = tagTable.getElementsByTagName("column");
362
363             if (nodeListTagColumn == null) {
364                 return ERROR_NO_COLUMN_TAG;
365             }
366             // loop through all column tags
367
for (int j = 0; j < nodeListTagColumn.getLength(); j++) {
368                 Column column = new Column(table.doSecure(), table.doInSecure());
369                 NamedNodeMap JavaDoc columnAttrs = nodeListTagColumn.item(j).getAttributes();
370
371                 if (columnAttrs == null) {
372                     continue;
373                 }
374                 // read and process all column attributes
375
for (int k = 0; k < COLUMN_ATT_NAMES.length; k++) {
376                     Node JavaDoc nodeColumn = columnAttrs.getNamedItem(COLUMN_ATT_NAMES[k]);
377
378                     if (nodeColumn != null) {
379                         nodeValue = nodeColumn.getNodeValue();
380                         switch (k) { // depend on column attributes
381
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                         } // switch
410
} // if (nodeColumn != null)
411
}
412                 Element JavaDoc columnTag = (Element JavaDoc) nodeListTagColumn.item(j);
413                 // read error tag
414
NodeList JavaDoc nodeListTagError = columnTag.getElementsByTagName("error");
415
416                 if (nodeListTagError != null) {
417                     for (int k = 0; k < nodeListTagError.getLength(); k++) {
418                         NodeList JavaDoc nodeText = nodeListTagError.item(k).getChildNodes();
419
420                         if (nodeText.item(0) != null) {// column.error(nodeText.item(0).getNodeValue());
421
}
422                     }
423                 }
424                 // read javadoctag tag
425
NodeList JavaDoc nodeListTagJavadoc = columnTag.getElementsByTagName("javadoc");
426
427                 if (nodeListTagJavadoc != null) {
428                     for (int k = 0; k < nodeListTagJavadoc.getLength(); k++) {
429                         NodeList JavaDoc nodeText = nodeListTagJavadoc.item(k).getChildNodes();
430
431                         if (nodeText.item(0) != null) {
432                             column.javadoc(nodeText.item(0).getNodeValue());
433                         }
434                     }
435                 }
436                 // read initialValue tag
437
NodeList JavaDoc nodeListTagInitialValue = columnTag.getElementsByTagName("initialValue");
438
439                 if (nodeListTagInitialValue != null) {
440                     for (int k = 0; k < nodeListTagInitialValue.getLength(); k++) {
441                         NodeList JavaDoc nodeText = nodeListTagInitialValue.item(k).getChildNodes();
442
443                         if (nodeText.item(0) != null) {
444                             column.javaDefaultValue(nodeText.item(0).getNodeValue());
445                         }
446                     }
447                 }
448                 // read referenceObject tag
449
NodeList JavaDoc nodeListTagReferenceObject = columnTag.getElementsByTagName("referenceObject");
450
451                 if (nodeListTagReferenceObject != null) {
452                     for (int k = 0; k < nodeListTagReferenceObject.getLength(); k++) {
453                         NamedNodeMap JavaDoc referenceObjectAttrs = nodeListTagReferenceObject.item(k).getAttributes();
454
455                         if (referenceObjectAttrs == null) {
456                             break;
457                         }
458                         // read all referenceObject attributes
459
for (int l = 0; l < REF_OBJECT_ATTR_NAMES.length; l++) {
460                             Node JavaDoc nodeReferenceObject = referenceObjectAttrs.getNamedItem(REF_OBJECT_ATTR_NAMES[l]);//
461

462                             if (nodeReferenceObject != null) {
463                                 nodeValue = nodeReferenceObject.getNodeValue();
464                                 switch (l) { // depend on column reference attributes
465
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                                 } // switch
478
} // if (nodeReferenceObject != null)
479
} // for l < REF_OBJECT_ATTR_NAMES.length;
480
} // for k < nodeListTagReferenceObject.getLength
481
} // if (nodeListTagReferenceObject != null)
482
// read type tag
483
NodeList JavaDoc 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 JavaDoc typeAttrs = nodeListTagType.item(k).getAttributes();
489
490                             if (typeAttrs == null) {
491                                 break;
492                             }
493                                 
494                             // read all type attributes
495
for (int l = 0; l < TYPE_ATTR_NAMES.length; l++) {
496                                 Node JavaDoc nodeType = typeAttrs.getNamedItem(TYPE_ATTR_NAMES[l]);//
497

498                                 if (nodeType != null) {
499                                     nodeValue = nodeType.getNodeValue();
500                                     switch (l) { // depend on column reference attributes
501
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                                     } // switch
521
} // if (nodeType != null)
522
} // for l < TYPE_ATTR_NAMES.length
523
} // if(nodeListTagType.item(k) != null
524
} // for k < nodeListTagType.getLength();
525
} // if (nodeListTagType != null)
526
table.addColumn(column);
527                 // if column has reference, adds referrer attribute in referrer of appropriate table
528
if (column.isReference()) {
529                     if (column.refPackage() != null && column.refName() != null) {
530                         String JavaDoc refTableID = column.refPackage() + "."
531                                 + column.refName();
532
533                         // extra conditions for referrer atribute
534
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             } // for j < nodeListTagColumn.getLength(); -- end of one table
550

551             // if table has referrer, adds it to approperate table
552
if (referrer != null) {
553                 for (int j = 0; j < referrer.size(); j++) {
554                     String JavaDoc 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             // read index tag
566
NodeList JavaDoc 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 JavaDoc indexAttrs = nodeListTagIndex.item(j).getAttributes();
572
573                         if (indexAttrs == null) {
574                             break;
575                         }
576                         Index index = new Index();
577
578                         // read all type attributes
579
for (int k = 0; k < INDEX_ATTR_NAMES.length; k++) {
580                             Node JavaDoc nodeType = indexAttrs.getNamedItem(INDEX_ATTR_NAMES[k]);//
581

582                             if (nodeType != null) {
583                                 nodeValue = nodeType.getNodeValue();
584                                 switch (k) { // depend on column reference attributes
585
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                                 } // switch
600
} // if (nodeType != null)
601
} // for k < TYPE_ATTR_NAMES.length
602
// read indexColumns tag
603
Element JavaDoc indexTag = (Element JavaDoc) nodeListTagIndex.item(j);
604                         NodeList JavaDoc nodeListTagIndexColumn = indexTag.getElementsByTagName("indexColumn");
605
606                         if (nodeListTagIndexColumn != null) {
607                             for (int k = 0; k
608                                     < nodeListTagIndexColumn.getLength(); k++) {
609                                 NamedNodeMap JavaDoc indexColumnAttrs = nodeListTagIndexColumn.item(k).getAttributes();
610
611                                 if (indexColumnAttrs == null) {
612                                     break;
613                                 }
614                                 // read all referenceObject attributes
615
Node JavaDoc nodeIndexColumnID = indexColumnAttrs.getNamedItem("id");//
616

617                                 if (nodeIndexColumnID != null) {
618                                     index.addIndexColumn(new String JavaDoc(nodeIndexColumnID.getNodeValue()));
619                                 }
620                             } // for k < nodeListTagIndexColumn.getLength
621
} // if (nodeListTagIndexColumn != null)
622
table.addIndex(index);
623                     } // if(nodeListTagIndex.item(j) != null
624
} // for j < nodeListTagIndex.getLength()
625
} // if (nodeListTagIndex != null)
626
table.doMassUpdates(mu);
627             table.doMassDeletes(md);
628         } // for i < nodeListTagTable.getLength();
629

630         return "";
631     }
632
633     /**
634      * Generate transient XML file from memory structure.
635      */

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 JavaDoc iter = tables.values().iterator();
643         // loop through all tables
644
// String templateSet = Common.getExtensions();
645
StringBuffer JavaDoc tablesBuff = new StringBuffer JavaDoc("<?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             // errors in table tags of doml file
652
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             // write table
671
//StringBuffer xmlBuff = new StringBuffer();
672
StringBuffer JavaDoc xmlBuff = new StringBuffer JavaDoc("<?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             // loop through all columns of table
710
for (int i = 0; i < table.columnsSize(); i++) {
711                 Column column = table.column(i);
712
713                 // errors in column tags of doml file
714
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                 // write column
731
xmlBuff.append(" <COLUMN name=\"" + column.name() + "\">\n");
732                 if (column.isReference()) {
733                     String JavaDoc 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             // loop through all idexes of table
807
for (int i = 0; i < table.indexesSize(); i++) {
808                 Index index = table.index(i);
809
810                 // errors in column tags of doml file
811
if (index.id() == null) {
812                     throw new InvalidDomlException("Invalid index id");
813                 }
814                 // write column
815
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             // loop through all referrers of table
826
for (Iterator JavaDoc 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 JavaDoc 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); // look at other attr
854

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             // Creating transient XML file and target directory
871
String JavaDoc filename = Common.getProjectRoot() + File.separator
872                     + (XSLTUtil.getAdjustedPackageName(table.pckg()) + "." + table.className()).replace('.',
873                             File.separatorChar) + ".xml";
874             String JavaDoc 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 JavaDoc file = new File JavaDoc(filename);
884             File JavaDoc dir = new File JavaDoc(dirname);
885
886             dir.mkdirs();
887             try {
888                 FileOutputStream JavaDoc outStream = new FileOutputStream JavaDoc(file);
889
890 // outStream.write(xmlBuff.toString().getBytes());
891
outStream.write(xmlBuff.toString().getBytes("UTF-8"));
892                 outStream.close();
893             } catch (Exception JavaDoc e) {
894                 e.printStackTrace();
895             }
896         } // while (iter.hasNext())
897
tablesBuff.append("</DATABASE>\n");
898         String JavaDoc tablesname = Common.getProjectRoot() + File.separator
899                 + "tables.xml";
900         File JavaDoc file = new File JavaDoc(tablesname);
901
902         try {
903             FileOutputStream JavaDoc outStream = new FileOutputStream JavaDoc(file);
904
905             //outStream.write(tablesBuff.toString().getBytes());
906
outStream.write(tablesBuff.toString().getBytes("UTF-8"));
907             outStream.close();
908         } catch (Exception JavaDoc e) {
909             e.printStackTrace();
910         }
911     }
912
913     /**
914      * Generate tables.xml file from memory structure.
915      */

916     public void generateTablesXML() throws InvalidDomlException {
917         Iterator JavaDoc iter = tables.values().iterator();
918         // loop through all tables
919
StringBuffer JavaDoc tablesBuff = new StringBuffer JavaDoc("<?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             // errors in table tags of doml file
926
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 JavaDoc tablesname = Common.getProjectRoot() + File.separator
951                 + "tables.xml";
952         File JavaDoc file = new File JavaDoc(tablesname);
953
954         try {
955             FileOutputStream JavaDoc outStream = new FileOutputStream JavaDoc(file);
956
957             //outStream.write(tablesBuff.toString().getBytes());
958
outStream.write(tablesBuff.toString().getBytes("UTF-8"));
959             outStream.close();
960         } catch (Exception JavaDoc e) {
961             e.printStackTrace();
962         }
963     }
964
965     /**
966      * Generate tables.xml file from memory structure.
967      */

968     public void generateClassList() throws InvalidDomlException {
969         Iterator JavaDoc iter = tables.values().iterator();
970         // loop through all tables
971
StringBuffer JavaDoc tablesBuff = new StringBuffer JavaDoc("<?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             // errors in table tags of doml file
979
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 JavaDoc dirname = Common.getProjectRoot() +File.separator+ "org"+File.separator+"enhydra"+File.separator+"dods";
995         
996         String JavaDoc filename = Common.getProjectRoot() +File.separator+ "org"+File.separator+"enhydra"+File.separator+"dods"+ File.separator+"DODSClassList.xml";
997
998         File JavaDoc file = new File JavaDoc(filename);
999         File JavaDoc dir = new File JavaDoc(dirname);
1000
1001        dir.mkdirs();
1002        
1003        try {
1004            FileOutputStream JavaDoc outStream = new FileOutputStream JavaDoc(file);
1005
1006            //outStream.write(tablesBuff.toString().getBytes());
1007
outStream.write(tablesBuff.toString().getBytes("UTF-8"));
1008            outStream.close();
1009        } catch (Exception JavaDoc e) {
1010            e.printStackTrace();
1011        }
1012    }
1013
1014
1015    /*
1016     * Created for debugging.
1017     */

1018    protected void showAllTables() {
1019        Iterator JavaDoc iter = tables.entrySet().iterator();
1020
1021        while (iter.hasNext()) {
1022            Map.Entry JavaDoc map = (Map.Entry JavaDoc) iter.next();
1023            String JavaDoc tableID = (String JavaDoc) map.getKey();
1024            Table table = (Table) map.getValue();
1025
1026            System.out.println("Table: " + tableID + "\n" + table);
1027            
1028        }
1029    }
1030    
1031    /*
1032     * Argument: parameter isTablesOnly
1033     * If it is true, class generates only tables.xml file;
1034     * otherwise class generates both (tables.xml and transient XML) files for all tables.
1035     */

1036    public static void main(String JavaDoc[] args) throws Exception JavaDoc{
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 JavaDoc(args[0])).booleanValue())
1048                transientXMLBuilder.generateTablesXML();
1049             else
1050                transientXMLBuilder.generateTransientXML();
1051          } catch (InvalidDomlException ide) {
1052                ide.printStackTrace();
1053        } catch (Exception JavaDoc e) {
1054            e.printStackTrace();
1055        }
1056    }
1057}
1058
Popular Tags