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

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