Example usage for org.apache.commons.io FilenameUtils separatorsToUnix

List of usage examples for org.apache.commons.io FilenameUtils separatorsToUnix

Introduction

In this page you can find the example usage for org.apache.commons.io FilenameUtils separatorsToUnix.

Prototype

public static String separatorsToUnix(String path) 

Source Link

Document

Converts all separators to the Unix separator of forward slash.

Usage

From source file:org.craftercms.deployer.impl.processors.GitDiffProcessor.java

protected String asContentStoreUrl(String path) {
    path = FilenameUtils.separatorsToUnix(path);

    if (!path.startsWith("/")) {
        path = "/" + path;
    }/*  ww w.ja v a  2s.c om*/

    return path;
}

From source file:org.craftercms.studio.impl.v1.repository.git.GitContentRepositoryHelper.java

public String getGitPath(String path) {
    Path gitPath = Paths.get(path);
    gitPath = gitPath.normalize();// w  ww.  j a  va 2s. c  o m
    try {
        gitPath = Paths.get(FILE_SEPARATOR).relativize(gitPath);
    } catch (IllegalArgumentException e) {
        logger.debug("Path: " + path + " is already relative path.");
    }
    if (StringUtils.isEmpty(gitPath.toString())) {
        return ".";
    }
    String toRet = gitPath.toString();
    toRet = FilenameUtils.separatorsToUnix(toRet);
    return toRet;
}

From source file:org.dataconservancy.dcs.util.FilePathUtil.java

/**
 * This method has been deprecated use Apache FileNameUtils instead.
 * Takes the file path string and converts all slashes to unix style '/'.
 * @param filePath The original file path to convert slashes on.
 * @return The updated file path will all slashes in the form '/', or null if filePath is null.
 *///from  ww w .  j ava2 s .  c om
@Deprecated
public static String convertToUnixSlashes(String filePath) {
    return FilenameUtils.separatorsToUnix(filePath);
}

From source file:org.dataconservancy.dcs.util.UriUtility.java

/**
 * Create a URI string for a file, ensuring that it has 3 slashes to meet File URL specifications
 * @param file The file to check.  This doesn't have to be an actual existing file
 * @param basedir The directory to make the file URI relative to.  Can be null.  If not null, the basedir must be
 *                in the path of the file parameter, or an exception will be thrown
 * @return A string representing the URI to the file on the local disk.
 * @throws URISyntaxException if there is an error in the URI syntax
 *//*  ww  w .ja v  a  2 s .  c o m*/
public static URI makeFileUriString(File file, File basedir) throws URISyntaxException {
    if (basedir == null) {
        basedir = new File(".");
    }

    Path relativePath = file.toPath();

    if (relativePath.startsWith(basedir.toPath())) {
        relativePath = basedir.toPath().relativize(file.toPath());
    }

    String path = FilenameUtils.separatorsToUnix(relativePath.toString());

    // Remove leading slashes from the path
    path = path.replaceFirst("^\\/*", "");

    return new URI("file", null, "///" + path, null);
}

From source file:org.dataconservancy.dcs.util.UriUtility.java

/**
* Create a URI string for a file in a BagIt bag,
* @param file The file to check.  This doesn't have to be an actual existing file
* @param basedir The directory to make the file URI relative to.  Can be null.  If not null, the basedir must be
*                in the path of the file parameter, or an exception will be thrown
* @return A string representing the URI to the file on the local disk.
* @throws URISyntaxException if there is an error in the URI syntax
*//*from w  w  w.j  a v a 2 s  .  c o m*/
public static URI makeBagUriString(File file, File basedir) throws URISyntaxException {
    if (basedir == null) {
        basedir = new File(".");
    }

    Path relativePath = file.toPath();

    if (relativePath.startsWith(basedir.toPath())) {
        relativePath = basedir.toPath().relativize(file.toPath());
    }

    String path = FilenameUtils.separatorsToUnix(relativePath.toString());
    if (relativePath.getNameCount() > 1) {

        Path uriAuthority = relativePath.getName(0);
        Path uriPath = relativePath.subpath(1, relativePath.getNameCount());
        path = FilenameUtils.separatorsToUnix(uriPath.toString());
        if (!uriPath.isAbsolute()) {
            path = "/" + path;
        }

        return new URI(BAG_URI_SCHEME, uriAuthority.toString(), path, null, null);
    }

    return new URI(BAG_URI_SCHEME, path, null, null, null);
}

From source file:org.dataconservancy.packaging.impl.UriUtility.java

/**
 * Create a URI string for a file, ensuring that it has 3 slashes to meet File URL specifications
 *
 * @param file The file to check. This doesn't have to be an actual existing file
 * @param basedir The directory to make the file URI relative to. Can be null. If not null, the basedir must be in
 *        the path of the file parameter, or an exception will be thrown
 * @return A string representing the URI to the file on the local disk.
 * @throws URISyntaxException if there is an error in the URI syntax
 *//*from   w  w w  . j  a  v  a 2  s.  co  m*/
public static URI makeFileUriString(final File file, final File basedir) throws URISyntaxException {
    final File dir = basedir == null ? new File(".") : basedir;

    Path relativePath = file.toPath();

    if (relativePath.startsWith(dir.toPath())) {
        relativePath = dir.toPath().relativize(file.toPath());
    }

    String path = FilenameUtils.separatorsToUnix(relativePath.toString());

    // Remove leading slashes from the path
    path = path.replaceFirst("^\\/*", "");

    return new URI("file", null, "///" + path, null);
}

From source file:org.dataconservancy.packaging.impl.UriUtility.java

/**
 * Create a URI string for a file in a BagIt bag,
 *
 * @param file The file to check. This doesn't have to be an actual existing file
 * @param basedir The directory to make the file URI relative to. Can be null. If not null, the basedir must be in
 *        the path of the file parameter, or an exception will be thrown
 * @return A string representing the URI to the file on the local disk.
 * @throws URISyntaxException if there is an error in the URI syntax
 *//*from   w w  w. j  ava 2s  .c  o  m*/
public static URI makeBagUriString(final File file, final File basedir) throws URISyntaxException {
    final File dir = basedir == null ? new File(".") : basedir;

    Path relativePath = file.toPath();

    if (relativePath.startsWith(dir.toPath())) {
        relativePath = dir.toPath().relativize(file.toPath());
    }

    String path = FilenameUtils.separatorsToUnix(relativePath.toString());
    if (relativePath.getNameCount() > 1) {

        final Path uriAuthority = relativePath.getName(0);
        final Path uriPath = relativePath.subpath(1, relativePath.getNameCount());
        path = FilenameUtils.separatorsToUnix(uriPath.toString());
        if (!uriPath.isAbsolute()) {
            path = "/" + path;
        }

        return new URI(BAG_URI_SCHEME, uriAuthority.toString(), path, null, null);
    }

    return new URI(BAG_URI_SCHEME, path, null, null, null);
}

From source file:org.dataconservancy.packaging.tool.impl.generator.BagItPackageAssembler.java

private File writeManifestFile(String alg, Map<File, List<Checksum>> fileChecksums, String fileName)
        throws PackageToolException {
    File manifestFile = new File(bagBaseDir, fileName);
    try {/*from www  .j  av a2 s.  co m*/
        FileWriter writer = new FileWriter(manifestFile);
        String newLine = System.getProperty("line.separator");
        String lineFormat = "%s  %s";

        Set<File> files = fileChecksums.keySet();

        for (File file : files) {
            List<Checksum> checksums = fileChecksums.get(file);

            final String filePath = FilenameUtils.separatorsToUnix(
                    Paths.get(bagBaseDir.getPath()).relativize(Paths.get(file.getPath())).toString());
            for (Checksum checksum : checksums) {
                if (checksum.getAlgorithm().equals(alg)) {
                    writer.write(String.format(lineFormat, checksum.getValue(), filePath + newLine));
                }
            }
        }
        writer.close();
    } catch (IOException e) {
        throw new PackageToolException(PackagingToolReturnInfo.PKG_IO_EXCEPTION, e,
                "Exception occurred when writing one of the manifest files.");
    }
    return manifestFile;
}

From source file:org.dataconservancy.packaging.tool.impl.generator.BagItPackageAssembler.java

/**
 * According to Usecase DCS-TXT-10, BagIt bags that include ORE ReM will contain "PKG-BAG-DIR", which is "a string
 * consisting of a valid Unix directory name that, if specified, will be treated as the top-level bag directory
 * name." Base on this assertion, path used to create resources will be relative to this PKG-BAG-DIR.
 * @param string the string used to build the path
 * @return  a String representing the path
 *//*  ww  w. j a va 2  s . c  o m*/
private String buildPath(String string) {

    String rootLocation = params.getParam(GeneralParameterNames.CONTENT_ROOT_LOCATION, 0);

    File file;
    if (rootLocation != null) {
        String contentRoot = FilenameUtils.separatorsToUnix(rootLocation);
        file = new File(contentRoot, string);
    } else {
        file = new File(string);
    }

    if (!file.exists()) {
        throw new PackageToolException(PackagingToolReturnInfo.PKG_ASSEMBLER_STRAY_FILE,
                "Provided file path indicates that that file " + file.getPath() + " does not reside within the "
                        + "specified content root location of: "
                        + params.getParam(GeneralParameterNames.CONTENT_ROOT_LOCATION, 0));

    }

    //return file.getPath();
    return string;
}

From source file:org.dataconservancy.packaging.tool.impl.generator.BagItPackageAssembler.java

private void addFilesToArchive(ArchiveOutputStream taos, File file) throws IOException {
    // Create an entry for the file
    //taos.putArchiveEntry(new TarArchiveEntry(file, file.getParentFile().toURI().relativize(file.toURI()).toString()));
    switch (archivingFormat) {
    case ArchiveStreamFactory.TAR:
        taos.putArchiveEntry(new TarArchiveEntry(file, FilenameUtils.separatorsToUnix(
                Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString())));
        break;//  w  ww  . j  a v  a2s. c  o  m
    case ArchiveStreamFactory.ZIP:
        taos.putArchiveEntry(new ZipArchiveEntry(file, FilenameUtils.separatorsToUnix(
                Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString())));
        break;
    case ArchiveStreamFactory.JAR:
        taos.putArchiveEntry(new JarArchiveEntry(new ZipArchiveEntry(file, FilenameUtils.separatorsToUnix(
                Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString()))));
        break;
    case ArchiveStreamFactory.AR:
        taos.putArchiveEntry(new ArArchiveEntry(file, FilenameUtils.separatorsToUnix(
                Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString())));
        break;
    case ArchiveStreamFactory.CPIO:
        taos.putArchiveEntry(new CpioArchiveEntry(file, FilenameUtils.separatorsToUnix(
                Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString())));
        break;
    }
    if (file.isFile()) {
        // Add the file to the archive
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(bis, taos);
        taos.closeArchiveEntry();
        bis.close();
    } else if (file.isDirectory()) {
        // close the archive entry
        taos.closeArchiveEntry();
        // go through all the files in the directory and using recursion, add them to the archive
        for (File childFile : file.listFiles()) {
            addFilesToArchive(taos, childFile);
        }
    }
}