Package javaEffect.xml

Source Code of javaEffect.xml.MissionsReader

package javaEffect.xml;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.zip.DataFormatException;

import javaEffect.ErrEmptyString;
import javaEffect.ErrNegativeNumber;
import javaEffect.ErrNotFountOrMistake;
import javaEffect.Size;

import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import javaEffect.characters.Asari;
import javaEffect.characters.Character;
import javaEffect.characters.Human;
import javaEffect.characters.Krogan;
import javaEffect.planet.*;
import javaEffect.spacecraft.Spacecraft;
import javaEffect.spacecraft.Spaceship;

/**
* This class is used to read the document which contain the missions
* descriptions.
*
* @author Jack OUTRAN & Thibaut LOCQUET
* @version 0.2
*/
public class MissionsReader {

  private org.jdom2.Document document;

  private String fileName;

  static protected ArrayList<Character> characterInMissionList;

  static {
    characterInMissionList = new ArrayList<Character>();
  }

  /**
   * The constructor get the name of the document the user want to open. If
   * the file doesn't exist, it will throw an exception. And it creates the
   * characters define in this file.
   *
   * @param fileName
   *            The name of the file the user wanna open.
   * @throws ErrNotFountOrMistake
   */
  public MissionsReader(String fileName) throws ErrNotFountOrMistake {
    this.fileName = new String(fileName + ".xml");

    SAXBuilder sxb = new SAXBuilder();

    try {
      document = sxb.build(new File(this.fileName));
    } catch (JDOMException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    Element root = document.getRootElement();

    addPlanets(root);

    addCharacters(root);

    addSpacecrafts(root);

    addSpaceship(root);

  }

  private void addSpaceship(Element node) {

    for (Element spaceship : node.getChildren("spaceship")) {

      @SuppressWarnings("unused")
      Spaceship toAdd = null;

      try {
        toAdd = new Spaceship(
            spaceship.getAttributeValue("name"),
            Planet.getPlanet(spaceship.getAttributeValue("planet")),
            Integer.parseInt(spaceship.getAttributeValue("cost")),
            Integer.parseInt(spaceship.getAttributeValue("attack")),
            Integer.parseInt(spaceship.getAttributeValue("armor")),
            Integer.parseInt(spaceship
                .getAttributeValue("shieldEfficiency")),
            Integer.parseInt(spaceship.getAttributeValue("speed")));
      } catch (ErrNotFountOrMistake e) {
        e.printStackTrace();
      } catch (NumberFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (DataFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (ErrNegativeNumber e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

    }

  }

  private void addSpacecrafts(Element node) {

    for (Element spacecraft : node.getChildren("spacecraft")) {
      @SuppressWarnings("unused")
      Spacecraft toAdd = null;

      try {
        toAdd = new Spacecraft(
            spacecraft.getAttributeValue("name"),
            Planet.getPlanet(spacecraft.getAttributeValue("planet")),
            Integer.parseInt(spacecraft.getAttributeValue("cost")),
            Integer.parseInt(spacecraft.getAttributeValue("armor")),
            Integer.parseInt(spacecraft.getAttributeValue("speed")));
      } catch (ErrNotFountOrMistake e) {
        e.printStackTrace();
      } catch (NumberFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (DataFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (ErrNegativeNumber e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

  }

  private void addPlanets(Element node) {

    for (Element planet : node.getChildren("planet")) {
      @SuppressWarnings("unused")
      Planet toAdd = null;

      try {
        toAdd = new Planet(planet.getAttributeValue("name"),
            Size.valueOf(planet.getAttributeValue("size")
                .toUpperCase()));
      } catch (ErrEmptyString e) {
        e.printStackTrace();
      }
    }

  }

  private ArrayList<Character> addCharacters(Element node)
      throws ErrNotFountOrMistake {

    ArrayList<Character> list = new ArrayList<Character>();

    for (Element character : node.getChildren("character")) {

      Character npg = null;

      if (!Character.isInList(character.getAttributeValue("firstName"),
          character.getAttributeValue("lastName"))) {

        if (character.getAttributeValue("race").equalsIgnoreCase(
            "Human")) {
          npg = new Human(character.getAttributeValue("firstName"),
              character.getAttributeValue("lastName"));
        } else if (character.getAttributeValue("race")
            .equalsIgnoreCase("Asari")) {
          npg = new Asari(character.getAttributeValue("firstName"),
              character.getAttributeValue("lastName"));
        } else if (character.getAttributeValue("race")
            .equalsIgnoreCase("Krogan")) {
          npg = new Krogan(character.getAttributeValue("firstName"),
              character.getAttributeValue("lastName"));
        } else {
          throw new ErrNotFountOrMistake("The race of the character "
              + character.getAttributeValue("firstName") + " "
              + character.getAttributeValue("lastName")
              + " is undefined or misdefined in the xml file.");
        }

        if (npg != null) {
          try {
            Planet.getPlanet(character.getAttributeValue("planet"))
                .addCharacter(npg);
          } catch (DataFormatException e) {
            e.printStackTrace();
          }
        }

      } else {

        if (Character
            .getCharacter(character.getAttributeValue("firstName"),
                character.getAttributeValue("lastName"))
            .getRace()
            .equalsIgnoreCase(character.getAttributeValue("race"))) {
          npg = Character.getCharacter(
              character.getAttributeValue("firstName"),
              character.getAttributeValue("lastName"));
        } else {
          throw new ErrNotFountOrMistake("The race " +  character.getAttributeValue("race") + " of the character "
              + character.getAttributeValue("firstName") + " "
              + character.getAttributeValue("lastName")
              + " is undefined or misdefined in the xml file.");
        }

      }

      for(Element text : character.getChildren("text")){
        npg.setText(text.getText());
      }
     
      list.add(npg);
    }
    return list;

  }

  /**
   * Return to the user the title of the mission of the id given in parameter.
   *
   * @param id
   *            The id of the mission title to return.
   * @return The string which contain the mission title.
   * @throws ErrNotFountOrMistake
   *
   * @throws FileNotFoundException
   *             If the file doesn't exist.
   */
  public String getMissionTitle(int id) throws ErrNotFountOrMistake {

    Element root = document.getRootElement();
    for (Element target : root.getChildren("mission")) {
      if (target.getAttributeValue("id").equals(id + "")) {
        return target.getChild("title").getText();
      }
    }

    throw new ErrNotFountOrMistake("Mission " + id + " not found");
  }

  /**
   * Return to the user the title of the mission of the id is done in
   * parameter.
   *
   * @param id
   *            The id of the mission to return.
   * @return The string which contain the mission text.
   * @throws ErrNotFountOrMistake
   *
   * @throws FileNotFoundException
   *             If the file doesn't exist.
   */
  public String getMissionText(int id) throws ErrNotFountOrMistake {
    Element root = document.getRootElement();
    for (Element target : root.getChildren("mission")) {
      if (target.getAttributeValue("id").equals(id + "")) {
        return target.getChild("text").getText();
      }
    }

    throw new ErrNotFountOrMistake("Mission " + id + " not found");
  }

  public Character startMission(int id) throws ErrNotFountOrMistake {

    boolean isPresent = false;
    Character target = null;

    Element root = document.getRootElement();
    for (Element node : root.getChildren("mission")) {

      if (node.getAttributeValue("id").equals(id + "")) {

        characterInMissionList = addCharacters(node);
        addSpaceship(node);

        isPresent = true;

        target = Character.getCharacter(
            node.getChild("reward").getChild("target")
                .getChild("firstName").getValue(),
            node.getChild("reward").getChild("target")
                .getChild("lastName").getValue());

      }
    }

    if (!isPresent) {
      return null;
    }

    return target;
  }

  public void endMission() {
    for (Planet p : Planet.planetList) {
      p.removeInList(characterInMissionList);
    }

    Character.removeInList(characterInMissionList);
  }

  /**
   *
   * @param firstName
   *            The name of the character you want to speak.
   * @param textId
   *            The id of the text to say.
   * @param missionId
   *            The id of the mission where is the main character.
   * @return The text that the character want to say.
   * @throws ErrNotFountOrMistake
   */
  public String getTextFromCharacter(String firstName, String lastName,
      int textId, int missionId) throws ErrNotFountOrMistake {
    SAXBuilder sxb = new SAXBuilder();

    try {
      document = sxb.build(new File(fileName));
    } catch (JDOMException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    Element root = document.getRootElement();

    for (Element target : root.getChildren("mission")) {

      if (target.getAttributeValue("id").equals(missionId + "")) {

        for (Element character : target.getChildren("character")) {

          if (character.getAttributeValue("firstName")
              .equalsIgnoreCase(firstName)
              && character.getAttributeValue("lastName")
                  .equalsIgnoreCase(lastName)) {

            for (Element text : character.getChildren("text")) {

              if (text.getAttributeValue("id")
                  .equals(textId + "")) {
                return text.getText();
              }
            }
          }
        }
      }
    }

    throw new ErrNotFountOrMistake(firstName + " " + lastName
        + " have no text ID n�" + textId + " to say");
  }
}
TOP

Related Classes of javaEffect.xml.MissionsReader

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.