Advertisement
Guest User

NDEBUG

a guest
Aug 4th, 2009
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.64 KB | None | 0 0
  1. // ****************************************************************************
  2. // **
  3. // ** Mannschaft.java
  4. // ** Description
  5. // **
  6. // ****************************************************************************
  7. package de.fussballmanager.data;
  8.  
  9. // ============================================================================
  10. // IMPLEMENTATION REQUIRED IMPORTS
  11. // ============================================================================
  12. import java.util.Iterator;
  13. import java.util.List;
  14. import java.util.ArrayList;
  15.  
  16. import javax.xml.transform.Transformer;
  17. import javax.xml.transform.TransformerConfigurationException;
  18. import javax.xml.transform.TransformerException;
  19. import javax.xml.transform.TransformerFactory;
  20. import javax.xml.transform.dom.DOMSource;
  21. import javax.xml.transform.stream.StreamResult;
  22. import javax.xml.xpath.XPath;
  23. import javax.xml.xpath.XPathConstants;
  24. import javax.xml.xpath.XPathExpressionException;
  25. import javax.xml.xpath.XPathFactory;
  26.  
  27. import org.w3c.dom.Attr;
  28. import org.w3c.dom.Element;
  29. import org.w3c.dom.NamedNodeMap;
  30. import org.w3c.dom.Node;
  31. import org.w3c.dom.NodeList;
  32.  
  33. // ============================================================================
  34. // IMPLEMENTATION CLASS BODY
  35. // ============================================================================
  36. public class Mannschaft {
  37.     // ========================================================================
  38.     // IMPLEMENTATION PRIVATE CONSTANTS
  39.     // ========================================================================
  40.     // ========================================================================
  41.     // IMPLEMENTATION PUBLIC CONSTANTS
  42.     // ========================================================================
  43.     // ========================================================================
  44.     // IMPLEMENTATION PRIVATE DATA
  45.     // ========================================================================
  46.     // mannschaft specific instance variables
  47.     private String name;
  48.     private String manager;
  49.  
  50.     // all collections that belong to a mannschaft
  51.     private List<Spieler> spielerListe;
  52.  
  53.     // instance variables for easy reading and writing of xml
  54.     private String filename;
  55.     private Node mannschaftNodeXML;
  56.  
  57.     // ========================================================================
  58.     // IMPLEMENTATION PUBLIC DATA
  59.     // ========================================================================
  60.     // ========================================================================
  61.     // IMPLEMENTATION PRIVATE METHODS
  62.     // ========================================================================
  63.     private void parseMannschaftXML(Node mannschaftNodeXML) {
  64.         // store the node that belongs to the mannschaft
  65.         this.mannschaftNodeXML = mannschaftNodeXML;
  66.  
  67.         // an xpath object for easy access to everything in the dom tree
  68.         XPath xpath = XPathFactory.newInstance().newXPath();
  69.  
  70.         // get the attributes of the mannschaft node and extract what we need
  71.         try {
  72.             NamedNodeMap mannschaftAttributes = this.mannschaftNodeXML.getAttributes();
  73.             this.name = mannschaftAttributes.getNamedItem("name").getTextContent();
  74.             this.manager = mannschaftAttributes.getNamedItem("manager").getTextContent();
  75.         } catch (NullPointerException npEx) {
  76.             System.err.println("Error while parsing " + this.filename
  77.                     + " while reading the attributes of a mannschaft.");
  78.             System.err.println("There are missing mannschaft attributes.");
  79.             System.err.println("Required attributes are: \"name\", \"manager\".");
  80.             System.exit(4);
  81.         }
  82.  
  83.         // extract all spieler
  84.         try {
  85.             NodeList spieler = (NodeList) xpath.evaluate(
  86.                     "/liga/mannschaften/mannschaft[@name=\"" + this.name
  87.                             + "\"]/spieler", this.mannschaftNodeXML,
  88.                     XPathConstants.NODESET);
  89.             if (spieler != null && spieler.getLength() > 0) {
  90.                 for (int i = 0; i < spieler.getLength(); i++) {
  91.                     Element spielerNodeXML = (Element) spieler.item(i);
  92.                     Spieler newSpieler = new Spieler(this.filename,
  93.                             spielerNodeXML);
  94.                     this.spielerListe.add(newSpieler);
  95.                 }
  96.             }
  97.         } catch (XPathExpressionException xpeEX) {
  98.             System.err.println("Error while parsing " + this.filename
  99.                     + " while reading the spieler of " + this.name + ".");
  100.             System.err.println(xpeEX.getMessage());
  101.             System.exit(3);
  102.         }
  103.     }
  104.  
  105.     // ========================================================================
  106.     // IMPLEMENTATION PUBLIC METHODS
  107.     // ========================================================================
  108.     public Mannschaft(String filename, Node mannschaftNodeXML) {
  109.         // store the filename to this liga's xml file
  110.         this.filename = filename;
  111.  
  112.         // initializie the collections
  113.         this.spielerListe = new ArrayList<Spieler>();
  114.  
  115.         // parse the node and extract all informations we need
  116.         this.parseMannschaftXML(mannschaftNodeXML);
  117.     }
  118.  
  119.     // getters/setters
  120.     public String getName() {
  121.         return this.name;
  122.     }
  123.  
  124.     public void setName(String name) {
  125.         Attr attribute = (Attr) this.mannschaftNodeXML.getAttributes().getNamedItem(
  126.                 "name");
  127.         attribute.setValue(name);
  128.         this.name = name;
  129.     }
  130.  
  131.     public String getManager() {
  132.         return this.manager;
  133.     }
  134.  
  135.     public void setManager(String manager) {
  136.         Attr attribute = (Attr) this.mannschaftNodeXML.getAttributes().getNamedItem(
  137.                 "manager");
  138.         attribute.setValue(manager);
  139.         this.manager = manager;
  140.     }
  141.  
  142.     // for testing only!
  143.     public void testPrintToScreen() {
  144.         try {
  145.             DOMSource sourceXML = new DOMSource(this.mannschaftNodeXML);
  146.             StreamResult outputDest = new StreamResult(System.out);
  147.             Transformer transformer = TransformerFactory.newInstance().newTransformer();
  148.             transformer.transform(sourceXML, outputDest);
  149.         } catch (TransformerConfigurationException transConEx) {
  150.             System.err.println("\nTransformer Configuration Error while generating XML for Mannschaft.");
  151.             System.err.println(transConEx.getMessage());
  152.  
  153.             Throwable ex = transConEx;
  154.             if (transConEx.getException() != null) {
  155.                 ex = transConEx.getException();
  156.                 System.err.println(ex.getMessage());
  157.             }
  158.         } catch (TransformerException transEx) {
  159.             System.err.println("\nTransformation error while generating XML for Mannschaft.");
  160.             System.err.println(transEx.getMessage());
  161.  
  162.             Throwable ex = transEx;
  163.             if (transEx.getException() != null) {
  164.                 ex = transEx.getException();
  165.                 System.err.println(ex.getMessage());
  166.             }
  167.         }
  168.     }
  169.  
  170.     public void testPrintSpieler() {
  171.         System.out.println("Spieler von " + this.name +":");
  172.         System.out.println("====================================");
  173.         for (Iterator<Spieler> i = this.spielerListe.iterator(); i.hasNext();) {
  174.             System.out.println(i.next().getName());
  175.         }
  176.     }
  177.  
  178.     public List<Spieler> testGetSpielerliste() {
  179.         return this.spielerListe;
  180.     }
  181. }
  182. // ****************************************************************************
  183. // **
  184. // ** Mannschaft.java
  185. // **
  186. // ****************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement