## defs/AttributeDef.java

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

## FUNCTIONS

This source file includes following functions.

1. AttributeDef
2. appendQueries
3. getTextFromNode
4. getNodeRawValue
5. getCode
6. getName
7. getAltName
8. getStatus
9. getDescription
10. getFormat
11. getEnum
12. getChoice
13. getNumber
14. getKeytype
15. getInsert
16. getInsertQ_type
17. getUpdate
18. getUpdateQ_type
19. getDummy
20. getDummyQ_type
21. getSelect
22. getSelectQ_type
23. getKeytype2
24. getKeytype3
25. getForeign
26. getInverse
27. getPrimary
28. getQueries
29. setChoice
30. setNumber
31. clone
32. toString

       1 import java.util.*;
       2 import org.w3c.dom.*;
       3 import com.sun.xml.tree.*;
       4
       5 /**
       6  * RIPE attribute.
       7  *
       8  * @author ottrey@ripe.net
       9  * @version $Version$
      10  *
      11  */
      12 public class AttributeDef implements Cloneable {
         /* [<][>][^][v][top][bottom][index][help] */
      13
      14   final static int QI_SQL   = 1;
      15   final static int QI_RADIX = 2;
      16
      17   private String name;
      18   private String altName;
      19   private String code;
      20   private String status;
      21
      22   private String description;
      23   private String format;
      24
      25   private boolean lookup;
      26   private boolean inverse;
      27   private boolean primary;
      28   private String foreign;
      29   private String keytype;
      30
      31   private String insert;
      32   private String insertQ_type;
      33   private String update;
      34   private String updateQ_type;
      35   private String dummy;
      36   private String dummyQ_type;
      37   private String select;
      38   private String selectQ_type;
      39
      40   private String choice;
      41   private String number;
      42
      43   private Vector queries;
      44
      45   // -----------------oOo-----------------
      46   //              Constructors
      47   // -----------------oOo-----------------
      48   /**
      49    * Creates a RIPE attribute.
      50    *
      51    * @author ottrey@ripe.net
      52    * @version $Version$
      53    *
      54    * @param obj The node from which a RIPE attribute is made.
      55    *
      56    */
      57   public AttributeDef(Node obj) {
      58     name      = obj.getAttributes().getNamedItem("name").getNodeValue();
      59     code      = obj.getAttributes().getNamedItem("code").getNodeValue();
      60     status    = obj.getAttributes().getNamedItem("status").getNodeValue();
      61
      62     // Blindly ask for the optional items.
      63     try {
      64       altName   = obj.getAttributes().getNamedItem("altName").getNodeValue();
      65     }
      66     catch (NullPointerException e) {
      67       altName = new String();
      68       // Throw the exception away.  :-)
      69     }
      70
      71     // Prepare to walk the tree.
      72     TreeWalker tw = new TreeWalker(obj);
      73
      74     // Get the "description" node.
      75     description = getNodeRawValue(tw.getNextElement("description"));
      76
      77     // Get the "format" node.
      78     format = getNodeRawValue(tw.getNextElement("format"));
      79
      80     // Initialize
      81     foreign = new String();
      82     lookup = false;
      83     inverse = false;
      84     primary = false;
      85
      86     insert       = new String();
      87     insertQ_type = new String("UD_NULL_");
      88     update       = new String();
      89     updateQ_type = new String("UD_NULL_");
      90     dummy        = new String();
      91     dummyQ_type  = new String("UD_NULL_");
      92     select       = new String();
      93     selectQ_type = new String("UD_NULL_");
      94
      95     queries = new Vector();
      96
      97     Node rp = tw.getNextElement("representation");
      98     if (rp != null) {
      99       // Get the insert.
     100       Node in = (new TreeWalker(rp)).getNextElement("insert");
     101       if (in != null) {
     102         insert = getTextFromNode(in);
     103         if( insert.length() > 0 ) {
     104             insert = " " + insert + " ";
     105         }
     106         try {
     107           insertQ_type = in.getAttributes().getNamedItem("qtype").getNodeValue().toUpperCase();
     108         }
     109         catch (NullPointerException e) {
     110         }
     111       }
     112
     113       // Get the updates.
     114       Node un = (new TreeWalker(rp)).getNextElement("update");
     115       if (un != null) {
     116         update = getTextFromNode(un);
     117         if( update.length() > 0 ) {
     118               update = " " + update + " ";
     119         }
     120         try {
     121           updateQ_type = un.getAttributes().getNamedItem("qtype").getNodeValue().toUpperCase();
     122         }
     123         catch (NullPointerException e) {
     124         }
     125       }
     126
     127       // Get the dummies.
     128       Node dn = (new TreeWalker(rp)).getNextElement("dummy");
     129       if (dn != null) {
     130         dummy =  getTextFromNode(dn);
     131         if( dummy.length() > 0 ) {
     132               dummy = " " + dummy + " ";
     133         }
     134         try {
     135           dummyQ_type = dn.getAttributes().getNamedItem("qtype").getNodeValue().toUpperCase();
     136         }
     137         catch (NullPointerException e) {
     138         }
     139       }
     140
     141       // Get the selects.
     142       Node sn = (new TreeWalker(rp)).getNextElement("select");
     143       if (sn != null) {
     144         select = getTextFromNode(sn);
     145         if( select.length() > 0 ) {
     146               select = " " + select + " ";
     147         }
     148         try {
     149           selectQ_type = sn.getAttributes().getNamedItem("qtype").getNodeValue().toUpperCase();
     150         }
     151         catch (NullPointerException e) {
     152         }
     153       }
     154     } // rp!=NULL
     155
     156     Node kn = tw.getNextElement("keys");
     157     if (kn != null) {
     158       String searchable = kn.getAttributes().getNamedItem("searchable").getNodeValue();
     159       inverse = searchable.equals("inverse");
     160       lookup = searchable.equals("lookup");
     161
     162       TreeWalker fw = new TreeWalker(kn);
     163       Node f = fw.getNextElement("foreign");
     164       if (f != null) {
     165         foreign = f.getAttributes().getNamedItem("value").getNodeValue();
     166       }
     167
     168       TreeWalker pw = new TreeWalker(kn);
     169       Node p = pw.getNextElement("primary");
     170       if (p != null) {
     171         primary = true;
     172       }
     173
     174       // Get the queries.
     175       Node qsn = (new TreeWalker(kn)).getNextElement("queries");
     176
     177       appendQueries(queries, qsn, "sqlquery",  code);
     178       appendQueries(queries, qsn, "radixquery",code);
     179     }
     180
     181     // Now check cominations.
     182     // XXX TODO
     183
     184   } // AttributeDef()
     185
     186   private void appendQueries(Vector queries, Node qsn, String qrytype, String attrcode) {
         /* [<][>][^][v][top][bottom][index][help] */
     187     if (qsn != null) {
     188       TreeWalker qsw;
     189       Node q;
     190       String qryt;
     191
     192       qsw = new TreeWalker(qsn);
     193       while ((q = qsw.getNextElement(qrytype)) != null) {
     194         String keytype = q.getAttributes().getNamedItem("keytype").getNodeValue();
     195
     196         // Blindly get the optional values.
     197         String clars = new String();
     198         try {
     199           clars = q.getAttributes().getNamedItem("class").getNodeValue();
     200         }
     201         catch (NullPointerException e) {
     202             // XXX take the default
     203           clars = attrcode;
     204         }
     205
     206         String space = new String();
     207         try {
     208             space = q.getAttributes().getNamedItem("space").getNodeValue();
     209         }
     210         catch (NullPointerException e) {
     211         }
     212
     213
     214         String sqlQuery = getTextFromNode(q);
     215         //System.err.println("sqlquery = " + sqlQuery);
     216
     217         if ( qrytype.equals("sqlquery") ) {
     218             qryt = "SQL";
     219         } else {
     220             qryt = "RADIX";
     221         }
     222
     223         Query query = new Query(qryt, lookup, keytype, code, clars, sqlQuery);
     224         queries.addElement(query);
     225       }
     226     }
     227   } // getQueries()
     228
     229
     230
     231     // getting parsed contents of the text node is not simple.
     232     // see http://www.developerlife.com/xmljavatutorial1/default.htm
     233
     234     // it was made simpler by the getNodeValue(Node n) method
     235     // defined below, but it operated on raw XML text fragments
     236
     237 private String getTextFromNode( Node q ) {
         /* [<][>][^][v][top][bottom][index][help] */
     238     Element query_elem = (Element) q;
     239     NodeList list = query_elem.getChildNodes();
     240     int size = list.getLength();
     241
     242     for (int i = 0 ; i < size ; i ++ ){
     243         String value =
     244             ((Node)list.item( i )).getNodeValue().trim();
     245         //System.err.println("i=" + i + " val=" + value );
     246
     247         if( value.equals("") || value.equals("\r") ){
     248             continue; //keep iterating
     249         }
     250         else{
     251             return value;
     252         }
     253     }
     254
     255     return "";
     256   }
     257   /**
     258    * Aaaargh I shouldn't have to write this. :-(
     259    *
     260    * @param        node
     261    * @return       The value of the node.
     262    * @see          ClassDef
     263    *
     264    */
     265   private String getNodeRawValue(Node node) {
         /* [<][>][^][v][top][bottom][index][help] */
     266     String nodeStr = node.toString();
     267     int startIndex = nodeStr.indexOf('>') + 1;
     268     int endIndex = nodeStr.lastIndexOf('<') - 1;
     269
     270     return nodeStr.substring(startIndex, endIndex);
     271   } // getNodeRaw Value()
     272
     273
     274
     275   public String getCode() {
         /* [<][>][^][v][top][bottom][index][help] */
     276     return code;
     277   } // getCode()
     278
     279   public String getName() {
         /* [<][>][^][v][top][bottom][index][help] */
     280     return name;
     281   } // getName()
     282
     283   public String getAltName() {
         /* [<][>][^][v][top][bottom][index][help] */
     284     return altName;
     285   } // getAltName()
     286
     287   public String getStatus() {
         /* [<][>][^][v][top][bottom][index][help] */
     288     return status;
     289   } // getStatus()
     290
     291   public String getDescription() {
         /* [<][>][^][v][top][bottom][index][help] */
     292     return description;
     293   } // getDescription()
     294
     295   public String getFormat() {
         /* [<][>][^][v][top][bottom][index][help] */
     296     return format;
     297   } // getFormat()
     298
     299   public String getEnum() {
         /* [<][>][^][v][top][bottom][index][help] */
     300     return new String("A_" + code).toUpperCase();
     301   } // getEnum()
     302
     303   public String getChoice() {
         /* [<][>][^][v][top][bottom][index][help] */
     304     return choice;
     305   } // getChoice()
     306
     307   public String getNumber() {
         /* [<][>][^][v][top][bottom][index][help] */
     308     return number;
     309   } // getNumber()
     310
     311   public String getKeytype() {
         /* [<][>][^][v][top][bottom][index][help] */
     312     return keytype;
     313   } // getKeytype()
     314
     315   public String getInsert() {
         /* [<][>][^][v][top][bottom][index][help] */
     316     return insert;
     317   } // getInsert()
     318
     319   public String getInsertQ_type() {
         /* [<][>][^][v][top][bottom][index][help] */
     320     return insertQ_type;
     321   } // getInsertQ_type()
     322
     323   public String getUpdate() {
         /* [<][>][^][v][top][bottom][index][help] */
     324     return update;
     325   } // getUpdate()
     326
     327   public String getUpdateQ_type() {
         /* [<][>][^][v][top][bottom][index][help] */
     328     return updateQ_type;
     329   } // getUpdateQ_type()
     330
     331   public String getDummy() {
         /* [<][>][^][v][top][bottom][index][help] */
     332     return dummy;
     333   } // getDummy()
     334
     335   public String getDummyQ_type() {
         /* [<][>][^][v][top][bottom][index][help] */
     336     return dummyQ_type;
     337   } // getDummyQ_type()
     338
     339   public String getSelect() {
         /* [<][>][^][v][top][bottom][index][help] */
     340     return select;
     341   } // getSelect()
     342
     343   public String getSelectQ_type() {
         /* [<][>][^][v][top][bottom][index][help] */
     344     return selectQ_type;
     345   } // getSelectQ_type()
     346
     347   public String getKeytype2() {
         /* [<][>][^][v][top][bottom][index][help] */
     348     String result = new String();
     349
     350     if      (!lookup && !inverse && !primary) {
     351       result = " ";
     352     }
     353     else if (!lookup && !inverse &&  primary) {
     354       result = "primary key";
     355     }
     356     else if (!lookup &&  inverse && !primary) {
     357       result = "inverse key";
     358     }
     359     else if (!lookup &&  inverse &&  primary) {
     360       result = "primary/inverse key";
     361     }
     362     else if ( lookup && !inverse && !primary) {
     363       result = "lookup key";
     364     }
     365     else if ( lookup && !inverse &&  primary) {
     366       result = "primary/look-up key";
     367     }
     368     else if ( lookup &&  inverse && !primary) {
     369       result = "look-up/inverse key";
     370     }
     371     else if ( lookup &&  inverse &&  primary) {
     372       result = "Gimmie a break!";
     373     }
     374
     375     return result;
     376   } // getKeytype()
     377
     378   public String getKeytype3() {
         /* [<][>][^][v][top][bottom][index][help] */
     379     String result = new String();
     380
     381     if (primary) {
     382       result = "[P]";
     383     }
     384     else  {
     385       result = "   ";
     386     }
     387
     388     if (inverse) {
     389       result += "[I]";
     390     }
     391     else if (lookup) {
     392       result += "[L]";
     393     }
     394     else {
     395       result += "   ";
     396     }
     397
     398     return result;
     399   } // getKeytype()
     400
     401   public String getForeign() {
         /* [<][>][^][v][top][bottom][index][help] */
     402     return foreign;
     403   } // getForeign()
     404
     405   public boolean getInverse() {
         /* [<][>][^][v][top][bottom][index][help] */
     406     return inverse;
     407   } // getInverse()
     408
     409   public boolean getPrimary() {
         /* [<][>][^][v][top][bottom][index][help] */
     410     return primary;
     411   } // getPrimary()
     412
     413   public Vector getQueries() {
         /* [<][>][^][v][top][bottom][index][help] */
     414     return queries;
     415   } // getQueries()
     416
     417   public boolean setChoice(String choice) {
         /* [<][>][^][v][top][bottom][index][help] */
     418     boolean result=true;
     419
     420     this.choice = choice;
     421
     422     return result;
     423   } // setChoice()
     424
     425   public boolean setNumber(String number) {
         /* [<][>][^][v][top][bottom][index][help] */
     426     boolean result=true;
     427
     428     this.number = number;
     429
     430     return result;
     431   } // setNumber()
     432
     433   public Object clone() throws CloneNotSupportedException {
         /* [<][>][^][v][top][bottom][index][help] */
     434     return (AttributeDef)super.clone();
     435   } // clone()
     436
     437   /*
     438   public boolean equals(String code) {
     439     return code.equals(code);
     440   } // equals()
     441   */
     442
     443   public String toString() {
         /* [<][>][^][v][top][bottom][index][help] */
     444     return new String("ripe attribute={" +
     445                          "\n\tname="        + name        +
     446                          "\n\taltName="     + altName     +
     447                          "\n\tcode="        + code        +
     448                          "\n\tstatus="      + status      +
     449                          "\n\tkeytype="     + keytype     +
     450                          "\n\tdescription=" + description +
     451                          "\n\tformat="      + format      +
     452                          "\n\tchoice="      + choice      +
     453                          "\n\tnumber="      + number      +
     454                          "\n}");
     455   } // toString()
     456
     457
     458 } // AttributeDef

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