示例#1
0
  public static void load() {
    File dir = Central.getPluginInstance().getDataFolder();
    if (!dir.exists()) dir.mkdir();

    File config =
        new File(
            Central.getPluginInstance().getDataFolder(),
            Central.getConfigManager().getConfigFile().filename);
    if (!config.exists()) {
      return;
    }
    Document doc = null;
    try {
      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
      doc = dBuilder.parse(config.toURI().getPath());
      doc.getDocumentElement().normalize();

      NodeList list;

      for (Object configLine :
          Central.getConfigManager().getConfigFile().ConfigSettings.keySet().toArray()) {
        String configKey = (String) configLine;

        list = doc.getElementsByTagName(configKey);

        try {
          String value = list.item(0).getChildNodes().item(0).getNodeValue();
          Central.getConfigManager().getConfigFile().ConfigSettings.put(configKey, value);
        } catch (Exception ex) {

        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
示例#2
0
  public static void save() {
    File dir = Central.getPluginInstance().getDataFolder();
    if (!dir.exists()) dir.mkdir();

    File configuration =
        new File(
            Central.getPluginInstance().getDataFolder(),
            Central.getConfigManager().getConfigFile().filename);
    // test if filename contains ".xml", if not, freak out a little

    Element setting = null;

    try {
      DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder docBuilder;
      docBuilder = docFactory.newDocumentBuilder();
      Document doc = docBuilder.newDocument();
      doc.setXmlStandalone(true);
      Element rootElement = doc.createElement("MoveCraft-Configuration");
      doc.appendChild(rootElement);

      for (Object configLine :
          Central.getConfigManager().getConfigFile().ConfigSettings.keySet().toArray()) {
        String configKey = (String) configLine;
        setting = doc.createElement(configKey);
        setting.appendChild(
            doc.createTextNode(
                Central.getConfigManager().getConfigFile().ConfigSettings.get(configKey)));
        rootElement.appendChild(setting);

        if (Central.getConfigManager().getConfigFile().ConfigComments.containsKey(configKey)) {
          Comment comment =
              doc.createComment(
                  Central.getConfigManager().getConfigFile().ConfigComments.get(configKey));
          rootElement.insertBefore(comment, setting);
        }
      }

      Comment comment =
          doc.createComment(
              "Do you find this config file strange and confusing? "
                  + "This guy made this nifty GUI editor for it. Get it here: http://bit.ly/ewPebA");
      rootElement.appendChild(comment);

      TransformerFactory transformerFactory = TransformerFactory.newInstance();
      Transformer transformer;
      try {
        transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(configuration);
        transformer.transform(source, result);
      } catch (TransformerConfigurationException e) {
        e.printStackTrace();
      } catch (TransformerException e) {
        e.printStackTrace();
      }
    } catch (ParserConfigurationException e1) {
      e1.printStackTrace();
    }
  }