defs/AttributeDef.java

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

FUNCTIONS

This source file includes following functions.
  1. AttributeDef
  2. appendQueries
  3. getNodeValue
  4. getCode
  5. getName
  6. getAltName
  7. getStatus
  8. getDescription
  9. getFormat
  10. getEnum
  11. getChoice
  12. getNumber
  13. getKeytype
  14. getInsert
  15. getInsertQ_type
  16. getUpdate
  17. getUpdateQ_type
  18. getDummy
  19. getDummyQ_type
  20. getSelect
  21. getSelectQ_type
  22. getKeytype2
  23. getKeytype3
  24. getForeign
  25. getInverse
  26. getPrimary
  27. getQueries
  28. setChoice
  29. setNumber
  30. clone
  31. 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.
   * 
   * NOTE: I don't know why Node.getNodeValue() isn't working.
   *       description = tw.getNextElement("description").getNodeValue();
   * @see getNodeValue
   */
  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 = getNodeValue(tw.getNextElement("description"));

    // Get the "format" node.
    format = getNodeValue(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 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 insert.
      Node in = (new TreeWalker(kn)).getNextElement("insert");
      if (in != null) {
        insert = getNodeValue(in);
        try {
          insertQ_type = in.getAttributes().getNamedItem("qtype").getNodeValue().toUpperCase();
        }
        catch (NullPointerException e) {
        }
      }

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

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

      // Get the selects.
      Node sn = (new TreeWalker(kn)).getNextElement("select");
      if (sn != null) {
        select = getNodeValue(sn);
        try {
          selectQ_type = sn.getAttributes().getNamedItem("qtype").getNodeValue().toUpperCase();
        }
        catch (NullPointerException e) {
        }
      }

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

      appendQueries(queries, qsn, "sql");
      appendQueries(queries, qsn, "radix");
    }

    // Now check cominations.
    // XXX TODO

  } // AttributeDef()

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

      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) {
        }
        String space = new String();
        try {
          space = q.getAttributes().getNamedItem("space").getNodeValue();
        }
        catch (NullPointerException e) {
        }
        String family = new String();
        try {
          family = q.getAttributes().getNamedItem("family").getNodeValue();
        }
        catch (NullPointerException e) {
        }

        String sqlQuery = getNodeValue(q);

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

  /**
   * Aaaargh I shouldn't have to write this. :-(
   *
   * @param        node
   * @return       The value of the node.
   * @see          AttributeDef
   *
   */
  private String getNodeValue(Node node) {
/* [<][>][^][v][top][bottom][index][help] */
    String result = new String();

    String nodeStr = node.toString();
    int startIndex = nodeStr.indexOf('>') + 1;
    int endIndex = nodeStr.lastIndexOf('<');
    
    if ((startIndex < endIndex) && (startIndex > 0)) {
      result = nodeStr.substring(startIndex, endIndex);
    }

    return result;
  } // getNodeValue()

  
  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] */