Changed parsing of =var variables to handle multi-line results better

This commit is contained in:
GideonLeGrange 2014-12-22 15:41:02 +02:00
parent 047cd4246b
commit 2a450e260c
1 changed files with 29 additions and 3 deletions

View File

@ -315,6 +315,15 @@ public final class ApiConnectionImpl extends ApiConnection {
return !lines.isEmpty() || !reader.isEmpty(); return !lines.isEmpty() || !reader.isEmpty();
} }
private String peekLine() throws ApiConnectionException, ApiDataException {
if (lines.isEmpty()) {
String block = reader.take();
String parts[] = block.split("\n");
lines.addAll(Arrays.asList(parts));
}
return lines.get(0);
}
private Response unpack() throws MikrotikApiException { private Response unpack() throws MikrotikApiException {
if (line == null) { if (line == null) {
nextLine(); nextLine();
@ -345,9 +354,7 @@ public final class ApiConnectionImpl extends ApiConnection {
String parts[] = line.split("=", 3); String parts[] = line.split("=", 3);
if (parts.length == 3) { if (parts.length == 3) {
if (!parts[2].endsWith("\r")) { if (!parts[2].endsWith("\r")) {
res.put(parts[1], parts[2]); res.put(parts[1], unpackResult(parts[2]));
} else {
res.put(parts[1], unpackMultiLine(parts[2]));
} }
} else { } else {
throw new ApiDataException(String.format("Malformed line '%s'", line)); throw new ApiDataException(String.format("Malformed line '%s'", line));
@ -370,6 +377,25 @@ public final class ApiConnectionImpl extends ApiConnection {
return res; return res;
} }
private String unpackResult(String first )throws ApiConnectionException, ApiDataException {
StringBuilder buf = new StringBuilder(first);
line = null;
while (hasNextLine()) {
String peek = peekLine();
if (!(peek.startsWith("!") || peek.startsWith("=") || peek.startsWith(".tag="))) {
nextLine();
buf.append("\n");
buf.append(line);
}
else {
break;
}
}
return buf.toString();
}
private String unpackMultiLine(String head) throws ApiConnectionException, ApiDataException { private String unpackMultiLine(String head) throws ApiConnectionException, ApiDataException {
StringBuilder buf = new StringBuilder(head); StringBuilder buf = new StringBuilder(head);
do { do {