All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.itemis.maven.plugins.unleash.scm.utils.FileToRelativePath Maven / Gradle / Ivy

package com.itemis.maven.plugins.unleash.scm.utils;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;

import com.google.common.base.Function;

/**
 * A function to convert a file's absolute path into the relative path starting from a reference file
 * (identical to former com.itemis.maven.plugins.unleash.util.FileToRelativePath within module unleash-mabven-plugin).
 *
 * @author Stanley Hillner
 * @since 3.2.0
 */
public class FileToRelativePath implements Function {
  private File workingDir;

  public FileToRelativePath(File workingDir) {
    this.workingDir = workingDir;
  }

  @Override
  public String apply(File f) {
    URI workingDirURI = this.workingDir.toURI();
    URI fileURI = f.toURI();
    if (SystemUtils.IS_OS_WINDOWS) {
      // On Windows OS deviations in character case of the drive letter may occur
      // => we have to normalize by lower casing the URI!
      String lcURIString = StringUtils.EMPTY;
      try {
        lcURIString = workingDirURI.toString().toLowerCase();
        workingDirURI = new URI(lcURIString);
        lcURIString = fileURI.toString().toLowerCase();
        fileURI = new URI(lcURIString);
      } catch (URISyntaxException e) {
        throw new RuntimeException("Failed to create URI for path: " + lcURIString, e);
      }
    }

    return workingDirURI.relativize(fileURI).toString();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy