mikrotik-utils/MikroTikAPI/src/com/eightOceans/mikroTik/MtSentence.java

75 lines
1.6 KiB
Java

package com.eightOceans.mikroTik;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author mh
*/
public class MtSentence {
private boolean isDone;
private boolean isTrap;
private Map<String, String> words;
public static MtSentence from(String rawString) {
MtSentence s = new MtSentence();
String[] words = rawString.split("\n");
Map<String, String> newWords = new HashMap<>();
for (String w : words) {
if (w.contains("!done")) {
s.isDone = true;
break;
} else if (w.contains("!trap")) {
s.isTrap = true;
break;
} else if (w.contains("!re")) {
} else if (w.startsWith("=")) {
String[] p = w.substring(1).split("=");
if (p.length == 2) {
newWords.put(p[0], p[1]);
} else {
newWords.put(w, null);
}
} else {
throw new UnsupportedOperationException("Unsupported word in rawString: " + w);
}
}
if ((!s.isDone) && (!s.isTrap)) {
s.words = newWords;
}
return s;
}
protected MtSentence() {
words = new HashMap<>();
}
public boolean isDone() {
return isDone;
}
public boolean isTrap() {
return isTrap;
}
/**
*
* @return [KEY]=[VALUE] if word was key=value style or [KEY]=null if word does not contain a "="
*/
public Map<String, String> getWordAsMap() {
return words;
}
}