本文整理匯總了Java中org.apache.tools.ant.util.FileUtils.getRelativePath方法的典型用法代碼示例。如果您正苦於以下問題:Java FileUtils.getRelativePath方法的具體用法?Java FileUtils.getRelativePath怎麽用?Java FileUtils.getRelativePath使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.tools.ant.util.FileUtils
的用法示例。
在下文中一共展示了FileUtils.getRelativePath方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: relativizePaths
import org.apache.tools.ant.util.FileUtils; //導入方法依賴的package包/類
public static @Nullable String relativizePaths(
@NotNull String aAbsolutePath,
@NotNull String bAbsolutePath
) {
String relPath;
try {
relPath = FileUtils.getRelativePath(new File(aAbsolutePath), new File(bAbsolutePath));
// If the first character is not a '.' we add a ./ to indicate that
// the file is in the same directory and not a node module
if (relPath.charAt(0) != '.') {
relPath = "./".concat(relPath);
}
} catch (Exception e) {
return null;
}
return relPath;
}
示例2: safeRelativePath
import org.apache.tools.ant.util.FileUtils; //導入方法依賴的package包/類
private static String safeRelativePath(File from, File to) {
try {
return FileUtils.getRelativePath(from, to);
} catch (Exception ex) {
return to.getAbsolutePath();
}
}
示例3: setLiaisonDynamicFileParameters
import org.apache.tools.ant.util.FileUtils; //導入方法依賴的package包/類
/**
* Sets file parameter(s) for directory and filename if the attribute
* 'filenameparameter' or 'filedirparameter' are set in the task.
*
* @param liaison to change parameters for
* @param inFile to get the additional file information from
* @throws Exception if an exception occurs on filename lookup
*
* @since Ant 1.7
*/
private void setLiaisonDynamicFileParameters(
final XSLTLiaison liaison, final File inFile) throws Exception { //NOSONAR
if (fileNameParameter != null) {
liaison.addParam(fileNameParameter, inFile.getName());
}
if (fileDirParameter != null) {
final String fileName = FileUtils.getRelativePath(baseDir, inFile);
final File file = new File(fileName);
// Give always a slash as file separator, so the stylesheet could be sure about that
// Use '.' so a dir + "/" + name would not result in an absolute path
liaison.addParam(fileDirParameter, file.getParent() != null ? file.getParent().replace(
'\\', '/') : ".");
}
}
示例4: execute
import org.apache.tools.ant.util.FileUtils; //導入方法依賴的package包/類
/**
* set the property in the project to the value.
* if the task was give a file, resource or env attribute
* here is where it is loaded
* @throws BuildException on error
*/
@Override
public void execute() throws BuildException {
if (getProject() == null) {
throw new IllegalStateException("project has not been set");
}
if (name != null) {
if (untypedValue == null && ref == null) {
throw new BuildException(
"You must specify value, location or refid with the name attribute",
getLocation());
}
} else {
if (url == null && file == null && resource == null
&& env == null) {
throw new BuildException(
"You must specify url, file, resource or environment when not using the name attribute",
getLocation());
}
}
if (url == null && file == null && resource == null && prefix != null) {
throw new BuildException(
"Prefix is only valid when loading from a url, file or resource",
getLocation());
}
if (name != null && untypedValue != null) {
if (relative) {
try {
File from =
untypedValue instanceof File ? (File) untypedValue
: new File(untypedValue.toString());
File to = basedir != null ? basedir : getProject().getBaseDir();
String relPath = FileUtils.getRelativePath(to, from);
relPath = relPath.replace('/', File.separatorChar);
addProperty(name, relPath);
} catch (Exception e) {
throw new BuildException(e, getLocation());
}
} else {
addProperty(name, untypedValue);
}
}
if (file != null) {
loadFile(file);
}
if (url != null) {
loadUrl(url);
}
if (resource != null) {
loadResource(resource);
}
if (env != null) {
loadEnvironment(env);
}
if ((name != null) && (ref != null)) {
try {
addProperty(name,
ref.getReferencedObject(getProject()).toString());
} catch (BuildException be) {
if (fallback != null) {
addProperty(name,
ref.getReferencedObject(fallback).toString());
} else {
throw be;
}
}
}
}
示例5: execute
import org.apache.tools.ant.util.FileUtils; //導入方法依賴的package包/類
/**
* Sets a property, which must not already exist, with a space
* separated list of files and directories relative to the jar
* file's parent directory.
*/
@Override
public void execute() {
if (name == null) {
throw new BuildException("Missing 'property' attribute!");
}
if (dir == null) {
throw new BuildException("Missing 'jarfile' attribute!");
}
if (getProject().getProperty(name) != null) {
throw new BuildException("Property '%s' already set!", name);
}
if (path == null) {
throw new BuildException("Missing nested <classpath>!");
}
StringBuilder tooLongSb = new StringBuilder();
for (int i = 0; i < maxParentLevels + 1; i++) {
tooLongSb.append("../");
}
final String tooLongPrefix = tooLongSb.toString();
// Normalize the reference directory (containing the jar)
final FileUtils fileUtils = FileUtils.getFileUtils();
dir = fileUtils.normalize(dir.getAbsolutePath());
String[] elements = path.list();
StringBuilder buffer = new StringBuilder();
for (String element : elements) {
// Normalize the current file
File pathEntry = new File(element);
String fullPath = pathEntry.getAbsolutePath();
pathEntry = fileUtils.normalize(fullPath);
String relPath = null;
String canonicalPath = null;
try {
if (dir.equals(pathEntry)) {
relPath = ".";
} else {
relPath = FileUtils.getRelativePath(dir, pathEntry);
}
canonicalPath = pathEntry.getCanonicalPath();
// getRelativePath always uses '/' as separator, adapt
if (File.separatorChar != '/') {
canonicalPath =
canonicalPath.replace(File.separatorChar, '/');
}
} catch (Exception e) {
throw new BuildException("error trying to get the relative path"
+ " from " + dir + " to " + fullPath,
e);
}
// No match, so bail out!
if (relPath.equals(canonicalPath)
|| relPath.startsWith(tooLongPrefix)) {
throw new BuildException(
"No suitable relative path from %s to %s", dir, fullPath);
}
if (pathEntry.isDirectory() && !relPath.endsWith("/")) {
relPath = relPath + '/';
}
relPath = Locator.encodeURI(relPath);
// Manifest's ClassPath: attribute always uses forward
// slashes '/', and is space-separated. Ant will properly
// format it on 72 columns with proper line continuation
buffer.append(relPath);
buffer.append(' ');
}
// Finally assign the property with the manifest classpath
getProject().setNewProperty(name, buffer.toString().trim());
}