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

import java.util.*;
import org.w3c.dom.*;
import com.sun.xml.tree.*;

/**
 * RIPE attribute.
 *
 * @author ottrey@ripe.net
 * @version $Version$
 *
 */
public class AttributeDef implements Cloneable {
/* [<][>][^][v][top][bottom][index][help] */
  
  final static int QI_SQL   = 1;
  final static int QI_RADIX = 2;

  private String name;
  private String altName;
  private String code;
  private String status;

  private String description;
  private String format;

  private boolean lookup;
  private boolean inverse;
  private boolean primary;
  private String foreign;
  private String keytype;

  private String insert;
  private String insertQ_type;
  private String update;
  private String updateQ_type;
  private String dummy;
  private String dummyQ_type;
  private String select;
  private String selectQ_type;

  private String choice;
  private String number;

  private Vector queries;

  // -----------------oOo-----------------
  //              Constructors
  // -----------------oOo-----------------
  /**
   * Creates a RIPE attribute.
   *               
   * @author ottrey@ripe.net
   * @version $Version$
   *               
   * @param obj The node from which a RIPE attribute is made.
   * 
   */
  public AttributeDef(Node obj) {
    name      = obj.getAttributes().getNamedItem("name").getNodeValue();
    code      = obj.getAttributes().getNamedItem("code").getNodeValue();
    status    = obj.getAttributes().getNamedItem("status").getNodeValue();

    // Blindly ask for the optional items.
    try {
      altName   = obj.getAttributes().getNamedItem("altName").getNodeValue();
    }
    catch (NullPointerException e) {
      altName = new String();
      // Throw the exception away.  :-)
    }

    // Prepare to walk the tree.
    TreeWalker tw = new TreeWalker(obj);

    // Get the "description" node.
    description = getNodeRawValue(tw.getNextElement("description"));

    // Get the "format" node.
    format = getNodeRawValue(tw.getNextElement("format"));

    // Initialize
    foreign = new String();
    lookup = false;
    inverse = false;
    primary = false;

    insert       = new String();
    insertQ_type = new String("UD_NULL_");
    update       = new String();
    updateQ_type = new String("UD_NULL_");
    dummy        = new String();
    dummyQ_type  = new String("UD_NULL_");
    select       = new String();
    selectQ_type = new String("UD_NULL_");

    queries = new Vector();

    Node rp = tw.getNextElement("representation");
    if (rp != null) {
      // Get the insert.
      Node in = (new TreeWalker(rp)).getNextElement("insert");
      if (in != null) {
        insert = getTextFromNode(in);
        if( insert.length() > 0 ) {
            insert = " " + insert + " ";
        }
        try {
          insertQ_type = in.getAttributes().getNamedItem("qtype").getNodeValue().toUpperCase();
        }
        catch (NullPointerException e) {
        }
      }

      // Get the updates.
      Node un = (new TreeWalker(rp)).getNextElement("update");
      if (un != null) {
        update = getTextFromNode(un);
        if( update.length() > 0 ) {
              update = " " + update + " ";
        }
        try {
          updateQ_type = un.getAttributes().getNamedItem("qtype").getNodeValue().toUpperCase();
        }
        catch (NullPointerException e) {
        }
      }

      // Get the dummies.
      Node dn = (new TreeWalker(rp)).getNextElement("dummy");
      if (dn != null) {
        dummy =  getTextFromNode(dn);
        if( dummy.length() > 0 ) {
              dummy = " " + dummy + " ";
        }
        try {
          dummyQ_type = dn.getAttributes().getNamedItem("qtype").getNodeValue().toUpperCase();
        }
        catch (NullPointerException e) {
        }
      }

      // Get the selects.
      Node sn = (new TreeWalker(rp)).getNextElement("select");
      if (sn != null) {
        select = getTextFromNode(sn);
        if( select.length() > 0 ) {
              select = " " + select + " ";
        }
        try {
          selectQ_type = sn.getAttributes().getNamedItem("qtype").getNodeValue().toUpperCase();
        }
        catch (NullPointerException e) {
        }
      }
    } // rp!=NULL

    Node kn = tw.getNextElement("keys");
    if (kn != null) {
      String searchable = kn.getAttributes().getNamedItem("searchable").getNodeValue();
      inverse = searchable.equals("inverse");
      lookup = searchable.equals("lookup");

      TreeWalker fw = new TreeWalker(kn);
      Node f = fw.getNextElement("foreign");
      if (f != null) {
        foreign = f.getAttributes().getNamedItem("value").getNodeValue();
      }

      TreeWalker pw = new TreeWalker(kn);
      Node p = pw.getNextElement("primary");
      if (p != null) {
        primary = true;
      }

      // Get the queries.
      Node qsn = (new TreeWalker(kn)).getNextElement("queries");

      appendQueries(queries, qsn, "sqlquery",  code);
      appendQueries(queries, qsn, "radixquery",code);
    }

    // Now check cominations.
    // XXX TODO

  } // AttributeDef()

  private void appendQueries(Vector queries, Node qsn, String qrytype, String attrcode) {
/* [<][>][^][v][top][bottom][index][help] */
    if (qsn != null) {
      TreeWalker qsw;
      Node q;
      String qryt;

      qsw = new TreeWalker(qsn);
      while ((q = qsw.getNextElement(qrytype)) != null) {
        String keytype = q.getAttributes().getNamedItem("keytype").getNodeValue();

        // Blindly get the optional values.
        String clars = new String();
        try {
          clars = q.getAttributes().getNamedItem("class").getNodeValue();
        }
        catch (NullPointerException e) {
            // XXX take the default
          clars = attrcode;
        }

        String space = new String();
        try {
            space = q.getAttributes().getNamedItem("space").getNodeValue();
        }
        catch (NullPointerException e) {
        }


        String sqlQuery = getTextFromNode(q);
        //System.err.println("sqlquery = " + sqlQuery);

        if ( qrytype.equals("sqlquery") ) {
            qryt = "SQL";
        } else { 
            qryt = "RADIX";
        }

        Query query = new Query(qryt, lookup, keytype, code, clars, sqlQuery);
        queries.addElement(query);
      }
    }
  } // getQueries()


    
    // getting parsed contents of the text node is not simple.
    // see http://www.developerlife.com/xmljavatutorial1/default.htm
    
    // it was made simpler by the getNodeValue(Node n) method 
    // defined below, but it operated on raw XML text fragments
    
private String getTextFromNode( Node q ) {
/* [<][>][^][v][top][bottom][index][help] */
    Element query_elem = (Element) q;
    NodeList list = query_elem.getChildNodes();
    int size = list.getLength();
    
    for (int i = 0 ; i < size ; i ++ ){
        String value =
            ((Node)list.item( i )).getNodeValue().trim();
        //System.err.println("i=" + i + " val=" + value );
        
        if( value.equals("") || value.equals("\r") ){
            continue; //keep iterating
        }
        else{
            return value;
        }
    }
    
    return "";
  }
  /**
   * Aaaargh I shouldn't have to write this. :-(
   *
   * @param        node
   * @return       The value of the node.
   * @see          ClassDef
   *
   */
  private String getNodeRawValue(Node node) {
/* [<][>][^][v][top][bottom][index][help] */
    String nodeStr = node.toString();
    int startIndex = nodeStr.indexOf('>') + 1;
    int endIndex = nodeStr.lastIndexOf('<') - 1;
    
    return nodeStr.substring(startIndex, endIndex);
  } // getNodeRaw Value()
  

  
  public String getCode() {
/* [<][>][^][v][top][bottom][index][help] */
    return code;
  } // getCode()

  public String getName() {
/* [<][>][^][v][top][bottom][index][help] */
    return name;
  } // getName()

  public String getAltName() {
/* [<][>][^][v][top][bottom][index][help] */
    return altName;
  } // getAltName()

  public String getStatus() {
/* [<][>][^][v][top][bottom][index][help] */
    return status;
  } // getStatus()

  public String getDescription() {
/* [<][>][^][v][top][bottom][index][help] */
    return description;
  } // getDescription()

  public String getFormat() {
/* [<][>][^][v][top][bottom][index][help] */
    return format;
  } // getFormat()

  public String getEnum() {
/* [<][>][^][v][top][bottom][index][help] */
    return new String("A_" + code).toUpperCase();
  } // getEnum()

  public String getChoice() {
/* [<][>][^][v][top][bottom][index][help] */
    return choice;
  } // getChoice()

  public String getNumber() {
/* [<][>][^][v][top][bottom][index][help] */
    return number;
  } // getNumber()

  public String getKeytype() {
/* [<][>][^][v][top][bottom][index][help] */
    return keytype;
  } // getKeytype()

  public String getInsert() {
/* [<][>][^][v][top][bottom][index][help] */
    return insert;
  } // getInsert()

  public String getInsertQ_type() {
/* [<][>][^][v][top][bottom][index][help] */
    return insertQ_type;
  } // getInsertQ_type()

  public String getUpdate() {
/* [<][>][^][v][top][bottom][index][help] */
    return update;
  } // getUpdate()

  public String getUpdateQ_type() {
/* [<][>][^][v][top][bottom][index][help] */
    return updateQ_type;
  } // getUpdateQ_type()

  public String getDummy() {
/* [<][>][^][v][top][bottom][index][help] */
    return dummy;
  } // getDummy()

  public String getDummyQ_type() {
/* [<][>][^][v][top][bottom][index][help] */
    return dummyQ_type;
  } // getDummyQ_type()

  public String getSelect() {
/* [<][>][^][v][top][bottom][index][help] */
    return select;
  } // getSelect()

  public String getSelectQ_type() {
/* [<][>][^][v][top][bottom][index][help] */
    return selectQ_type;
  } // getSelectQ_type()

  public String getKeytype2() {
/* [<][>][^][v][top][bottom][index][help] */
    String result = new String();

    if      (!lookup && !inverse && !primary) {
      result = " ";
    }
    else if (!lookup && !inverse &&  primary) {
      result = "primary key";
    }
    else if (!lookup &&  inverse && !primary) {
      result = "inverse key";
    }
    else if (!lookup &&  inverse &&  primary) {
      result = "primary/inverse key";
    }
    else if ( lookup && !inverse && !primary) {
      result = "lookup key";
    }
    else if ( lookup && !inverse &&  primary) {
      result = "primary/look-up key";
    }
    else if ( lookup &&  inverse && !primary) {
      result = "look-up/inverse key";
    }
    else if ( lookup &&  inverse &&  primary) {
      result = "Gimmie a break!";
    }

    return result;
  } // getKeytype()

  public String getKeytype3() {
/* [<][>][^][v][top][bottom][index][help] */
    String result = new String();
    
    if (primary) {
      result = "[P]";
    }
    else  {
      result = "   ";
    }

    if (inverse) {
      result += "[I]";
    }
    else if (lookup) {
      result += "[L]";
    }
    else {
      result += "   ";
    }

    return result;
  } // getKeytype()

  public String getForeign() {
/* [<][>][^][v][top][bottom][index][help] */
    return foreign;
  } // getForeign()

  public boolean getInverse() {
/* [<][>][^][v][top][bottom][index][help] */
    return inverse;
  } // getInverse()

  public boolean getPrimary() {
/* [<][>][^][v][top][bottom][index][help] */
    return primary;
  } // getPrimary()

  public Vector getQueries() {
/* [<][>][^][v][top][bottom][index][help] */
    return queries;
  } // getQueries()

  public boolean setChoice(String choice) {
/* [<][>][^][v][top][bottom][index][help] */
    boolean result=true;

    this.choice = choice;

    return result;
  } // setChoice()

  public boolean setNumber(String number) {
/* [<][>][^][v][top][bottom][index][help] */
    boolean result=true;

    this.number = number;

    return result;
  } // setNumber()

  public Object clone() throws CloneNotSupportedException {
/* [<][>][^][v][top][bottom][index][help] */
    return (AttributeDef)super.clone();
  } // clone()

  /*
  public boolean equals(String code) {
    return code.equals(code);
  } // equals()
  */
  
  public String toString() {
/* [<][>][^][v][top][bottom][index][help] */
    return new String("ripe attribute={" +
                         "\n\tname="        + name        +
                         "\n\taltName="     + altName     +
                         "\n\tcode="        + code        +
                         "\n\tstatus="      + status      +
                         "\n\tkeytype="     + keytype     +
                         "\n\tdescription=" + description +
                         "\n\tformat="      + format      +
                         "\n\tchoice="      + choice      +
                         "\n\tnumber="      + number      +
                         "\n}");
  } // toString()


} // AttributeDef

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