X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Ffile%2Fprovider%2FContentDirectoryStream.java;fp=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Ffile%2Fprovider%2FContentDirectoryStream.java;h=2cddf6275a67c4d338b7088ccc198e7ad1404248;hb=60bf0339227cc064a4ead694e3a699581a025233;hp=0000000000000000000000000000000000000000;hpb=4086635cfaa04c8a184124048794398b0ba96a55;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/file/provider/ContentDirectoryStream.java b/org.argeo.cms/src/org/argeo/cms/file/provider/ContentDirectoryStream.java new file mode 100644 index 000000000..2cddf6275 --- /dev/null +++ b/org.argeo.cms/src/org/argeo/cms/file/provider/ContentDirectoryStream.java @@ -0,0 +1,88 @@ +package org.argeo.cms.file.provider; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.DirectoryStream; +import java.nio.file.Path; +import java.util.Iterator; + +import org.argeo.api.acr.Content; +import org.argeo.api.acr.DName; + +public class ContentDirectoryStream implements DirectoryStream { + private final CmsPath dir; + private final Filter filter; + + private FilesAndCollectionsIterator iterator; + + public ContentDirectoryStream(CmsPath dir, Filter filter) { + this.dir = dir; + this.filter = filter; + } + + @Override + public void close() throws IOException { + } + + @Override + public Iterator iterator() { + if (iterator == null) + iterator = new FilesAndCollectionsIterator(); + return iterator; + } + + static boolean isFile(Content c) { + return !c.get(DName.getcontenttype, String.class).isEmpty(); + } + + static boolean isDirectory(Content c) { + return c.isContentClass(DName.collection); + } + + class FilesAndCollectionsIterator implements Iterator { + private Content next; + private final Iterator it; + + public FilesAndCollectionsIterator() { + Content content = dir.getContent(); + if (!content.isContentClass(DName.collection)) + throw new IllegalStateException("Content " + content + " is not a collection"); + it = content.iterator(); + findNext(); + } + + private void findNext() { + next = null; + while (it.hasNext() && next != null) { + Content n = it.next(); + if (isFile(n) || isDirectory(n)) { + if (filter != null) { + try { + if (filter.accept(new CmsPath(dir.getFileSystem(), n))) + next = n; + } catch (IOException e) { + throw new UncheckedIOException("Cannot filter " + dir, e); + } + } else { + next = n; + } + } + } + } + + @Override + public boolean hasNext() { + return next != null; + } + + @Override + public Path next() { + if (next == null) + throw new IllegalStateException("Iterator doesn't have more elements"); + CmsPath p = new CmsPath(dir.getFileSystem(), next); + findNext(); + return p; + } + + } +}