defs/Defs.java
/* [<][>][^][v][top][bottom][index][help] */
FUNCTIONS
This source file includes following functions.
- Defs
- getValueByEnum
- getDOM
- validate
- printDF_attribute_aliases
- printDF_attribute_aliases_map
- printDF_attribute_codes
- printDF_attribute_enum
- printDF_attribute_inv_attr_mask
- printDF_attribute_names
- printDF_class_aliases
- printDF_class_aliases_map
- printDF_class_codes
- printDF_class_dbase_code_map
- printDF_class_enum
- printDF_class_mask
- printDF_class_names
- printQI_queries
- printUD_queries
- printTemplates
- printDF_class_templates
- printDF_class_templates_v
- printTemplatesV
- printDiagrams
- main
1 import java.io.*;
2 import java.util.*;
3 import com.sun.xml.parser.*;
4 import com.sun.xml.tree.*;
5 import org.xml.sax.*;
6 import org.w3c.dom.*;
7
8 /**
9 * RIPE classes generated from Data Object Models.
10 *
11 * @author ottrey@ripe.net
12 * @version $Version$
13 *
14 */
15 public class Defs {
/* [<][>][^][v][top][bottom][index][help] */
16
17
18 private Hashtable ripeAttributes;
19 private Vector ripeAttributeCodes;
20 private Vector ripeClasses;
21 private Vector ripeAttributeAliases;
22 private Vector ripeAttributeAliasesMap;
23 private Vector ripeClassAliases;
24 private Vector ripeClassAliasesMap;
25 private Vector ripeQueries;
26
27
28 // -----------------oOo-----------------
29 // Constructors
30 // -----------------oOo-----------------
31 public Defs(boolean normalize) {
32 // Create a normalized DOM from the xml file for the attributes.
33 XmlDocument attrDOM = getDOM("attributes.xml", "ripe_attribute", normalize);
34
35 // Initialize.
36 ripeAttributes = new Hashtable();
37 ripeAttributeCodes = new Vector();
38 ripeAttributeAliases = new Vector();
39 ripeAttributeAliasesMap = new Vector();
40 ripeClassAliases = new Vector();
41 ripeClassAliasesMap = new Vector();
42 ripeQueries = new Vector();
43
44 // Recurse through node tree
45 NodeList attrNodes = attrDOM.getElementsByTagName("ripe_attribute");
46 for (int i=0; i < attrNodes.getLength(); i++) {
47 // (Checking if the attribute is valid)
48 if (validate("attribute", attrNodes.item(i))) {
49 AttributeDef ad = new AttributeDef(attrNodes.item(i));
50
51 // and each attribute,
52 ripeAttributes.put(ad.getCode(), ad);
53
54 // and it's code.
55 ripeAttributeCodes.addElement(ad.getCode());
56
57 // and it's aliases.
58 // also map the alias to the attribute index.
59
60 // set the index to map to.
61 Integer mapIndex = new Integer(ripeAttributeCodes.size()-1);
62
63 // first the code.
64 ripeAttributeAliases.addElement(ad.getCode());
65 ripeAttributeAliasesMap.addElement(mapIndex);
66
67 // then the name.
68 ripeAttributeAliases.addElement(ad.getName());
69 ripeAttributeAliasesMap.addElement(mapIndex);
70
71 if (ad.getAltName().length() > 1) {
72 // then the altName.
73 ripeAttributeAliases.addElement(ad.getAltName());
74 ripeAttributeAliasesMap.addElement(mapIndex);
75 }
76 }
77 }
78
79 // Create a normalized DOM from the xml file for the classes.
80 XmlDocument objDOM = getDOM("classes.xml", "ripe_class", normalize);
81
82 // Create a vector to store the classes.
83 ripeClasses = new Vector();
84
85 // Recurse through node tree
86 NodeList objNodes = objDOM.getElementsByTagName("ripe_class");
87 for (int i=0; i < objNodes.getLength(); i++) {
88 // Check if the class is valid
89 if (validate("class", objNodes.item(i))) {
90 ClassDef od = new ClassDef(objNodes.item(i), ripeAttributes);
91
92 // Add the class.
93 ripeClasses.addElement(od);
94
95 // set the index to map to.
96 Integer mapIndex = new Integer(ripeClasses.size()-1);
97
98 // first the code.
99 ripeClassAliases.addElement(od.getCode());
100 ripeClassAliasesMap.addElement(mapIndex);
101
102 // then the name.
103 ripeClassAliases.addElement(od.getName());
104 ripeClassAliasesMap.addElement(mapIndex);
105
106 }
107 }
108
109 // replace class/attribute variables in queries
110
111
112
113 } // Defs()
114
115 public String getValueByEnum(String name) {
/* [<][>][^][v][top][bottom][index][help] */
116 Enumeration e = ripeClasses.elements();
117 for( int i = 0; e.hasMoreElements(); i++) {
118 ClassDef d = (ClassDef)e.nextElement();
119 String a = d.getEnum();
120
121 //System.out.println( d );
122
123 if( name.equals(a) ) {
124 return (new Integer(i)).toString();
125 }
126 }
127 System.out.println("ERROR: cannot resolve variable name " + name );
128 System.exit(-1);
129
130 return ""; // bloody idiot, the compiler
131 }
132
133
134
135 /**
136 * Creates a Data Object Model from the RIPE classes defined
137 * in the XML document.
138 *
139 * @author ottrey@ripe.net
140 * @version $Version$
141 *
142 * @param xmlDocName The URI of the XML document.
143 * @param ripeClass The class to be created from.
144 * @param normalize Return a normalized DOM.
145 *
146 */
147 private XmlDocument getDOM(String xmlDocName, String ripeClass, boolean normalize) {
/* [<][>][^][v][top][bottom][index][help] */
148
149 // Convert filename to an input source.
150 InputSource inSrc= new InputSource();
151 try {
152 inSrc = Resolver.createInputSource(new File(xmlDocName));
153 }
154 catch (IOException e) {
155 System.err.println("Failed to convert filename: " + xmlDocName +
156 "to an input source" + e);
157 e.printStackTrace(); System.exit(-1);
158 }
159
160 // Create and validate a DOM.
161 XmlDocument dom=null;
162 try {
163 dom = XmlDocument.createXmlDocument(inSrc, true);
164 }
165 catch (SAXException e) {
166 System.err.println("Failed to create DOM & validate." + e);
167 e.printStackTrace(); System.exit(-1);
168 }
169 catch (IOException e) {
170 System.err.println("Failed to create DOM & validate." + e);
171 e.printStackTrace(); System.exit(-1);
172 }
173
174 // Normalize the document.
175 if (normalize) {
176 dom.getDocumentElement().normalize();
177 }
178
179 return dom;
180
181 } // getDOM()
182
183 private boolean validate(String type, Node obj) {
/* [<][>][^][v][top][bottom][index][help] */
184 boolean result=false;
185 String status = obj.getAttributes().getNamedItem("status").getNodeValue();
186 String name = obj.getAttributes().getNamedItem("name").getNodeValue();
187
188 if (status.equals("valid")) {
189 result=true;
190 }
191 else {
192 System.err.println("Warning: " + type + " " + name + " is " + status);
193 }
194
195 return result;
196 } // validClass()
197
198
199 // -----------------oOo-----------------
200 // Print Methods
201 // -----------------oOo-----------------
202 private void printDF_attribute_aliases() {
/* [<][>][^][v][top][bottom][index][help] */
203 System.out.println("char * const Attribute_aliases[] = {");
204 Enumeration e = ripeAttributeAliases.elements();
205 while (e.hasMoreElements()) {
206 String a = (String)e.nextElement();
207 System.out.println(" \"" + a + "\",");
208 }
209 System.out.println(" " + "NULL" + "\n" + "}; /* Attribute_aliases */");
210 } // printDF_attribute_aliases()
211
212 private void printDF_attribute_aliases_map() {
/* [<][>][^][v][top][bottom][index][help] */
213 System.out.println("int const Attribute_aliases_map[] = {");
214 Enumeration e = ripeAttributeAliasesMap.elements();
215 while (e.hasMoreElements()) {
216 Integer am = (Integer)e.nextElement();
217 System.out.println(" " + am + ",");
218 }
219 System.out.println(" " + "NULL" + "\n" + "}; /* Attribute_aliases_map */");
220 } // printDF_attribute_aliases_map()
221
222 private void printDF_attribute_codes() {
/* [<][>][^][v][top][bottom][index][help] */
223 System.out.println("char * const Attribute_codes[] = {");
224 Enumeration e = ripeAttributeCodes.elements();
225 while (e.hasMoreElements()) {
226 String ac = (String)e.nextElement();
227 AttributeDef ad = (AttributeDef)ripeAttributes.get(ac);
228
229 // If the attribute has status="valid".
230 if (ad.getStatus().equals("valid")) {
231 // Output the attribute code.
232 System.out.println(" \"" + ad.getCode() + "\",");
233 }
234 }
235 System.out.println(" " + "NULL" + "\n" + "}; /* Attribute_codes */");
236 } // printDF_attribute_codes()
237
238 private void printDF_attribute_enum() {
/* [<][>][^][v][top][bottom][index][help] */
239 System.out.println("typedef enum _A_Type_t {");
240
241 // Enumerate through the attribute codes.
242 Enumeration e = ripeAttributeCodes.elements();
243 while (e.hasMoreElements()) {
244 String ac = (String)e.nextElement();
245 AttributeDef ad = (AttributeDef)ripeAttributes.get(ac);
246
247 // If the attribute has status="valid".
248 if (ad.getStatus().equals("valid")) {
249 // Output the attribute enum.
250 System.out.println(" " + ad.getEnum() + ",");
251 }
252 }
253
254 System.out.println(" " + "A_END" + "\n" + "} A_Type_t;");
255 } // printDF_attribute_enum()
256
257 private void printDF_attribute_inv_attr_mask() {
/* [<][>][^][v][top][bottom][index][help] */
258 System.out.print("#define INV_ATTR_MASK ");
259
260 // Enumerate through the attribute codes.
261 Enumeration e = ripeAttributeCodes.elements();
262 while (e.hasMoreElements()) {
263 String ac = (String)e.nextElement();
264 AttributeDef ad = (AttributeDef)ripeAttributes.get(ac);
265
266 // If the attribute has status="valid".
267 if (ad.getStatus().equals("valid") && ad.getInverse()) {
268 // Output the attribute enum.
269 System.out.print(ad.getEnum() + ", ");
270 }
271 }
272
273 System.out.println("MA_END");
274 } // printDF_attribute_inv_attr_mask()
275
276 private void printDF_attribute_names() {
/* [<][>][^][v][top][bottom][index][help] */
277 System.out.println("char * const Attribute_names[] = {");
278 Enumeration e = ripeAttributeCodes.elements();
279 while (e.hasMoreElements()) {
280 String ac = (String)e.nextElement();
281 AttributeDef ad = (AttributeDef)ripeAttributes.get(ac);
282
283 // If the attribute has status="valid".
284 if (ad.getStatus().equals("valid")) {
285 // Output the attribute name.
286 System.out.println(" \"" + ad.getName() + "\",");
287 }
288 }
289 System.out.println(" " + "NULL" + "\n" + "}; /* Attribute_names */");
290 } // printDF_attribute_names()
291
292 private void printDF_class_aliases() {
/* [<][>][^][v][top][bottom][index][help] */
293 System.out.println("char * const Class_aliases[] = {");
294 Enumeration e = ripeClassAliases.elements();
295 while (e.hasMoreElements()) {
296 String a = (String)e.nextElement();
297 System.out.println(" \"" + a + "\",");
298 }
299 System.out.println(" " + "NULL" + "\n" + "}; /* Class_aliases */");
300 } // printDF_class_aliases()
301
302 private void printDF_class_aliases_map() {
/* [<][>][^][v][top][bottom][index][help] */
303 System.out.println("int const Class_aliases_map[] = {");
304 Enumeration e = ripeClassAliasesMap.elements();
305 while (e.hasMoreElements()) {
306 Integer am = (Integer)e.nextElement();
307 System.out.println(" " + am + ",");
308 }
309 System.out.println(" " + "NULL" + "\n" + "}; /* Class_aliases_map */");
310 } // printDF_class_aliases_map()
311
312 private void printDF_class_codes() {
/* [<][>][^][v][top][bottom][index][help] */
313 System.out.println("char * const Class_codes[] = {");
314 Enumeration e = ripeClasses.elements();
315 while (e.hasMoreElements()) {
316 ClassDef od = (ClassDef)e.nextElement();
317 System.out.println(" \"" + od.getCode() + "\",");
318 }
319 System.out.println(" " + "NULL" + "\n" + "}; /* Class_codes */");
320 } // printDF_class_codes()
321
322 private void printDF_class_dbase_code_map() {
/* [<][>][^][v][top][bottom][index][help] */
323 System.out.println("int const Class_dbase_code_map[] = {");
324 Enumeration e = ripeClasses.elements();
325 while (e.hasMoreElements()) {
326 ClassDef cd = (ClassDef)e.nextElement();
327 System.out.println(" " + cd.getDbaseCode() + ",");
328 }
329 System.out.println(" " + "NULL" + "\n" + "}; /* Class_dbase_code_map */");
330 } // printDF_class_dbase_code_map()
331
332 private void printDF_class_enum() {
/* [<][>][^][v][top][bottom][index][help] */
333 System.out.println("typedef enum _C_Type_t {");
334 Enumeration e = ripeClasses.elements();
335 while (e.hasMoreElements()) {
336 ClassDef od = (ClassDef)e.nextElement();
337 System.out.println(" " + od.getEnum() + ",");
338 }
339 System.out.println(" " + "C_END" + "\n" + "} C_Type_t;");
340 } // printDF_class_enum()
341
342 private void printDF_class_mask() {
/* [<][>][^][v][top][bottom][index][help] */
343 System.out.print("#define CLASS_MASK ");
344 Enumeration e = ripeClasses.elements();
345 while (e.hasMoreElements()) {
346 ClassDef cd = (ClassDef)e.nextElement();
347 System.out.print(cd.getEnum() + ", ");
348 }
349 System.out.println("MA_END");
350 } // printDF_class_mask()
351
352 private void printDF_class_names() {
/* [<][>][^][v][top][bottom][index][help] */
353 System.out.println("char * const Class_names[] = {");
354 Enumeration e = ripeClasses.elements();
355 while (e.hasMoreElements()) {
356 String a = ((ClassDef)e.nextElement()).getName();
357 System.out.println(" \"" + a + "\",");
358 }
359 System.out.println(" " + "NULL" + "\n" + "}; /* Class_names */");
360 } // printDF_class_names()
361
362 private void printQI_queries() {
/* [<][>][^][v][top][bottom][index][help] */
363 System.out.println(Query.startDoc());
364 Enumeration e1 = ripeAttributes.elements();
365 while (e1.hasMoreElements()) {
366 AttributeDef ad = (AttributeDef)e1.nextElement();
367 Enumeration e2 = ad.getQueries().elements();
368 while (e2.hasMoreElements()) {
369 Query q = (Query)e2.nextElement();
370 System.out.println(q.getStruct(" ", this));
371 }
372 }
373 System.out.println(Query.endDoc());
374 } // printQI_queries()
375
376 private void printUD_queries() {
/* [<][>][^][v][top][bottom][index][help] */
377
378 Enumeration e;
379
380 System.out.println("UD_query Insert[] = {");
381 e = ripeAttributeCodes.elements();
382 while (e.hasMoreElements()) {
383 String ac = (String)e.nextElement();
384 AttributeDef ad = (AttributeDef)ripeAttributes.get(ac);
385 System.out.println(" {" + ad.getInsertQ_type() + ", " + "\"" + ad.getInsert() + "\"},");
386 }
387 System.out.println(" " + "{0, NULL}" + "\n" + "}; /* Insert */\n");
388
389
390 System.out.println("UD_query Update[] = {");
391 e = ripeAttributeCodes.elements();
392 while (e.hasMoreElements()) {
393 String ac = (String)e.nextElement();
394 AttributeDef ad = (AttributeDef)ripeAttributes.get(ac);
395 System.out.println(" {" + ad.getUpdateQ_type() + ", " + "\"" + ad.getUpdate() + "\"},");
396 }
397 System.out.println(" " + "{0, NULL}" + "\n" + "}; /* Update */\n");
398
399 System.out.println("UD_query Dummy[] = {");
400 e = ripeAttributeCodes.elements();
401 while (e.hasMoreElements()) {
402 String ac = (String)e.nextElement();
403 AttributeDef ad = (AttributeDef)ripeAttributes.get(ac);
404 System.out.println(" {" + ad.getDummyQ_type() + ", " + "\"" + ad.getDummy() + "\"},");
405 }
406 System.out.println(" " + "{0, NULL}" + "\n" + "}; /* Dummy */\n");
407
408 System.out.println("UD_query Select[] = {");
409 e = ripeAttributeCodes.elements();
410 while (e.hasMoreElements()) {
411 String ac = (String)e.nextElement();
412 AttributeDef ad = (AttributeDef)ripeAttributes.get(ac);
413 System.out.println(" {" + ad.getSelectQ_type() + ", " + "\"" + ad.getSelect() + "\"},");
414 }
415 System.out.println(" " + "{0, NULL}" + "\n" + "}; /* Select */\n");
416
417 } // printUD_queries()
418
419 private void printTemplates() {
/* [<][>][^][v][top][bottom][index][help] */
420 Enumeration e = ripeClasses.elements();
421 while (e.hasMoreElements()) {
422 ClassDef cd = (ClassDef)e.nextElement();
423 System.out.println(cd.getName() + "\n");
424 System.out.println(cd.getTemplate(false) + "\n");
425 }
426 } // printTemplates()
427
428 private void printDF_class_templates() {
/* [<][>][^][v][top][bottom][index][help] */
429 Enumeration e = ripeClasses.elements();
430 System.out.println("const char *Templates[] = {");
431 while (e.hasMoreElements()) {
432 ClassDef cd = (ClassDef)e.nextElement();
433 System.out.print(cd.getTemplate(true));
434 System.out.println(",");
435 }
436 System.out.println("NULL");
437 System.out.println("}; /* Templates */");
438 } // printDF_class_templates()
439
440 private void printDF_class_templates_v() {
/* [<][>][^][v][top][bottom][index][help] */
441 Enumeration e = ripeClasses.elements();
442 System.out.println("const char *Templates_v[] = {");
443 while (e.hasMoreElements()) {
444 ClassDef od = (ClassDef)e.nextElement();
445 System.out.print(od.getTemplateV(true));
446 System.out.println(",");
447 }
448 System.out.println("NULL");
449 System.out.println("}; /* Templates_v */");
450 } // printDF_class_templates_v()
451
452 private void printTemplatesV() {
/* [<][>][^][v][top][bottom][index][help] */
453 Enumeration e = ripeClasses.elements();
454 while (e.hasMoreElements()) {
455 ClassDef od = (ClassDef)e.nextElement();
456 System.out.println(od.getName() + "\n");
457 System.out.println(od.getTemplateV(false) + "\n");
458 }
459 } // printTemplatesV()
460
461 private void printDiagrams() {
/* [<][>][^][v][top][bottom][index][help] */
462 int maxWidth=0; // Widest diagram
463 Hashtable foreigns = new Hashtable();
464
465 Enumeration e1 = ripeClasses.elements();
466 while (e1.hasMoreElements()) {
467 ClassDef od = (ClassDef)e1.nextElement();
468 if (maxWidth < od.getWidth()) {
469 maxWidth = od.getWidth();
470 }
471
472 Hashtable foriegnAttrs = od.getForeignAttrs();
473 if (foriegnAttrs != null) {
474 Enumeration e2 = foriegnAttrs.keys();
475 while (e2.hasMoreElements()) {
476 String key = (String)e2.nextElement();
477 if (!foreigns.containsKey(key)) {
478 foreigns.put(key, foriegnAttrs.get(key));
479 }
480 }
481 }
482 }
483
484 System.out.print("Classes:");
485 for (int i=0; i < maxWidth; i++) {
486 System.out.print(" ");
487 }
488 System.out.println("Foreign keys:");
489
490 Enumeration e3 = ripeClasses.elements();
491 while (e3.hasMoreElements()) {
492 ClassDef od = (ClassDef)e3.nextElement();
493 System.out.print(od.getDiagram(maxWidth, foreigns));
494 }
495 } // printDiagrams()
496
497
498 // -----------------oOo-----------------
499 // Unit test driver
500 // -----------------oOo-----------------
501 public static void main (String argv[]) {
/* [<][>][^][v][top][bottom][index][help] */
502 int n=0;
503 boolean err=true;
504 boolean normalize=false;
505
506 if (argv.length > 0) {
507 try {
508 n = Integer.parseInt(argv[0]);
509 err=false;
510 }
511 catch (NumberFormatException e) {
512 }
513 if (argv.length == 2) {
514 if (argv[1].equals("normalize")) {
515 normalize = true;
516 }
517 }
518 }
519
520 if (!err) {
521 Defs rc = new Defs(normalize);
522
523 switch (n) {
524 case 1: rc.printDF_attribute_aliases(); break;
525 case 2: rc.printDF_attribute_aliases_map(); break;
526 case 3: rc.printDF_attribute_codes(); break;
527 case 4: rc.printDF_attribute_enum(); break;
528 case 5: rc.printDF_attribute_inv_attr_mask(); break;
529 case 6: rc.printDF_attribute_names(); break;
530 case 7: rc.printDF_class_aliases(); break;
531 case 8: rc.printDF_class_aliases_map(); break;
532 case 9: rc.printDF_class_codes(); break;
533 case 10: rc.printDF_class_dbase_code_map(); break;
534 case 11: rc.printDF_class_enum(); break;
535 case 12: rc.printDF_class_mask(); break;
536 case 13: rc.printDF_class_names(); break;
537 case 14: rc.printQI_queries(); break;
538 case 15: rc.printUD_queries(); break;
539 case 16: rc.printDF_class_templates(); break;
540 case 17: rc.printDF_class_templates_v(); break;
541 case 18: rc.printDiagrams(); break;
542 case 19: rc.printTemplates(); break;
543 case 20: rc.printTemplatesV(); break;
544 default:
545 err=true;
546 }
547 }
548
549 if (err) {
550 System.err.println("Usage: makedefs n (Where n = a number 1..21)");
551 }
552
553 } // main()
554 // -----------------oOo-----------------
555
556 } // Defs