/*******************************************************************************
* Copyright (c) 2013, Perforce Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* 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 HOLDER 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.
******************************************************************************/
package com.perforce.p4java.extension.utility;
import com.perforce.p4java.extension.path.PathStringFormat;
/**
* Utilities for manipulating depot paths.
*/
public class PathUtil {
public static String escape(final String path) {
if (path == null)
return path;
return PathStringFormat.ESCAPE.modify(path);
}
public static String escapeAndEnquote(final String path) {
String p = path;
p = PathStringFormat.ESCAPE.modify(p);
return PathStringFormat.ENQUOTE.modify(p);
}
public static String unescape(final String path) {
if (path == null)
return path;
return PathStringFormat.UNESCAPE.modify(path);
}
public static String unescapeAndunenquote(final String path) {
String p = path;
p = PathStringFormat.UNENQUOTE.modify(p);
return PathStringFormat.UNESCAPE.modify(p);
}
public static String[] append(String[] segments, String newSeg) {
String[] newSegments = new String[segments.length + 1];
System.arraycopy(segments, 0, newSegments, 0, segments.length);
newSegments[segments.length] = newSeg;
return newSegments;
}
/**
* Convert array to depot path.
* <p/>
* e.g., ["depot1","folder1","file1"] to "depot1/folder1/file1"
*
* @param segments
* @return concatenation of the segments with "/"
*/
public static String concatenate(String[] segments, String separator) {
if (segments == null || segments.length == 0)
return "";
StringBuilder sb = new StringBuilder(segments[0]);
for (int i = 1; i < segments.length; i++) {
sb.append(separator);
sb.append(segments[i]);
}
return sb.toString();
}
public static String ensureTrailingSlash(String path) {
if (path.length() > 0 && !path.endsWith("/"))
path = path + "/";
return path;
}
public static String removeAsteriskAndEnsureTrailingSlash(String path) {
if (path.endsWith("*"))
path = path.substring(0, path.length() - 1);
return ensureTrailingSlash(path);
}
public static String stripEndingSlashDots(String path) {
while (path.endsWith(".")) {
path = path.substring(0, path.length() - 1);
}
while (path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
return path;
}
public static String stripLeadingOrTrailingSlash(String path) {
while (path.startsWith("/")) {
path = path.substring(1);
}
while (path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
return path;
}
public static String stripTrailingSlash(String depotPath) {
if (depotPath == null || depotPath.length() <= 0)
return "";
if (depotPath.endsWith("/"))
depotPath = depotPath.substring(0, depotPath.length() - 1);
return depotPath;
}
public static String extractUnqualifiedName(String depotPath) {
if (depotPath == null || depotPath.length() <= 0)
return "";
return depotPath.substring(depotPath.lastIndexOf('/') + 1);
}
public static String stripRevisionSpecifier(String depotPath) {
if (depotPath == null || depotPath.length() <= 0)
return "";
if (!depotPath.contains("#"))
return depotPath;
return depotPath.substring(0, depotPath.lastIndexOf('#'));
}
public static String extractParent(String depotPath) {
if (depotPath == null || depotPath.length() <= 0
|| depotPath.lastIndexOf('/') <= 0)
return "";
return depotPath.substring(0, depotPath.lastIndexOf('/'));
}
public static String[] removeLastSegment(String[] path) {
if (path == null || path.length <= 1) {
return new String[0];
}
String[] dest = new String[path.length - 1];
System.arraycopy(path, 0, dest, 0, path.length - 1);
return dest;
}
public static String extractParentWithTrailingSlash(String depotPath) {
if (depotPath == null || depotPath.length() <= 0)
return "";
return depotPath.substring(0, depotPath.lastIndexOf('/') + 1);
}
public static String getRevisionString(String depotPath) {
String revisionString = "";
if (depotPath != null) {
if (depotPath.lastIndexOf('#') > 0) {
String rev = depotPath
.substring(depotPath.lastIndexOf('#') + 1);
try {
int retVal = Integer.valueOf(rev);
revisionString = "#" + retVal;
} catch (NumberFormatException e) {
if (rev.equals("head") || rev.equals("none"))
revisionString = "#" + rev;
}
}
}
return revisionString;
}
public static String quote(String src) {
if (src == null || src.length() <= 0)
return "";
return "\"" + src + "\"";
}
public static String dequote(String src) {
if (src != null && src.length() > 2 && src.startsWith("\"")
&& src.endsWith("\"")) {
return src.substring(1, src.length() - 1);
}
return src;
}
}