Here you can find the source of normalizePath(String path)
Parameter | Description |
---|---|
path | Path to be normalized |
public static final String normalizePath(String path)
//package com.java2s; /*//from w w w .java 2 s. c om * Copyright (c) 2007-2016 AREasy Runtime * * This library, AREasy Runtime and API for BMC Remedy AR System, is free software ("Licensed Software"); * you can redistribute it and/or modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either version 2.1 of the License, * or (at your option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * including but not limited to, the implied warranty of MERCHANTABILITY, NONINFRINGEMENT, * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. */ public class Main { /** * The empty String <code>""</code>. */ public static final String EMPTY = ""; /** * Return a context-relative path, beginning with a "/", that represents * the canonical version of the specified path after ".." and "." elements * are resolved out. If the specified path attempts to go outside the * boundaries of the current context (i.e. too many ".." path elements * are present), return <code>null</code> instead. * * @param path Path to be normalized * @return String normalized path */ public static final String normalizePath(String path) { // Normalize the slashes and add leading slash if necessary String normalized = path; if (normalized.indexOf('\\') >= 0) normalized = normalized.replace('\\', '/'); if (!normalized.startsWith("/")) normalized = "/" + normalized; // Resolve occurrences of "//" in the normalized path while (true) { int index = normalized.indexOf("//"); if (index < 0) break; normalized = normalized.substring(0, index) + normalized.substring(index + 1); } // Resolve occurrences of "%20" in the normalized path while (true) { int index = normalized.indexOf("%20"); if (index < 0) break; normalized = normalized.substring(0, index) + " " + normalized.substring(index + 3); } // Resolve occurrences of "/./" in the normalized path while (true) { int index = normalized.indexOf("/./"); if (index < 0) break; normalized = normalized.substring(0, index) + normalized.substring(index + 2); } // Resolve occurrences of "/../" in the normalized path while (true) { int index = normalized.indexOf("/../"); if (index < 0) break; if (index == 0) return (null); // Trying to go outside our context int index2 = normalized.lastIndexOf('/', index - 1); normalized = normalized.substring(0, index2) + normalized.substring(index + 3); } // Return the normalized path that we have completed return (normalized); } /** * <p>Finds the first index within a String, handling <code>null</code>. * This method uses {@link String#indexOf(int)}.</p> * <p/> * <p>A <code>null</code> or empty ("") String will return <code>-1</code>.</p> * <p/> * <pre> * StringUtility.indexOf(null, *) = -1 * StringUtility.indexOf("", *) = -1 * StringUtility.indexOf("aabaabaa", 'a') = 0 * StringUtility.indexOf("aabaabaa", 'b') = 2 * </pre> * * @param str the String to check, may be null * @param searchChar the character to find * @return the first index of the search character, * -1 if no match or <code>null</code> string input */ public static int indexOf(String str, char searchChar) { if (isEmpty(str)) { return -1; } return str.indexOf(searchChar); } /** * <p>Finds the first index within a String from a start position, * handling <code>null</code>. * This method uses {@link String#indexOf(int, int)}.</p> * <p/> * <p>A <code>null</code> or empty ("") String will return <code>-1</code>. * A negative start position is treated as zero. * A start position greater than the string length returns <code>-1</code>.</p> * <p/> * <pre> * StringUtility.indexOf(null, *, *) = -1 * StringUtility.indexOf("", *, *) = -1 * StringUtility.indexOf("aabaabaa", 'b', 0) = 2 * StringUtility.indexOf("aabaabaa", 'b', 3) = 5 * StringUtility.indexOf("aabaabaa", 'b', 9) = -1 * StringUtility.indexOf("aabaabaa", 'b', -1) = 2 * </pre> * * @param str the String to check, may be null * @param searchChar the character to find * @param startPos the start position, negative treated as zero * @return the first index of the search character, * -1 if no match or <code>null</code> string input */ public static int indexOf(String str, char searchChar, int startPos) { if (isEmpty(str)) { return -1; } return str.indexOf(searchChar, startPos); } /** * <p>Finds the first index within a String, handling <code>null</code>. * This method uses {@link String#indexOf(String)}.</p> * <p/> * <p>A <code>null</code> String will return <code>-1</code>.</p> * <p/> * <pre> * StringUtility.indexOf(null, *) = -1 * StringUtility.indexOf(*, null) = -1 * StringUtility.indexOf("", "") = 0 * StringUtility.indexOf("aabaabaa", "a") = 0 * StringUtility.indexOf("aabaabaa", "b") = 2 * StringUtility.indexOf("aabaabaa", "ab") = 1 * StringUtility.indexOf("aabaabaa", "") = 0 * </pre> * * @param str the String to check, may be null * @param searchStr the String to find, may be null * @return the first index of the search String, * -1 if no match or <code>null</code> string input */ public static int indexOf(String str, String searchStr) { if (str == null || searchStr == null) { return -1; } return str.indexOf(searchStr); } /** * <p>Finds the first index within a String, handling <code>null</code>. * This method uses {@link String#indexOf(String, int)}.</p> * <p/> * <p>A <code>null</code> String will return <code>-1</code>. * A negative start position is treated as zero. * An empty ("") search String always matches. * A start position greater than the string length only matches * an empty search String.</p> * <p/> * <pre> * StringUtility.indexOf(null, *, *) = -1 * StringUtility.indexOf(*, null, *) = -1 * StringUtility.indexOf("", "", 0) = 0 * StringUtility.indexOf("aabaabaa", "a", 0) = 0 * StringUtility.indexOf("aabaabaa", "b", 0) = 2 * StringUtility.indexOf("aabaabaa", "ab", 0) = 1 * StringUtility.indexOf("aabaabaa", "b", 3) = 5 * StringUtility.indexOf("aabaabaa", "b", 9) = -1 * StringUtility.indexOf("aabaabaa", "b", -1) = 2 * StringUtility.indexOf("aabaabaa", "", 2) = 2 * StringUtility.indexOf("abc", "", 9) = 3 * </pre> * * @param str the String to check, may be null * @param searchStr the String to find, may be null * @param startPos the start position, negative treated as zero * @return the first index of the search String, * -1 if no match or <code>null</code> string input */ public static int indexOf(String str, String searchStr, int startPos) { if (str == null || searchStr == null) return -1; // JDK1.2/JDK1.3 have a bug, when startPos > str.length for "", hence if (searchStr.length() == 0 && startPos >= str.length()) return str.length(); return str.indexOf(searchStr, startPos); } /** * <p>Replaces all occurrences of a String within another String.</p> * <p/> * <p>A <code>null</code> reference passed to this method is a no-op.</p> * <p/> * <pre> * StringUtility.replace(null, *, *) = null * StringUtility.replace("", *, *) = "" * StringUtility.replace("any", null, *) = "any" * StringUtility.replace("any", *, null) = "any" * StringUtility.replace("any", "", *) = "any" * StringUtility.replace("aba", "a", null) = "aba" * StringUtility.replace("aba", "a", "") = "b" * StringUtility.replace("aba", "a", "z") = "zbz" * </pre> * * @param text text to search and replace in, may be null * @param repl the String to search for, may be null * @param with the String to replace with, may be null * @return the text with any replacements processed, * <code>null</code> if null String input * @see #replace(String text, String repl, String with, int max) */ public static String replace(String text, String repl, String with) { return replace(text, repl, with, -1); } /** * <p>Replaces a String with another String inside a larger String, * for the first <code>max</code> values of the search String.</p> * <p/> * <p>A <code>null</code> reference passed to this method is a no-op.</p> * <p/> * <pre> * StringUtility.replace(null, *, *, *) = null * StringUtility.replace("", *, *, *) = "" * StringUtility.replace("any", null, *, *) = "any" * StringUtility.replace("any", *, null, *) = "any" * StringUtility.replace("any", "", *, *) = "any" * StringUtility.replace("any", *, *, 0) = "any" * StringUtility.replace("abaa", "a", null, -1) = "abaa" * StringUtility.replace("abaa", "a", "", -1) = "b" * StringUtility.replace("abaa", "a", "z", 0) = "abaa" * StringUtility.replace("abaa", "a", "z", 1) = "zbaa" * StringUtility.replace("abaa", "a", "z", 2) = "zbza" * StringUtility.replace("abaa", "a", "z", -1) = "zbzz" * </pre> * * @param text text to search and replace in, may be null * @param repl the String to search for, may be null * @param with the String to replace with, may be null * @param max maximum number of values to replace, or <code>-1</code> if no maximum * @return the text with any replacements processed, * <code>null</code> if null String input */ public static String replace(String text, String repl, String with, int max) { if (text == null || isEmpty(repl) || with == null || max == 0) { return text; } StringBuffer buf = new StringBuffer(text.length()); int start = 0, end = 0; while ((end = text.indexOf(repl, start)) != -1) { buf.append(text.substring(start, end)).append(with); start = end + repl.length(); if (--max == 0) { break; } } buf.append(text.substring(start)); return buf.toString(); } /** * <p>Gets a substring from the specified String avoiding exceptions.</p> * <p/> * <p>A negative start position can be used to start <code>n</code> * characters from the end of the String.</p> * <p/> * <p>A <code>null</code> String will return <code>null</code>. * An empty ("") String will return "".</p> * <p/> * <pre> * StringUtility.substring(null, *) = null * StringUtility.substring("", *) = "" * StringUtility.substring("abc", 0) = "abc" * StringUtility.substring("abc", 2) = "c" * StringUtility.substring("abc", 4) = "" * StringUtility.substring("abc", -2) = "bc" * StringUtility.substring("abc", -4) = "abc" * </pre> * * @param str the String to get the substring from, may be null * @param start the position to start from, negative means * count back from the end of the String by this many characters * @return substring from start position, <code>null</code> if null String input */ public static String substring(String str, int start) { if (str == null) return null; // handle negatives, which means last n characters if (start < 0) start = str.length() + start; // remember start is negative if (start < 0) start = 0; if (start > str.length()) return EMPTY; return str.substring(start); } /** * <p>Gets a substring from the specified String avoiding exceptions.</p> * <p/> * <p>A negative start position can be used to start/end <code>n</code> * characters from the end of the String.</p> * <p/> * <p>The returned substring starts with the character in the <code>start</code> * position and ends before the <code>end</code> position. All position counting is * zero-based -- i.e., to start at the beginning of the string use * <code>start = 0</code>. Negative start and end positions can be used to * specify offsets relative to the end of the String.</p> * <p/> * <p>If <code>start</code> is not strictly to the left of <code>end</code>, "" * is returned.</p> * <p/> * <pre> * StringUtility.substring(null, *, *) = null * StringUtility.substring("", * , *) = ""; * StringUtility.substring("abc", 0, 2) = "ab" * StringUtility.substring("abc", 2, 0) = "" * StringUtility.substring("abc", 2, 4) = "c" * StringUtility.substring("abc", 4, 6) = "" * StringUtility.substring("abc", 2, 2) = "" * StringUtility.substring("abc", -2, -1) = "b" * StringUtility.substring("abc", -4, 2) = "ab" * </pre> * * @param str the String to get the substring from, may be null * @param start the position to start from, negative means * count back from the end of the String by this many characters * @param end the position to end at (exclusive), negative means * count back from the end of the String by this many characters * @return substring from start position to end positon, * <code>null</code> if null String input */ public static String substring(String str, int start, int end) { if (str == null) return null; // handle negatives if (end < 0) end = str.length() + end; // remember end is negative if (start < 0) start = str.length() + start; // remember start is negative // check length next if (end > str.length()) end = str.length(); // if start is greater than end, return "" if (start > end) return EMPTY; if (start < 0) start = 0; if (end < 0) end = 0; return str.substring(start, end); } /** * <p>Finds the last index within a String, handling <code>null</code>. * This method uses {@link String#lastIndexOf(int)}.</p> * <p/> * <p>A <code>null</code> or empty ("") String will return <code>-1</code>.</p> * <p/> * <pre> * StringUtility.lastIndexOf(null, *) = -1 * StringUtility.lastIndexOf("", *) = -1 * StringUtility.lastIndexOf("aabaabaa", 'a') = 7 * StringUtility.lastIndexOf("aabaabaa", 'b') = 5 * </pre> * * @param str the String to check, may be null * @param searchChar the character to find * @return the last index of the search character, * -1 if no match or <code>null</code> string input */ public static int lastIndexOf(String str, char searchChar) { if (isEmpty(str)) return -1; return str.lastIndexOf(searchChar); } /** * <p>Finds the last index within a String from a start position, * handling <code>null</code>. * This method uses {@link String#lastIndexOf(int, int)}.</p> * <p/> * <p>A <code>null</code> or empty ("") String will return <code>-1</code>. * A negative start position returns <code>-1</code>. * A start position greater than the string length searches the whole string.</p> * <p/> * <pre> * StringUtility.lastIndexOf(null, *, *) = -1 * StringUtility.lastIndexOf("", *, *) = -1 * StringUtility.lastIndexOf("aabaabaa", 'b', 8) = 5 * StringUtility.lastIndexOf("aabaabaa", 'b', 4) = 2 * StringUtility.lastIndexOf("aabaabaa", 'b', 0) = -1 * StringUtility.lastIndexOf("aabaabaa", 'b', 9) = 5 * StringUtility.lastIndexOf("aabaabaa", 'b', -1) = -1 * StringUtility.lastIndexOf("aabaabaa", 'a', 0) = 0 * </pre> * * @param str the String to check, may be null * @param searchChar the character to find * @param startPos the start position * @return the last index of the search character, * -1 if no match or <code>null</code> string input */ public static int lastIndexOf(String str, char searchChar, int startPos) { if (isEmpty(str)) return -1; return str.lastIndexOf(searchChar, startPos); } /** * <p>Finds the last index within a String, handling <code>null</code>. * This method uses {@link String#lastIndexOf(String)}.</p> * <p/> * <p>A <code>null</code> String will return <code>-1</code>.</p> * <p/> * <pre> * StringUtility.lastIndexOf(null, *) = -1 * StringUtility.lastIndexOf(*, null) = -1 * StringUtility.lastIndexOf("", "") = 0 * StringUtility.lastIndexOf("aabaabaa", "a") = 0 * StringUtility.lastIndexOf("aabaabaa", "b") = 2 * StringUtility.lastIndexOf("aabaabaa", "ab") = 1 * StringUtility.lastIndexOf("aabaabaa", "") = 8 * </pre> * * @param str the String to check, may be null * @param searchStr the String to find, may be null * @return the last index of the search String, * -1 if no match or <code>null</code> string input */ public static int lastIndexOf(String str, String searchStr) { if (str == null || searchStr == null) return -1; return str.lastIndexOf(searchStr); } /** * <p>Finds the first index within a String, handling <code>null</code>. * This method uses {@link String#lastIndexOf(String, int)}.</p> * <p/> * <p>A <code>null</code> String will return <code>-1</code>. * A negative start position returns <code>-1</code>. * An empty ("") search String always matches unless the start position is negative. * A start position greater than the string length searches the whole string.</p> * <p/> * <pre> * StringUtility.lastIndexOf(null, *, *) = -1 * StringUtility.lastIndexOf(*, null, *) = -1 * StringUtility.lastIndexOf("aabaabaa", "a", 8) = 7 * StringUtility.lastIndexOf("aabaabaa", "b", 8) = 5 * StringUtility.lastIndexOf("aabaabaa", "ab", 8) = 4 * StringUtility.lastIndexOf("aabaabaa", "b", 9) = 5 * StringUtility.lastIndexOf("aabaabaa", "b", -1) = -1 * StringUtility.lastIndexOf("aabaabaa", "a", 0) = 0 * StringUtility.lastIndexOf("aabaabaa", "b", 0) = -1 * </pre> * * @param str the String to check, may be null * @param searchStr the String to find, may be null * @param startPos the start position, negative treated as zero * @return the first index of the search String, * -1 if no match or <code>null</code> string input */ public static int lastIndexOf(String str, String searchStr, int startPos) { if (str == null || searchStr == null) return -1; return str.lastIndexOf(searchStr, startPos); } /** * <p>Checks if a String is empty ("") or null.</p> * <p/> * <pre> * StringUtility.isEmpty(null) = true * StringUtility.isEmpty("") = true * StringUtility.isEmpty(" ") = false * StringUtility.isEmpty("bob") = false * StringUtility.isEmpty(" bob ") = false * </pre> * <p/> * <p>NOTE: This method changed in Lang version 2.0. * It no longer trims the String. * That functionality is available in isBlank().</p> * * @param str the String to check, may be null * @return <code>true</code> if the String is empty or null */ public static boolean isEmpty(String str) { return str == null || str.length() == 0; } /** * Append a new string arguments into an original text. */ public static String append(String original, String argStr) { if (isNotEmpty(argStr)) return (original + argStr); else return original; } /** * <p>Checks if a String is not empty ("") and not null.</p> * <p/> * <pre> * StringUtility.isNotEmpty(null) = false * StringUtility.isNotEmpty("") = false * StringUtility.isNotEmpty(" ") = true * StringUtility.isNotEmpty("bob") = true * StringUtility.isNotEmpty(" bob ") = true * </pre> * * @param str the String to check, may be null * @return <code>true</code> if the String is not empty and not null */ public static boolean isNotEmpty(String str) { return str != null && str.length() > 0; } }