home *** CD-ROM | disk | FTP | other *** search
- package org.xbill.DNS;
-
- import java.io.BufferedInputStream;
- import java.io.ByteArrayInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.PushbackInputStream;
- import org.xbill.DNS.utils.base16;
- import org.xbill.DNS.utils.base64;
-
- public class Tokenizer {
- private static String delim = " \t\n;()\"";
- private static String quotes = "\"";
- public static final int EOF = 0;
- public static final int EOL = 1;
- public static final int WHITESPACE = 2;
- public static final int IDENTIFIER = 3;
- public static final int QUOTED_STRING = 4;
- public static final int COMMENT = 5;
- // $FF: renamed from: is java.io.PushbackInputStream
- private PushbackInputStream field_0;
- private boolean ungottenToken;
- private int multiline;
- private boolean quoting;
- private String delimiters;
- private Token current;
- // $FF: renamed from: sb java.lang.StringBuffer
- private StringBuffer field_1;
- private boolean wantClose;
- private String filename;
- private int line;
-
- public Tokenizer(InputStream is) {
- if (!(is instanceof BufferedInputStream)) {
- is = new BufferedInputStream(is);
- }
-
- this.field_0 = new PushbackInputStream(is, 2);
- this.ungottenToken = false;
- this.multiline = 0;
- this.quoting = false;
- this.delimiters = delim;
- this.current = new Token((Token)null);
- this.field_1 = new StringBuffer();
- this.filename = "<none>";
- this.line = 1;
- }
-
- public Tokenizer(String s) {
- this((InputStream)(new ByteArrayInputStream(s.getBytes())));
- }
-
- public Tokenizer(File f) throws FileNotFoundException {
- this((InputStream)(new FileInputStream(f)));
- this.wantClose = true;
- this.filename = f.getName();
- }
-
- private int getChar() throws IOException {
- int c = this.field_0.read();
- if (c == 13) {
- int next = this.field_0.read();
- if (next != 10) {
- this.field_0.unread(next);
- }
-
- c = 10;
- }
-
- if (c == 10) {
- ++this.line;
- }
-
- return c;
- }
-
- private void ungetChar(int c) throws IOException {
- if (c != -1) {
- this.field_0.unread(c);
- if (c == 10) {
- --this.line;
- }
-
- }
- }
-
- private int skipWhitespace() throws IOException {
- int skipped = 0;
-
- while(true) {
- int c = this.getChar();
- if (c != 32 && c != 9 && (c != 10 || this.multiline <= 0)) {
- this.ungetChar(c);
- return skipped;
- }
-
- ++skipped;
- }
- }
-
- private void checkUnbalancedParens() throws TextParseException {
- if (this.multiline > 0) {
- throw this.exception("unbalanced parentheses");
- }
- }
-
- public Token get(boolean wantWhitespace, boolean wantComment) throws IOException {
- if (this.ungottenToken) {
- this.ungottenToken = false;
- if (this.current.type == 2) {
- if (wantWhitespace) {
- return this.current;
- }
- } else {
- if (this.current.type != 5) {
- return this.current;
- }
-
- if (wantComment) {
- return this.current;
- }
- }
- }
-
- int skipped = this.skipWhitespace();
- if (skipped > 0 && wantWhitespace) {
- return org.xbill.DNS.Tokenizer.Token.access$1(this.current, 2, (StringBuffer)null);
- } else {
- int type = 3;
- this.field_1.setLength(0);
-
- while(true) {
- int c = this.getChar();
- if (c == -1 || this.delimiters.indexOf(c) != -1) {
- if (c == -1) {
- if (this.quoting) {
- throw this.exception("EOF in quoted string");
- }
-
- if (this.field_1.length() == 0) {
- return org.xbill.DNS.Tokenizer.Token.access$1(this.current, 0, (StringBuffer)null);
- }
-
- return org.xbill.DNS.Tokenizer.Token.access$1(this.current, type, this.field_1);
- }
-
- if (this.field_1.length() != 0 || type == 4) {
- this.ungetChar(c);
- if (this.field_1.length() == 0 && type != 4) {
- this.checkUnbalancedParens();
- return org.xbill.DNS.Tokenizer.Token.access$1(this.current, 0, (StringBuffer)null);
- } else {
- return org.xbill.DNS.Tokenizer.Token.access$1(this.current, type, this.field_1);
- }
- }
-
- if (c == 40) {
- ++this.multiline;
- this.skipWhitespace();
- } else if (c == 41) {
- if (this.multiline <= 0) {
- throw this.exception("invalid close parenthesis");
- }
-
- --this.multiline;
- this.skipWhitespace();
- } else if (c == 34) {
- if (!this.quoting) {
- this.quoting = true;
- this.delimiters = quotes;
- type = 4;
- } else {
- this.quoting = false;
- this.delimiters = delim;
- this.skipWhitespace();
- }
- } else {
- if (c == 10) {
- return org.xbill.DNS.Tokenizer.Token.access$1(this.current, 1, (StringBuffer)null);
- }
-
- if (c != 59) {
- throw new IllegalStateException();
- }
-
- while(true) {
- c = this.getChar();
- if (c == 10 || c == -1) {
- if (wantComment) {
- this.ungetChar(c);
- return org.xbill.DNS.Tokenizer.Token.access$1(this.current, 5, this.field_1);
- }
-
- if (c == -1 && type != 4) {
- this.checkUnbalancedParens();
- return org.xbill.DNS.Tokenizer.Token.access$1(this.current, 0, (StringBuffer)null);
- }
-
- if (this.multiline <= 0) {
- return org.xbill.DNS.Tokenizer.Token.access$1(this.current, 1, (StringBuffer)null);
- }
-
- this.skipWhitespace();
- this.field_1.setLength(0);
- break;
- }
-
- this.field_1.append((char)c);
- }
- }
- } else {
- if (c == 92) {
- c = this.getChar();
- if (c == -1) {
- throw this.exception("unterminated escape sequence");
- }
-
- this.field_1.append('\\');
- } else if (this.quoting && c == 10) {
- throw this.exception("newline in quoted string");
- }
-
- this.field_1.append((char)c);
- }
- }
- }
- }
-
- public Token get() throws IOException {
- return this.get(false, false);
- }
-
- public void unget() {
- if (this.ungottenToken) {
- throw new IllegalStateException("Cannot unget multiple tokens");
- } else {
- this.ungottenToken = true;
- }
- }
-
- public String getString() throws IOException {
- Token next = this.get();
- if (!next.isString()) {
- throw this.exception("expected a string");
- } else {
- return next.value;
- }
- }
-
- public String getIdentifier() throws IOException {
- Token next = this.get();
- if (next.type != 3) {
- throw this.exception("expected an identifier");
- } else {
- return next.value;
- }
- }
-
- public long getLong() throws IOException {
- String next = this.getIdentifier();
- if (!Character.isDigit(next.charAt(0))) {
- throw this.exception("expecting an integer");
- } else {
- try {
- return Long.parseLong(next);
- } catch (NumberFormatException var3) {
- throw this.exception("expecting an integer");
- }
- }
- }
-
- public long getUInt32() throws IOException {
- long l = this.getLong();
- if (l >= 0L && l <= 4294967295L) {
- return l;
- } else {
- throw this.exception("expecting an 32 bit unsigned integer");
- }
- }
-
- public int getUInt16() throws IOException {
- long l = this.getLong();
- if (l >= 0L && l <= 65535L) {
- return (int)l;
- } else {
- throw this.exception("expecting an 16 bit unsigned integer");
- }
- }
-
- public int getUInt8() throws IOException {
- long l = this.getLong();
- if (l >= 0L && l <= 255L) {
- return (int)l;
- } else {
- throw this.exception("expecting an 8 bit unsigned integer");
- }
- }
-
- public double getDouble() throws IOException {
- String next = this.getIdentifier();
- if (!Character.isDigit(next.charAt(0))) {
- throw this.exception("expecting an integer");
- } else {
- try {
- return Double.parseDouble(next);
- } catch (NumberFormatException var3) {
- throw this.exception("expecting an floating point value");
- }
- }
- }
-
- public long getTTL() throws IOException {
- String next = this.getIdentifier();
-
- try {
- return TTL.parseTTL(next);
- } catch (NumberFormatException var3) {
- throw this.exception("expecting an 32 bit unsigned integer");
- }
- }
-
- public Name getName(Name origin) throws IOException {
- try {
- Name name = Name.fromString(this.getIdentifier(), origin);
- if (!name.isAbsolute()) {
- throw new RelativeNameException(name);
- } else {
- return name;
- }
- } catch (TextParseException e) {
- throw this.exception(e.getMessage());
- }
- }
-
- public void getEOL() throws IOException {
- Token next = this.get();
- if (next.type != 1 && next.type != 0) {
- throw this.exception("expecting EOL or EOF");
- }
- }
-
- private String remainingStrings() throws IOException {
- StringBuffer sb = null;
-
- while(true) {
- Token t = this.get();
- if (!t.isString()) {
- this.unget();
- if (sb == null) {
- return null;
- }
-
- return sb.toString();
- }
-
- if (sb == null) {
- sb = new StringBuffer();
- }
-
- sb.append(t.value);
- }
- }
-
- public byte[] getBase64(boolean required) throws IOException {
- String s = this.remainingStrings();
- if (s == null) {
- if (required) {
- throw this.exception("expected base64 encoded string");
- } else {
- return null;
- }
- } else {
- byte[] array = base64.fromString(s);
- if (array == null) {
- throw this.exception("invalid base64 encoding");
- } else {
- return array;
- }
- }
- }
-
- public byte[] getBase64() throws IOException {
- return this.getBase64(false);
- }
-
- public byte[] getHex(boolean required) throws IOException {
- String s = this.remainingStrings();
- if (s == null) {
- if (required) {
- throw this.exception("expected hex encoded string");
- } else {
- return null;
- }
- } else {
- byte[] array = base16.fromString(s);
- if (array == null) {
- throw this.exception("invalid hex encoding");
- } else {
- return array;
- }
- }
- }
-
- public byte[] getHex() throws IOException {
- return this.getHex(false);
- }
-
- public TextParseException exception(String s) {
- return new TextParseException(this.filename + ":" + this.line + ": " + s);
- }
-
- public void close() {
- if (this.wantClose) {
- try {
- this.field_0.close();
- } catch (IOException var2) {
- }
- }
-
- }
-
- protected void finalize() {
- this.close();
- }
- }
-