public Repository connectRepository(
      RepositoriesMeta repositoriesMeta, String repositoryName, String username, String password)
      throws KettleException {
    RepositoryMeta repositoryMeta = repositoriesMeta.findRepository(repositoryName);
    if (repositoryMeta == null) {
      log.logBasic("I couldn't find the repository with name '" + repositoryName + "'");
      return null;
    }

    Repository rep =
        PluginRegistry.getInstance()
            .loadClass(RepositoryPluginType.class, repositoryMeta, Repository.class);
    rep.init(repositoryMeta);

    try {
      rep.connect(username, password);
      log.logBasic("Connected to " + repositoryName + " as " + username);
      setRepository(rep);
      return rep;
    } catch (Exception e) {
      log.logBasic("Unable to connect to the repository with name '" + repositoryName + "'");
      return null;
    }
  }
  public void getUsedArguments(
      JobMeta jobMeta, String[] commandLineArguments, IMetaStore metaStore) {

    for (JobEntryCopy jobEntryCopy : jobMeta.jobcopies) {
      if (jobEntryCopy.isTransformation()) {
        JobEntryTrans jobEntryTrans = (JobEntryTrans) jobEntryCopy.getEntry();
        try {
          TransMeta transMeta = jobEntryTrans.getTransMeta(repository, metaStore, jobMeta);
          Map<String, String> map = transMeta.getUsedArguments(commandLineArguments);
          for (String key : map.keySet()) {
            String value = map.get(key);
            if (!arguments.containsKey(key)) {
              arguments.put(key, value);
            }
          }
        } catch (KettleException ke) {
          log.logBasic(ke.getMessage(), ke);
        }
      }
    }
  }
  public boolean validate() {

    boolean retval = false;

    FileObject xmlfile = null;
    FileObject DTDfile = null;

    ByteArrayInputStream ba = null;
    try {
      if (xmlfilename != null
          && ((getDTDFilename() != null && !isInternDTD()) || (isInternDTD()))) {
        xmlfile = KettleVFS.getFileObject(getXMLFilename());

        if (xmlfile.exists()) {

          URL xmlFile = new File(KettleVFS.getFilename(xmlfile)).toURI().toURL();
          StringBuffer xmlStringbuffer = new StringBuffer("");

          BufferedReader xmlBufferedReader = null;
          InputStreamReader is = null;
          try {
            // open XML File
            is = new InputStreamReader(xmlFile.openStream());
            xmlBufferedReader = new BufferedReader(is);

            char[] buffertXML = new char[1024];
            int LenXML = -1;
            while ((LenXML = xmlBufferedReader.read(buffertXML)) != -1) {
              xmlStringbuffer.append(buffertXML, 0, LenXML);
            }
          } finally {
            if (is != null) {
              is.close();
            }
            if (xmlBufferedReader != null) {
              xmlBufferedReader.close();
            }
          }

          // Prepare parsing ...
          DocumentBuilderFactory DocBuilderFactory = DocumentBuilderFactory.newInstance();
          DocumentBuilder DocBuilder = DocBuilderFactory.newDocumentBuilder();

          // Let's try to get XML document encoding

          DocBuilderFactory.setValidating(false);
          ba = new ByteArrayInputStream(xmlStringbuffer.toString().getBytes("UTF-8"));
          Document xmlDocDTD = DocBuilder.parse(ba);
          if (ba != null) {
            ba.close();
          }

          String encoding = null;
          if (xmlDocDTD.getXmlEncoding() == null) {
            encoding = "UTF-8";
          } else {
            encoding = xmlDocDTD.getXmlEncoding();
          }

          int xmlStartDTD = xmlStringbuffer.indexOf("<!DOCTYPE");

          if (isInternDTD()) {
            // DTD find in the XML document
            if (xmlStartDTD != -1) {
              log.logBasic(
                  BaseMessages.getString(
                      PKG, "JobEntryDTDValidator.ERRORDTDFound.Label", getXMLFilename()));
            } else {
              setErrorMessage(
                  BaseMessages.getString(
                      PKG, "JobEntryDTDValidator.ERRORDTDNotFound.Label", getXMLFilename()));
            }

          } else {
            // DTD in external document
            // If we find an intern declaration, we remove it
            DTDfile = KettleVFS.getFileObject(getDTDFilename());

            if (DTDfile.exists()) {
              if (xmlStartDTD != -1) {
                int EndDTD = xmlStringbuffer.indexOf(">", xmlStartDTD);
                // String DocTypeDTD = xmlStringbuffer.substring(xmlStartDTD, EndDTD + 1);
                xmlStringbuffer.replace(xmlStartDTD, EndDTD + 1, "");
              }

              String xmlRootnodeDTD = xmlDocDTD.getDocumentElement().getNodeName();

              String RefDTD =
                  "<?xml version='"
                      + xmlDocDTD.getXmlVersion()
                      + "' encoding='"
                      + encoding
                      + "'?>\n<!DOCTYPE "
                      + xmlRootnodeDTD
                      + " SYSTEM '"
                      + KettleVFS.getFilename(DTDfile)
                      + "'>\n";

              int xmloffsetDTD = xmlStringbuffer.indexOf("<" + xmlRootnodeDTD);
              xmlStringbuffer.replace(0, xmloffsetDTD, RefDTD);
            } else {
              log.logError(
                  BaseMessages.getString(PKG, "JobEntryDTDValidator.ERRORDTDFileNotExists.Subject"),
                  BaseMessages.getString(
                      PKG, "JobEntryDTDValidator.ERRORDTDFileNotExists.Msg", getDTDFilename()));
            }
          }

          if (!(isInternDTD() && xmlStartDTD == -1 || (!isInternDTD() && !DTDfile.exists()))) {

            // Let's parse now ...
            MyErrorHandler error = new MyErrorHandler();
            DocBuilderFactory.setValidating(true);
            DocBuilder = DocBuilderFactory.newDocumentBuilder();
            DocBuilder.setErrorHandler(error);

            ba = new ByteArrayInputStream(xmlStringbuffer.toString().getBytes(encoding));
            xmlDocDTD = DocBuilder.parse(ba);

            if (error.errorMessage == null) {
              log.logBasic(
                  BaseMessages.getString(PKG, "JobEntryDTDValidator.DTDValidatorOK.Subject"),
                  BaseMessages.getString(
                      PKG, "JobEntryDTDValidator.DTDValidatorOK.Label", getXMLFilename()));

              // Everything is OK
              retval = true;
            } else {
              // Invalid DTD
              setNrErrors(error.nrErrors);
              setErrorMessage(
                  BaseMessages.getString(
                      PKG,
                      "JobEntryDTDValidator.DTDValidatorKO",
                      getXMLFilename(),
                      error.nrErrors,
                      error.errorMessage));
            }
          }

        } else {
          if (!xmlfile.exists()) {
            setErrorMessage(
                BaseMessages.getString(
                    PKG, "JobEntryDTDValidator.FileDoesNotExist.Label", getXMLFilename()));
          }
        }
      } else {
        setErrorMessage(BaseMessages.getString(PKG, "JobEntryDTDValidator.AllFilesNotNull.Label"));
      }
    } catch (Exception e) {
      setErrorMessage(
          BaseMessages.getString(
              PKG,
              "JobEntryDTDValidator.ErrorDTDValidator.Label",
              getXMLFilename(),
              getDTDFilename(),
              e.getMessage()));
    } finally {
      try {
        if (xmlfile != null) {
          xmlfile.close();
        }
        if (DTDfile != null) {
          DTDfile.close();
        }
        if (ba != null) {
          ba.close();
        }
      } catch (IOException e) {
        // Ignore close errors
      }
    }
    return retval;
  }
 @Override
 public void setLabel(String labelText) {
   log.logBasic(labelText);
 }
 @Override
 public void addLog(String line) {
   log.logBasic(line);
 }
  @Override
  public synchronized void importAll(
      RepositoryImportFeedbackInterface feedback,
      String fileDirectory,
      String[] filenames,
      RepositoryDirectoryInterface baseDirectory,
      boolean overwrite,
      boolean continueOnError,
      String versionComment) {
    this.baseDirectory = baseDirectory;
    this.overwrite = overwrite;
    this.continueOnError = continueOnError;
    this.versionComment = versionComment;

    String importPathCompatibility =
        System.getProperty(Const.KETTLE_COMPATIBILITY_IMPORT_PATH_ADDITION_ON_VARIABLES, "N");
    this.needToCheckPathForVariables = "N".equalsIgnoreCase(importPathCompatibility);

    askReplace = Props.getInstance().askAboutReplacingDatabaseConnections();

    if (askReplace) {
      if (feedback instanceof HasOverwritePrompter) {
        Props.getInstance().setProperty(IMPORT_ASK_ABOUT_REPLACE_CS, "Y");
        Props.getInstance().setProperty(IMPORT_ASK_ABOUT_REPLACE_DB, "Y");
        Props.getInstance().setProperty(IMPORT_ASK_ABOUT_REPLACE_PS, "Y");
        Props.getInstance().setProperty(IMPORT_ASK_ABOUT_REPLACE_SS, "Y");
        this.overwritePrompter = ((HasOverwritePrompter) feedback).getOverwritePrompter();
      } else {
        this.overwritePrompter =
            new OverwritePrompter() {

              @Override
              public boolean overwritePrompt(String arg0, String arg1, String arg2) {
                throw new RuntimeException(
                    BaseMessages.getString(PKG, "RepositoryImporter.CannotPrompt.Label"));
              }
            };
      }
    } else {
      final boolean replaceExisting = Props.getInstance().replaceExistingDatabaseConnections();
      this.overwritePrompter =
          new OverwritePrompter() {

            @Override
            public boolean overwritePrompt(String arg0, String arg1, String arg2) {
              return replaceExisting;
            }
          };
    }

    referencingObjects = new ArrayList<RepositoryObject>();

    feedback.setLabel(BaseMessages.getString(PKG, "RepositoryImporter.ImportXML.Label"));
    try {

      loadSharedObjects();

      RepositoryImportLocation.setRepositoryImportLocation(baseDirectory);

      for (int ii = 0; ii < filenames.length; ++ii) {

        final String filename =
            (!Const.isEmpty(fileDirectory))
                ? fileDirectory + Const.FILE_SEPARATOR + filenames[ii]
                : filenames[ii];
        if (log.isBasic()) {
          log.logBasic("Import objects from XML file [" + filename + "]");
        }
        feedback.addLog(BaseMessages.getString(PKG, "RepositoryImporter.WhichFile.Log", filename));

        // To where?
        feedback.setLabel(BaseMessages.getString(PKG, "RepositoryImporter.WhichDir.Label"));

        // Read it using SAX...
        //
        try {
          RepositoryExportSaxParser parser = new RepositoryExportSaxParser(filename, feedback);
          parser.parse(this);
        } catch (Exception e) {
          addException(e);
          feedback.showError(
              BaseMessages.getString(PKG, "RepositoryImporter.ErrorGeneral.Title"),
              BaseMessages.getString(PKG, "RepositoryImporter.ErrorGeneral.Message"),
              e);
        }
      }

      // Correct those jobs and transformations that contain references to other objects.
      for (RepositoryObject repoObject : referencingObjects) {
        switch (repoObject.getObjectType()) {
          case TRANSFORMATION:
            TransMeta transMeta = rep.loadTransformation(repoObject.getObjectId(), null);
            saveTransformationToRepo(transMeta, feedback);
            break;
          case JOB:
            JobMeta jobMeta = rep.loadJob(repoObject.getObjectId(), null);
            saveJobToRepo(jobMeta, feedback);
            break;
          default:
            throw new KettleException(
                BaseMessages.getString(PKG, "RepositoryImporter.ErrorDetectFileType"));
        }
      }

      feedback.addLog(BaseMessages.getString(PKG, "RepositoryImporter.ImportFinished.Log"));
    } catch (Exception e) {
      addException(e);
      feedback.showError(
          BaseMessages.getString(PKG, "RepositoryImporter.ErrorGeneral.Title"),
          BaseMessages.getString(PKG, "RepositoryImporter.ErrorGeneral.Message"),
          e);
    } finally {
      // set the repository import location to null when done!
      RepositoryImportLocation.setRepositoryImportLocation(null);
    }
  }