diff options
author | Shuyi Chen <shuyichen@google.com> | 2013-05-22 14:51:55 -0700 |
---|---|---|
committer | Shuyi Chen <shuyichen@google.com> | 2013-05-22 17:19:30 -0700 |
commit | d7955ce24d294fb2014c59d11fca184471056f44 (patch) | |
tree | e260500b0b7639127038495d46a0ad6dcbb6d96c /src/org/xbill/DNS/Generator.java | |
parent | 8f4ce9ea0de51fee918bffe19c434612d6bbb2d7 (diff) | |
download | smack-d7955ce24d294fb2014c59d11fca184471056f44.tar.gz |
Add android smack source.HEADandroid-wear-5.0.0_r1android-sdk-4.4.2_r1.0.1android-sdk-4.4.2_r1android-l-preview_r2android-cts-5.1_r9android-cts-5.1_r8android-cts-5.1_r7android-cts-5.1_r6android-cts-5.1_r5android-cts-5.1_r4android-cts-5.1_r3android-cts-5.1_r28android-cts-5.1_r27android-cts-5.1_r26android-cts-5.1_r25android-cts-5.1_r24android-cts-5.1_r23android-cts-5.1_r22android-cts-5.1_r21android-cts-5.1_r20android-cts-5.1_r2android-cts-5.1_r19android-cts-5.1_r18android-cts-5.1_r17android-cts-5.1_r16android-cts-5.1_r15android-cts-5.1_r14android-cts-5.1_r13android-cts-5.1_r10android-cts-5.1_r1android-cts-5.0_r9android-cts-5.0_r8android-cts-5.0_r7android-cts-5.0_r6android-cts-5.0_r5android-cts-5.0_r4android-cts-5.0_r3android-cts-4.4_r4android-cts-4.4_r1android-5.1.1_r9android-5.1.1_r8android-5.1.1_r7android-5.1.1_r6android-5.1.1_r5android-5.1.1_r4android-5.1.1_r38android-5.1.1_r37android-5.1.1_r36android-5.1.1_r35android-5.1.1_r34android-5.1.1_r33android-5.1.1_r30android-5.1.1_r3android-5.1.1_r29android-5.1.1_r28android-5.1.1_r26android-5.1.1_r25android-5.1.1_r24android-5.1.1_r23android-5.1.1_r22android-5.1.1_r20android-5.1.1_r2android-5.1.1_r19android-5.1.1_r18android-5.1.1_r17android-5.1.1_r16android-5.1.1_r15android-5.1.1_r14android-5.1.1_r13android-5.1.1_r12android-5.1.1_r10android-5.1.1_r1android-5.1.0_r5android-5.1.0_r4android-5.1.0_r3android-5.1.0_r1android-5.0.2_r3android-5.0.2_r1android-5.0.1_r1android-5.0.0_r7android-5.0.0_r6android-5.0.0_r5.1android-5.0.0_r5android-5.0.0_r4android-5.0.0_r3android-5.0.0_r2android-5.0.0_r1android-4.4w_r1android-4.4_r1.2.0.1android-4.4_r1.2android-4.4_r1.1.0.1android-4.4_r1.1android-4.4_r1.0.1android-4.4_r1android-4.4_r0.9android-4.4_r0.8android-4.4_r0.7android-4.4.4_r2.0.1android-4.4.4_r2android-4.4.4_r1.0.1android-4.4.4_r1android-4.4.3_r1.1.0.1android-4.4.3_r1.1android-4.4.3_r1.0.1android-4.4.3_r1android-4.4.2_r2.0.1android-4.4.2_r2android-4.4.2_r1.0.1android-4.4.2_r1android-4.4.1_r1.0.1android-4.4.1_r1android-4.3_r3.1android-4.3_r3android-4.3_r2.3android-4.3_r2.2android-4.3_r2.1android-4.3_r2android-4.3_r1.1android-4.3_r1android-4.3_r0.9.1android-4.3_r0.9android-4.3.1_r1tools_r22.2mastermainlollipop-wear-releaselollipop-releaselollipop-mr1-wfc-releaselollipop-mr1-releaselollipop-mr1-fi-releaselollipop-mr1-devlollipop-mr1-cts-releaselollipop-devlollipop-cts-releasel-previewkitkat-wearkitkat-releasekitkat-mr2.2-releasekitkat-mr2.1-releasekitkat-mr2-releasekitkat-mr1.1-releasekitkat-mr1-releasekitkat-devkitkat-cts-releasekitkat-cts-devjb-mr2.0.0-releasejb-mr2.0-releasejb-mr2-releasejb-mr2-devidea133-weekly-releaseidea133
Change-Id: I49ce97136c17173c4ae3965c694af6e7bc49897d
Diffstat (limited to 'src/org/xbill/DNS/Generator.java')
-rw-r--r-- | src/org/xbill/DNS/Generator.java | 264 |
1 files changed, 264 insertions, 0 deletions
diff --git a/src/org/xbill/DNS/Generator.java b/src/org/xbill/DNS/Generator.java new file mode 100644 index 0000000..a08d343 --- /dev/null +++ b/src/org/xbill/DNS/Generator.java @@ -0,0 +1,264 @@ +// Copyright (c) 2004 Brian Wellington (bwelling@xbill.org) + +package org.xbill.DNS; + +import java.io.*; +import java.util.*; + +/** + * A representation of a $GENERATE statement in a master file. + * + * @author Brian Wellington + */ + +public class Generator { + +/** The start of the range. */ +public long start; + +/** The end of the range. */ +public long end; + +/** The step value of the range. */ +public long step; + +/** The pattern to use for generating record names. */ +public final String namePattern; + +/** The type of the generated records. */ +public final int type; + +/** The class of the generated records. */ +public final int dclass; + +/** The ttl of the generated records. */ +public final long ttl; + +/** The pattern to use for generating record data. */ +public final String rdataPattern; + +/** The origin to append to relative names. */ +public final Name origin; + +private long current; + +/** + * Indicates whether generation is supported for this type. + * @throws InvalidTypeException The type is out of range. + */ +public static boolean +supportedType(int type) { + Type.check(type); + return (type == Type.PTR || type == Type.CNAME || type == Type.DNAME || + type == Type.A || type == Type.AAAA || type == Type.NS); +} + +/** + * Creates a specification for generating records, as a $GENERATE + * statement in a master file. + * @param start The start of the range. + * @param end The end of the range. + * @param step The step value of the range. + * @param namePattern The pattern to use for generating record names. + * @param type The type of the generated records. The supported types are + * PTR, CNAME, DNAME, A, AAAA, and NS. + * @param dclass The class of the generated records. + * @param ttl The ttl of the generated records. + * @param rdataPattern The pattern to use for generating record data. + * @param origin The origin to append to relative names. + * @throws IllegalArgumentException The range is invalid. + * @throws IllegalArgumentException The type does not support generation. + * @throws IllegalArgumentException The dclass is not a valid class. + */ +public +Generator(long start, long end, long step, String namePattern, + int type, int dclass, long ttl, String rdataPattern, Name origin) +{ + if (start < 0 || end < 0 || start > end || step <= 0) + throw new IllegalArgumentException + ("invalid range specification"); + if (!supportedType(type)) + throw new IllegalArgumentException("unsupported type"); + DClass.check(dclass); + + this.start = start; + this.end = end; + this.step = step; + this.namePattern = namePattern; + this.type = type; + this.dclass = dclass; + this.ttl = ttl; + this.rdataPattern = rdataPattern; + this.origin = origin; + this.current = start; +} + +private String +substitute(String spec, long n) throws IOException { + boolean escaped = false; + byte [] str = spec.getBytes(); + StringBuffer sb = new StringBuffer(); + + for (int i = 0; i < str.length; i++) { + char c = (char)(str[i] & 0xFF); + if (escaped) { + sb.append(c); + escaped = false; + } else if (c == '\\') { + if (i + 1 == str.length) + throw new TextParseException + ("invalid escape character"); + escaped = true; + } else if (c == '$') { + boolean negative = false; + long offset = 0; + long width = 0; + long base = 10; + boolean wantUpperCase = false; + if (i + 1 < str.length && str[i + 1] == '$') { + // '$$' == literal '$' for backwards + // compatibility with old versions of BIND. + c = (char)(str[++i] & 0xFF); + sb.append(c); + continue; + } else if (i + 1 < str.length && str[i + 1] == '{') { + // It's a substitution with modifiers. + i++; + if (i + 1 < str.length && str[i + 1] == '-') { + negative = true; + i++; + } + while (i + 1 < str.length) { + c = (char)(str[++i] & 0xFF); + if (c == ',' || c == '}') + break; + if (c < '0' || c > '9') + throw new TextParseException( + "invalid offset"); + c -= '0'; + offset *= 10; + offset += c; + } + if (negative) + offset = -offset; + + if (c == ',') { + while (i + 1 < str.length) { + c = (char)(str[++i] & 0xFF); + if (c == ',' || c == '}') + break; + if (c < '0' || c > '9') + throw new + TextParseException( + "invalid width"); + c -= '0'; + width *= 10; + width += c; + } + } + + if (c == ',') { + if (i + 1 == str.length) + throw new TextParseException( + "invalid base"); + c = (char)(str[++i] & 0xFF); + if (c == 'o') + base = 8; + else if (c == 'x') + base = 16; + else if (c == 'X') { + base = 16; + wantUpperCase = true; + } + else if (c != 'd') + throw new TextParseException( + "invalid base"); + } + + if (i + 1 == str.length || str[i + 1] != '}') + throw new TextParseException + ("invalid modifiers"); + i++; + } + long v = n + offset; + if (v < 0) + throw new TextParseException + ("invalid offset expansion"); + String number; + if (base == 8) + number = Long.toOctalString(v); + else if (base == 16) + number = Long.toHexString(v); + else + number = Long.toString(v); + if (wantUpperCase) + number = number.toUpperCase(); + if (width != 0 && width > number.length()) { + int zeros = (int)width - number.length(); + while (zeros-- > 0) + sb.append('0'); + } + sb.append(number); + } else { + sb.append(c); + } + } + return sb.toString(); +} + +/** + * Constructs and returns the next record in the expansion. + * @throws IOException The name or rdata was invalid after substitutions were + * performed. + */ +public Record +nextRecord() throws IOException { + if (current > end) + return null; + String namestr = substitute(namePattern, current); + Name name = Name.fromString(namestr, origin); + String rdata = substitute(rdataPattern, current); + current += step; + return Record.fromString(name, type, dclass, ttl, rdata, origin); +} + +/** + * Constructs and returns all records in the expansion. + * @throws IOException The name or rdata of a record was invalid after + * substitutions were performed. + */ +public Record [] +expand() throws IOException { + List list = new ArrayList(); + for (long i = start; i < end; i += step) { + String namestr = substitute(namePattern, current); + Name name = Name.fromString(namestr, origin); + String rdata = substitute(rdataPattern, current); + list.add(Record.fromString(name, type, dclass, ttl, + rdata, origin)); + } + return (Record []) list.toArray(new Record[list.size()]); +} + +/** + * Converts the generate specification to a string containing the corresponding + * $GENERATE statement. + */ +public String +toString() { + StringBuffer sb = new StringBuffer(); + sb.append("$GENERATE "); + sb.append(start + "-" + end); + if (step > 1) + sb.append("/" + step); + sb.append(" "); + sb.append(namePattern + " "); + sb.append(ttl + " "); + if (dclass != DClass.IN || !Options.check("noPrintIN")) + sb.append(DClass.string(dclass) + " "); + sb.append(Type.string(type) + " "); + sb.append(rdataPattern + " "); + return sb.toString(); +} + +} |