Java Unix Path unixPath(String sText)

Here you can find the source of unixPath(String sText)

Description

Convert path to unix style path

License

LGPL

Parameter

Parameter Description
sText String, the text to convert

Return

String, the converted text

Declaration

public static String unixPath(String sText) 

Method Source Code

//package com.java2s;
/********************************************************************************
 *                                                                              *
 *  (c) Copyright 2010 Verizon Communications USA and The Open University UK    *
 *                                                                              *
 *  This software is freely distributed in accordance with                      *
 *  the GNU Lesser General Public (LGPL) license, version 3 or later            *
 *  as published by the Free Software Foundation.                               *
 *  For details see LGPL: http://www.fsf.org/licensing/licenses/lgpl.html       *
 *               and GPL: http://www.fsf.org/licensing/licenses/gpl-3.0.html    *
 *                                                                              *
 *  This software is provided by the copyright holders and contributors "as is" *
 *  and any express or implied warranties, including, but not limited to, the   *
 *  implied warranties of merchantability and fitness for a particular purpose  *
 *  are disclaimed. In no event shall the copyright owner or contributors be    *
 *  liable for any direct, indirect, incidental, special, exemplary, or         *
 *  consequential damages (including, but not limited to, procurement of        *
 *  substitute goods or services; loss of use, data, or profits; or business    *
 *  interruption) however caused and on any theory of liability, whether in     *
 *  contract, strict liability, or tort (including negligence or otherwise)     *
 *  arising in any way out of the use of this software, even if advised of the  *
 *  possibility of such damage.                                                 *
 *                                                                              *
 ********************************************************************************/

public class Main {
    /**/*from  ww w  .  j a  va 2  s .  c  o  m*/
     * Convert path to unix style path
     *
     * @param sText String, the text to convert
     * @return String, the converted text
     */
    public static String unixPath(String sText) {

        if (sText == null || sText.equals(""))
            return "";

        sText = replace(sText, '\\', "/");

        return sText;
    }

    /**
     * Replace String a, in the given text with String b
     *
     * @param text the text to replace character in.
     * @param a the string to replace
     * @param b the string to replace the character with.
     *
     * @return String, the replaced text.
     */
    public static String replace(String text, String a, String b) {

        int length = text.length();
        int lenA = a.length();
        int lenB = b.length();
        int gofrom = 0;
        boolean goon = true;

        while (goon) {
            int next = text.indexOf(a, gofrom);
            if (next != -1) {

                if (next + lenA > length) {
                    text = text.substring(0, next) + b;
                } else {
                    text = text.substring(0, next) + b + text.substring(next + lenA);
                }
                gofrom = next + lenB;
            } else {
                goon = false;
            }

            length = text.length();
            if (gofrom > length)
                goon = false;
        }
        return text;
    }

    /**
     * Replace char a, in the given text with String b
     *
     * @param text the text to replace character in.
     * @param a the character to replace
     * @param b the string to replace the character with.
     *
     * @return String, the replaced text.
     */
    public static String replace(String text, char a, String b) {

        int length = text.length();
        int len = b.length();
        int gofrom = 0;
        boolean goon = true;

        while (goon) {
            int next = text.indexOf(a, gofrom);
            if (next != -1) {

                if (next + 1 > length)
                    text = text.substring(0, next) + b;
                else
                    text = text.substring(0, next) + b + text.substring(next + 1);

                gofrom = next + len;
            } else
                goon = false;

            length = text.length();
            if (gofrom > length)
                goon = false;
        }
        return text;
    }
}

Related

  1. getUNIXfilePath(String fileName)
  2. getUnixRelativePath(File base, File path)
  3. toUNIXfilePath(String fileName)
  4. unixizeWindowsAbsolutePath(final String pathToCheck)
  5. unixPathStr(String anyPath)
  6. unixPathToWindows(String strPath)
  7. unixToUserPathName(String unixPathName)