/* Read the main XML elements and call appropriate private functions to handle * reading child elements. */ private void readXML(String filename) { Document xml = util.fileToXML(filename); Node root = xml.getDocumentElement(); if ("gte".equals(root.getNodeName())) { for (Node child = root.getFirstChild(); child != null; child = child.getNextSibling()) { if ("gameDescription".equals(child.getNodeName())) { // this.gameDescription = "\"" + child.getTextContent() + "\""; } if ("players".equals(child.getNodeName())) { this.playerNames = util.readPlayersXML(child); } if ("strategicForm".equals(child.getNodeName())) { this.readStrategicForm(child); this.numRows = Integer.parseInt(this.numPlayerStrategies.get(0)); this.numCols = Integer.parseInt(this.numPlayerStrategies.get(1)); } if ("display".equals(child.getNodeName())) { this.readDisplayXML(child); } } } else { System.out.println("XMLToLaTeX error: first XML element not recognized."); } }
/* Parse the data from a strategy node */ private void processStrategy(Node node) { NodeList nl = node.getChildNodes(); String value = nl.item(0).getNodeValue(); String playerName = util.getAttribute(node, "player"); this.addPlayerName(playerName); value = value.replace("}", ""); value = value.replace("{", ""); value = value.trim(); ArrayList<String> strategies = util.extractTokens(value); this.playerStrategies.put(playerName, strategies); }
/* Process the data from a payoff node */ private void processPayoff(Node node) { NodeList nl = node.getChildNodes(); String value = nl.item(0).getNodeValue(); String playerName = util.getAttribute(node, "player"); // this call is superfluous if <players> present, but keeping to ensure players populated this.addPlayerName(playerName); this.playerPayoffs.put(playerName, value.trim()); }
/* read and process the XML data from the strategicForm XML node */ private void readStrategicForm(Node stratForm) { String gameSize = util.getAttribute(stratForm, "size"); gameSize = gameSize.replace("}", ""); gameSize = gameSize.replace("{", "").trim(); String[] strategies = gameSize.split("\\s+"); for (int i = 0; i < strategies.length; i++) { this.numPlayerStrategies.add(strategies[i].trim()); } for (Node child = stratForm.getFirstChild(); child != null; child = child.getNextSibling()) { if ("strategy".equals(child.getNodeName())) { processStrategy((Element) child); } else if ("payoffs".equals(child.getNodeName())) { processPayoff((Element) child); } } }
/* create the output file leveraging the utilities class */ private void createLaTeXFile(String filename) { String outFile = this.filename.substring(0, filename.length() - 4) + this.fileSuffix; util.createFile(outFile, this.getLaTeXString()); }