home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2007 April / PCpro_2007_04.ISO / files / dsl / jNetTool.exe / org / xbill / DNS / SetResponse.class (.txt) < prev    next >
Encoding:
Java Class File  |  2005-06-05  |  3.3 KB  |  141 lines

  1. package org.xbill.DNS;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class SetResponse {
  7.    static final int UNKNOWN = 0;
  8.    static final int NXDOMAIN = 1;
  9.    static final int NXRRSET = 2;
  10.    static final int DELEGATION = 3;
  11.    static final int CNAME = 4;
  12.    static final int DNAME = 5;
  13.    static final int SUCCESSFUL = 6;
  14.    private static final SetResponse unknown = new SetResponse(0);
  15.    private static final SetResponse nxdomain = new SetResponse(1);
  16.    private static final SetResponse nxrrset = new SetResponse(2);
  17.    private int type;
  18.    private Object data;
  19.  
  20.    private SetResponse() {
  21.    }
  22.  
  23.    SetResponse(int type, RRset rrset) {
  24.       if (type >= 0 && type <= 6) {
  25.          this.type = type;
  26.          this.data = rrset;
  27.       } else {
  28.          throw new IllegalArgumentException("invalid type");
  29.       }
  30.    }
  31.  
  32.    SetResponse(int type) {
  33.       if (type >= 0 && type <= 6) {
  34.          this.type = type;
  35.          this.data = null;
  36.       } else {
  37.          throw new IllegalArgumentException("invalid type");
  38.       }
  39.    }
  40.  
  41.    static SetResponse ofType(int type) {
  42.       switch (type) {
  43.          case 0:
  44.             return unknown;
  45.          case 1:
  46.             return nxdomain;
  47.          case 2:
  48.             return nxrrset;
  49.          case 3:
  50.          case 4:
  51.          case 5:
  52.          case 6:
  53.             SetResponse sr = new SetResponse();
  54.             sr.type = type;
  55.             sr.data = null;
  56.             return sr;
  57.          default:
  58.             throw new IllegalArgumentException("invalid type");
  59.       }
  60.    }
  61.  
  62.    void addRRset(RRset rrset) {
  63.       if (this.data == null) {
  64.          this.data = new ArrayList();
  65.       }
  66.  
  67.       List l = (List)this.data;
  68.       l.add(rrset);
  69.    }
  70.  
  71.    public boolean isUnknown() {
  72.       return this.type == 0;
  73.    }
  74.  
  75.    public boolean isNXDOMAIN() {
  76.       return this.type == 1;
  77.    }
  78.  
  79.    public boolean isNXRRSET() {
  80.       return this.type == 2;
  81.    }
  82.  
  83.    public boolean isDelegation() {
  84.       return this.type == 3;
  85.    }
  86.  
  87.    public boolean isCNAME() {
  88.       return this.type == 4;
  89.    }
  90.  
  91.    public boolean isDNAME() {
  92.       return this.type == 5;
  93.    }
  94.  
  95.    public boolean isSuccessful() {
  96.       return this.type == 6;
  97.    }
  98.  
  99.    public RRset[] answers() {
  100.       if (this.type != 6) {
  101.          return null;
  102.       } else {
  103.          List l = (List)this.data;
  104.          return (RRset[])l.toArray(new RRset[l.size()]);
  105.       }
  106.    }
  107.  
  108.    public CNAMERecord getCNAME() {
  109.       return (CNAMERecord)((RRset)this.data).first();
  110.    }
  111.  
  112.    public DNAMERecord getDNAME() {
  113.       return (DNAMERecord)((RRset)this.data).first();
  114.    }
  115.  
  116.    public RRset getNS() {
  117.       return (RRset)this.data;
  118.    }
  119.  
  120.    public String toString() {
  121.       switch (this.type) {
  122.          case 0:
  123.             return "unknown";
  124.          case 1:
  125.             return "NXDOMAIN";
  126.          case 2:
  127.             return "NXRRSET";
  128.          case 3:
  129.             return "delegation: " + this.data;
  130.          case 4:
  131.             return "CNAME: " + this.data;
  132.          case 5:
  133.             return "DNAME: " + this.data;
  134.          case 6:
  135.             return "successful";
  136.          default:
  137.             return null;
  138.       }
  139.    }
  140. }
  141.