Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
#49 Added DocumentFileFinder
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Rudin committed Feb 20, 2017
1 parent b0d59cf commit 66c7d91
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.marklogic.client.util;

import com.marklogic.client.helper.LoggingObject;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;

public class DefaultDocumentFileFinder extends LoggingObject implements FileVisitor<Path>, DocumentFileFinder {

private Path currentAssetPath;
private FileFilter fileFilter;
private List<DocumentFile> documentFiles;

public List<DocumentFile> findDocumentFiles(String... paths) {
documentFiles = new ArrayList<>();
for (String path : paths) {
if (logger.isDebugEnabled()) {
logger.debug(format("Finding documents at path: %s", path));
}
this.currentAssetPath = Paths.get(path);
try {
Files.walkFileTree(this.currentAssetPath, this);
} catch (IOException ie) {
throw new RuntimeException(format("IO error while walking file tree at path: %s", path), ie);
}
}
return documentFiles;
}

@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
boolean accept = fileFilter == null || fileFilter.accept(dir.toFile());
if (accept) {
if (logger.isTraceEnabled()) {
logger.trace("Visiting directory: " + dir);
}
return FileVisitResult.CONTINUE;
} else {
if (logger.isTraceEnabled()) {
logger.trace("Skipping directory: " + dir);
}
return FileVisitResult.SKIP_SUBTREE;
}
}

@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
if (fileFilter == null || fileFilter.accept(path.toFile())) {
Path relPath = currentAssetPath.relativize(path);
String uri = "/" + relPath.toString().replace("\\", "/");
File f = path.toFile();
this.documentFiles.add(new DocumentFile(uri, f));
}
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}

public void setFileFilter(FileFilter fileFilter) {
this.fileFilter = fileFilter;
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/marklogic/client/util/DocumentFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.marklogic.client.util;

import java.io.File;

public class DocumentFile {

private String uri;
private File file;

public DocumentFile(String uri, File file) {
this.uri = uri;
this.file = file;
}

public String getUri() {
return uri;
}

public File getFile() {
return file;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.marklogic.client.util;

import java.util.List;

public interface DocumentFileFinder {

List<DocumentFile> findDocumentFiles(String... paths);
}
27 changes: 27 additions & 0 deletions src/test/java/com/marklogic/client/util/DocumentFinderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.marklogic.client.util;

import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class DocumentFinderTest extends Assert {

private DocumentFileFinder sut = new DefaultDocumentFileFinder();

@Test
public void noFileFilter() {
String path = "src/test/resources/schemas";
List<DocumentFile> list = sut.findDocumentFiles(path);
assertEquals(3, list.size());

List<String> uris = new ArrayList<>();
for (DocumentFile file : list) {
uris.add(file.getUri());
}
assertTrue(uris.contains("/child/child.tde"));
assertTrue(uris.contains("/child/grandchild/grandchild.tde"));
assertTrue(uris.contains("/parent.tde"));
}
}
Empty file.
Empty file.
Empty file.

0 comments on commit 66c7d91

Please sign in to comment.