/*******************************************************************************
* 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.path;
import com.perforce.p4java.extension.path.internal.PerforcePathImpl;
import com.perforce.p4java.extension.utility.PathUtil;
/**
*
* @since 2012.1
*/
public class PerforcePathFactory {
private static final String HEAD_REVISION = "#head";
private static final String SINGLE_LEVEL_MATCH = "*";
private static final String RECURSIVE_MATCH = "...";
/**
* Construct an instance with given escaped (ASCII-expanded) depot path.
*
* @param depotPath
* The depot path with ASCII expansion.
*/
public static PerforcePath getPath(String depotPath) {
if (depotPath == null)
return null;
String basePath = PathUtil.extractParentWithTrailingSlash(depotPath);
String fileName = PathUtil.extractUnqualifiedName(depotPath);
if (fileName.isEmpty())
return new PerforcePathImpl(basePath, null, null);
if (fileName.equals(RECURSIVE_MATCH)
|| fileName.equals(SINGLE_LEVEL_MATCH))
return new PerforcePathImpl(basePath, null, fileName);
if (fileName.endsWith(HEAD_REVISION))
return new PerforcePathImpl(basePath, fileName.substring(0,
fileName.length() - HEAD_REVISION.length()), HEAD_REVISION);
String revisionString = PathUtil.getRevisionString(fileName);
if (!revisionString.equals(""))
fileName = PathUtil.stripRevisionSpecifier(fileName);
return new PerforcePathImpl(basePath, fileName, revisionString);
}
/**
*
* @param revisionlessDepotPath
* An escaped path without revision info.
* @return
*/
public static PerforcePath getPathFromRevisionlessDepotPath(
String revisionlessDepotPath) {
if (revisionlessDepotPath == null)
return null;
// revisionlessDepotPath = PathStringFormat.UNESCAPE
// .modify(revisionlessDepotPath);
String basePath = PathUtil
.extractParentWithTrailingSlash(revisionlessDepotPath);
String fileName = PathUtil
.extractUnqualifiedName(revisionlessDepotPath);
return new PerforcePathImpl(basePath, fileName, "");
}
/**
*
* @param depotPath
* an escaped depot path
* @param revisionString
* @return
*/
public static PerforcePath getPath(String depotPath, String revisionString) {
if (depotPath == null)
return null;
// depotPath = PathStringFormat.UNESCAPE.modify(depotPath);
String basePath = PathUtil.extractParentWithTrailingSlash(depotPath);
String fileName = PathUtil.extractUnqualifiedName(depotPath);
if (fileName.isEmpty())
return new PerforcePathImpl(basePath, null, revisionString);
else if (fileName.equals(RECURSIVE_MATCH)
|| fileName.equals(SINGLE_LEVEL_MATCH))
return new PerforcePathImpl(basePath, fileName, null);
return new PerforcePathImpl(basePath, fileName, revisionString);
}
/**
*
* @param depotPath
* an escaped depot path
* @return
*/
public static PerforcePath getHeadDepotPath(String depotPath) {
if (depotPath == null)
return null;
// depotPath = PathStringFormat.UNESCAPE.modify(depotPath);
String basePath = PathUtil.extractParentWithTrailingSlash(depotPath);
String fileName = PathUtil.extractUnqualifiedName(depotPath);
if (fileName.isEmpty())
return new PerforcePathImpl(basePath, null, null);
fileName = PathUtil.stripRevisionSpecifier(fileName);
return new PerforcePathImpl(basePath, fileName, HEAD_REVISION);
}
public static PerforcePath getHeadDepotPath(PerforcePath path) {
return new PerforcePathImpl(path, HEAD_REVISION);
}
public static PerforcePath getPathWithNewRevision(PerforcePath path,
int revision) {
return getPathWithNewRevision(path, "#" + String.valueOf(revision));
}
public static PerforcePath getPathWithNewRevision(PerforcePath path,
String revisionString) {
return new PerforcePathImpl(path, revisionString);
}
/**
*
* @param folderPath
* An escaped path
* @return
*/
public static PerforcePath getFolderPath(String folderPath) {
if (folderPath == null)
return null;
if (folderPath.endsWith(RECURSIVE_MATCH))
return new PerforcePathImpl(folderPath.substring(0,
folderPath.length() - RECURSIVE_MATCH.length()), null,
RECURSIVE_MATCH);
if (folderPath.endsWith(SINGLE_LEVEL_MATCH))
return new PerforcePathImpl(folderPath.substring(0,
folderPath.length() - SINGLE_LEVEL_MATCH.length()), null,
SINGLE_LEVEL_MATCH);
return new PerforcePathImpl(folderPath, null, null);
}
/**
*
* @param folderPath
* An escaped path
* @param revisionString
* in the format of #123
* @return
*/
public static PerforcePath getFolderPath(String folderPath,
String revisionString) {
return new PerforcePathImpl(folderPath, null, revisionString);
}
}