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

  1. package org.xbill.DNS;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.ByteArrayInputStream;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileNotFoundException;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.PushbackInputStream;
  11. import org.xbill.DNS.utils.base16;
  12. import org.xbill.DNS.utils.base64;
  13.  
  14. public class Tokenizer {
  15.    private static String delim = " \t\n;()\"";
  16.    private static String quotes = "\"";
  17.    public static final int EOF = 0;
  18.    public static final int EOL = 1;
  19.    public static final int WHITESPACE = 2;
  20.    public static final int IDENTIFIER = 3;
  21.    public static final int QUOTED_STRING = 4;
  22.    public static final int COMMENT = 5;
  23.    // $FF: renamed from: is java.io.PushbackInputStream
  24.    private PushbackInputStream field_0;
  25.    private boolean ungottenToken;
  26.    private int multiline;
  27.    private boolean quoting;
  28.    private String delimiters;
  29.    private Token current;
  30.    // $FF: renamed from: sb java.lang.StringBuffer
  31.    private StringBuffer field_1;
  32.    private boolean wantClose;
  33.    private String filename;
  34.    private int line;
  35.  
  36.    public Tokenizer(InputStream is) {
  37.       if (!(is instanceof BufferedInputStream)) {
  38.          is = new BufferedInputStream(is);
  39.       }
  40.  
  41.       this.field_0 = new PushbackInputStream(is, 2);
  42.       this.ungottenToken = false;
  43.       this.multiline = 0;
  44.       this.quoting = false;
  45.       this.delimiters = delim;
  46.       this.current = new Token((Token)null);
  47.       this.field_1 = new StringBuffer();
  48.       this.filename = "<none>";
  49.       this.line = 1;
  50.    }
  51.  
  52.    public Tokenizer(String s) {
  53.       this((InputStream)(new ByteArrayInputStream(s.getBytes())));
  54.    }
  55.  
  56.    public Tokenizer(File f) throws FileNotFoundException {
  57.       this((InputStream)(new FileInputStream(f)));
  58.       this.wantClose = true;
  59.       this.filename = f.getName();
  60.    }
  61.  
  62.    private int getChar() throws IOException {
  63.       int c = this.field_0.read();
  64.       if (c == 13) {
  65.          int next = this.field_0.read();
  66.          if (next != 10) {
  67.             this.field_0.unread(next);
  68.          }
  69.  
  70.          c = 10;
  71.       }
  72.  
  73.       if (c == 10) {
  74.          ++this.line;
  75.       }
  76.  
  77.       return c;
  78.    }
  79.  
  80.    private void ungetChar(int c) throws IOException {
  81.       if (c != -1) {
  82.          this.field_0.unread(c);
  83.          if (c == 10) {
  84.             --this.line;
  85.          }
  86.  
  87.       }
  88.    }
  89.  
  90.    private int skipWhitespace() throws IOException {
  91.       int skipped = 0;
  92.  
  93.       while(true) {
  94.          int c = this.getChar();
  95.          if (c != 32 && c != 9 && (c != 10 || this.multiline <= 0)) {
  96.             this.ungetChar(c);
  97.             return skipped;
  98.          }
  99.  
  100.          ++skipped;
  101.       }
  102.    }
  103.  
  104.    private void checkUnbalancedParens() throws TextParseException {
  105.       if (this.multiline > 0) {
  106.          throw this.exception("unbalanced parentheses");
  107.       }
  108.    }
  109.  
  110.    public Token get(boolean wantWhitespace, boolean wantComment) throws IOException {
  111.       if (this.ungottenToken) {
  112.          this.ungottenToken = false;
  113.          if (this.current.type == 2) {
  114.             if (wantWhitespace) {
  115.                return this.current;
  116.             }
  117.          } else {
  118.             if (this.current.type != 5) {
  119.                return this.current;
  120.             }
  121.  
  122.             if (wantComment) {
  123.                return this.current;
  124.             }
  125.          }
  126.       }
  127.  
  128.       int skipped = this.skipWhitespace();
  129.       if (skipped > 0 && wantWhitespace) {
  130.          return org.xbill.DNS.Tokenizer.Token.access$1(this.current, 2, (StringBuffer)null);
  131.       } else {
  132.          int type = 3;
  133.          this.field_1.setLength(0);
  134.  
  135.          while(true) {
  136.             int c = this.getChar();
  137.             if (c == -1 || this.delimiters.indexOf(c) != -1) {
  138.                if (c == -1) {
  139.                   if (this.quoting) {
  140.                      throw this.exception("EOF in quoted string");
  141.                   }
  142.  
  143.                   if (this.field_1.length() == 0) {
  144.                      return org.xbill.DNS.Tokenizer.Token.access$1(this.current, 0, (StringBuffer)null);
  145.                   }
  146.  
  147.                   return org.xbill.DNS.Tokenizer.Token.access$1(this.current, type, this.field_1);
  148.                }
  149.  
  150.                if (this.field_1.length() != 0 || type == 4) {
  151.                   this.ungetChar(c);
  152.                   if (this.field_1.length() == 0 && type != 4) {
  153.                      this.checkUnbalancedParens();
  154.                      return org.xbill.DNS.Tokenizer.Token.access$1(this.current, 0, (StringBuffer)null);
  155.                   } else {
  156.                      return org.xbill.DNS.Tokenizer.Token.access$1(this.current, type, this.field_1);
  157.                   }
  158.                }
  159.  
  160.                if (c == 40) {
  161.                   ++this.multiline;
  162.                   this.skipWhitespace();
  163.                } else if (c == 41) {
  164.                   if (this.multiline <= 0) {
  165.                      throw this.exception("invalid close parenthesis");
  166.                   }
  167.  
  168.                   --this.multiline;
  169.                   this.skipWhitespace();
  170.                } else if (c == 34) {
  171.                   if (!this.quoting) {
  172.                      this.quoting = true;
  173.                      this.delimiters = quotes;
  174.                      type = 4;
  175.                   } else {
  176.                      this.quoting = false;
  177.                      this.delimiters = delim;
  178.                      this.skipWhitespace();
  179.                   }
  180.                } else {
  181.                   if (c == 10) {
  182.                      return org.xbill.DNS.Tokenizer.Token.access$1(this.current, 1, (StringBuffer)null);
  183.                   }
  184.  
  185.                   if (c != 59) {
  186.                      throw new IllegalStateException();
  187.                   }
  188.  
  189.                   while(true) {
  190.                      c = this.getChar();
  191.                      if (c == 10 || c == -1) {
  192.                         if (wantComment) {
  193.                            this.ungetChar(c);
  194.                            return org.xbill.DNS.Tokenizer.Token.access$1(this.current, 5, this.field_1);
  195.                         }
  196.  
  197.                         if (c == -1 && type != 4) {
  198.                            this.checkUnbalancedParens();
  199.                            return org.xbill.DNS.Tokenizer.Token.access$1(this.current, 0, (StringBuffer)null);
  200.                         }
  201.  
  202.                         if (this.multiline <= 0) {
  203.                            return org.xbill.DNS.Tokenizer.Token.access$1(this.current, 1, (StringBuffer)null);
  204.                         }
  205.  
  206.                         this.skipWhitespace();
  207.                         this.field_1.setLength(0);
  208.                         break;
  209.                      }
  210.  
  211.                      this.field_1.append((char)c);
  212.                   }
  213.                }
  214.             } else {
  215.                if (c == 92) {
  216.                   c = this.getChar();
  217.                   if (c == -1) {
  218.                      throw this.exception("unterminated escape sequence");
  219.                   }
  220.  
  221.                   this.field_1.append('\\');
  222.                } else if (this.quoting && c == 10) {
  223.                   throw this.exception("newline in quoted string");
  224.                }
  225.  
  226.                this.field_1.append((char)c);
  227.             }
  228.          }
  229.       }
  230.    }
  231.  
  232.    public Token get() throws IOException {
  233.       return this.get(false, false);
  234.    }
  235.  
  236.    public void unget() {
  237.       if (this.ungottenToken) {
  238.          throw new IllegalStateException("Cannot unget multiple tokens");
  239.       } else {
  240.          this.ungottenToken = true;
  241.       }
  242.    }
  243.  
  244.    public String getString() throws IOException {
  245.       Token next = this.get();
  246.       if (!next.isString()) {
  247.          throw this.exception("expected a string");
  248.       } else {
  249.          return next.value;
  250.       }
  251.    }
  252.  
  253.    public String getIdentifier() throws IOException {
  254.       Token next = this.get();
  255.       if (next.type != 3) {
  256.          throw this.exception("expected an identifier");
  257.       } else {
  258.          return next.value;
  259.       }
  260.    }
  261.  
  262.    public long getLong() throws IOException {
  263.       String next = this.getIdentifier();
  264.       if (!Character.isDigit(next.charAt(0))) {
  265.          throw this.exception("expecting an integer");
  266.       } else {
  267.          try {
  268.             return Long.parseLong(next);
  269.          } catch (NumberFormatException var3) {
  270.             throw this.exception("expecting an integer");
  271.          }
  272.       }
  273.    }
  274.  
  275.    public long getUInt32() throws IOException {
  276.       long l = this.getLong();
  277.       if (l >= 0L && l <= 4294967295L) {
  278.          return l;
  279.       } else {
  280.          throw this.exception("expecting an 32 bit unsigned integer");
  281.       }
  282.    }
  283.  
  284.    public int getUInt16() throws IOException {
  285.       long l = this.getLong();
  286.       if (l >= 0L && l <= 65535L) {
  287.          return (int)l;
  288.       } else {
  289.          throw this.exception("expecting an 16 bit unsigned integer");
  290.       }
  291.    }
  292.  
  293.    public int getUInt8() throws IOException {
  294.       long l = this.getLong();
  295.       if (l >= 0L && l <= 255L) {
  296.          return (int)l;
  297.       } else {
  298.          throw this.exception("expecting an 8 bit unsigned integer");
  299.       }
  300.    }
  301.  
  302.    public double getDouble() throws IOException {
  303.       String next = this.getIdentifier();
  304.       if (!Character.isDigit(next.charAt(0))) {
  305.          throw this.exception("expecting an integer");
  306.       } else {
  307.          try {
  308.             return Double.parseDouble(next);
  309.          } catch (NumberFormatException var3) {
  310.             throw this.exception("expecting an floating point value");
  311.          }
  312.       }
  313.    }
  314.  
  315.    public long getTTL() throws IOException {
  316.       String next = this.getIdentifier();
  317.  
  318.       try {
  319.          return TTL.parseTTL(next);
  320.       } catch (NumberFormatException var3) {
  321.          throw this.exception("expecting an 32 bit unsigned integer");
  322.       }
  323.    }
  324.  
  325.    public Name getName(Name origin) throws IOException {
  326.       try {
  327.          Name name = Name.fromString(this.getIdentifier(), origin);
  328.          if (!name.isAbsolute()) {
  329.             throw new RelativeNameException(name);
  330.          } else {
  331.             return name;
  332.          }
  333.       } catch (TextParseException e) {
  334.          throw this.exception(e.getMessage());
  335.       }
  336.    }
  337.  
  338.    public void getEOL() throws IOException {
  339.       Token next = this.get();
  340.       if (next.type != 1 && next.type != 0) {
  341.          throw this.exception("expecting EOL or EOF");
  342.       }
  343.    }
  344.  
  345.    private String remainingStrings() throws IOException {
  346.       StringBuffer sb = null;
  347.  
  348.       while(true) {
  349.          Token t = this.get();
  350.          if (!t.isString()) {
  351.             this.unget();
  352.             if (sb == null) {
  353.                return null;
  354.             }
  355.  
  356.             return sb.toString();
  357.          }
  358.  
  359.          if (sb == null) {
  360.             sb = new StringBuffer();
  361.          }
  362.  
  363.          sb.append(t.value);
  364.       }
  365.    }
  366.  
  367.    public byte[] getBase64(boolean required) throws IOException {
  368.       String s = this.remainingStrings();
  369.       if (s == null) {
  370.          if (required) {
  371.             throw this.exception("expected base64 encoded string");
  372.          } else {
  373.             return null;
  374.          }
  375.       } else {
  376.          byte[] array = base64.fromString(s);
  377.          if (array == null) {
  378.             throw this.exception("invalid base64 encoding");
  379.          } else {
  380.             return array;
  381.          }
  382.       }
  383.    }
  384.  
  385.    public byte[] getBase64() throws IOException {
  386.       return this.getBase64(false);
  387.    }
  388.  
  389.    public byte[] getHex(boolean required) throws IOException {
  390.       String s = this.remainingStrings();
  391.       if (s == null) {
  392.          if (required) {
  393.             throw this.exception("expected hex encoded string");
  394.          } else {
  395.             return null;
  396.          }
  397.       } else {
  398.          byte[] array = base16.fromString(s);
  399.          if (array == null) {
  400.             throw this.exception("invalid hex encoding");
  401.          } else {
  402.             return array;
  403.          }
  404.       }
  405.    }
  406.  
  407.    public byte[] getHex() throws IOException {
  408.       return this.getHex(false);
  409.    }
  410.  
  411.    public TextParseException exception(String s) {
  412.       return new TextParseException(this.filename + ":" + this.line + ": " + s);
  413.    }
  414.  
  415.    public void close() {
  416.       if (this.wantClose) {
  417.          try {
  418.             this.field_0.close();
  419.          } catch (IOException var2) {
  420.          }
  421.       }
  422.  
  423.    }
  424.  
  425.    protected void finalize() {
  426.       this.close();
  427.    }
  428. }
  429.