/**
   * Transforms the given XML file using the specified XSL file.
   *
   * @param origXmlFile The file containg the XML to transform
   * @param xslString The XML Stylesheet conntaining the transform instructions
   * @return String representation of the transformation
   */
  public static String transform(File origXmlFile, String xslString) {
    if (!origXmlFile.exists()) {
      GuiUtils.showErrorMessage(
          logger, "Warning, XML file: " + origXmlFile + " doesn't exist", null, null);
      return null;
    }

    try {

      TransformerFactory tFactory = TransformerFactory.newInstance();

      StreamSource xslFileSource = new StreamSource(new StringReader(xslString));

      Transformer transformer = tFactory.newTransformer(xslFileSource);

      StringWriter writer = new StringWriter();

      transformer.transform(new StreamSource(origXmlFile), new StreamResult(writer));

      return writer.toString();
    } catch (Exception e) {
      GuiUtils.showErrorMessage(logger, "Error when loading the XML file: " + origXmlFile, e, null);
      return null;
    }
  }
  /**
   * Transforms the given XML string using the XSL string.
   *
   * @param xmlString The String containg the XML to transform
   * @param xslString The XML Stylesheet String containing the transform instructions
   * @return String representation of the transformation
   */
  public static String transform(String xmlString, String xslString) {

    String shortString = new String(xmlString);
    if (shortString.length() > 100) shortString = shortString.substring(0, 100) + "...";

    try {

      TransformerFactory tFactory = TransformerFactory.newInstance();

      logger.logComment("Transforming string: " + shortString);

      StreamSource xslFileSource = new StreamSource(new StringReader(xslString));

      Transformer transformer = tFactory.newTransformer(xslFileSource);

      StringWriter writer = new StringWriter();

      transformer.transform(
          new StreamSource(new StringReader(xmlString)), new StreamResult(writer));

      String shortResult = writer.toString();
      if (shortResult.length() > 100) shortResult = shortResult.substring(0, 100) + "...";

      logger.logComment("Result: " + shortResult);

      return writer.toString();
    } catch (TransformerException e) {
      GuiUtils.showErrorMessage(logger, "Error when transforming the XML: " + shortString, e, null);
      return null;
    }
  }
  /**
   * Transforms the given XML string using the specified XSL file.
   *
   * @param xmlString The String containg the XML to transform
   * @param xslFile The XML Stylesheet conntaining the transform instructions
   * @return String representation of the transformation
   */
  public static String transform(String xmlString, File xslFile) {

    if (!xslFile.exists()) {
      GuiUtils.showErrorMessage(
          logger, "Warning, XSL file: " + xslFile + " doesn't exist", null, null);
      return null;
    }

    String shortString = new String(xmlString);
    if (shortString.length() > 100) shortString = shortString.substring(0, 100) + "...";

    try {
      logger.logComment("The xslFile is " + xslFile.getAbsolutePath() + " *************");

      TransformerFactory tFactory = TransformerFactory.newInstance();

      logger.logComment("Transforming string: " + shortString);

      StreamSource xslFileSource = new StreamSource(xslFile);

      Transformer transformer = tFactory.newTransformer(xslFileSource);

      StringWriter writer = new StringWriter();

      transformer.transform(
          new StreamSource(new StringReader(xmlString)), new StreamResult(writer));

      String shortResult = writer.toString();
      if (shortResult.length() > 100) shortResult = shortResult.substring(0, 100) + "...";

      logger.logComment("Result: " + shortResult);

      return writer.toString();
    } catch (TransformerException e) {
      GuiUtils.showErrorMessage(logger, "Error when transforming the XML: " + shortString, e, null);
      return null;
    }
  }
  // Gets the specified XML Schema doc if one is mentioned in the file
  public static File getSchemaFile(File xmlDoc) {
    /** @todo Must be an easier way of doing this... */
    logger.logComment("Getting schema file for: " + xmlDoc);
    try {
      // File xslFile = null;
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

      DocumentBuilder db = dbf.newDocumentBuilder();

      Document doc = db.parse(xmlDoc);

      return getSchemaFile(doc);
    } catch (Exception e) {
      GuiUtils.showErrorMessage(logger, "Error when looking at the XML file: " + xmlDoc, e, null);
      return null;
    }
  }
  public static boolean transform(
      File origXmlFileOrDir, File xslFile, File targetDir, String extension) {
    logger.logComment(
        "Going to transform " + origXmlFileOrDir + " into dir " + targetDir + " using: " + xslFile,
        true);

    if (!origXmlFileOrDir.exists()) {
      GuiUtils.showErrorMessage(
          logger,
          "Warning, XML file/directory: " + origXmlFileOrDir + " doesn't exist",
          null,
          null);
      return false;
    }

    if (!xslFile.exists()) {
      GuiUtils.showErrorMessage(
          logger, "Warning, XSL file: " + xslFile + " doesn't exist", null, null);
      return false;
    }

    if (!targetDir.exists()) {
      GuiUtils.showErrorMessage(
          logger, "Warning, target directory: " + targetDir + " doesn't exist", null, null);
      return false;
    }

    if (origXmlFileOrDir.isDirectory()) {
      logger.logComment("That file is a directory. Converting all of the XML files in it");
      File[] files = origXmlFileOrDir.listFiles();

      boolean totalSuccess = true;
      for (int i = 0; i < files.length; i++) {
        if (!files[i].isDirectory()
            && (files[i].getName().endsWith(".xml") || files[i].getName().endsWith(".XML"))) {
          boolean partialSuccess = transform(files[i], xslFile, targetDir, extension);

          totalSuccess = totalSuccess || partialSuccess;
        } else if (files[i].isDirectory() && !GeneralUtils.isVersionControlDir(files[i])) {
          File newFolder = new File(targetDir, files[i].getName());
          newFolder.mkdir();

          logger.logComment(
              "Found a sub folder. Going to convert all there into: " + newFolder + "...");

          transform(files[i], xslFile, newFolder, extension);
        }
      }
      return totalSuccess;
    }

    String result = transform(origXmlFileOrDir, xslFile);

    String newName = origXmlFileOrDir.getName();

    if (newName.endsWith(".xml") || newName.endsWith(".XML")) {
      newName = newName.substring(0, newName.length() - 4) + extension;
    }
    File targetFile = new File(targetDir, newName);

    try {
      FileWriter fw = new FileWriter(targetFile);
      fw.write(result);
      fw.close();
    } catch (IOException ex) {
      GuiUtils.showErrorMessage(logger, "Exception writing to file: " + targetFile, ex, null);
      return false;
    }

    logger.logComment("The result is in " + targetFile + " *************");

    return result != null;
  }