當前位置: 首頁>>代碼示例>>Java>>正文


Java FileUtils.getRelativePath方法代碼示例

本文整理匯總了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;
}
 
開發者ID:jballant,項目名稱:CommonJSAutoComplete,代碼行數:18,代碼來源:StringUtil.java

示例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();
    }
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:8,代碼來源:JNLPUpdateManifestStartup.java

示例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(
                '\\', '/') : ".");
    }
}
 
開發者ID:apache,項目名稱:ant,代碼行數:25,代碼來源:XSLTProcess.java

示例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;
            }
        }
    }
}
 
開發者ID:apache,項目名稱:ant,代碼行數:82,代碼來源:Property.java

示例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());
}
 
開發者ID:apache,項目名稱:ant,代碼行數:82,代碼來源:ManifestClassPath.java


注:本文中的org.apache.tools.ant.util.FileUtils.getRelativePath方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。