org.apache.taverna.robundle.utils.RecursiveCopyFileVisitor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of taverna-robundle Show documentation
Show all versions of taverna-robundle Show documentation
API for dealing with RO Bundles
package org.apache.taverna.robundle.utils;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import static java.lang.Integer.MAX_VALUE;
import static java.nio.file.FileVisitOption.FOLLOW_LINKS;
import static java.nio.file.FileVisitResult.CONTINUE;
import static java.nio.file.FileVisitResult.SKIP_SUBTREE;
import static java.nio.file.Files.copy;
import static java.nio.file.Files.getFileAttributeView;
import static java.nio.file.Files.isDirectory;
import static java.nio.file.Files.readAttributes;
import static java.nio.file.Files.walkFileTree;
import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static java.util.EnumSet.noneOf;
import static org.apache.taverna.robundle.utils.RecursiveCopyFileVisitor.RecursiveCopyOption.IGNORE_ERRORS;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.CopyOption;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;
public class RecursiveCopyFileVisitor extends SimpleFileVisitor {
public enum RecursiveCopyOption implements java.nio.file.CopyOption {
/**
* Ignore any errors, copy as much as possible. The default is to stop
* on the first IOException.
*/
IGNORE_ERRORS,
}
public static void copyRecursively(final Path source,
final Path destination, final CopyOption... copyOptions)
throws IOException {
final Set copyOptionsSet = new HashSet<>(
Arrays.asList(copyOptions));
if (!isDirectory(source))
throw new FileNotFoundException("Not a directory: " + source);
if (isDirectory(destination)
&& !copyOptionsSet.contains(REPLACE_EXISTING))
throw new FileAlreadyExistsException(destination.toString());
Path destinationParent = destination.getParent();
if (destinationParent != null && !isDirectory(destinationParent))
throw new FileNotFoundException("Not a directory: "
+ destinationParent);
RecursiveCopyFileVisitor visitor = new RecursiveCopyFileVisitor(
destination, copyOptionsSet, source);
Set walkOptions = noneOf(FileVisitOption.class);
if (!copyOptionsSet.contains(NOFOLLOW_LINKS))
walkOptions = EnumSet.of(FOLLOW_LINKS);
walkFileTree(source, walkOptions, MAX_VALUE, visitor);
}
private final CopyOption[] copyOptions;
private final Set copyOptionsSet;
private final Path destination;
private boolean ignoreErrors;
private final LinkOption[] linkOptions;
private final Path source;
RecursiveCopyFileVisitor(Path destination, Set copyOptionsSet,
Path source) {
this.destination = destination;
this.source = source;
this.copyOptionsSet = new HashSet<>(copyOptionsSet);
HashSet