Java String Sanitize sanitizePath(String input)

Here you can find the source of sanitizePath(String input)

Description

Sanitize the given filename/path, so that (1) it is not surrounded with quotation marks "", (2) all occurences of "\\" are changed to a "/", and (3) all occurences of "\" are changed to a "/".

License

Apache License

Parameter

Parameter Description
input the input path

Return

a StringBuffer containing the sanitized input path

Declaration

public static StringBuffer sanitizePath(String input) 

Method Source Code

//package com.java2s;
/*/*from w  ww. j  a  v  a  2  s .  c  o m*/
 Copyright 2011 Opera Software ASA

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

public class Main {
    /**
     * Sanitize the given filename/path, so that (1) it is not surrounded with quotation marks "", (2)
     * all occurences of "\\" are changed to a "/", and (3) all occurences of "\" are changed to a
     * "/".
     *
     * @param input the input path
     * @return a StringBuffer containing the sanitized input path
     */
    public static StringBuffer sanitizePath(String input) {
        StringBuffer buf = new StringBuffer();

        // Strip any surrounding quotation marks, that might have came with the file name from
        // any external source like the Windows environment variable.
        if (input.matches("^\".+\"$")) {
            input = input.substring(1, input.length() - 1);
        }

        // Make sure we use "/" as the path separator, seems to be the best solution. Also, strip double
        // "\\" used as separators.
        input = input.replaceAll("\\\\\\\\", "/");
        input = input.replaceAll("\\\\", "/");

        buf.append(input);
        return buf;
    }
}

Related

  1. sanitizeNamespace(String namespace)
  2. sanitizeOperationId(String operationId)
  3. sanitizePackageName(String raw)
  4. sanitizePackageVersion(String string)
  5. sanitizePath(String apiPath)
  6. sanitizePrimitives(Class clazz)
  7. sanitizeProcOrFuncName(String src)
  8. sanitizeProjectName(final String projectName)
  9. sanitizeRefName(String refName)