private static Command parseCommand(Node n) { NamedNodeMap atts = n.getAttributes(); String name = atts.getNamedItem("name").getNodeValue(); String desc = atts.getNamedItem("description").getNodeValue(); String command = atts.getNamedItem("command").getNodeValue(); Command com = new Command(name, desc, command); NodeList children = n.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (!child.getNodeName().equals("Argument")) continue; NamedNodeMap childAtts = child.getAttributes(); String childName = childAtts.getNamedItem("name").getNodeValue(); String childDesc = childAtts.getNamedItem("description").getNodeValue(); String childVal = ""; if (childAtts.getNamedItem("default") != null) childVal = childAtts.getNamedItem("default").getNodeValue(); Argument arg = new Argument(childName, childDesc, childVal); com.addArgument(arg); } return com; }
private static Category parseCategory(Node n) { NamedNodeMap atts = n.getAttributes(); String name = atts.getNamedItem("name").getNodeValue(); String desc = atts.getNamedItem("description").getNodeValue(); Category cat = new Category(name, desc); NodeList children = n.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { parseNode(children.item(i), cat); } return cat; }
private static void parseNode(Node n, Category parent) { String name = n.getNodeName(); if (name.equals("Category")) { Category cat = parseCategory(n); parent.addCategory(cat); } if (name.equals("Command")) { Command com = parseCommand(n); parent.addCommand(com); } }
private static void parseXML() { try { Document doc = DocumentBuilderFactory.newInstance() .newDocumentBuilder() .parse(CommandBuilder.class.getResourceAsStream("/CommandMap.xml")); NodeList nodes = doc.getElementsByTagName("CommandMap"); for (int i = 0; i < nodes.getLength(); i++) { Node n = nodes.item(i); NamedNodeMap atts = n.getAttributes(); String rootCommand = atts.getNamedItem("rootCommand").getNodeValue(); rootNode = new Category(rootCommand, ""); NodeList children = n.getChildNodes(); for (int p = 0; p < children.getLength(); p++) { parseNode(children.item(p), rootNode); } } } catch (Exception e) { e.printStackTrace(); } }