Skip to content

Commit 6212959

Browse files
committed
More reformat; catch NumberFormatException
1 parent e3f535e commit 6212959

13 files changed

Lines changed: 289 additions & 246 deletions

src/main/java/org/z3950/zing/cql/CQLGenerator.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,17 @@ public class CQLGenerator {
9494
public CQLGenerator(Properties params) {
9595
this.params = params;
9696
String seed = params.getProperty("seed");
97-
if (seed != null)
98-
rnd = new Random(Long.parseLong(seed));
99-
else
97+
if (seed != null) {
98+
try {
99+
rnd = new Random(Long.parseLong(seed));
100+
} catch (NumberFormatException e) {
101+
throw new NumberFormatException(
102+
"Bad seed value '" + seed + "'");
103+
}
104+
} else {
100105
rnd = new Random();
101106
}
107+
}
102108

103109
private static void debug(String str) {
104110
if (DEBUG)

src/main/java/org/z3950/zing/cql/CQLNode.java

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -259,69 +259,80 @@ protected static final int putNum(long num, byte record[], int offset) {
259259
private static final Map<String, byte[]> madeOIDs = new HashMap<String, byte[]>(10);
260260

261261
protected static final byte[] makeOID(String oid) {
262-
byte[] o;
262+
byte[] o = (byte[]) madeOIDs.get(oid);
263+
if (o != null) {
264+
return o;
265+
}
266+
try {
267+
o = parseOID(oid);
268+
} catch (NumberFormatException e) {
269+
throw new NumberFormatException("Bad OID string: '" + oid + "'");
270+
}
271+
madeOIDs.put(oid, o);
272+
return o;
273+
}
274+
275+
private static final byte[] parseOID(String oid) {
276+
byte[] o = (byte[]) madeOIDs.get(oid);
277+
if (o != null) {
278+
return o;
279+
}
263280
int dot, offset = 0, oidOffset = 0, value;
281+
o = new byte[100];
282+
// Isn't this kind of thing excruciating in Java?
283+
while (oidOffset < oid.length() &&
284+
Character.isDigit(oid.charAt(oidOffset)) == true) {
285+
if (offset > 90) // too large
286+
return null;
287+
288+
dot = oid.indexOf('.', oidOffset);
289+
if (dot == -1)
290+
dot = oid.length();
264291

265-
if ((o = (byte[]) madeOIDs.get(oid)) == null) {
266-
o = new byte[100];
292+
value = Integer.parseInt(oid.substring(oidOffset, dot));
267293

268-
// Isn't this kind of thing excruciating in Java?
269-
while (oidOffset < oid.length() &&
270-
Character.isDigit(oid.charAt(oidOffset)) == true) {
271-
if (offset > 90) // too large
272-
return null;
294+
if (offset == 0) { // 1st two are special
295+
if (dot == -1) // ### can't happen: -1 is reassigned above
296+
return null; // can't be this short
297+
oidOffset = dot + 1; // skip past '.'
273298

274299
dot = oid.indexOf('.', oidOffset);
275300
if (dot == -1)
276301
dot = oid.length();
277302

278-
value = Integer.parseInt(oid.substring(oidOffset, dot));
279-
280-
if (offset == 0) { // 1st two are special
281-
if (dot == -1) // ### can't happen: -1 is reassigned above
282-
return null; // can't be this short
283-
oidOffset = dot + 1; // skip past '.'
284-
285-
dot = oid.indexOf('.', oidOffset);
286-
if (dot == -1)
287-
dot = oid.length();
288-
289-
// ### Eh?!
290-
value = value * 40 +
291-
Integer.parseInt(oid.substring(oidOffset, dot));
292-
}
293-
294-
if (value < 0x80) {
295-
o[offset++] = (byte) value;
296-
} else {
297-
int count = 0;
298-
byte bits[] = new byte[12]; // save a 84 (12*7) bit number
299-
300-
while (value != 0) {
301-
bits[count++] = (byte) (value & 0x7f);
302-
value >>= 7;
303-
}
303+
// ### Eh?!
304+
value = value * 40 +
305+
Integer.parseInt(oid.substring(oidOffset, dot));
306+
}
304307

305-
// Now place in the correct order
306-
while (--count > 0)
307-
o[offset++] = (byte) (bits[count] | 0x80);
308+
if (value < 0x80) {
309+
o[offset++] = (byte) value;
310+
} else {
311+
int count = 0;
312+
byte bits[] = new byte[12]; // save a 84 (12*7) bit number
308313

309-
o[offset++] = bits[count];
314+
while (value != 0) {
315+
bits[count++] = (byte) (value & 0x7f);
316+
value >>= 7;
310317
}
311318

312-
dot = oid.indexOf('.', oidOffset);
313-
if (dot == -1)
314-
break;
319+
// Now place in the correct order
320+
while (--count > 0)
321+
o[offset++] = (byte) (bits[count] | 0x80);
315322

316-
oidOffset = dot + 1;
323+
o[offset++] = bits[count];
317324
}
318325

319-
byte[] ptr = new byte[offset];
320-
System.arraycopy(o, 0, ptr, 0, offset);
321-
madeOIDs.put(oid, ptr);
322-
return ptr;
326+
dot = oid.indexOf('.', oidOffset);
327+
if (dot == -1)
328+
break;
329+
330+
oidOffset = dot + 1;
323331
}
324-
return o;
332+
333+
byte[] ptr = new byte[offset];
334+
System.arraycopy(o, 0, ptr, 0, offset);
335+
return ptr;
325336
}
326337

327338
public static final byte[] makeQuery(CQLNode root, Properties properties)

src/main/java/org/z3950/zing/cql/CQLProxNode.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ byte[] opType1() {
8989
offset = putNum(value, op, offset);
9090

9191
offset = putTag(CONTEXT, 2, PRIMITIVE, op, offset); // distance
92-
value = Integer.parseInt(ms.modifier("distance"));
92+
try {
93+
value = Integer.parseInt(ms.modifier("distance"));
94+
} catch (NumberFormatException e) {
95+
throw new NumberFormatException("Bad distance modifier: " + ms.modifier("distance"));
96+
}
9397
offset = putLen(numLen(value), op, offset);
9498
offset = putNum(value, op, offset);
9599

src/main/java/org/z3950/zing/cql/CQLTermNode.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,12 +291,20 @@ public byte[] toType1BER(Properties config) throws PQFTranslationException {
291291
offset = putTag(UNIVERSAL, SEQUENCE, CONSTRUCTED, operand, offset);
292292
operand[offset++] = (byte) (0x80 & 0xff);
293293
offset = putTag(CONTEXT, 120, PRIMITIVE, operand, offset);
294-
type = Integer.parseInt(attr.substring(0, j));
294+
try {
295+
type = Integer.parseInt(attr.substring(0, j));
296+
} catch (NumberFormatException e) {
297+
throw new PQFTranslationException("Bad attribute type: " + attr.substring(0, j));
298+
}
295299
offset = putLen(numLen(type), operand, offset);
296300
offset = putNum(type, operand, offset);
297301

298302
offset = putTag(CONTEXT, 121, PRIMITIVE, operand, offset);
299-
value = Integer.parseInt(attr.substring(j + 1));
303+
try {
304+
value = Integer.parseInt(attr.substring(j + 1));
305+
} catch (NumberFormatException e) {
306+
throw new PQFTranslationException("Bad attribute value: " + attr.substring(j + 1));
307+
}
300308
offset = putLen(numLen(value), operand, offset);
301309
offset = putNum(value, operand, offset);
302310
operand[offset++] = 0x00; // end of SEQUENCE

src/main/java/org/z3950/zing/cql/Modifier.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/**
44
* Represents a single modifier, consisting of three elements: a type,
5-
* a comparision and a value. For example, "distance", "&lt;", "3". The
5+
* a comparison and a value. For example, "distance", "&lt;", "3". The
66
* type is mandatory; either the comparison and value must both occur,
77
* or neither must.
88
* <P>
@@ -19,59 +19,60 @@ public class Modifier {
1919
* and value.
2020
*/
2121
public Modifier(String type, String comparison, String value) {
22-
this.type = type;
23-
this.comparison = comparison;
24-
this.value = value;
25-
//System.err.println("Made new modifier with " + "type='" + type + "', " + "comparison='" + comparison + "', " + "value='" + value + "',\n");
22+
this.type = type;
23+
this.comparison = comparison;
24+
this.value = value;
25+
// System.err.println("Made new modifier with " + "type='" + type + "', " +
26+
// "comparison='" + comparison + "', " + "value='" + value + "',\n");
2627
}
2728

2829
/**
2930
* Creates a new Modifier with the specified type but no
3031
* comparison or value.
3132
*/
3233
public Modifier(String type) {
33-
this.type = type;
34-
//System.err.println("Made new modifier of type '" + type + "'\n");
34+
this.type = type;
35+
// System.err.println("Made new modifier of type '" + type + "'\n");
3536
}
3637

3738
/**
3839
* Returns the type with which the Modifier was created.
3940
*/
4041
public String getType() {
41-
return type;
42+
return type;
4243
}
4344

4445
/**
4546
* Returns the comparison with which the Modifier was created.
4647
*/
4748
public String getComparison() {
48-
return comparison;
49+
return comparison;
4950
}
5051

5152
/**
5253
* Returns the value with which the Modifier was created.
5354
*/
5455
public String getValue() {
55-
return value;
56+
return value;
5657
}
5758

5859
void toXCQLInternal(XCQLBuilder b, int level, String relationElement) {
59-
b.indent(level).append("<modifier>\n");
60+
b.indent(level).append("<modifier>\n");
6061
b.indent(level + 1).append("<type>");
6162
b.xq(type).append("</type>\n");
62-
if (value != null) {
63+
if (value != null) {
6364
b.indent(level + 1).append("<").append(relationElement).append(">");
6465
b.xq(comparison).append("</").append(relationElement).append(">\n");
6566
b.indent(level + 1).append("<value>");
6667
b.xq(value).append("</value>\n");
67-
}
68-
b.indent(level).append("</modifier>\n");
68+
}
69+
b.indent(level).append("</modifier>\n");
6970
}
7071

7172
public String toCQL() {
72-
StringBuilder buf = new StringBuilder(type);
73-
if (value != null)
74-
buf.append(" ").append(comparison).append(" ").append(value);
75-
return buf.toString();
73+
StringBuilder buf = new StringBuilder(type);
74+
if (value != null)
75+
buf.append(" ").append(comparison).append(" ").append(value);
76+
return buf.toString();
7677
}
7778
}

0 commit comments

Comments
 (0)