## defs/AttributeDef.java

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

## DEFINITIONS

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. getXmlName
12. getStatus
13. getDescription
14. getSyntax
15. getEnum
16. getChoice
17. getNumber
18. getPseudo
19. getKeytype
20. getInsert
21. getInsertQ_type
22. getUpdate
23. getUpdateQ_type
24. getDummy
25. getDummyQ_type
26. getSelect
27. getSelectQ_type
28. getKeytype2
29. getKeytype3
30. getForeign
31. getLookup
32. getInverse
33. getPrimary
34. getList
35. getRipeList
36. getQueries
37. setChoice
38. setNumber
39. setPseudo
40. clone
41. toString

       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.util.*;
      23 import org.w3c.dom.*;
      24 import org.xml.sax.*;
      25
      26 /**
      27  * RIPE attribute.
      28  *
      29  * @author ottrey@ripe.net
      30  * @version $Version$
      31  *
      32  */
      33 public class AttributeDef implements Cloneable {
         /* [<][>][^][v][top][bottom][index][help] */
      34
      35   final static int QI_SQL   = 1;
      36   final static int QI_RADIX = 2;
      37
      38   private String name;
      39   private String altName;
      40   private String xmlName;
      41   private String code;
      42   private String status;
      43
      44   private String description;
      45   private String syntax;
      46
      47   private boolean lookup;
      48   private boolean inverse;
      49   private boolean primary;
      50   private boolean list;
      51   private boolean ripeList;
      52   private String foreign;
      53   private String keytype;
      54
      55     // UD loading/updating/deleting queries
      56   private String insert;
      57   private String insertQ_type;
      58   private String update;
      59   private String updateQ_type;
      60   private String dummy;
      61   private String dummyQ_type;
      62   private String select;
      63   private String selectQ_type;
      64
      65   // these actually come from the RPSL *class* definition, but they got
      66   // stuck in this Java class anyway...
      67   private String choice;
      68   private String number;
      69   private boolean pseudo;    // a pseudo-attribute is "delete" or "override"
      70                              // these are not "real" attributes, and as such
      71                              // should not appear in template definitions
      72
      73   // radix tree representation
      74   private String family;
      75   private String load_ipv4; // query to load the ipv4 tree
      76   private String load_ipv6; // query to load the ipv6 tree
      77
      78   private Vector queries;
      79
      80   // -----------------oOo-----------------
      81   //              Constructors
      82   // -----------------oOo-----------------
      83   /**
      84    * Creates a RIPE attribute.
      85    *
      86    * @author ottrey@ripe.net
      87    * @version $Version$
      88    *
      89    * @param obj The node from which a RIPE attribute is made.
      90    *
      91    */
      92   public AttributeDef(Node obj) {
      93     name      = obj.getAttributes().getNamedItem("name").getNodeValue();
      94     code      = obj.getAttributes().getNamedItem("code").getNodeValue();
      95     status    = obj.getAttributes().getNamedItem("status").getNodeValue();
      96
      97     // Blindly ask for the optional items.
      98     try {
      99       altName   = obj.getAttributes().getNamedItem("altName").getNodeValue();
     100     }
     101     catch (NullPointerException e) {
     102       altName = new String();
     103       // Throw the exception away.  :-)
     104     }
     105
     106     // get xml name if defined, otherwise set to name
     107     try
     108     {
     109         xmlName = obj.getAttributes().getNamedItem("xmlname").getNodeValue();
     110     }
     111     catch (NullPointerException e)
     112     {
     113         xmlName = name;
     114     }
     115
     116     // Initialize
     117     foreign = new String();
     118     lookup = false;
     119     inverse = false;
     120     primary = false;
     121     list = false;
     122     ripeList = false;
     123
     124     insert       = new String();
     125     insertQ_type = new String("UD_NULL_");
     126     update       = new String();
     127     updateQ_type = new String("UD_NULL_");
     128     dummy        = new String();
     129     dummyQ_type  = new String("UD_NULL_");
     130     select       = new String();
     131     selectQ_type = new String("UD_NULL_");
     132
     133     queries = new Vector();
     134
     135     // representation Node
     136     Node rp = null;
     137
     138     // keys node
     139     Node kn = null;
     140
     141     // loop to retrieve elements
     142     Node search = obj.getFirstChild();
     143     while (search != null) {
     144         String nodeName = search.getNodeName();
     145         if (nodeName.equals("description")) {
     146             // Get the "description" node.
     147             description = getNodeRawValue(search);
     148         } else if (nodeName.equals("syntax")) {
     149             Node listSearch = search.getFirstChild();
     150             while (listSearch != null) {
     151                 if (listSearch.getNodeName().equals("list")) {
     152                     list = true;
     153                 } else if (listSearch.getNodeName().equals("ripe-list")) {
     154                     ripeList = true;
     155                 }
     156                 listSearch = listSearch.getNextSibling();
     157             }
     158             // Get the "syntax" node.
     159             syntax = getNodeRawValue(search);
     160         } else if (nodeName.equals("representation")) {
     161             rp = search;
     162         } else if (nodeName.equals("keys")) {
     163             kn = search;
     164         }
     165         search = search.getNextSibling();
     166     }
     167
     168     // find representation details
     169     Node sqlTable = null;
     170     if (rp != null) {
     171         Node rpSearch = rp.getFirstChild();
     172         while (rpSearch != null) {
     173             if (rpSearch.getNodeName().equals("sqltable")) {
     174                 sqlTable = rpSearch;
     175             } else if (rpSearch.getNodeName().equals("radixtrees")) {
     176                 NamedNodeMap map = rpSearch.getAttributes();
     177                 family = map.getNamedItem("family").getNodeValue();
     178                 Node rxSearch = rpSearch.getFirstChild();
     179                 while (rxSearch != null) {
     180                     if (rxSearch.getNodeName().equals("IP_V4")) {
     181                         load_ipv4 = getTextFromNode(rxSearch);
     182                     } else if (rxSearch.getNodeName().equals("IP_V6")) {
     183                         load_ipv6 = getTextFromNode(rxSearch);
     184                     }
     185                     rxSearch = rxSearch.getNextSibling();
     186                 }
     187             }
     188             rpSearch = rpSearch.getNextSibling();
     189         }
     190     }
     191     // get sqltable details here
     192     if (sqlTable != null) {
     193         Node sqlTableSearch = sqlTable.getFirstChild();
     194         while (sqlTableSearch != null) {
     195             String nodeName = sqlTableSearch.getNodeName();
     196             if (nodeName.equals("insert")) {
     197                 // Get the insert.
     198                 insert = getTextFromNode(sqlTableSearch);
     199                 if (insert.length() > 0) {
     200                     insert = " " + insert + " ";
     201                 }
     202                 Node qtypeAttr =
     203                   sqlTableSearch.getAttributes().getNamedItem("qtype");
     204                 if (qtypeAttr != null) {
     205                     insertQ_type = qtypeAttr.getNodeValue().toUpperCase();
     206                 }
     207             } else if (nodeName.equals("update")) {
     208                 // Get the updates.
     209                 update = getTextFromNode(sqlTableSearch);
     210                 if (update.length() > 0) {
     211                     update = " " + update + " ";
     212                 }
     213                 Node qtypeAttr =
     214                   sqlTableSearch.getAttributes().getNamedItem("qtype");
     215                 if (qtypeAttr != null) {
     216                     updateQ_type = qtypeAttr.getNodeValue().toUpperCase();
     217                 }
     218             } else if (nodeName.equals("dummy")) {
     219                 // Get the dummies.
     220                 dummy = getTextFromNode(sqlTableSearch);
     221                 if (dummy.length() > 0) {
     222                     dummy = " " + dummy + " ";
     223                 }
     224                 Node qtypeAttr =
     225                   sqlTableSearch.getAttributes().getNamedItem("qtype");
     226                 if (qtypeAttr != null) {
     227                     dummyQ_type = qtypeAttr.getNodeValue().toUpperCase();
     228                 }
     229             } else if (nodeName.equals("select")) {
     230                 // Get the selects.
     231                 select = getTextFromNode(sqlTableSearch);
     232                 if (select.length() > 0) {
     233                     select = " " + select + " ";
     234                 }
     235                 Node qtypeAttr =
     236                   sqlTableSearch.getAttributes().getNamedItem("qtype");
     237                 if (qtypeAttr != null) {
     238                     selectQ_type = qtypeAttr.getNodeValue().toUpperCase();
     239                 }
     240             }
     241             sqlTableSearch = sqlTableSearch.getNextSibling();
     242         }
     243     } // rp!=NULL
     244
     245     if (kn != null) {
     246       String searchable =
     247         kn.getAttributes().getNamedItem("searchable").getNodeValue();
     248       inverse = searchable.equals("inverse");
     249       lookup = searchable.equals("lookup");
     250
     251       Node knSearch = kn.getFirstChild();
     252       while (knSearch != null) {
     253           if (knSearch.getNodeName().equals("foreign")) {
     254               Node value = knSearch.getAttributes().getNamedItem("value");
     255               foreign = value.getNodeValue();
     256           } else if (knSearch.getNodeName().equals("primary")) {
     257               primary = true;
     258           } else if (knSearch.getNodeName().equals("queries")) {
     259               // Get the queries.
     260               appendQueries(queries, knSearch, "sqlquery", code);
     261               appendQueries(queries, knSearch, "radixquery", code);
     262           }
     263           knSearch = knSearch.getNextSibling();
     264       }
     265     }
     266
     267     choice = "";
     268     number = "";
     269     pseudo = false;
     270
     271     // Now check cominations.
     272     // XXX TODO
     273
     274   } // AttributeDef()
     275
     276   private void appendQueries(Vector queries, Node qsn, String qrytype, String attrcode) {
         /* [<][>][^][v][top][bottom][index][help] */
     277     // if we have no node, exit
     278     if (qsn == null) {
     279         return;
     280     }
     281
     282     // setup based on our query type (or exit if unknown)
     283     String qryt;
     284     if (qrytype.equals("sqlquery")) {
     285         qryt = "SQL";
     286     } else {
     287         qryt = "RADIX";
     288     }
     289
     290     Node q = qsn.getFirstChild();
     291     while (q != null) {
     292
     293         if (q.getNodeName().equals(qrytype)) {
     294             String keytype =
     295               q.getAttributes().getNamedItem("keytype").getNodeValue();
     296
     297             // Blindly get the optional values.
     298             String clars = new String();
     299             try {
     300               clars = q.getAttributes().getNamedItem("class").getNodeValue();
     301             }
     302             catch (NullPointerException e) {
     303               // XXX take the default
     304               clars = attrcode;
     305             }
     306
     307             String space = new String();
     308             try {
     309                 space = q.getAttributes().getNamedItem("space").getNodeValue();
     310             }
     311             catch (NullPointerException e) {
     312             }
     313
     314
     315             String sqlQuery = getTextFromNode(q);
     316             //System.err.println("sqlquery = " + sqlQuery);
     317
     318             Query query = new Query(qryt, lookup, keytype, code,
     319                                     clars, sqlQuery);
     320             queries.addElement(query);
     321         }
     322
     323         q = q.getNextSibling();
     324     }
     325   } // getQueries()
     326
     327
     328
     329     // getting parsed contents of the text node is not simple.
     330     // see http://www.developerlife.com/xmljavatutorial1/default.htm
     331
     332     // it was made simpler by the getNodeValue(Node n) method
     333     // defined below, but it operated on raw XML text fragments
     334
     335 /* deleted, expunged, and otherwise removed - Shane
     336 private String getTextFromNode( Node q ) {
     337     Element query_elem = (Element) q;
     338     NodeList list = query_elem.getChildNodes();
     339     int size = list.getLength();
     340
     341     for (int i = 0 ; i < size ; i ++ ){
     342         // skip non-text nodes (e.g. comments)
     343         if (list.item(i).getNodeType() != Node.TEXT_NODE) {
     344             continue;
     345         }
     346         String value =
     347             ((Node)list.item( i )).getNodeValue().trim();
     348         //System.err.println("i=" + i + " val=" + value );
     349
     350         if( value.equals("") || value.equals("\r") ){
     351             continue; //keep iterating
     352         }
     353         else{
     354             return value;
     355         }
     356     }
     357
     358     return "";
     359   }
     360 */
     361
     362   // shorter, friendlier version (Shane)
     363   private String getTextFromNode(Node node)
         /* [<][>][^][v][top][bottom][index][help] */
     364   {
     365       // concatenate all text nodes together
     366       StringBuffer sb = new StringBuffer();
     367       Node kid = node.getFirstChild();
     368       while (kid != null) {
     369           if (kid.getNodeType() == Node.TEXT_NODE) {
     370               sb.append(kid.getNodeValue());
     371           }
     372           kid = kid.getNextSibling();
     373       }
     374       // convert newlines to spaces
     375       for (int i=0; i<sb.length(); i++) {
     376           char c = sb.charAt(i);
     377           if ((c == '\r') || (c == '\n')) {
     378               sb.setCharAt(i, ' ');
     379           }
     380       }
     381       return sb.toString().trim();
     382   }
     383
     384   /**
     385    * Aaaargh I shouldn't have to write this. :-(
     386    *
     387    * @param        node
     388    * @return       The value of the node.
     389    * @see          ClassDef
     390    *
     391    */
     392   private String getNodeRawValue(Node node) {
         /* [<][>][^][v][top][bottom][index][help] */
     393       StringBuffer sb = new StringBuffer();
     394       Node kid = node.getFirstChild();
     395       while (kid != null) {
     396           if (kid.getNodeType() == Node.TEXT_NODE) {
     397               sb.append(kid.getNodeValue());
     398           }
     399           kid = kid.getNextSibling();
     400       }
     401       return sb.toString();
     402   } // getNodeRawValue()
     403
     404   public String getFamily() {
         /* [<][>][^][v][top][bottom][index][help] */
     405     return family;
     406   }
     407
     408   public String getV4Load() {
         /* [<][>][^][v][top][bottom][index][help] */
     409     return load_ipv4;
     410   }
     411
     412   public String getV6Load() {
         /* [<][>][^][v][top][bottom][index][help] */
     413     return load_ipv6;
     414   }
     415
     416   public String getCode() {
         /* [<][>][^][v][top][bottom][index][help] */
     417     return code;
     418   } // getCode()
     419
     420   public String getName() {
         /* [<][>][^][v][top][bottom][index][help] */
     421     return name;
     422   } // getName()
     423
     424   public String getAltName() {
         /* [<][>][^][v][top][bottom][index][help] */
     425     return altName;
     426   } // getAltName()
     427
     428   public String getXmlName() {
         /* [<][>][^][v][top][bottom][index][help] */
     429     return xmlName;
     430   } // getAltName()
     431
     432   public String getStatus() {
         /* [<][>][^][v][top][bottom][index][help] */
     433     return status;
     434   } // getStatus()
     435
     436   public String getDescription() {
         /* [<][>][^][v][top][bottom][index][help] */
     437     return description;
     438   } // getDescription()
     439
     440   public String getSyntax() {
         /* [<][>][^][v][top][bottom][index][help] */
     441     return syntax;
     442   } // getSyntax()
     443
     444   public String getEnum() {
         /* [<][>][^][v][top][bottom][index][help] */
     445     return new String("A_" + code).toUpperCase();
     446   } // getEnum()
     447
     448   public String getChoice() {
         /* [<][>][^][v][top][bottom][index][help] */
     449     return choice;
     450   } // getChoice()
     451
     452   public String getNumber() {
         /* [<][>][^][v][top][bottom][index][help] */
     453     return number;
     454   } // getNumber()
     455
     456   public boolean getPseudo()
         /* [<][>][^][v][top][bottom][index][help] */
     457   {
     458       return pseudo;
     459   }
     460
     461   public String getKeytype() {
         /* [<][>][^][v][top][bottom][index][help] */
     462     return keytype;
     463   } // getKeytype()
     464
     465   public String getInsert() {
         /* [<][>][^][v][top][bottom][index][help] */
     466     return insert;
     467   } // getInsert()
     468
     469   public String getInsertQ_type() {
         /* [<][>][^][v][top][bottom][index][help] */
     470     return insertQ_type;
     471   } // getInsertQ_type()
     472
     473   public String getUpdate() {
         /* [<][>][^][v][top][bottom][index][help] */
     474     return update;
     475   } // getUpdate()
     476
     477   public String getUpdateQ_type() {
         /* [<][>][^][v][top][bottom][index][help] */
     478     return updateQ_type;
     479   } // getUpdateQ_type()
     480
     481   public String getDummy() {
         /* [<][>][^][v][top][bottom][index][help] */
     482     return dummy;
     483   } // getDummy()
     484
     485   public String getDummyQ_type() {
         /* [<][>][^][v][top][bottom][index][help] */
     486     return dummyQ_type;
     487   } // getDummyQ_type()
     488
     489   public String getSelect() {
         /* [<][>][^][v][top][bottom][index][help] */
     490     return select;
     491   } // getSelect()
     492
     493   public String getSelectQ_type() {
         /* [<][>][^][v][top][bottom][index][help] */
     494     return selectQ_type;
     495   } // getSelectQ_type()
     496
     497   public String getKeytype2() {
         /* [<][>][^][v][top][bottom][index][help] */
     498     String result = new String();
     499
     500     if      (!lookup && !inverse && !primary) {
     501       result = " ";
     502     }
     503     else if (!lookup && !inverse &&  primary) {
     504       result = "primary key";
     505     }
     506     else if (!lookup &&  inverse && !primary) {
     507       result = "inverse key";
     508     }
     509     else if (!lookup &&  inverse &&  primary) {
     510       result = "primary/inverse key";
     511     }
     512     else if ( lookup && !inverse && !primary) {
     513       result = "lookup key";
     514     }
     515     else if ( lookup && !inverse &&  primary) {
     516       result = "primary/look-up key";
     517     }
     518     else if ( lookup &&  inverse && !primary) {
     519       result = "look-up/inverse key";
     520     }
     521     else if ( lookup &&  inverse &&  primary) {
     522       result = "Gimmie a break!";
     523     }
     524
     525     return result;
     526   } // getKeytype()
     527
     528   public String getKeytype3() {
         /* [<][>][^][v][top][bottom][index][help] */
     529     String result = new String();
     530
     531     if (primary) {
     532       result = "[P]";
     533     }
     534     else  {
     535       result = "   ";
     536     }
     537
     538     if (inverse) {
     539       result += "[I]";
     540     }
     541     else if (lookup) {
     542       result += "[L]";
     543     }
     544     else {
     545       result += "   ";
     546     }
     547
     548     return result;
     549   } // getKeytype()
     550
     551   public String getForeign() {
         /* [<][>][^][v][top][bottom][index][help] */
     552     return foreign;
     553   } // getForeign()
     554
     555   public boolean getLookup() {
         /* [<][>][^][v][top][bottom][index][help] */
     556       return lookup;
     557   }
     558
     559   public boolean getInverse() {
         /* [<][>][^][v][top][bottom][index][help] */
     560     return inverse;
     561   } // getInverse()
     562
     563   public boolean getPrimary() {
         /* [<][>][^][v][top][bottom][index][help] */
     564     return primary;
     565   } // getPrimary()
     566
     567   public boolean getList() {
         /* [<][>][^][v][top][bottom][index][help] */
     568       return list;
     569   }
     570
     571   public boolean getRipeList() {
         /* [<][>][^][v][top][bottom][index][help] */
     572       return ripeList;
     573   }
     574
     575   public Vector getQueries() {
         /* [<][>][^][v][top][bottom][index][help] */
     576     return queries;
     577   } // getQueries()
     578
     579   public boolean setChoice(String choice) {
         /* [<][>][^][v][top][bottom][index][help] */
     580     boolean result=true;
     581
     582     this.choice = choice;
     583
     584     return result;
     585   } // setChoice()
     586
     587   public boolean setNumber(String number) {
         /* [<][>][^][v][top][bottom][index][help] */
     588     boolean result=true;
     589
     590     this.number = number;
     591
     592     return result;
     593   } // setNumber()
     594
     595   public void setPseudo(boolean pseudo)
         /* [<][>][^][v][top][bottom][index][help] */
     596   {
     597       this.pseudo = pseudo;
     598   }
     599
     600   public Object clone() throws CloneNotSupportedException {
         /* [<][>][^][v][top][bottom][index][help] */
     601     return (AttributeDef)super.clone();
     602   } // clone()
     603
     604   /*
     605   public boolean equals(String code) {
     606     return code.equals(code);
     607   } // equals()
     608   */
     609
     610   public String toString() {
         /* [<][>][^][v][top][bottom][index][help] */
     611     return new String("ripe attribute={" +
     612                          "\n\tname="        + name        +
     613                          "\n\taltName="     + altName     +
     614                          "\n\txmlName="     + xmlName     +
     615                          "\n\tcode="        + code        +
     616                          "\n\tstatus="      + status      +
     617                          "\n\tkeytype="     + keytype     +
     618                          "\n\tdescription=" + description +
     619                          "\n\tsyntax="      + syntax      +
     620                          "\n\tchoice="      + choice      +
     621                          "\n\tnumber="      + number      +
     622                          "\n}");
     623   } // toString()
     624
     625
     626 } // AttributeDef

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