Java Code Examples for java.nio.file.Path#isAbsolute()

The following examples show how to use java.nio.file.Path#isAbsolute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SecretAnnotationProcessor.java    From module-ballerina-kubernetes with Apache License 2.0 6 votes vote down vote up
private SecretModel getBallerinaConfSecret(String configFilePath, String serviceName) throws
        KubernetesPluginException {
    //create a new config map model with ballerina conf
    SecretModel secretModel = new SecretModel();
    secretModel.setName(getValidName(serviceName) + "-ballerina-conf" + SECRET_POSTFIX);
    secretModel.setMountPath(BALLERINA_CONF_MOUNT_PATH);
    Path dataFilePath = Paths.get(configFilePath);
    if (!dataFilePath.isAbsolute()) {
        dataFilePath = KubernetesContext.getInstance().getDataHolder().getSourceRoot().resolve(dataFilePath)
                .normalize();
    }
    String content = Base64.encodeBase64String(KubernetesUtils.readFileContent(dataFilePath));
    Map<String, String> dataMap = new HashMap<>();
    dataMap.put(BALLERINA_CONF_FILE_NAME, content);
    secretModel.setData(dataMap);
    secretModel.setBallerinaConf(configFilePath);
    secretModel.setReadOnly(false);
    return secretModel;
}
 
Example 2
Source File: Serializer.java    From buck with Apache License 2.0 6 votes vote down vote up
@Override
public void visitPath(Path path) throws IOException {
  if (path.isAbsolute()) {
    stream.writeBoolean(true);
    Path cellPath = rootCellPath;
    Optional<String> cellName = Optional.empty();
    for (Map.Entry<AbsPath, Optional<String>> candidate : cellMap.entrySet()) {
      if (path.startsWith(candidate.getKey().getPath())) {
        cellPath = candidate.getKey().getPath();
        cellName = candidate.getValue();
      }
    }
    ValueTypeInfoFactory.forTypeToken(new TypeToken<Optional<String>>() {})
        .visit(cellName, this);
    writeRelativePath(cellPath.relativize(path));
  } else {
    stream.writeBoolean(false);
    writeRelativePath(path);
  }
}
 
Example 3
Source File: PathOps.java    From yajsync with GNU General Public License v3.0 6 votes vote down vote up
public static Path subtractPathOrNull(Path parent, Path sub)
{
    if (!parent.endsWith(sub)) {
        throw new IllegalArgumentException(String.format(
                "%s is not a parent path of %s", parent, sub));
    }
    if (parent.getNameCount() == sub.getNameCount()) {
        return parent.getRoot(); // NOTE: return null if parent has no root
    }
    Path res = parent.subpath(0,
                              parent.getNameCount() - sub.getNameCount());
    if (parent.isAbsolute()) {
        return parent.getRoot().resolve(res);
    } else {
        return res;
    }
}
 
Example 4
Source File: FileSystemView.java    From jimfs with Apache License 2.0 6 votes vote down vote up
/** Checks that the given file can be deleted, throwing an exception if it can't. */
private void checkDeletable(File file, DeleteMode mode, Path path) throws IOException {
  if (file.isRootDirectory()) {
    throw new FileSystemException(path.toString(), null, "can't delete root directory");
  }

  if (file.isDirectory()) {
    if (mode == DeleteMode.NON_DIRECTORY_ONLY) {
      throw new FileSystemException(path.toString(), null, "can't delete: is a directory");
    }

    checkEmpty(((Directory) file), path);
  } else if (mode == DeleteMode.DIRECTORY_ONLY) {
    throw new FileSystemException(path.toString(), null, "can't delete: is not a directory");
  }

  if (file == workingDirectory && !path.isAbsolute()) {
    // this is weird, but on Unix at least, the file system seems to be happy to delete the
    // working directory if you give the absolute path to it but fail if you use a relative path
    // that resolves to the working directory (e.g. "" or ".")
    throw new FileSystemException(path.toString(), null, "invalid argument");
  }
}
 
Example 5
Source File: FastPaths.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Adds the Path to the hasher as unencoded chars. Roughly equivalent to {@code
 * hasher.putUnencodedChars(path.toString())}.
 */
public static Hasher hashPathFast(Hasher hasher, Path path) {
  if (!(path instanceof BuckUnixPath)) {
    return hasher.putUnencodedChars(path.toString());
  }
  if (path.isAbsolute()) {
    hasher.putChar('/');
  }
  for (int i = 0; i < path.getNameCount(); i++) {
    if (i != 0) {
      hasher.putChar('/');
    }
    hasher.putUnencodedChars(FastPaths.getNameString(path, i));
  }
  return hasher;
}
 
Example 6
Source File: ApacheFlexJSUtils.java    From vscode-as3mxml with Apache License 2.0 6 votes vote down vote up
/**
 * Determines if a directory contains a valid Apache FlexJS SDK.
 */
public static boolean isValidSDK(Path absolutePath)
{
	if(absolutePath == null || !absolutePath.isAbsolute())
	{
		return false;
	}
	File file = absolutePath.toFile();
	if(!file.isDirectory())
	{
		return false;
	}
	Path sdkDescriptionPath = absolutePath.resolve(FLEX_SDK_DESCRIPTION);
	file = sdkDescriptionPath.toFile();
	if(!file.exists() || file.isDirectory())
	{
		return false;
	}
	Path compilerPath = absolutePath.resolve(JS).resolve(BIN).resolve(ASJSC);
	file = compilerPath.toFile();
	if(!file.exists() || file.isDirectory())
	{
		return false;
	}
	return true;
}
 
Example 7
Source File: JigReportsTask.java    From jig with Apache License 2.0 5 votes vote down vote up
Path outputDirectory(JigConfig config) {
    Project project = getProject();
    Path path = Paths.get(config.getOutputDirectory());
    if (path.isAbsolute()) return path;

    return project.getBuildDir().toPath().resolve("jig");
}
 
Example 8
Source File: WatchServiceFileWatcher.java    From mirror with Apache License 2.0 5 votes vote down vote up
private void onChangedSymbolicLink(BlockingQueue<Update> queue, Path path) throws IOException {
  Path symlink = Files.readSymbolicLink(path);
  String targetPath;
  if (symlink.isAbsolute()) {
    targetPath = path.getParent().toAbsolutePath().relativize(symlink).toString();
  } else {
    // the symlink is already relative, so we can leave it alone, e.g. foo.txt
    targetPath = symlink.toString();
  }
  String relativePath = toRelativePath(path);
  log.trace("Symlink {}, relative={}, target={}", path, relativePath, targetPath);
  put(queue, Update.newBuilder().setPath(relativePath).setSymlink(targetPath).setModTime(lastModified(path)).setLocal(true).build());
}
 
Example 9
Source File: MCRDirectoryStream.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void deleteFile(Path path) throws IOException {
    checkClosed();
    if (path.isAbsolute()) {
        Files.delete(path);
    }
    final MCRStoredNode storedNode = resolve(path);
    final MCRPath mcrPath = getCurrentSecurePath(storedNode);
    storedNode.delete();
    MCRPathEventHelper.fireFileDeleteEvent(mcrPath);
}
 
Example 10
Source File: TaskUpdateWebpack.java    From flow with Apache License 2.0 5 votes vote down vote up
private String getEscapedRelativeWebpackPath(Path path) {
    if (path.isAbsolute()) {
        return FrontendUtils.getUnixRelativePath(webpackConfigPath, path);
    } else {
        return FrontendUtils.getUnixPath(path);
    }
}
 
Example 11
Source File: PathArguments.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Filter files under the project root, and convert to canonical relative path style. For example,
 * the project root is /project, 1. file path /project/./src/com/facebook/./test/../Test.java will
 * be converted to src/com/facebook/Test.java 2. file path
 * /otherproject/src/com/facebook/Test.java will be ignored.
 */
static ReferencedFiles getCanonicalFilesUnderProjectRoot(
    AbsPath projectRoot, Iterable<String> nonCanonicalFilePaths) throws IOException {
  // toRealPath() is used throughout to resolve symlinks or else the Path.startsWith() check will
  // not be reliable.
  ImmutableSet.Builder<RelPath> projectFiles = ImmutableSet.builder();
  ImmutableSet.Builder<AbsPath> nonProjectFiles = ImmutableSet.builder();
  AbsPath normalizedRoot = projectRoot.toRealPath();
  for (String filePath : nonCanonicalFilePaths) {
    Path canonicalFullPath = Paths.get(filePath);
    if (!canonicalFullPath.isAbsolute()) {
      canonicalFullPath = projectRoot.resolve(canonicalFullPath).getPath();
    }
    if (!canonicalFullPath.toFile().exists()) {
      nonProjectFiles.add(AbsPath.of(canonicalFullPath));
      continue;
    }
    canonicalFullPath = canonicalFullPath.toRealPath();

    // Ignore files that aren't under project root.
    if (canonicalFullPath.startsWith(normalizedRoot.getPath())) {
      Path relativePath =
          canonicalFullPath.subpath(
              normalizedRoot.getPath().getNameCount(), canonicalFullPath.getNameCount());
      projectFiles.add(RelPath.of(relativePath));
    } else {
      nonProjectFiles.add(AbsPath.of(canonicalFullPath));
    }
  }
  return new ReferencedFiles(projectFiles.build(), nonProjectFiles.build());
}
 
Example 12
Source File: Location.java    From goclipse with Eclipse Public License 1.0 5 votes vote down vote up
/** @return a valid {@link Location} from given path. 
 * @throws CommonException if a valid Location could not be created. 
 * Given errorMessagePrefix will be used as a prefix in {@link CommonException}'s message. */
public static Location createValidLocation(Path path, String errorMessagePrefix) throws CommonException {
	if(!path.isAbsolute()) {
		throw new CommonException(errorMessagePrefix + "Path `"+ path.toString()+"` is not absolute.");
	}
	return new Location(path);
}
 
Example 13
Source File: IsolationChecker.java    From buck with Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<Path> visitPath(Path value) {
  if (value.isAbsolute()) {
    return ImmutableList.of(value);
  }
  return ImmutableList.of();
}
 
Example 14
Source File: StubConfiguration.java    From archiva with Apache License 2.0 5 votes vote down vote up
@Override
public Path getDataDirectory() {
    if (configuration!=null && configuration.getArchivaRuntimeConfiguration()!=null && StringUtils.isNotEmpty(configuration.getArchivaRuntimeConfiguration().getDataDirectory())) {
        Path dataDir = Paths.get(configuration.getArchivaRuntimeConfiguration().getDataDirectory());
        if (dataDir.isAbsolute()) {
            return dataDir;
        } else {
            return getAppServerBaseDir().resolve(dataDir);
        }
    } else {
        return getAppServerBaseDir().resolve("data");
    }

}
 
Example 15
Source File: WorkDir.java    From tmc-cli with MIT License 5 votes vote down vote up
private Path makeAbsolute(Path path) {
    if (path.isAbsolute()) {
        return path;
    } else {
        return workdir.resolve(path);
    }
}
 
Example 16
Source File: DefaultUpdateHandler.java    From update4j with Apache License 2.0 4 votes vote down vote up
private String compactName(Path name) {
    Path relative = FileUtils.relativize(context.getConfiguration().getBasePath(), name);
    return relative.isAbsolute() ? relative.getFileName().toString() : relative.toString();
}
 
Example 17
Source File: DockerOperationsImpl.java    From vespa with Apache License 2.0 4 votes vote down vote up
private Path resolveNodePath(String pathString) {
    Path path = context.fileSystem().getPath(pathString);
    return path.isAbsolute() ? path : context.pathInNodeUnderVespaHome(path);
}
 
Example 18
Source File: MCRFileSystemProvider.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
BasicFileAttributeViewImpl(Path path) {
    this.path = MCRPath.toMCRPath(path);
    if (!path.isAbsolute()) {
        throw new InvalidPathException(path.toString(), "'path' must be absolute.");
    }
}
 
Example 19
Source File: AbsPathImpl.java    From buck with Apache License 2.0 4 votes vote down vote up
public AbsPathImpl(Path path) {
  super(path);
  if (!path.isAbsolute()) {
    throw new IllegalArgumentException("path must be absolute: " + path);
  }
}
 
Example 20
Source File: ProjectUtils.java    From vscode-as3mxml with Apache License 2.0 4 votes vote down vote up
public static String findOutputDirectory(String mainFile, String outputValue, boolean isSWF)
{
	if(outputValue == null)
	{
		if(mainFile == null)
		{
			return System.getProperty("user.dir");
		}
		Path mainFilePath = Paths.get(mainFile);
		if(!mainFilePath.isAbsolute())
		{
			mainFilePath = Paths.get(System.getProperty("user.dir"), mainFile);
		}
		Path mainFileParentPath = mainFilePath.getParent();
		if(mainFileParentPath == null)
		{
			return System.getProperty("user.dir");
		}
		if(!isSWF)
		{
			//Royale treats these directory structures as a special case
			String mainFileParentPathAsString = mainFileParentPath.toString();
			if(mainFileParentPathAsString.endsWith("/src") ||
				mainFileParentPathAsString.endsWith("\\src"))
			{
				mainFileParentPath = mainFileParentPath.resolve("../");
			}
			else if(mainFileParentPathAsString.endsWith("/src/main/flex") ||
				mainFileParentPathAsString.endsWith("\\src\\main\\flex") ||
				mainFileParentPathAsString.endsWith("/src/main/royale") ||
				mainFileParentPathAsString.endsWith("\\src\\main\\royale"))
			{
				mainFileParentPath = mainFileParentPath.resolve("../../../");
			}
			try
			{
				return mainFileParentPath.toFile().getCanonicalPath();
			}
			catch(IOException e)
			{
				return null;
			}
		}
		return mainFileParentPath.toString();
	}
	Path outputPath = Paths.get(outputValue);
	if(!outputPath.isAbsolute())
	{
		outputPath = Paths.get(System.getProperty("user.dir"), outputValue);
	}
	if(!isSWF)
	{
		return outputPath.toString();
	}
	Path outputValueParentPath = outputPath.getParent();
	return outputValueParentPath.toString();
}