Example #1
0
  public static boolean saveTombstones(List<Tombstone> deathChests, String chestsPath) {
    long timestamp = System.currentTimeMillis();
    try {
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();

      // Create the writer
      Document xmlDoc = builder.newDocument();

      Element rootNode = xmlDoc.createElement("deathchestList");
      //	        String pluginName = Settings.getPluginName();
      String version = Settings.getVersion();
      rootNode.setAttribute("version", version);
      xmlDoc.appendChild(rootNode);

      for (Tombstone tombstone : deathChests) {
        rootNode.appendChild(
            tombstone.createXmlNode(
                xmlDoc.createElement(Utils.DEATHCHEST_XML_TAG), xmlDoc, timestamp));
      }

      // Put the XML file into domSource
      DOMSource domSource = new DOMSource(xmlDoc);

      // PrintStream will be responsible for writing
      // the text data to the file
      PrintStream ps = new PrintStream(chestsPath);
      StreamResult fileWriter = new StreamResult(ps);

      TransformerFactory transformerFactory = TransformerFactory.newInstance();
      Transformer transformer = transformerFactory.newTransformer();

      // Finally save the file
      transformer.transform(domSource, fileWriter);

      return true;
    } catch (ParserConfigurationException | TransformerException | FileNotFoundException ex) {
      System.err.println("Error while writing deathchest data:");
      ex.printStackTrace();
    }
    return false;
  }
Example #2
0
  public static List<Tombstone> loadTombstone(String chestsPath, DeathChests plugin) {
    LinkedList<Tombstone> deathChests = new LinkedList<>();
    try {
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();

      // Start the parser
      Document doc = builder.parse(new File(chestsPath));

      Element rootElement = doc.getDocumentElement();

      String version = rootElement.getAttribute("version");
      if (!Settings.getVersion().equals(version)) {
        System.out.println(
            "The version of the Deathchests-File changed. There are possible errors at loading them. (Probably not if your updated it)");
      }

      long timestamp = System.currentTimeMillis();

      NodeList list = rootElement.getChildNodes();
      int length = list.getLength();
      for (int i = 0; i < length; i++) {
        try {
          Node node = list.item(i);
          String name = node.getNodeName();
          if (name.equalsIgnoreCase(Utils.DEATHCHEST_XML_TAG)) {
            deathChests.add(new Tombstone((Element) node, timestamp, plugin));
          }
        } catch (XMLParseException ex) {
          System.err.println("Corrupted deathchest! Skipping this one!");
          ex.printStackTrace();
        }
      }
    } catch (IOException | ParserConfigurationException | SAXException ex) {
      System.err.println("Error while loading deathchest data:");
      ex.printStackTrace();
    }
    return deathChests;
  }