This repository was archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rob Rudin
committed
Feb 20, 2017
1 parent
b0d59cf
commit 66c7d91
Showing
7 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
src/main/java/com/marklogic/client/util/DefaultDocumentFileFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/marklogic/client/util/DocumentFileFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
src/test/java/com/marklogic/client/util/DocumentFinderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.