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
- printDF_radix_load
- 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
336 System.out.println(" C_ANY = -1, ");
337 while (e.hasMoreElements()) {
338 ClassDef od = (ClassDef)e.nextElement();
339 System.out.println(" " + od.getEnum() + ",");
340 }
341 System.out.println(" " + "C_END" + "\n" + "} C_Type_t;");
342 } // printDF_class_enum()
343
344 private void printDF_class_mask() {
/* [<][>][^][v][top][bottom][index][help] */
345 System.out.print("#define CLASS_MASK ");
346 Enumeration e = ripeClasses.elements();
347 while (e.hasMoreElements()) {
348 ClassDef cd = (ClassDef)e.nextElement();
349 System.out.print(cd.getEnum() + ", ");
350 }
351 System.out.println("MA_END");
352 } // printDF_class_mask()
353
354 private void printDF_class_names() {
/* [<][>][^][v][top][bottom][index][help] */
355 System.out.println("char * const Class_names[] = {");
356 Enumeration e = ripeClasses.elements();
357 while (e.hasMoreElements()) {
358 String a = ((ClassDef)e.nextElement()).getName();
359 System.out.println(" \"" + a + "\",");
360 }
361 System.out.println(" " + "NULL" + "\n" + "}; /* Class_names */");
362 } // printDF_class_names()
363
364 private void printQI_queries() {
/* [<][>][^][v][top][bottom][index][help] */
365 System.out.println(Query.startDoc());
366 Enumeration e1 = ripeAttributes.elements();
367 while (e1.hasMoreElements()) {
368 AttributeDef ad = (AttributeDef)e1.nextElement();
369 Enumeration e2 = ad.getQueries().elements();
370 while (e2.hasMoreElements()) {
371 Query q = (Query)e2.nextElement();
372 System.out.println(q.getStruct(" ", this));
373 }
374 }
375 System.out.println(Query.endDoc());
376 } // printQI_queries()
377
378 private void printUD_queries() {
/* [<][>][^][v][top][bottom][index][help] */
379
380 Enumeration e;
381
382 System.out.println("UD_query Insert[] = {");
383 e = ripeAttributeCodes.elements();
384 while (e.hasMoreElements()) {
385 String ac = (String)e.nextElement();
386 AttributeDef ad = (AttributeDef)ripeAttributes.get(ac);
387 System.out.println(" {" + ad.getInsertQ_type() + ", " + "\"" + ad.getInsert() + "\"},");
388 }
389 System.out.println(" " + "{0, NULL}" + "\n" + "}; /* Insert */\n");
390
391
392 System.out.println("UD_query Update[] = {");
393 e = ripeAttributeCodes.elements();
394 while (e.hasMoreElements()) {
395 String ac = (String)e.nextElement();
396 AttributeDef ad = (AttributeDef)ripeAttributes.get(ac);
397 System.out.println(" {" + ad.getUpdateQ_type() + ", " + "\"" + ad.getUpdate() + "\"},");
398 }
399 System.out.println(" " + "{0, NULL}" + "\n" + "}; /* Update */\n");
400
401 System.out.println("UD_query Dummy[] = {");
402 e = ripeAttributeCodes.elements();
403 while (e.hasMoreElements()) {
404 String ac = (String)e.nextElement();
405 AttributeDef ad = (AttributeDef)ripeAttributes.get(ac);
406 System.out.println(" {" + ad.getDummyQ_type() + ", " + "\"" + ad.getDummy() + "\"},");
407 }
408 System.out.println(" " + "{0, NULL}" + "\n" + "}; /* Dummy */\n");
409
410 System.out.println("UD_query Select[] = {");
411 e = ripeAttributeCodes.elements();
412 while (e.hasMoreElements()) {
413 String ac = (String)e.nextElement();
414 AttributeDef ad = (AttributeDef)ripeAttributes.get(ac);
415 System.out.println(" {" + ad.getSelectQ_type() + ", " + "\"" + ad.getSelect() + "\"},");
416 }
417 System.out.println(" " + "{0, NULL}" + "\n" + "}; /* Select */\n");
418
419 } // printUD_queries()
420
421 private void printTemplates() {
/* [<][>][^][v][top][bottom][index][help] */
422 Enumeration e = ripeClasses.elements();
423 while (e.hasMoreElements()) {
424 ClassDef cd = (ClassDef)e.nextElement();
425 System.out.println(cd.getName() + "\n");
426 System.out.println(cd.getTemplate(false) + "\n");
427 }
428 } // printTemplates()
429
430 private void printDF_class_templates() {
/* [<][>][^][v][top][bottom][index][help] */
431 Enumeration e = ripeClasses.elements();
432 System.out.println("const char *Templates[] = {");
433 while (e.hasMoreElements()) {
434 ClassDef cd = (ClassDef)e.nextElement();
435 System.out.print(cd.getTemplate(true));
436 System.out.println(",");
437 }
438 System.out.println("NULL");
439 System.out.println("}; /* Templates */");
440 } // printDF_class_templates()
441
442 private void printDF_class_templates_v() {
/* [<][>][^][v][top][bottom][index][help] */
443 Enumeration e = ripeClasses.elements();
444 System.out.println("const char *Templates_v[] = {");
445 while (e.hasMoreElements()) {
446 ClassDef od = (ClassDef)e.nextElement();
447 System.out.print(od.getTemplateV(true));
448 System.out.println(",");
449 }
450 System.out.println("NULL");
451 System.out.println("}; /* Templates_v */");
452 } // printDF_class_templates_v()
453
454 private void printTemplatesV() {
/* [<][>][^][v][top][bottom][index][help] */
455 Enumeration e = ripeClasses.elements();
456 while (e.hasMoreElements()) {
457 ClassDef od = (ClassDef)e.nextElement();
458 System.out.println(od.getName() + "\n");
459 System.out.println(od.getTemplateV(false) + "\n");
460 }
461 } // printTemplatesV()
462
463 private void printDiagrams() {
/* [<][>][^][v][top][bottom][index][help] */
464 int maxWidth=0; // Widest diagram
465 Hashtable foreigns = new Hashtable();
466
467 Enumeration e1 = ripeClasses.elements();
468 while (e1.hasMoreElements()) {
469 ClassDef od = (ClassDef)e1.nextElement();
470 if (maxWidth < od.getWidth()) {
471 maxWidth = od.getWidth();
472 }
473
474 Hashtable foriegnAttrs = od.getForeignAttrs();
475 if (foriegnAttrs != null) {
476 Enumeration e2 = foriegnAttrs.keys();
477 while (e2.hasMoreElements()) {
478 String key = (String)e2.nextElement();
479 if (!foreigns.containsKey(key)) {
480 foreigns.put(key, foriegnAttrs.get(key));
481 }
482 }
483 }
484 }
485
486 System.out.print("Classes:");
487 for (int i=0; i < maxWidth; i++) {
488 System.out.print(" ");
489 }
490 System.out.println("Foreign keys:");
491
492 Enumeration e3 = ripeClasses.elements();
493 while (e3.hasMoreElements()) {
494 ClassDef od = (ClassDef)e3.nextElement();
495 System.out.print(od.getDiagram(maxWidth, foreigns));
496 }
497 } // printDiagrams()
498
499 private void printDF_radix_load() {
/* [<][>][^][v][top][bottom][index][help] */
500 System.out.print("DF_Load_t DF_radix_load[] = \n{\n");
501
502 // Enumerate through the attribute codes.
503 Enumeration e = ripeAttributeCodes.elements();
504 while (e.hasMoreElements()) {
505 String ac = (String)e.nextElement();
506 AttributeDef ad = (AttributeDef)ripeAttributes.get(ac);
507
508 // If the attribute has status="valid".
509 if (ad.getFamily() != null) {
510 String ip4 = ad.getV4Load() != null
511 ? "\"" + ad.getV4Load() + "\"" : "NULL";
512 String ip6 = ad.getV6Load() != null
513 ? "\"" + ad.getV6Load() + "\"" : "NULL";
514
515 System.out.print(" { " + ad.getEnum()
516 + ", " + ad.getFamily()
517 + ",\n\t" + ip4.replace('\n',' ')
518 + ",\n\t" + ip6.replace('\n',' ')
519 + "\n },\n");
520 }
521 } // while more
522
523
524 System.out.println(" { -1, -1, NULL, NULL }\n};");
525 } // printDF_radix_load()
526
527 // -----------------oOo-----------------
528 // Unit test driver
529 // -----------------oOo-----------------
530 public static void main (String argv[]) {
/* [<][>][^][v][top][bottom][index][help] */
531 int n=0;
532 boolean err=true;
533 boolean normalize=false;
534
535 if (argv.length > 0) {
536 try {
537 n = Integer.parseInt(argv[0]);
538 err=false;
539 }
540 catch (NumberFormatException e) {
541 }
542 if (argv.length == 2) {
543 if (argv[1].equals("normalize")) {
544 normalize = true;
545 }
546 }
547 }
548
549 if (!err) {
550 Defs rc = new Defs(normalize);
551
552 switch (n) {
553 case 1: rc.printDF_attribute_aliases(); break;
554 case 2: rc.printDF_attribute_aliases_map(); break;
555 case 3: rc.printDF_attribute_codes(); break;
556 case 4: rc.printDF_attribute_enum(); break;
557 case 5: rc.printDF_attribute_inv_attr_mask(); break;
558 case 6: rc.printDF_attribute_names(); break;
559 case 7: rc.printDF_class_aliases(); break;
560 case 8: rc.printDF_class_aliases_map(); break;
561 case 9: rc.printDF_class_codes(); break;
562 case 10: rc.printDF_class_dbase_code_map(); break;
563 case 11: rc.printDF_class_enum(); break;
564 case 12: rc.printDF_class_mask(); break;
565 case 13: rc.printDF_class_names(); break;
566 case 14: rc.printQI_queries(); break;
567 case 15: rc.printUD_queries(); break;
568 case 16: rc.printDF_class_templates(); break;
569 case 17: rc.printDF_class_templates_v(); break;
570 case 18: rc.printDiagrams(); break;
571 case 19: rc.printTemplates(); break;
572 case 20: rc.printTemplatesV(); break;
573 case 21: rc.printDF_radix_load(); break;
574 default:
575 err=true;
576 }
577 }
578
579 if (err) {
580 System.err.println("Usage: makedefs n (Where n = a number 1..21)");
581 }
582
583 } // main()
584 // -----------------oOo-----------------
585
586 } // Defs