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. getUpdate
  15. getKeytype2
  16. getKeytype3
  17. getForeign
  18. getInverse
  19. getPrimary
  20. getQueries
  21. setChoice
  22. setNumber
  23. clone
  24. 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 update;

  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;
    update = new String();
    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 updates.
      TreeWalker uw = new TreeWalker(kn);
      Node un = uw.getNextElement("update");
      if (un != null) {
        update = getNodeValue(un);
      }

      // Get the queries.
      TreeWalker qw = new TreeWalker(kn);
      Node qsn = qw.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('<') - 1;
    
    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 getUpdate() {
/* [<][>][^][v][top][bottom][index][help] */
    return update;
  } // getUpdate()

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

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

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