Package javaEffect.xml

Source Code of javaEffect.xml.MissionsWriter

package javaEffect.xml;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.Text;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

/**
* This class is used to write the
* document which contain the missions descriptions.
*
* @author Jack OUTRAN & Thibaut LOCQUET
* @version 0.1
*/
public class MissionsWriter {
  private ArrayList<String> missionsName;
  private ArrayList<String> missions;

  private Document xml;

  private String fileName;

  /**
   * The constructor get the name of the document the user
   * want to create.
   * If the file doesn't exist, it will be created,
   * and if it exist, it will be overwrite.
   *
   * @param fileName The name of the file the user wanna open.
   */
  public MissionsWriter(String fileName) {
    missionsName = new ArrayList<String>();
    missions = new ArrayList<String>();

    xml = new Document();

    this.fileName = new String(fileName + ".xml");
  }

  /**
   * Add a mission to the tree of the xml document.
   *
   * @param missionName The name of the mission.
   * @param mission The mission text.
   */
  public void addMission(String missionName, String mission) {
    this.missionsName.add(missionName);
    this.missions.add(mission);
  }

  /**
   * Create the tree of the xml file with all the data
   * entered with addMission method.
   *
   * @see MissionsWriter#addMission(String, String)
   */
  public void creatTree() {
    Element game = new Element("game");

    for (int i = 0; i < missions.size(); i++) {
      Element mission = new Element("mission");
      mission.setAttribute("id", i + "");

      Element title = new Element("title");
      title.addContent(new Text(this.missionsName.get(i)));
      mission.addContent(title);

      Element text = new Element("text");
      text.addContent(new Text(this.missions.get(i)));
      mission.addContent(text);

      game.addContent(mission);
    }

    xml.addContent(game);
  }

  /**
   * Write the tree create by creatTree in the file.
   *
   * @see MissionsWriter#creatTree()
   */
  public void writeDocument() {
    XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
    try {
      xout.output(xml, new FileOutputStream(fileName));
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}
TOP

Related Classes of javaEffect.xml.MissionsWriter

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.