Ejemplo n.º 1
0
  public static boolean saveXml(String file, Document doc) {
    if (doc == null) {
      return false;
    }
    File f = new File(file);
    BukkitLogger.info("Saving data into file: " + f.getAbsolutePath());
    try {
      TransformerFactory transformerFactory = TransformerFactory.newInstance();
      Transformer transformer = transformerFactory.newTransformer();

      transformer.setOutputProperty(OutputKeys.INDENT, "yes");
      transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

      // doc.setXmlStandalone(true);

      if (PluginInfo.settingsXml.xslt != null && !PluginInfo.settingsXml.xslt.isEmpty()) {
        ProcessingInstruction pi =
            doc.createProcessingInstruction(
                "xml-stylesheet", "type=\"text/xsl\" href=\"" + PluginInfo.settingsXml.xslt + "\"");
        doc.insertBefore(pi, doc.getDocumentElement());
      }

      DOMSource source = new DOMSource(doc);
      StreamResult result = new StreamResult(f);
      transformer.transform(source, result);
      return true;
    } catch (TransformerFactoryConfigurationError ex) {
      BukkitLogger.warning(
          "Transform Configuration Exception while saving XML file "
              + file
              + ": "
              + ex.getMessage());
    } catch (TransformerException ex) {
      BukkitLogger.warning(
          "Transform Exception while saving XML file " + file + ": " + ex.getMessage());
    } catch (Exception ex) {
      BukkitLogger.warning(
          "Unknown Exception while saving XML file " + file + ": " + ex.getMessage());
    }
    return false;
  }
Ejemplo n.º 2
0
  public static boolean save(String file, InputStream in) {
    File f = new File(file);
    BukkitLogger.info("Saving data into file: " + f.getAbsolutePath());
    try {
      FileOutputStream fos = null;
      BufferedOutputStream out = null;

      try {
        if (f.exists()) {
          f.delete();
          f.createNewFile();
        } else {
          f.createNewFile();
        }
      } catch (IOException ex) {
        BukkitLogger.warning("Could not create file " + file + ": " + ex.getMessage());
      }

      try {
        fos = new FileOutputStream(file);
        out = new BufferedOutputStream(fos);
      } catch (FileNotFoundException ex) {
        BukkitLogger.warning("File not found (" + file + "): " + ex.getMessage());
      }

      try {
        while (in.available() > 0) {
          out.write(in.read());
        }
        out.flush();
        return true;
      } catch (IOException ex) {
        BukkitLogger.warning("File writing error (" + file + "): " + ex.getMessage());
      }
    } catch (Exception ex) {
      BukkitLogger.warning("Unknown Exception while saving file " + file + ": " + ex.getMessage());
    }
    return false;
  }
Ejemplo n.º 3
0
  public static InputStream load(String file) {
    FileInputStream fis = null;
    BufferedInputStream in = null;

    try {
      fis = new FileInputStream(file);
      in = new BufferedInputStream(fis);
    } catch (FileNotFoundException ex) {
      BukkitLogger.warning("Could not read file " + file + ": " + ex.getMessage());
    }

    return in;
  }
Ejemplo n.º 4
0
 public static String loadAsString(String file) {
   BufferedInputStream in = (BufferedInputStream) load(file);
   BufferedReader reader = new BufferedReader(new InputStreamReader(in));
   StringBuilder string = new StringBuilder();
   try {
     String s = null;
     while ((s = reader.readLine()) != null) {
       string.append(s);
     }
   } catch (IOException ex) {
     BukkitLogger.warning("Could not read file " + file + ": " + ex.getMessage());
   }
   return string.toString();
 }