下面列出了org.apache.commons.io.FilenameUtils#getPrefixLength ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private String parent(final String absolute) {
final String prefix = FilenameUtils.getPrefix(absolute);
if(absolute.equals(prefix)) {
return null;
}
int index = absolute.length() - 1;
if(absolute.charAt(index) == this.getDelimiter()) {
if(index > 0) {
index--;
}
}
final int cut = absolute.lastIndexOf(this.getDelimiter(), index);
if(cut > FilenameUtils.getPrefixLength(absolute)) {
return absolute.substring(0, cut);
}
return String.valueOf(prefix);
}
private ApplicationArchiveReader getApplicationArchiveReaderForAbsolutePath() {
return new ApplicationArchiveReader() {
@Override
protected void validateEntry(ZipEntry entry) {
String path = entry.getName();
if (!path.equals(FilenameUtils.normalize(path, true))) {
throw new IllegalArgumentException(MessageFormat.format(FileUtils.PATH_SHOULD_BE_NORMALIZED, path));
}
if (FilenameUtils.getPrefixLength(path) != 0 || Paths.get(path)
.isAbsolute()) {
throw new IllegalArgumentException(MessageFormat.format(FileUtils.PATH_SHOULD_NOT_BE_ABSOLUTE, path));
}
}
};
}
/**
* Checks if is file is a relative path.
*
* @param path the path
* @return true if the path is relative
*/
public static Boolean isRelativePath(String path) {
if (path == null) {
return false;
}
int prefixLen = FilenameUtils.getPrefixLength(path);
return !(testPathWin(path, prefixLen) || testPathLinux(prefixLen));
}
/**
* Parse fileset definitions <filelist><fileset>[...]</fileset></filelist> ignoring the rest of the document
*
* @param doc
* @return a list of string matching the contents of the <filelist><fileset> tags in the document
* @throws XPathExpressionException
*/
private List<String> parseFileSets(Document doc, File configFile) throws XPathExpressionException {
List<String> result = new ArrayList<>();
NodeList nodeList = getNodeListMatchingXpath("//filelist/fileset/file", doc);
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Node fileSet = node.getParentNode();
String parentDirPath = nullSafeGetStringAttribute(fileSet, "dir");
if (parentDirPath == null) {
parentDirPath = configFile.getAbsoluteFile().getParent();
}
String filePath = extractFilePath(node);
// warn if file in fileset is using absolute path mode
if (FilenameUtils.getPrefixLength(filePath) > 0) {
LOG.warn("File " + filePath + " in fileset "
+ StringUtils.defaultIfBlank(nullSafeGetStringAttribute(fileSet, "dir"), "")
+ " seems to be an absolute path. Will _not_ be resolved relative to the <fileset>, but as an absolute path. Normally you would want to use relative paths in a //filelist/fileset/file, and absolute paths in a //filelist/file.");
}
result.add(FilenameUtils.concat(parentDirPath, filePath));
}
return result;
}
private static boolean isAbsolute(String path) {
return FilenameUtils.getPrefixLength(path) != 0;
}
private boolean isAbsolute(final String path) {
return FilenameUtils.getPrefixLength(path) != 0;
}
private boolean canResolveRelativeFileName(final String fileName, final String projectDir) {
return StringUtils.isNotBlank(projectDir) && FilenameUtils.getPrefixLength(fileName) == 0
&& !"MSBUILD".equals(fileName.trim());
}