Java Path String Clean cleanPath(String sText, boolean isWindows)

Here you can find the source of cleanPath(String sText, boolean isWindows)

Description

Clean path if not correct for the platform

License

LGPL

Parameter

Parameter Description
sText String, the text to clean
is this the Windows platform

Return

String, the clean text

Declaration

public static String cleanPath(String sText, boolean isWindows) 

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  w w  w . j  av  a  2s  .  c  om
     * Clean path if not correct for the platform
     *
     * @param sText String, the text to clean
     * @param is this the Windows platform
     * @return String, the clean text
     */
    public static String cleanPath(String sText, boolean isWindows) {

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

        if (sText.startsWith("http://") || sText.startsWith("https://"))
            return sText;

        String sFS = System.getProperty("file.separator");

        if (isWindows) {
            sText = replace(sText, '/', sFS);
        } else {
            sText = replace(sText, '\\', sFS);
        }

        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. cleanPath(String path)
  2. cleanPath(String path)
  3. cleanPath(String path)
  4. cleanPath(String path, boolean trimStartSeparator)
  5. cleanPath(String s)
  6. cleanPathSegment(final String toClean)
  7. filePathReplaceAll(String value)
  8. filePathTail(String filePath)