import java.io.*; import java.util.*; import org.w3c.dom.*; // import DOM interfaces import org.apache.xerces.parsers.DOMParser; import org.apache.xerces.dom.DocumentImpl; import org.apache.xml.serialize.*; public class League1 { static private final int WPTS = 3; // number of points for a win static private final int DPTS = 1; // number of points for a draw public static Document createTeams() throws IOException { // create document and a root element Document doc= new DocumentImpl(); Element root = doc.createElement("LEAGUE"); BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter teams or nothing to end input..."); Vector usedNames = new Vector(); while (true) { String teamName = input.readLine(); if (teamName.equals("")) break; // end of input // each team must have a unique name if (usedNames.contains(teamName)) { System.out.println("This team has allready been entered."); } else { usedNames.add(teamName); // create the hierarchie of elements which is necessary to store the information Element position = doc.createElement("POSITION"); Element team = doc.createElement("TEAM"); team.appendChild(doc.createTextNode(teamName)); position.appendChild(team); Element points = doc.createElement("POINTS"); points.appendChild(doc.createTextNode("0")); position.appendChild(points); Element goals = doc.createElement("GOALS"); goals.appendChild(doc.createTextNode("0")); position.appendChild(goals); Element againsts = doc.createElement("AGAINSTS"); againsts.appendChild(doc.createTextNode("0")); position.appendChild(againsts); Element record = doc.createElement("RECORD"); record.setAttribute("WINS", "0"); record.setAttribute("DRAWS", "0"); record.setAttribute("LOSSES", "0"); position.appendChild(record); root.appendChild(position); } } doc.appendChild(root); return doc; } public static void processResult(Element e, int gls, int ags) { try { // draw if (gls == ags) { // update points -> add DPTS for a draw Element points = (Element) e.getElementsByTagName("POINTS").item(0); int p = Integer.parseInt(points.getFirstChild().getNodeValue()) + DPTS; points.getFirstChild().setNodeValue( Integer.toString(p) ); // update record Element record = (Element) e.getElementsByTagName("RECORD").item(0); Attr draws = (Attr) record.getAttributeNode("DRAWS"); draws.setValue(Integer.toString(Integer.parseInt(draws.getValue()) + 1) ); } else if (gls > ags) { // win // update points -> add WPTS for a win Element points = (Element) e.getElementsByTagName("POINTS").item(0); // Element points = (Element) e.getFirstChild(); int p = Integer.parseInt(points.getFirstChild().getNodeValue()) + WPTS; points.getFirstChild().setNodeValue( Integer.toString(p) ); // update record Element record = (Element) e.getElementsByTagName("RECORD").item(0); Attr wins = (Attr) record.getAttributeNode("WINS"); wins.setValue(Integer.toString(Integer.parseInt(wins.getValue()) + 1) ); } else { // lose - no points need to be added // update record Element record = (Element) e.getElementsByTagName("RECORD").item(0); Attr losses = (Attr) record.getAttributeNode("LOSSES"); losses.setValue(Integer.toString(Integer.parseInt(losses.getValue()) + 1) ); } // update goals Element goals = (Element) e.getElementsByTagName("GOALS").item(0); int g = Integer.parseInt(goals.getFirstChild().getNodeValue()) + gls; goals.getFirstChild().setNodeValue( Integer.toString(g) ); Element againsts = (Element) e.getElementsByTagName("AGAINSTS").item(0); int a = Integer.parseInt(againsts.getFirstChild().getNodeValue()) + ags; againsts.getFirstChild().setNodeValue( Integer.toString(a) ); } catch (Exception x) { System.out.println("The specified XML-File seems to be corrupted."); System.exit(1); } } // this method is used in the sort-routine, to identify which of two teams is better private static int comparePositions(Element p1, Element p2) { Element points1 = (Element) p1.getElementsByTagName("POINTS").item(0); int p1Value = Integer.parseInt( points1.getFirstChild().getNodeValue() ); Element points2 = (Element) p2.getElementsByTagName("POINTS").item(0); int p2Value = Integer.parseInt( points1.getFirstChild().getNodeValue() ); if (p1Value - p2Value != 0) { return p2Value - p1Value; } else { // if points are equal look at the goals // calculate goal difference for p1 Element goals1 = (Element) p1.getElementsByTagName("GOALS").item(0); int goalValue1 = Integer.parseInt( goals1.getFirstChild().getNodeValue() ); Element againsts1 = (Element) p1.getElementsByTagName("AGAINSTS").item(0); int againstsValue1 = Integer.parseInt( againsts1.getFirstChild().getNodeValue() ); int diff1 = goalValue1 - againstsValue1; // calculate goal difference for p2 Element goals2 = (Element) p2.getElementsByTagName("GOALS").item(0); int goalValue2 = Integer.parseInt( goals2.getFirstChild().getNodeValue() ); Element againsts2 = (Element) p2.getElementsByTagName("AGAINSTS").item(0); int againstsValue2 = Integer.parseInt( againsts2.getFirstChild().getNodeValue() ); int diff2 = goalValue2 - againstsValue2; // the team with the better goal difference is better return diff2 - diff1; } } /* This method sorts the POSITION Elements of the root element using a selection sort-algorithm. */ public static void sortPositions(Element root) { NodeList positions = root.getElementsByTagName("POSITION"); for (int i=0; i 0) max = cand; } System.out.println(max.getElementsByTagName("TEAM").item(0).getFirstChild().getNodeValue()); // swap the elements if (first != max) { Node n = root.removeChild(max); root.insertBefore(n,first); } } } public static void main (String args[]) { // commandline ok? if (args.length != 1) { System.out.println("Usage: java League1 XML-File"); System.exit(1); } // Parse the data Document doc = null; try { // Get an instance of the Xerces Dom-Parser. // This process is dependent on the currently used DOM Implementation DOMParser p = new DOMParser(); // Datei parsen p.parse(args[0]); doc = p.getDocument(); } catch (Exception e) { System.out.println("Specified file not found. Please enter teams."); try { // let the user input teams doc = createTeams(); } catch (Exception x) { System.out.println("Unrecoverable error occured. Exiting..."); System.exit(1); } } BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); // get root Element Element root = doc.getDocumentElement(); // read in the results of the teams NodeList teams = root.getElementsByTagName("POSITION"); for (int i=0; i0\""); } catch (Exception e) { System.out.println("Unrecoverable error occured. Exiting..."); System.exit(1); } } } // sort the positions, so that the team with the most points is the first child of root sortPositions(root); // The saving of the document to a file is again dependent on the implementation. try { FileOutputStream out = new FileOutputStream(args[0]); OutputFormat format = new OutputFormat(doc); XMLSerializer serializer = new XMLSerializer(out, format); serializer.serialize(root); out.flush(); out.close(); } catch (IOException e) { System.err.println(e); } } }