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
- writeAttributeInfo
- printAttributeTab
- getAttrOfs
- writeClassInfo
- printClassTab
- main
1 /******************
2 Copyright (c) 2002 RIPE NCC
3
4 All Rights Reserved
5
6 Permission to use, copy, modify, and distribute this software and its
7 documentation for any purpose and without fee is hereby granted,
8 provided that the above copyright notice appear in all copies and that
9 both that copyright notice and this permission notice appear in
10 supporting documentation, and that the name of the author not be
11 used in advertising or publicity pertaining to distribution of the
12 software without specific, written prior permission.
13
14 THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
15 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
16 AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
17 DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
18 AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 ***************************************/
21
22 import java.io.*;
23 import java.util.*;
24 import org.xml.sax.*;
25 import org.w3c.dom.*;
26 import javax.xml.parsers.*;
27
28 /**
29 * RIPE classes generated from Data Object Models.
30 *
31 * @author ottrey@ripe.net
32 * @version $Version$
33 *
34 */
35 public class Defs {
/* [<][>][^][v][top][bottom][index][help] */
36
37
38 // hash of attribute xmlnames to attribute objects
39 private Hashtable xmlNameToAttribute;
40 // hash of attribute names to objects
41 private Hashtable nameToAttribute;
42 // hash of attribute codes to objects
43 private Hashtable codeToAttribute;
44 private Vector ripeAttributes;
45 private Vector ripeAttributeCodes;
46 private Vector ripeClasses;
47 private Vector ripeAttributeAliases;
48 private Vector ripeAttributeAliasesMap;
49 private Vector ripeClassAliases;
50 private Vector ripeClassAliasesMap;
51 private Vector ripeQueries;
52 private RPSLSyntax syntaxes;
53
54
55 // -----------------oOo-----------------
56 // Constructors
57 // -----------------oOo-----------------
58 public Defs(boolean normalize) {
59 Document syntaxDOM = getDOM("syntax.xml", normalize);
60 Node syntax_search = syntaxDOM.getFirstChild();
61 while (syntax_search != null) {
62 if (syntax_search.getNodeName().equals("rpsl_syntax")) {
63 syntaxes = new RPSLSyntax(syntax_search);
64 }
65 syntax_search = syntax_search.getNextSibling();
66 }
67
68 // Create a normalized DOM from the xml file for the attributes.
69 Document attrDOM = getDOM("attributes.xml", normalize);
70
71 // Initialize.
72 xmlNameToAttribute = new Hashtable();
73 nameToAttribute = new Hashtable();
74 codeToAttribute = new Hashtable();
75 ripeAttributes = new Vector();
76 ripeAttributeCodes = new Vector();
77 ripeAttributeAliases = new Vector();
78 ripeAttributeAliasesMap = new Vector();
79 ripeClassAliases = new Vector();
80 ripeClassAliasesMap = new Vector();
81 ripeQueries = new Vector();
82
83 // Recurse through node tree
84 NodeList attrNodes = attrDOM.getElementsByTagName("ripe_attribute");
85 for (int i=0; i < attrNodes.getLength(); i++) {
86 // (Checking if the attribute is valid)
87 if (validate("attribute", attrNodes.item(i))) {
88 AttributeDef ad = new AttributeDef(attrNodes.item(i));
89
90 ripeAttributes.addElement(ad);
91
92 // and each attribute,
93 //ripeAttributes.put(ad.getCode(), ad);
94 xmlNameToAttribute.put(ad.getXmlName(), ad);
95
96 //
97 // only add the information if this is the first time for the attribute
98 //
99 if (!nameToAttribute.containsKey(ad.getName()))
100 {
101 // add the name of the attribute
102 nameToAttribute.put(ad.getName(), ad);
103
104 // and it's code.
105 ripeAttributeCodes.addElement(ad.getCode());
106 codeToAttribute.put(ad.getCode(), ad);
107
108 // and it's aliases.
109 // also map the alias to the attribute index.
110
111 // set the index to map to.
112 Integer mapIndex = new Integer(ripeAttributeCodes.size()-1);
113
114 // first the code.
115 ripeAttributeAliases.addElement(ad.getCode());
116 ripeAttributeAliasesMap.addElement(mapIndex);
117
118 // then the name.
119 ripeAttributeAliases.addElement(ad.getName());
120 ripeAttributeAliasesMap.addElement(mapIndex);
121
122 if (ad.getAltName().length() > 1) {
123 // then the altName.
124 ripeAttributeAliases.addElement(ad.getAltName());
125 ripeAttributeAliasesMap.addElement(mapIndex);
126 }
127 } // endif (!nameToAttribute.containsKey(ad.getName())
128 }
129 }
130
131 // Create a normalized DOM from the xml file for the classes.
132 Document objDOM = getDOM("classes.xml", normalize);
133
134 // Create a vector to store the classes.
135 ripeClasses = new Vector();
136
137 // Recurse through node tree
138 NodeList objNodes = objDOM.getElementsByTagName("ripe_class");
139 for (int i=0; i < objNodes.getLength(); i++) {
140 // Check if the class is valid
141 if (validate("class", objNodes.item(i))) {
142 ClassDef od = new ClassDef(objNodes.item(i), xmlNameToAttribute);
143
144 // Add the class.
145 // ripeClasses.addElement(od);
146 if (ripeClasses.size() <= od.getDbaseCode()) {
147 ripeClasses.setSize(od.getDbaseCode()+1);
148 }
149 ripeClasses.setElementAt(od, od.getDbaseCode());
150
151 // set the index to map to.
152 // Integer mapIndex = new Integer(ripeClasses.size()-1);
153 Integer mapIndex = new Integer(od.getDbaseCode());
154
155 // first the code.
156 ripeClassAliases.addElement(od.getCode());
157 // ripeClassAliases.setElementAt(od, od.getDbaseCode());
158 ripeClassAliasesMap.addElement(mapIndex);
159
160 // then the name.
161 ripeClassAliases.addElement(od.getName());
162 // ripeClassAliases.setElementAt(od.getName(), od.getDbaseCode());
163 ripeClassAliasesMap.addElement(mapIndex);
164
165 }
166 }
167
168 // replace class/attribute variables in queries
169
170
171
172 } // Defs()
173
174 public String getValueByEnum(String name) {
/* [<][>][^][v][top][bottom][index][help] */
175 Enumeration e = ripeClasses.elements();
176 for( int i = 0; e.hasMoreElements(); i++) {
177 ClassDef d = (ClassDef)e.nextElement();
178 String a = d.getEnum();
179
180 //System.out.println( d );
181
182 if( name.equals(a) ) {
183 return (new Integer(i)).toString();
184 }
185 }
186 System.err.println("ERROR: cannot resolve variable name " + name );
187 System.exit(-1);
188
189 return ""; // bloody idiot, the compiler
190 }
191
192
193
194 /**
195 * Creates a Data Object Model from the RIPE classes defined
196 * in the XML document.
197 *
198 * @author ottrey@ripe.net
199 * @version $Version$
200 *
201 * @param xmlDocName The URI of the XML document.
202 * @param ripeClass The class to be created from.
203 * @param normalize Return a normalized DOM.
204 *
205 */
206 private Document getDOM(String xmlDocName, boolean normalize) {
/* [<][>][^][v][top][bottom][index][help] */
207
208 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
209 DocumentBuilder db = null;
210 try {
211 db = dbf.newDocumentBuilder();
212 } catch (ParserConfigurationException pce) {
213 System.err.println(pce);
214 System.exit(1);
215 }
216
217
218 Document dom = null;
219 try {
220 dom = db.parse(new File(xmlDocName));
221 } catch (SAXException se) {
222 System.err.println(se.getMessage());
223 System.exit(1);
224 } catch (IOException ioe) {
225 System.err.println(ioe);
226 System.exit(1);
227 }
228
229 // Normalize the document.
230 if (normalize) {
231 dom.getDocumentElement().normalize();
232 }
233
234 return dom;
235
236 } // getDOM()
237
238 private boolean validate(String type, Node obj) {
/* [<][>][^][v][top][bottom][index][help] */
239 boolean result=false;
240 String status = obj.getAttributes().getNamedItem("status").getNodeValue();
241 String name = obj.getAttributes().getNamedItem("name").getNodeValue();
242
243 if (status.equals("valid")) {
244 result=true;
245 }
246 else {
247 System.err.println("Warning: " + type + " " + name + " is " + status);
248 }
249
250 return result;
251 } // validClass()
252
253
254 // -----------------oOo-----------------
255 // Print Methods
256 // -----------------oOo-----------------
257 private void printDF_attribute_aliases() {
/* [<][>][^][v][top][bottom][index][help] */
258 System.out.println("char * const Attribute_aliases[] = {");
259 Enumeration e = ripeAttributeAliases.elements();
260 while (e.hasMoreElements()) {
261 String a = (String)e.nextElement();
262 System.out.println(" \"" + a + "\",");
263 }
264 System.out.println(" " + "NULL" + "\n" + "}; /* Attribute_aliases */");
265 } // printDF_attribute_aliases()
266
267 private void printDF_attribute_aliases_map() {
/* [<][>][^][v][top][bottom][index][help] */
268 System.out.println("int const Attribute_aliases_map[] = {");
269 Enumeration e = ripeAttributeAliasesMap.elements();
270 while (e.hasMoreElements()) {
271 Integer am = (Integer)e.nextElement();
272 System.out.println(" " + am + ",");
273 }
274 System.out.println(" " + "NULL" + "\n" + "}; /* Attribute_aliases_map */");
275 } // printDF_attribute_aliases_map()
276
277 private void printDF_attribute_codes() {
/* [<][>][^][v][top][bottom][index][help] */
278 System.out.println("char * const Attribute_codes[] = {");
279 Enumeration e = ripeAttributeCodes.elements();
280 while (e.hasMoreElements()) {
281 String ac = (String)e.nextElement();
282 //AttributeDef ad = (AttributeDef)ripeAttributes.get(ac);
283 AttributeDef ad = (AttributeDef)codeToAttribute.get(ac);
284
285 // If the attribute has status="valid".
286 if (ad.getStatus().equals("valid")) {
287 // Output the attribute code.
288 System.out.println(" \"" + ad.getCode() + "\",");
289 }
290 }
291 System.out.println(" " + "NULL" + "\n" + "}; /* Attribute_codes */");
292 } // printDF_attribute_codes()
293
294 private void printDF_attribute_enum() {
/* [<][>][^][v][top][bottom][index][help] */
295 System.out.println("typedef enum _A_Type_t {");
296
297 // Enumerate through the attribute codes.
298 Enumeration e = ripeAttributeCodes.elements();
299 while (e.hasMoreElements()) {
300 String ac = (String)e.nextElement();
301 //AttributeDef ad = (AttributeDef)ripeAttributes.get(ac);
302 AttributeDef ad = (AttributeDef)codeToAttribute.get(ac);
303
304 // If the attribute has status="valid".
305 if (ad.getStatus().equals("valid")) {
306 // Output the attribute enum.
307 System.out.println(" " + ad.getEnum() + ",");
308 }
309 }
310
311 System.out.println(" " + "A_END" + "\n" + "} A_Type_t;");
312 } // printDF_attribute_enum()
313
314 private void printDF_attribute_inv_attr_mask() {
/* [<][>][^][v][top][bottom][index][help] */
315 System.out.print("#define INV_ATTR_MASK ");
316
317 // Enumerate through the attribute codes.
318 Enumeration e = ripeAttributeCodes.elements();
319 while (e.hasMoreElements()) {
320 String ac = (String)e.nextElement();
321 //AttributeDef ad = (AttributeDef)ripeAttributes.get(ac);
322 AttributeDef ad = (AttributeDef)codeToAttribute.get(ac);
323
324 // If the attribute has status="valid".
325 if (ad.getStatus().equals("valid") && ad.getInverse()) {
326 // Output the attribute enum.
327 System.out.print(ad.getEnum() + ", ");
328 }
329 }
330
331 System.out.println("MA_END");
332 } // printDF_attribute_inv_attr_mask()
333
334 private void printDF_attribute_names() {
/* [<][>][^][v][top][bottom][index][help] */
335 System.out.println("char * const Attribute_names[] = {");
336 Enumeration e = ripeAttributeCodes.elements();
337 while (e.hasMoreElements()) {
338 String ac = (String)e.nextElement();
339 //AttributeDef ad = (AttributeDef)ripeAttributes.get(ac);
340 AttributeDef ad = (AttributeDef)codeToAttribute.get(ac);
341
342 // If the attribute has status="valid".
343 if (ad.getStatus().equals("valid")) {
344 // Output the attribute name.
345 System.out.println(" \"" + ad.getName() + "\",");
346 }
347 }
348 System.out.println(" " + "NULL" + "\n" + "}; /* Attribute_names */");
349 } // printDF_attribute_names()
350
351 private void printDF_class_aliases() {
/* [<][>][^][v][top][bottom][index][help] */
352 System.out.println("char * const Class_aliases[] = {");
353 Enumeration e = ripeClassAliases.elements();
354 while (e.hasMoreElements()) {
355 String a = (String)e.nextElement();
356 System.out.println(" \"" + a + "\",");
357 }
358 System.out.println(" " + "NULL" + "\n" + "}; /* Class_aliases */");
359 } // printDF_class_aliases()
360
361 private void printDF_class_aliases_map() {
/* [<][>][^][v][top][bottom][index][help] */
362 System.out.println("int const Class_aliases_map[] = {");
363 Enumeration e = ripeClassAliasesMap.elements();
364 while (e.hasMoreElements()) {
365 Integer am = (Integer)e.nextElement();
366 System.out.println(" " + am + ",");
367 }
368 System.out.println(" " + "NULL" + "\n" + "}; /* Class_aliases_map */");
369 } // printDF_class_aliases_map()
370
371 private void printDF_class_codes() {
/* [<][>][^][v][top][bottom][index][help] */
372 System.out.println("char * const Class_codes[] = {");
373 Enumeration e = ripeClasses.elements();
374 while (e.hasMoreElements()) {
375 ClassDef od = (ClassDef)e.nextElement();
376 System.out.println(" \"" + od.getCode() + "\",");
377 }
378 System.out.println(" " + "NULL" + "\n" + "}; /* Class_codes */");
379 } // printDF_class_codes()
380
381 private void printDF_class_dbase_code_map() {
/* [<][>][^][v][top][bottom][index][help] */
382 System.out.println("int const Class_dbase_code_map[] = {");
383 Enumeration e = ripeClasses.elements();
384 while (e.hasMoreElements()) {
385 ClassDef cd = (ClassDef)e.nextElement();
386 System.out.println(" " + cd.getDbaseCode() + ",");
387 }
388 System.out.println(" " + "NULL" + "\n" + "}; /* Class_dbase_code_map */");
389 } // printDF_class_dbase_code_map()
390
391 private void printDF_class_enum() {
/* [<][>][^][v][top][bottom][index][help] */
392 System.out.println("typedef enum _C_Type_t {");
393 Enumeration e = ripeClasses.elements();
394
395 System.out.println(" C_ANY = -1, ");
396 while (e.hasMoreElements()) {
397 ClassDef od = (ClassDef)e.nextElement();
398 System.out.println(" " + od.getEnum() + ",");
399 }
400 System.out.println(" " + "C_END" + "\n" + "} C_Type_t;");
401 } // printDF_class_enum()
402
403 private void printDF_class_mask() {
/* [<][>][^][v][top][bottom][index][help] */
404 System.out.print("#define CLASS_MASK ");
405 Enumeration e = ripeClasses.elements();
406 while (e.hasMoreElements()) {
407 ClassDef cd = (ClassDef)e.nextElement();
408 System.out.print(cd.getEnum() + ", ");
409 }
410 System.out.println("MA_END");
411 } // printDF_class_mask()
412
413 private void printDF_class_names() {
/* [<][>][^][v][top][bottom][index][help] */
414 System.out.println("char * const Class_names[] = {");
415 Enumeration e = ripeClasses.elements();
416 while (e.hasMoreElements()) {
417 String a = ((ClassDef)e.nextElement()).getName();
418 System.out.println(" \"" + a + "\",");
419 }
420 System.out.println(" " + "NULL" + "\n" + "}; /* Class_names */");
421 } // printDF_class_names()
422
423 private void printQI_queries() {
/* [<][>][^][v][top][bottom][index][help] */
424 System.out.println(Query.startDoc());
425 //Enumeration e1 = ripeAttributes.elements();
426 Enumeration e1 = nameToAttribute.elements();
427 while (e1.hasMoreElements()) {
428 AttributeDef ad = (AttributeDef)e1.nextElement();
429 Enumeration e2 = ad.getQueries().elements();
430 while (e2.hasMoreElements()) {
431 Query q = (Query)e2.nextElement();
432 System.out.println(q.getStruct(" ", this));
433 }
434 }
435 System.out.println(Query.endDoc());
436 } // printQI_queries()
437
438 private void printUD_queries() {
/* [<][>][^][v][top][bottom][index][help] */
439
440 Enumeration e;
441
442 System.out.println("UD_query Insert[] = {");
443 e = ripeAttributeCodes.elements();
444 while (e.hasMoreElements()) {
445 String ac = (String)e.nextElement();
446 //AttributeDef ad = (AttributeDef)ripeAttributes.get(ac);
447 AttributeDef ad = (AttributeDef)codeToAttribute.get(ac);
448 System.out.println(" {" + ad.getInsertQ_type() + ", " + "\"" + ad.getInsert() + "\"},");
449 }
450 System.out.println(" " + "{0, NULL}" + "\n" + "}; /* Insert */\n");
451
452
453 System.out.println("UD_query Update[] = {");
454 e = ripeAttributeCodes.elements();
455 while (e.hasMoreElements()) {
456 String ac = (String)e.nextElement();
457 //AttributeDef ad = (AttributeDef)ripeAttributes.get(ac);
458 AttributeDef ad = (AttributeDef)codeToAttribute.get(ac);
459 System.out.println(" {" + ad.getUpdateQ_type() + ", " + "\"" + ad.getUpdate() + "\"},");
460 }
461 System.out.println(" " + "{0, NULL}" + "\n" + "}; /* Update */\n");
462
463 System.out.println("UD_query Dummy[] = {");
464 e = ripeAttributeCodes.elements();
465 while (e.hasMoreElements()) {
466 String ac = (String)e.nextElement();
467 //AttributeDef ad = (AttributeDef)ripeAttributes.get(ac);
468 AttributeDef ad = (AttributeDef)codeToAttribute.get(ac);
469 System.out.println(" {" + ad.getDummyQ_type() + ", " + "\"" + ad.getDummy() + "\"},");
470 }
471 System.out.println(" " + "{0, NULL}" + "\n" + "}; /* Dummy */\n");
472
473 System.out.println("UD_query Select[] = {");
474 e = ripeAttributeCodes.elements();
475 while (e.hasMoreElements()) {
476 String ac = (String)e.nextElement();
477 //AttributeDef ad = (AttributeDef)ripeAttributes.get(ac);
478 AttributeDef ad = (AttributeDef)codeToAttribute.get(ac);
479 System.out.println(" {" + ad.getSelectQ_type() + ", " + "\"" + ad.getSelect() + "\"},");
480 }
481 System.out.println(" " + "{0, NULL}" + "\n" + "}; /* Select */\n");
482
483 } // printUD_queries()
484
485 private void printTemplates() {
/* [<][>][^][v][top][bottom][index][help] */
486 Enumeration e = ripeClasses.elements();
487 while (e.hasMoreElements()) {
488 ClassDef cd = (ClassDef)e.nextElement();
489 System.out.println(cd.getName() + "\n");
490 System.out.println(cd.getTemplate(false) + "\n");
491 }
492 } // printTemplates()
493
494 private void printDF_class_templates() {
/* [<][>][^][v][top][bottom][index][help] */
495 Enumeration e = ripeClasses.elements();
496 System.out.println("const char *Templates[] = {");
497 while (e.hasMoreElements()) {
498 ClassDef cd = (ClassDef)e.nextElement();
499 System.out.print(cd.getTemplate(true));
500 System.out.println(",");
501 }
502 System.out.println("NULL");
503 System.out.println("}; /* Templates */");
504 } // printDF_class_templates()
505
506 private void printDF_class_templates_v() {
/* [<][>][^][v][top][bottom][index][help] */
507 Enumeration e = ripeClasses.elements();
508 System.out.println("const char *Templates_v[] = {");
509 while (e.hasMoreElements()) {
510 ClassDef od = (ClassDef)e.nextElement();
511 System.out.print(od.getTemplateV(true, syntaxes));
512 System.out.println(",");
513 }
514 System.out.println("NULL");
515 System.out.println("}; /* Templates_v */");
516 } // printDF_class_templates_v()
517
518 private void printTemplatesV() {
/* [<][>][^][v][top][bottom][index][help] */
519 Enumeration e = ripeClasses.elements();
520 while (e.hasMoreElements()) {
521 ClassDef od = (ClassDef)e.nextElement();
522 System.out.println(od.getName() + "\n");
523 System.out.println(od.getTemplateV(false, syntaxes) + "\n");
524 }
525 } // printTemplatesV()
526
527 private void printDiagrams() {
/* [<][>][^][v][top][bottom][index][help] */
528 int maxWidth=0; // Widest diagram
529 Hashtable foreigns = new Hashtable();
530
531 Enumeration e1 = ripeClasses.elements();
532 while (e1.hasMoreElements()) {
533 ClassDef od = (ClassDef)e1.nextElement();
534 if (maxWidth < od.getWidth()) {
535 maxWidth = od.getWidth();
536 }
537
538 Hashtable foreignAttrs = od.getForeignAttrs();
539 if (foreignAttrs != null) {
540 Enumeration e2 = foreignAttrs.keys();
541 while (e2.hasMoreElements()) {
542 String key = (String)e2.nextElement();
543 if (!foreigns.containsKey(key)) {
544 foreigns.put(key, foreignAttrs.get(key));
545 }
546 }
547 }
548 }
549
550 System.out.print("Classes:");
551 for (int i=0; i < maxWidth; i++) {
552 System.out.print(" ");
553 }
554 System.out.println("Foreign keys:");
555
556 Enumeration e3 = ripeClasses.elements();
557 while (e3.hasMoreElements()) {
558 ClassDef od = (ClassDef)e3.nextElement();
559 System.out.print(od.getDiagram(maxWidth, foreigns));
560 }
561 } // printDiagrams()
562
563 private void printDF_radix_load() {
/* [<][>][^][v][top][bottom][index][help] */
564 System.out.print("DF_Load_t DF_radix_load[] = \n{\n");
565
566 // Enumerate through the attribute codes.
567 Enumeration e = ripeAttributeCodes.elements();
568 while (e.hasMoreElements()) {
569 String ac = (String)e.nextElement();
570 //AttributeDef ad = (AttributeDef)ripeAttributes.get(ac);
571 AttributeDef ad = (AttributeDef)codeToAttribute.get(ac);
572
573 // If the attribute has status="valid".
574 if (ad.getFamily() != null) {
575 String ip4 = ad.getV4Load() != null
576 ? "\"" + ad.getV4Load() + "\"" : "NULL";
577 String ip6 = ad.getV6Load() != null
578 ? "\"" + ad.getV6Load() + "\"" : "NULL";
579
580 System.out.print(" { " + ad.getEnum()
581 + ", " + ad.getFamily()
582 + ",\n\t" + ip4.replace('\n',' ')
583 + ",\n\t" + ip6.replace('\n',' ')
584 + "\n },\n");
585 }
586 } // while more
587
588
589 System.out.println(" { -1, -1, NULL, NULL }\n};");
590 } // printDF_radix_load()
591
592 private void writeAttributeInfo(PrintStream out, AttributeDef ad, int id)
/* [<][>][^][v][top][bottom][index][help] */
593 {
594 out.println(" /* name */");
595 out.println(" \"" + ad.getName() + "\",");
596 out.println(" /* id */");
597 out.println(" " + id + ",");
598 out.println(" /* altname */");
599 String altname = ad.getAltName();
600 if ((altname == null) || (altname.length() == 0)) {
601 out.println(" NULL,");
602 } else {
603 out.println(" \"" + altname + "\",");
604 }
605 out.println(" /* xmlname */");
606 out.println(" \"" + ad.getXmlName() + "\",");
607 out.println(" /* code */");
608 out.println(" \"" + ad.getCode() + "\",");
609 out.println(" /* syntax_offset */");
610 out.println(" " + syntaxes.getSyntaxOfs(ad.getSyntax()) +
611 ", /* " + ad.getSyntax() + " */");
612 out.println(" /* is_lookup */");
613 if (ad.getLookup()) {
614 out.println(" 1,");
615 } else {
616 out.println(" 0,");
617 }
618 out.println(" /* is_inverse */");
619 if (ad.getInverse()) {
620 out.println(" 1,");
621 } else {
622 out.println(" 0,");
623 }
624 out.println(" /* is_primary */");
625 if (ad.getPrimary()) {
626 out.println(" 1,");
627 } else {
628 out.println(" 0,");
629 }
630 out.println(" /* is_list */");
631 if (ad.getList()) {
632 out.println(" 1,");
633 } else {
634 out.println(" 0,");
635 }
636 out.println(" /* is_ripe_list */");
637 if (ad.getRipeList()) {
638 out.println(" 1,");
639 } else {
640 out.println(" 0,");
641 }
642 out.println(" /* foreign_code */");
643 String foreign = ad.getForeign();
644 if ((foreign == null) || (foreign.length() == 0)) {
645 out.println(" NULL,");
646 } else {
647 out.println(" \"" + foreign + "\"");
648 }
649 }
650
651 // create attribute_tab.h
652 private void printAttributeTab()
/* [<][>][^][v][top][bottom][index][help] */
653 {
654 PrintStream out = System.out;
655
656 out.println("/* generated by 'Defs.java' - DO NOT HAND-EDIT */");
657 out.println("#ifndef ATTRIBUTE_TAB_H");
658 out.println("#define ATTRIBUTE_TAB_H");
659 out.println();
660 out.println("attribute_t attribute_tab[] = {");
661
662 if (!ripeAttributeCodes.isEmpty()) {
663 Hashtable attributeIds = new Hashtable();
664
665 out.println(" {");
666 //Enumeration e = ripeAttributeCodes.elements();
667 //String code = (String)e.nextElement();
668 Enumeration e = ripeAttributes.elements();
669 //AttributeDef ad = (AttributeDef)ripeAttributes.get(code);
670 AttributeDef ad = (AttributeDef)e.nextElement();
671 int id = 0;
672 writeAttributeInfo(out, ad, id);
673 attributeIds.put(ad.getName(), new Integer(id));
674 while (e.hasMoreElements()) {
675 out.println(" },");
676 out.println(" {");
677 //code = (String)e.nextElement();
678 //ad = (AttributeDef)ripeAttributes.get(code);
679 ad = (AttributeDef)e.nextElement();
680 if (attributeIds.containsKey(ad.getName())) {
681 // if already have an id for this name, use that
682 Integer prevId = (Integer)attributeIds.get(ad.getName());
683 writeAttributeInfo(out, ad, prevId.intValue());
684 } else {
685 // increase id for each unique attribute name
686 id++;
687 writeAttributeInfo(out, ad, id);
688 attributeIds.put(ad.getName(), new Integer(id));
689 }
690 }
691 out.println(" }");
692 }
693
694 out.println("};");
695 out.println();
696 out.println("#endif /* ATTRIBUTE_TAB_H */");
697 out.close();
698 }
699
700 private int getAttrOfs(String xmlName)
/* [<][>][^][v][top][bottom][index][help] */
701 {
702 for (int i=0; i<ripeAttributes.size(); i++) {
703 AttributeDef ad = (AttributeDef)ripeAttributes.elementAt(i);
704 if (ad.getXmlName().equals(xmlName)) {
705 return i;
706 }
707 }
708 return -1;
709 }
710
711 private void writeClassInfo(PrintStream out, ClassDef cd, int id)
/* [<][>][^][v][top][bottom][index][help] */
712 {
713 out.println(" /* name */");
714 out.println(" \"" + cd.getName() + "\",");
715 out.println(" /* id */");
716 out.println(" " + id + ",");
717 out.println(" /* num_attr */");
718 Vector attributes = cd.getAttributes();
719 out.println(" " + attributes.size() + ",");
720 out.println(" /* attr[] */");
721 out.println(" {");
722 for (int i=0; i<attributes.size(); i++) {
723 AttributeDef ad = (AttributeDef)attributes.elementAt(i);
724 String name = ad.getName();
725 String xmlName = ad.getXmlName();
726 out.println(" /* attribute: " + name +
727 " (" + xmlName + ") */");
728 out.println(" {");
729 int offset = getAttrOfs(xmlName);
730 out.println(" /* offset */");
731 out.println(" " + offset + ",");
732 String choice;
733 if (ad.getChoice().equals("mandatory")) {
734 choice = "ATTR_MANDATORY";
735 } else if (ad.getChoice().equals("optional")) {
736 choice = "ATTR_OPTIONAL";
737 } else {
738 choice = "ATTR_GENERATED";
739 }
740 out.println(" /* choice */");
741 out.println(" " + choice + ",");
742 String number;
743 if (ad.getNumber().equals("single")) {
744 number = "ATTR_SINGLE";
745 } else {
746 number = "ATTR_MULTIPLE";
747 }
748 out.println(" /* number */");
749 out.println(" " + number);
750 out.print(" }");
751 if (i != attributes.size()-1) {
752 out.print(",");
753 }
754 out.println();
755 }
756 out.println(" },");
757 out.println(" /* attr_hash (set by class_init()) */");
758 out.println(" NULL");
759 }
760
761 // create class_tab.h
762 private void printClassTab()
/* [<][>][^][v][top][bottom][index][help] */
763 {
764 PrintStream out = System.out;
765
766 out.println("/* generated by 'Defs.java' - DO NOT HAND-EDIT */");
767 out.println("#ifndef CLASS_TAB_H");
768 out.println("#define CLASS_TAB_H");
769 out.println();
770 out.println("class_t class_tab[] = {");
771
772 if (!ripeClasses.isEmpty()) {
773 out.println(" {");
774 Enumeration e = ripeClasses.elements();
775 ClassDef cd = (ClassDef)e.nextElement();
776 int id = 0;
777 writeClassInfo(out, cd, id);
778 while (e.hasMoreElements()) {
779 out.println(" },");
780 out.println(" {");
781 cd = (ClassDef)e.nextElement();
782 id++;
783 writeClassInfo(out, cd, id);
784 }
785 out.println(" }");
786 }
787
788 out.println("};");
789 out.println();
790 out.println("#endif /* CLASS_TAB_H */");
791 out.close();
792 }
793
794 // -----------------oOo-----------------
795 // Unit test driver
796 // -----------------oOo-----------------
797 public static void main (String argv[]) {
/* [<][>][^][v][top][bottom][index][help] */
798 int n=0;
799 boolean err=true;
800 boolean normalize=false;
801
802 if (argv.length > 0) {
803 try {
804 n = Integer.parseInt(argv[0]);
805 err=false;
806 }
807 catch (NumberFormatException e) {
808 }
809 if (argv.length == 2) {
810 if (argv[1].equals("normalize")) {
811 normalize = true;
812 }
813 }
814 }
815
816 if (!err) {
817 Defs rc = new Defs(normalize);
818
819 switch (n) {
820 case 1: rc.printDF_attribute_aliases(); break;
821 case 2: rc.printDF_attribute_aliases_map(); break;
822 case 3: rc.printDF_attribute_codes(); break;
823 case 4: rc.printDF_attribute_enum(); break;
824 case 5: rc.printDF_attribute_inv_attr_mask(); break;
825 case 6: rc.printDF_attribute_names(); break;
826 case 7: rc.printDF_class_aliases(); break;
827 case 8: rc.printDF_class_aliases_map(); break;
828 case 9: rc.printDF_class_codes(); break;
829 case 10: rc.printDF_class_dbase_code_map(); break;
830 case 11: rc.printDF_class_enum(); break;
831 case 12: rc.printDF_class_mask(); break;
832 case 13: rc.printDF_class_names(); break;
833 case 14: rc.printQI_queries(); break;
834 case 15: rc.printUD_queries(); break;
835 case 16: rc.printDF_class_templates(); break;
836 case 17: rc.printDF_class_templates_v(); break;
837 case 18: rc.printDiagrams(); break;
838 case 19: rc.printTemplates(); break;
839 case 20: rc.printTemplatesV(); break;
840 case 21: rc.printDF_radix_load(); break;
841 case 22: rc.printAttributeTab(); break;
842 case 23: rc.printClassTab(); break;
843 default:
844 err=true;
845 }
846 }
847
848 if (err) {
849 System.err.println("Usage: makedefs n (Where n = a number 1..23)");
850 }
851
852 } // main()
853 // -----------------oOo-----------------
854
855 } // Defs