in src/main/java/org/apache/accumulo/testing/randomwalk/Module.java [504:638]
private void loadFromXml() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docbuilder;
Document d;
// set the schema
SchemaFactory sf = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema moduleSchema =
sf.newSchema(this.getClass().getClassLoader().getResource("randomwalk/module.xsd"));
dbf.setSchema(moduleSchema);
// parse the document
try {
docbuilder = dbf.newDocumentBuilder();
d = docbuilder.parse(this.getClass().getResourceAsStream("/randomwalk/modules/" + id));
} catch (Exception e) {
log.error("Failed to parse xml at randomwalk/modules/" + id, e);
throw new Exception("Failed to parse xml at randomwalk/modules/" + id);
}
// parse packages
NodeList nodelist = d.getDocumentElement().getElementsByTagName("package");
for (int i = 0; i < nodelist.getLength(); i++) {
Element el = (Element) nodelist.item(i);
String value = el.getAttribute("value");
if (!value.endsWith(".")) {
value = value.concat(".");
}
prefixes.put(el.getAttribute("prefix"), value);
}
// parse fixture node
nodelist = d.getDocumentElement().getElementsByTagName("fixture");
if (nodelist.getLength() > 0) {
Element fixtureEl = (Element) nodelist.item(0);
fixture = (Fixture) Class.forName(getFullName(fixtureEl.getAttribute("id")))
.getDeclaredConstructor().newInstance();
}
// parse initial node
Element initEl = (Element) d.getDocumentElement().getElementsByTagName("init").item(0);
initNodeId = initEl.getAttribute("id");
Properties initProps = new Properties();
String attr = initEl.getAttribute("maxHops");
initProps.setProperty("maxHops", attr);
attr = initEl.getAttribute("maxSec");
initProps.setProperty("maxSec", attr);
attr = initEl.getAttribute("teardown");
initProps.setProperty("teardown", attr);
localProps.put("_init", initProps);
// parse all nodes
nodelist = d.getDocumentElement().getElementsByTagName("node");
for (int i = 0; i < nodelist.getLength(); i++) {
Element nodeEl = (Element) nodelist.item(i);
// get attributes
String id = nodeEl.getAttribute("id");
if (adjMap.containsKey(id)) {
throw new Exception("Module already contains: " + id);
}
String src = nodeEl.getAttribute("src");
// create node
createNode(id, src);
// set some attributes in properties for later use
Properties props = new Properties();
props.setProperty("maxHops", nodeEl.getAttribute("maxHops"));
props.setProperty("maxSec", nodeEl.getAttribute("maxSec"));
props.setProperty("teardown", nodeEl.getAttribute("teardown"));
// parse aliases
NodeList aliaslist = nodeEl.getElementsByTagName("alias");
Set<String> aliases = new TreeSet<>();
for (int j = 0; j < aliaslist.getLength(); j++) {
Element propEl = (Element) aliaslist.item(j);
if (!propEl.hasAttribute("name")) {
throw new Exception("Node " + id + " has alias with no identifying name");
}
String key = "alias." + propEl.getAttribute("name");
aliases.add(key);
createNode(key, null);
}
if (aliases.size() > 0)
aliasMap.put(id, aliases);
// parse properties of nodes
NodeList proplist = nodeEl.getElementsByTagName("property");
for (int j = 0; j < proplist.getLength(); j++) {
Element propEl = (Element) proplist.item(j);
if (!propEl.hasAttribute("key") || !propEl.hasAttribute("value")) {
throw new Exception("Node " + id + " has property with no key or value");
}
String key = propEl.getAttribute("key");
if (key.equals("maxHops") || key.equals("maxSec") || key.equals("teardown")) {
throw new Exception("The following property can only be set in attributes: " + key);
}
props.setProperty(key, propEl.getAttribute("value"));
}
localProps.put(id, props);
// parse edges of nodes
AdjList edges = new AdjList();
adjMap.put(id, edges);
NodeList edgelist = nodeEl.getElementsByTagName("edge");
if (edgelist.getLength() == 0) {
throw new Exception("Node " + id + " has no edges!");
}
for (int j = 0; j < edgelist.getLength(); j++) {
Element edgeEl = (Element) edgelist.item(j);
String edgeID = edgeEl.getAttribute("id");
if (!edgeEl.hasAttribute("weight")) {
throw new Exception("Edge with id=" + edgeID + " is missing weight");
}
int weight = Integer.parseInt(edgeEl.getAttribute("weight"));
edges.addEdge(edgeID, weight);
}
}
}