/**
   * Returns absolute path to the web content directory based on configuration of the war plugin or
   * default one otherwise.
   *
   * @param project
   * @return absolute directory path as String
   * @throws MojoExecutionException
   */
  private static String getWebContentBaseDirectory(EclipseWriterConfig config)
      throws MojoExecutionException {
    // getting true location of web source dir from config
    File warSourceDirectory =
        new File(
            IdeUtils.getPluginSetting(
                config.getProject(),
                JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN,
                "warSourceDirectory",
                "src/main/webapp"));
    // getting real and correct path to the web source dir
    String webContentDir =
        IdeUtils.toRelativeAndFixSeparator(
            config.getEclipseProjectDirectory(), warSourceDirectory, false);

    // getting the path to meta-inf base dir
    String result =
        config.getProject().getBasedir().getAbsolutePath() + File.separatorChar + webContentDir;

    return result;
  }
  /** @see org.apache.maven.plugin.eclipse.writers.EclipseWriter#write() */
  public void write() throws MojoExecutionException {

    // check if it's necessary to create project specific settings
    Properties coreSettings = new Properties();

    String source = IdeUtils.getCompilerSourceVersion(config.getProject());
    String encoding = IdeUtils.getCompilerSourceEncoding(config.getProject());
    String target = IdeUtils.getCompilerTargetVersion(config.getProject());

    if (source != null) {
      coreSettings.put(PROP_JDT_CORE_COMPILER_SOURCE, source);
      coreSettings.put(PROP_JDT_CORE_COMPILER_COMPLIANCE, source);
    }

    if (encoding != null) {
      File basedir = config.getProject().getBasedir();
      List compileSourceRoots = config.getProject().getCompileSourceRoots();
      if (compileSourceRoots != null) {
        for (Object compileSourceRoot : compileSourceRoots) {
          String sourcePath = (String) compileSourceRoot;
          String relativePath =
              IdeUtils.toRelativeAndFixSeparator(basedir, new File(sourcePath), false);
          coreSettings.put(PROP_JDT_CORE_COMPILER_ENCODING + relativePath, encoding);
        }
      }
      List testCompileSourceRoots = config.getProject().getTestCompileSourceRoots();
      if (testCompileSourceRoots != null) {
        for (Object testCompileSourceRoot : testCompileSourceRoots) {
          String sourcePath = (String) testCompileSourceRoot;
          String relativePath =
              IdeUtils.toRelativeAndFixSeparator(basedir, new File(sourcePath), false);
          coreSettings.put(PROP_JDT_CORE_COMPILER_ENCODING + relativePath, encoding);
        }
      }
      List resources = config.getProject().getResources();
      if (resources != null) {
        for (Object resource1 : resources) {
          Resource resource = (Resource) resource1;
          String relativePath =
              IdeUtils.toRelativeAndFixSeparator(basedir, new File(resource.getDirectory()), false);
          coreSettings.put(PROP_JDT_CORE_COMPILER_ENCODING + relativePath, encoding);
        }
      }
      List testResources = config.getProject().getTestResources();
      if (testResources != null) {
        for (Object testResource : testResources) {
          Resource resource = (Resource) testResource;
          String relativePath =
              IdeUtils.toRelativeAndFixSeparator(basedir, new File(resource.getDirectory()), false);
          coreSettings.put(PROP_JDT_CORE_COMPILER_ENCODING + relativePath, encoding);
        }
      }
    }

    if (target != null && !JDK_1_2_SOURCES.equals(target)) {
      coreSettings.put(
          "org.eclipse.jdt.core.compiler.codegen.targetPlatform", target); // $NON-NLS-1$
    }

    // write the settings, if needed
    if (!coreSettings.isEmpty()) {
      File settingsDir =
          new File(
              config.getEclipseProjectDirectory(),
              EclipseWorkspaceWriter.DIR_DOT_SETTINGS); // $NON-NLS-1$

      settingsDir.mkdirs();

      coreSettings.put(PROP_ECLIPSE_PREFERENCES_VERSION, "1"); // $NON-NLS-1$

      try {
        File oldCoreSettingsFile;

        File coreSettingsFile =
            new File(settingsDir, EclipseWorkspaceWriter.ECLIPSE_JDT_CORE_PREFS_FILE);

        if (coreSettingsFile.exists()) {
          oldCoreSettingsFile = coreSettingsFile;

          Properties oldsettings = new Properties();
          oldsettings.load(new FileInputStream(oldCoreSettingsFile));

          Properties newsettings = (Properties) oldsettings.clone();
          newsettings.putAll(coreSettings);

          if (!oldsettings.equals(newsettings)) {
            newsettings.store(new FileOutputStream(coreSettingsFile), null);
          }
        } else {
          coreSettings.store(new FileOutputStream(coreSettingsFile), null);

          log.info(
              Messages.getString(
                  "EclipseSettingsWriter.wrotesettings", //$NON-NLS-1$
                  coreSettingsFile.getCanonicalPath()));
        }
      } catch (FileNotFoundException e) {
        throw new MojoExecutionException(
            Messages.getString("EclipseSettingsWriter.cannotcreatesettings"), e); // $NON-NLS-1$
      } catch (IOException e) {
        throw new MojoExecutionException(
            Messages.getString("EclipseSettingsWriter.errorwritingsettings"), e); // $NON-NLS-1$
      }
    } else {
      log.info(Messages.getString("EclipseSettingsWriter.usingdefaults")); // $NON-NLS-1$
    }
  }
  private void addDependency(Properties ajdtSettings, IdeDependency dep, String propName, int index)
      throws MojoExecutionException {

    String path;

    if (dep.isReferencedProject() && !config.isPde()) {
      path = "/" + dep.getEclipseProjectName(); // $NON-NLS-1$
    } else if (dep.isReferencedProject() && config.isPde()) {
      // don't do anything, referenced projects are automatically handled by eclipse in PDE builds
      return;
    } else {
      File artifactPath = dep.getFile();

      if (artifactPath == null) {
        log.error(
            Messages.getString("EclipsePlugin.artifactpathisnull", dep.getId())); // $NON-NLS-1$
        return;
      }

      if (dep.isSystemScoped()) {
        path =
            IdeUtils.toRelativeAndFixSeparator(
                config.getEclipseProjectDirectory(), artifactPath, false);

        if (log.isDebugEnabled()) {
          log.debug(
              Messages.getString(
                  "EclipsePlugin.artifactissystemscoped", //$NON-NLS-1$
                  new Object[] {dep.getArtifactId(), path}));
        }
      } else {
        File localRepositoryFile = new File(config.getLocalRepository().getBasedir());

        // if the dependency is not provided and the plugin runs in "pde mode", the dependency is
        // added to the Bundle-Classpath:
        if (config.isPde() && (dep.isProvided() || dep.isOsgiBundle())) {
          return;
        } else if (config.isPde() && !dep.isProvided() && !dep.isTestDependency()) {
          // path for link created in .project, not to the actual file
          path = dep.getFile().getName();
        }
        // running in PDE mode and the dependency is provided means, that it is provided by
        // the target platform. This case is covered by adding the plugin container
        else {
          String fullPath = artifactPath.getPath();
          String relativePath =
              IdeUtils.toRelativeAndFixSeparator(localRepositoryFile, new File(fullPath), false);

          if (!new File(relativePath).isAbsolute()) {
            path =
                EclipseClasspathWriter.M2_REPO
                    + "/" //$NON-NLS-1$
                    + relativePath;
          } else {
            path = relativePath;
          }
        }
      }
    }

    ajdtSettings.setProperty(AJDT_PROP_PREFIX + propName + CONTENT_KIND + index, BINARY);
    ajdtSettings.setProperty(AJDT_PROP_PREFIX + propName + ENTRY_KIND + index, LIBRARY);
    ajdtSettings.setProperty(AJDT_PROP_PREFIX + propName + index, path);
  }