/*
* loads patterns from an InputStreamReader
*/
private void loadDevicePatterns(Reader in) throws IOException {
XMLParser parser = new XMLParser(in);
String tag;
String builder = "";
Device device = null;
String id = "";
List<String> patterns = new ArrayList<String>();
while (!(tag = parser.getNextTag()).isEmpty()) {
//new builder found
if (tag.startsWith("<builder ")) {
builder = XMLParser.getAttribute(tag, "class");
if (builder.lastIndexOf(".") >= 0) {
builder = builder.substring(builder.lastIndexOf(".") + 1);
}
} else if (tag.startsWith("<device ")) {
//new device found
id = XMLParser.getAttribute(tag, "id");
device = devices.get(id);
} else if (tag.equals("</device>")) {
//add the device
if (device != null) {
//TwoStep is an AND pattern, also index the unigram
if (builder.equals("TwoStepDeviceBuilder")) {
device.getPatterns().setAndPattern(patterns);
String unigram = "";
for (String pattern : patterns) {
if (pattern.contains(unigram)) {
unigram = pattern;
} else {
unigram += pattern;
}
}
device.getPatterns().setPattern(unigram);
} else {
device.getPatterns().setOrPattern(patterns);
}
if (builder.equals("SimpleDeviceBuilder")) {
device.setType("simple");
} else {
device.setType("weak");
}
} else {
Util.debugLog("ERROR: device not found: '" + id + "'");
}
//reset the device
device = null;
id = "";
patterns = new ArrayList<String>();
} else if (tag.equals("<value>")) {
//add the pattern to the device
String pattern = Util.normalize(parser.getTagValue());
if (pattern.isEmpty()) {
continue;
}