defs/AttributeDef.java

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

FUNCTIONS

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

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