## defs/Defs.java

/* [<][>][^][v][top][bottom][index][help] */

## DEFINITIONS

This source file includes following functions.

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

/* [<][>][^][v][top][bottom][index][help] */