View Javadoc
1   package org.argeo.jcr.fs;
2   
3   import java.io.IOException;
4   import java.nio.file.DirectoryStream;
5   import java.nio.file.Path;
6   import java.util.Iterator;
7   
8   import javax.jcr.Node;
9   import javax.jcr.NodeIterator;
10  
11  public class NodeDirectoryStream implements DirectoryStream<Path> {
12  	private final JcrFileSystem fs;
13  	private final NodeIterator nodeIterator;
14  	private final Iterator<JcrPath> additionalPaths;
15  	private final Filter<? super Path> filter;
16  
17  	public NodeDirectoryStream(JcrFileSystem fs, NodeIterator nodeIterator, Iterator<JcrPath> additionalPaths,
18  			Filter<? super Path> filter) {
19  		this.fs = fs;
20  		this.nodeIterator = nodeIterator;
21  		this.additionalPaths = additionalPaths;
22  		this.filter = filter;
23  	}
24  
25  	@Override
26  	public void close() throws IOException {
27  	}
28  
29  	@Override
30  	public Iterator<Path> iterator() {
31  		return new Iterator<Path>() {
32  			private JcrPath next = null;
33  
34  			@Override
35  			public synchronized boolean hasNext() {
36  				if (next != null)
37  					return true;
38  				nodes: while (nodeIterator.hasNext()) {
39  					try {
40  						Node node = nodeIterator.nextNode();
41  						String nodeName = node.getName();
42  						if (nodeName.startsWith("rep:") || nodeName.startsWith("jcr:"))
43  							continue nodes;
44  						if (fs.skipNode(node))
45  							continue nodes;
46  						next = new JcrPath(fs, node);
47  						if (filter != null) {
48  							if (filter.accept(next))
49  								break nodes;
50  						} else
51  							break nodes;
52  					} catch (Exception e) {
53  						throw new JcrFsException("Could not get next path", e);
54  					}
55  				}
56  
57  				if (next == null) {
58  					if (additionalPaths.hasNext())
59  						next = additionalPaths.next();
60  				}
61  
62  				return next != null;
63  			}
64  
65  			@Override
66  			public synchronized Path next() {
67  				if (!hasNext())// make sure has next has been called
68  					return null;
69  				JcrPath res = next;
70  				next = null;
71  				return res;
72  			}
73  
74  		};
75  	}
76  
77  }