/*
 * Copyright (c) 2004-2017, by Alex Leigh.
 * All rights reserved.
 *
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE
 * The copyright notice above does not evidence any
 * actual or intended publication of such source code.
 */

package lc.mecha.io;

import lc.mecha.log.MechaLogger;
import lc.mecha.log.MechaLoggerFactory;
import org.apache.commons.lang.StringUtils;

/**
 * Utility methods for working with filenames and paths.
 *
 * @author Alex Leigh
 */
public class FilenameUtils {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(FilenameUtils.class);

    /**
     * Collapse the given path by converting the "/" characters into "_" characters. The new path always begins with
     * "/".
     * <p>
     * Example:
     * <p>
     * /a/b/c/d/foo.txt becomes /a_b_c_d_foo.txt
     *
     * @param path
     * @return
     */
    public static String collapsePathToFile(String path) {
        String p = StringUtils.stripStart(path, "/");
        p = p.replace("/", "_");
        logger.info("Collapsed path: {}", p);
        return p;
    }

    public static String filenameOnlyWithoutExtension(String path) {
        return org.apache.commons.io.FilenameUtils.removeExtension(org.apache.commons.io.FilenameUtils.getName(path));
    }
}
