View Javadoc
1   package eu.simuline.m2latex.core;
2   
3   import java.io.File;
4   
5   import java.nio.file.Files;
6   import java.nio.file.LinkOption;
7   
8   import java.util.Set;
9   import java.util.TreeSet;
10  import java.util.Map;
11  import java.util.TreeMap;
12  
13  /**
14   * Represents the contents of a directory. 
15   *
16   *
17   * Created: Tue Dec  6 03:05:24 2016
18   *
19   * @author <a href="mailto:rei3ner@arcor.de">Ernst Reissner</a>
20   * @version 1.0
21   */
22  public class DirNode {
23  
24    // null iff this DirNode is invalid according to isValid() 
25    /**
26     * The set of names of regular files, i.e. files except directories 
27     * in the directory described by this node. 
28     * If the directory described by this node is not readable, 
29     * this field is <code>null</code>. 
30     *
31     * @see #isValid()
32     */
33    private final Set<String> regularFileNames;
34  
35    /**
36     * The set of subdirectories 
37     * in the directory described by this node: 
38     * The keys are the names and the according values 
39     * are the nodes describing the subdirectories. 
40     * If the directory described by this node is not readable, 
41     * this field is <code>null</code>. 
42     *
43     * @see #isValid()
44     */
45    private final Map<String, DirNode> name2node;
46  
47    /**
48     * Creates a new <code>DirNode</code> instance.
49     * <p>
50     * Logging: 
51     * WFU01: Cannot read directory 
52     *
53     * @param dir
54     *    The directory this node represents 
55     *    including subdirectories recursively. 
56     *    This is the latex source directory or a subdirectory recursively. 
57     * @param fileUtils
58     *    
59     */
60    // used recursively but in addition only in 
61    // TexFileUtils.cleanUpRec,
62    // LatexProcessor.create()
63    // LatexProcessor.processGraphics()
64    // LatexProcessor.clearAll()
65    public DirNode(File dir, TexFileUtils fileUtils) {
66      assert dir.isDirectory() : "The file '" + dir + "' is no directory. ";
67      // may log WFU01 Cannot read directory
68      File[] files = fileUtils.listFilesOrWarn(dir);
69      if (files == null) {
70        // Here, this node is irregular
71        // TBD: clarify whether this may occur 
72        assert false;// should only be the case if dir is no directory 
73        this.regularFileNames = null;
74        this.name2node = null;
75        return;
76      }
77      this.regularFileNames = new TreeSet<String>();
78      this.name2node = new TreeMap<String, DirNode>();
79      DirNode node;
80      for (File file : files) {
81        // with link option because file.exists() is false 
82        // if link with non-existing target
83        assert Files.exists(file.toPath(), LinkOption.NOFOLLOW_LINKS)
84          : "The file '" + file + "' does not exist. ";
85        if (file.isDirectory()) {
86          // may log WFU01 Cannot read directory
87          node = new DirNode(file, fileUtils);
88          if (node.isValid()) {
89            this.name2node.put(file.getName(), node);
90          }
91        } else {
92          // FIXME: skip hidden files
93          this.regularFileNames.add(file.getName());
94        }
95      }
96    }
97  
98    /**
99     * Whether the directory described by this node is readable. 
100    */
101   boolean isValid() {
102     assert (this.regularFileNames == null) == (this.name2node == null);
103     return this.regularFileNames != null;
104   }
105 
106   Set<String> getRegularFileNames() {
107     return this.regularFileNames;
108   }
109 
110   Map<String, DirNode> getSubdirs() {
111     return this.name2node;
112   }
113 }