|
| 1 | +package com.marklogic.client.file; |
| 2 | + |
| 3 | +import com.marklogic.client.helper.LoggingObject; |
| 4 | +import org.springframework.util.ClassUtils; |
| 5 | + |
| 6 | +import java.io.File; |
| 7 | +import java.io.FileFilter; |
| 8 | +import java.io.IOException; |
| 9 | +import java.nio.file.*; |
| 10 | +import java.nio.file.attribute.BasicFileAttributes; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +/** |
| 15 | + * Non-threadsafe implementation that implements FileVisitor as a way of descending one or more file paths. |
| 16 | + */ |
| 17 | +public class DefaultDocumentFileReader extends LoggingObject implements FileVisitor<Path>, DocumentFileReader { |
| 18 | + |
| 19 | + private Path currentRootPath; |
| 20 | + private List<FileFilter> fileFilters; |
| 21 | + private List<DocumentFile> documentFiles; |
| 22 | + private List<DocumentFileProcessor> documentFileProcessors; |
| 23 | + |
| 24 | + public DefaultDocumentFileReader() { |
| 25 | + initialize(); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Walk the file tree at each of the given paths, applying any configured DocumentFileProcessor instances on each |
| 30 | + * DocumentFile that is constructed by a File. |
| 31 | + * |
| 32 | + * @param paths |
| 33 | + * @return |
| 34 | + */ |
| 35 | + public List<DocumentFile> readDocumentFiles(String... paths) { |
| 36 | + documentFiles = new ArrayList<>(); |
| 37 | + for (String path : paths) { |
| 38 | + if (logger.isDebugEnabled()) { |
| 39 | + logger.debug(format("Finding documents at path: %s", path)); |
| 40 | + } |
| 41 | + this.currentRootPath = Paths.get(path); |
| 42 | + try { |
| 43 | + Files.walkFileTree(this.currentRootPath, this); |
| 44 | + } catch (IOException ie) { |
| 45 | + throw new RuntimeException(format("IO error while walking file tree at path: %s", path), ie); |
| 46 | + } |
| 47 | + } |
| 48 | + return documentFiles; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { |
| 53 | + boolean accept = acceptPath(dir, attrs); |
| 54 | + if (accept) { |
| 55 | + if (logger.isTraceEnabled()) { |
| 56 | + logger.trace("Visiting directory: " + dir); |
| 57 | + } |
| 58 | + return FileVisitResult.CONTINUE; |
| 59 | + } else { |
| 60 | + if (logger.isTraceEnabled()) { |
| 61 | + logger.trace("Skipping directory: " + dir); |
| 62 | + } |
| 63 | + return FileVisitResult.SKIP_SUBTREE; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { |
| 69 | + return FileVisitResult.CONTINUE; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { |
| 74 | + return FileVisitResult.CONTINUE; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { |
| 79 | + if (acceptPath(path, attrs)) { |
| 80 | + DocumentFile documentFile = buildDocumentFile(path, currentRootPath); |
| 81 | + documentFile = processDocumentFile(documentFile); |
| 82 | + if (documentFile != null) { |
| 83 | + this.documentFiles.add(documentFile); |
| 84 | + } |
| 85 | + } |
| 86 | + return FileVisitResult.CONTINUE; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * If any of the configured FileFilter objects do not accept the Path, then it is not accepted. |
| 91 | + * |
| 92 | + * @param path |
| 93 | + * @param attrs |
| 94 | + * @return |
| 95 | + */ |
| 96 | + protected boolean acceptPath(Path path, BasicFileAttributes attrs) { |
| 97 | + if (fileFilters != null) { |
| 98 | + File file = path.toFile(); |
| 99 | + for (FileFilter filter : fileFilters) { |
| 100 | + if (!filter.accept(file)) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + return true; |
| 106 | + } |
| 107 | + |
| 108 | + protected DocumentFile buildDocumentFile(Path path, Path currentRootPath) { |
| 109 | + Path relPath = currentRootPath.relativize(path); |
| 110 | + String uri = "/" + relPath.toString().replace("\\", "/"); |
| 111 | + File f = path.toFile(); |
| 112 | + return new DocumentFile(uri, f); |
| 113 | + } |
| 114 | + |
| 115 | + protected DocumentFile processDocumentFile(DocumentFile documentFile) { |
| 116 | + for (DocumentFileProcessor processor : documentFileProcessors) { |
| 117 | + documentFile = processor.processDocumentFile(documentFile); |
| 118 | + if (documentFile == null) { |
| 119 | + break; |
| 120 | + } |
| 121 | + } |
| 122 | + return documentFile; |
| 123 | + } |
| 124 | + |
| 125 | + protected void initialize() { |
| 126 | + CollectionsDocumentFileProcessor cdfp = new CollectionsDocumentFileProcessor(); |
| 127 | + PermissionsDocumentFileProcessor pdfp = new PermissionsDocumentFileProcessor(); |
| 128 | + |
| 129 | + addFileFilter(cdfp); |
| 130 | + addFileFilter(pdfp); |
| 131 | + |
| 132 | + addDocumentFileProcessor(cdfp); |
| 133 | + addDocumentFileProcessor(pdfp); |
| 134 | + addDocumentFileProcessor(new FormatDocumentFileProcessor()); |
| 135 | + } |
| 136 | + |
| 137 | + public DocumentFileProcessor getDocumentFileProcessor(String classShortName) { |
| 138 | + for (DocumentFileProcessor processor : documentFileProcessors) { |
| 139 | + if (ClassUtils.getShortName(processor.getClass()).equals(classShortName)) { |
| 140 | + return processor; |
| 141 | + } |
| 142 | + } |
| 143 | + return null; |
| 144 | + } |
| 145 | + |
| 146 | + public void addDocumentFileProcessor(DocumentFileProcessor processor) { |
| 147 | + if (documentFileProcessors == null) { |
| 148 | + documentFileProcessors = new ArrayList<>(); |
| 149 | + } |
| 150 | + documentFileProcessors.add(processor); |
| 151 | + } |
| 152 | + |
| 153 | + public void addFileFilter(FileFilter fileFilter) { |
| 154 | + if (fileFilters == null) { |
| 155 | + fileFilters = new ArrayList<>(); |
| 156 | + } |
| 157 | + fileFilters.add(fileFilter); |
| 158 | + } |
| 159 | + |
| 160 | + public List<DocumentFileProcessor> getDocumentFileProcessors() { |
| 161 | + return documentFileProcessors; |
| 162 | + } |
| 163 | + |
| 164 | + public void setDocumentFileProcessors(List<DocumentFileProcessor> documentFileProcessors) { |
| 165 | + this.documentFileProcessors = documentFileProcessors; |
| 166 | + } |
| 167 | + |
| 168 | + public List<FileFilter> getFileFilters() { |
| 169 | + return fileFilters; |
| 170 | + } |
| 171 | + |
| 172 | + public void setFileFilters(List<FileFilter> fileFilters) { |
| 173 | + this.fileFilters = fileFilters; |
| 174 | + } |
| 175 | +} |
0 commit comments