Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ****************************************************************************
- // **
- // ** Mannschaft.java
- // ** Description
- // **
- // ****************************************************************************
- package de.fussballmanager.data;
- // ============================================================================
- // IMPLEMENTATION REQUIRED IMPORTS
- // ============================================================================
- import java.util.Iterator;
- import java.util.List;
- import java.util.ArrayList;
- import javax.xml.transform.Transformer;
- import javax.xml.transform.TransformerConfigurationException;
- import javax.xml.transform.TransformerException;
- import javax.xml.transform.TransformerFactory;
- import javax.xml.transform.dom.DOMSource;
- import javax.xml.transform.stream.StreamResult;
- import javax.xml.xpath.XPath;
- import javax.xml.xpath.XPathConstants;
- import javax.xml.xpath.XPathExpressionException;
- import javax.xml.xpath.XPathFactory;
- import org.w3c.dom.Attr;
- import org.w3c.dom.Element;
- import org.w3c.dom.NamedNodeMap;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- // ============================================================================
- // IMPLEMENTATION CLASS BODY
- // ============================================================================
- public class Mannschaft {
- // ========================================================================
- // IMPLEMENTATION PRIVATE CONSTANTS
- // ========================================================================
- // ========================================================================
- // IMPLEMENTATION PUBLIC CONSTANTS
- // ========================================================================
- // ========================================================================
- // IMPLEMENTATION PRIVATE DATA
- // ========================================================================
- // mannschaft specific instance variables
- private String name;
- private String manager;
- // all collections that belong to a mannschaft
- private List<Spieler> spielerListe;
- // instance variables for easy reading and writing of xml
- private String filename;
- private Node mannschaftNodeXML;
- // ========================================================================
- // IMPLEMENTATION PUBLIC DATA
- // ========================================================================
- // ========================================================================
- // IMPLEMENTATION PRIVATE METHODS
- // ========================================================================
- private void parseMannschaftXML(Node mannschaftNodeXML) {
- // store the node that belongs to the mannschaft
- this.mannschaftNodeXML = mannschaftNodeXML;
- // an xpath object for easy access to everything in the dom tree
- XPath xpath = XPathFactory.newInstance().newXPath();
- // get the attributes of the mannschaft node and extract what we need
- try {
- NamedNodeMap mannschaftAttributes = this.mannschaftNodeXML.getAttributes();
- this.name = mannschaftAttributes.getNamedItem("name").getTextContent();
- this.manager = mannschaftAttributes.getNamedItem("manager").getTextContent();
- } catch (NullPointerException npEx) {
- System.err.println("Error while parsing " + this.filename
- + " while reading the attributes of a mannschaft.");
- System.err.println("There are missing mannschaft attributes.");
- System.err.println("Required attributes are: \"name\", \"manager\".");
- System.exit(4);
- }
- // extract all spieler
- try {
- NodeList spieler = (NodeList) xpath.evaluate(
- "/liga/mannschaften/mannschaft[@name=\"" + this.name
- + "\"]/spieler", this.mannschaftNodeXML,
- XPathConstants.NODESET);
- if (spieler != null && spieler.getLength() > 0) {
- for (int i = 0; i < spieler.getLength(); i++) {
- Element spielerNodeXML = (Element) spieler.item(i);
- Spieler newSpieler = new Spieler(this.filename,
- spielerNodeXML);
- this.spielerListe.add(newSpieler);
- }
- }
- } catch (XPathExpressionException xpeEX) {
- System.err.println("Error while parsing " + this.filename
- + " while reading the spieler of " + this.name + ".");
- System.err.println(xpeEX.getMessage());
- System.exit(3);
- }
- }
- // ========================================================================
- // IMPLEMENTATION PUBLIC METHODS
- // ========================================================================
- public Mannschaft(String filename, Node mannschaftNodeXML) {
- // store the filename to this liga's xml file
- this.filename = filename;
- // initializie the collections
- this.spielerListe = new ArrayList<Spieler>();
- // parse the node and extract all informations we need
- this.parseMannschaftXML(mannschaftNodeXML);
- }
- // getters/setters
- public String getName() {
- return this.name;
- }
- public void setName(String name) {
- Attr attribute = (Attr) this.mannschaftNodeXML.getAttributes().getNamedItem(
- "name");
- attribute.setValue(name);
- this.name = name;
- }
- public String getManager() {
- return this.manager;
- }
- public void setManager(String manager) {
- Attr attribute = (Attr) this.mannschaftNodeXML.getAttributes().getNamedItem(
- "manager");
- attribute.setValue(manager);
- this.manager = manager;
- }
- // for testing only!
- public void testPrintToScreen() {
- try {
- DOMSource sourceXML = new DOMSource(this.mannschaftNodeXML);
- StreamResult outputDest = new StreamResult(System.out);
- Transformer transformer = TransformerFactory.newInstance().newTransformer();
- transformer.transform(sourceXML, outputDest);
- } catch (TransformerConfigurationException transConEx) {
- System.err.println("\nTransformer Configuration Error while generating XML for Mannschaft.");
- System.err.println(transConEx.getMessage());
- Throwable ex = transConEx;
- if (transConEx.getException() != null) {
- ex = transConEx.getException();
- System.err.println(ex.getMessage());
- }
- } catch (TransformerException transEx) {
- System.err.println("\nTransformation error while generating XML for Mannschaft.");
- System.err.println(transEx.getMessage());
- Throwable ex = transEx;
- if (transEx.getException() != null) {
- ex = transEx.getException();
- System.err.println(ex.getMessage());
- }
- }
- }
- public void testPrintSpieler() {
- System.out.println("Spieler von " + this.name +":");
- System.out.println("====================================");
- for (Iterator<Spieler> i = this.spielerListe.iterator(); i.hasNext();) {
- System.out.println(i.next().getName());
- }
- }
- public List<Spieler> testGetSpielerliste() {
- return this.spielerListe;
- }
- }
- // ****************************************************************************
- // **
- // ** Mannschaft.java
- // **
- // ****************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement