1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package Torello.HTML.Tools.JavaDoc;

import Torello.HTML.HTMLNode;
import Torello.Java.*;

import Torello.Java.FileNode.RetTypeChoice;
import Torello.Java.Additional.VarList; // for javadoc

import java.util.*;
import java.io.*;

import java.util.stream.Stream;

/**
 * Retrieve Java Doc HTML Files from the Java Doc output directory.
 * 
 * <BR /><BR /><EMBED CLASS="external-html" DATA-FILE-ID=JDFILES>
 */
@StaticFunctional
public class JDFiles
{
    private JDFiles() { }

    /** A Java {@code Predicate} that filters for {@code '.html'} files. */
    public static final FilenameFilter HTML_FILE_FILTER =
        (File dir, String name) -> name.trim().endsWith(".html");

    /** A Java {@code Predicate} that avoids directories that contain extranneous files. */
    public static final FileFilter DIRS_TO_SKIP = (File dir) -> StrCmpr.equalsNAND
        (dir.getName(), "src-html", "index-files", "doc-files", "hilite-files" );

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=JDF_GJCHF>
     * @param rootJDOutDirName <EMBED CLASS='external-html' DATA-FILE-ID=JDF_ROOT>
     * @return This will return the list of files as a Java {@code Stream<String>}.
     * @see #HTML_FILE_FILTER
     * @see #DIRS_TO_SKIP
     * @see FileNode
     * @see FileNode#flattenJustFiles(VarList)
     */
    public static Stream<String> getJavaClassHTMLFiles(String rootJDOutDirName)
    {
        return FileNode
            .createRoot(rootJDOutDirName)
            .loadTree(-1, HTML_FILE_FILTER, DIRS_TO_SKIP)
            .prune((FileNode fn) -> StrCmpr.equalsNAND
                (fn.name, "package-summary.html", "package-tree.html", "package-frame.html"),
                true
            )
            // This 'prune' eliminates HTML files in the *ROOT* Java-Doc directory
            .prune((FileNode fn) -> ! rootJDOutDirName.equals(fn.getParentPathName()),
                true
            )
            .flattenJustFiles(RetTypeChoice.FULLPATH_STREAM);

            /*
            NOT-OPTIMIZED, WORKS-THOUGH
            .filter((String fileName) -> StrCmpr.containsNAND(fileName, "package-summary.html", "package-tree.html", "package-frame.html"))
            .filter((String fileName) -> (StringParse.countCharacters(fileName, File.separator.charAt(0)) > 1));
                // The previous filter prevents any ".html" files that are in the root java-doc output directory,
                // ... and are not inside one of the packages.  If a class 'does not have a package' it will be skipped.
            */
    }

    /**
     * This method returns the exact same files as {@link #getJavaClassHTMLFiles(String)}, but
     * each file returned <I>has been sorted into a specific 'bucket' based on which <B>Java
     * Package</B> the CIET (class, inteface, enumerated-type, record etc...) HTML file
     * belongs.</I>
     * 
     * @param rootJDOutDirName <EMBED CLASS='external-html' DATA-FILE-ID=JDF_ROOT>
     * 
     * @return This will return the list of files, sorted by 'Package Name' - where the package
     * is merely determined by the directory-name where the file was found.
     * 
     * <BR /><TABLE CLASS=BRIEFTABLE>
     * <TR><TH>Element</TH><TH>Contents</TH></TR>
     * <TR> <TD>{@code TreeMap} 'key'</TD>
     *      <TD>The <I>directory-name</I> of the Java-Doc HTML-Directory containing the CIET Java
     *          Doc HTML Files for a specific package.  (NOTE: This is different than the actual
     *          'Package Name')
     *      </TD>
     *      </TR>
     * <TR> <TD>{@code TreeMap} 'value'</TD>
     *      <TD>A {@code Vector} of file-name's (each as a {@code String}) of each Java Doc
     *          generated HTML file for a CIET in the specific package.
     *      </TD>
     *      </TR>
     * </TABLE>
     * 
     * @see #HTML_FILE_FILTER
     * @see #DIRS_TO_SKIP
     * @see FileNode
     * @see FileNode#getParentPathName()
     * @see FileNode#flattenJustFiles(VarList)
     */
    public static TreeMap<String, Vector<String>>
        getJavaClassHTMLFilesByPackage(String rootJDOutDirName)
    {
        TreeMap<String, Vector<String>> ret = new TreeMap<>();

        FileNode
            .createRoot(rootJDOutDirName)
            .loadTree(-1, HTML_FILE_FILTER, DIRS_TO_SKIP)
            .flattenJustFiles(RetTypeChoice.STREAM)
            .filter((FileNode fn) -> StrCmpr.equalsNAND
                (fn.name, "package-summary.html", "package-tree.html", "package-frame.html"))

            // This 'prune' eliminates HTML files in the *ROOT* Java-Doc directory
            .filter((FileNode fn) -> ! rootJDOutDirName.equals(fn.getParentPathName()))

            .forEach((FileNode jdHTMLFile) ->
            {
                String          packageDirectory    = jdHTMLFile.getParentPathName();
                Vector<String>  packageFilesList    = ret.get(packageDirectory);

                if (packageFilesList == null)
                    ret.put(packageDirectory, packageFilesList = new Vector<>());

                packageFilesList.add(jdHTMLFile.name);
            });

        return ret;
    }

    /**
     * Retrieves all User-Provided HTML Embed-Tag Map Files in his source-directory tree.
     * 
     * @param rootSrcCodeDirName The root source-directory
     * @return All Externa-HTML {@code <EMBED>} Tag-to-FileName map files.
     * @see FileNode
     * @see FileNode#flattenJustFiles(VarList)
     */
    @Deprecated
    public static Stream<String> allLocalEmbedTagMapFiles(String rootSrcCodeDirName)
    {
        return FileNode
            .createRoot(rootSrcCodeDirName)
            .loadTree(
                -1,
                (File dir, String fName) -> fName.equals("external-html-ids.properties"),
                null
            )
            .flattenJustFiles(RetTypeChoice.FULLPATH_STREAM);
    }

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=JDF_GSAHF>
     * @param rootJDOutDirName <EMBED CLASS='external-html' DATA-FILE-ID=JDF_ROOT>
     * @return This will return the list of files as a Java {@code Stream<String>}.
     */
    public static Stream<String> getSrcAsHTMLFiles(String rootJDOutDirName)
    {
        if (! (new File(rootJDOutDirName + "src-html" + File.separator)).exists())
            return Stream.empty();

        return FileNode
            .createRoot(rootJDOutDirName + "src-html" + File.separator)
            .loadTree(-1, HTML_FILE_FILTER, null)
            .flattenJustFiles(RetTypeChoice.FULLPATH_STREAM);
    }

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=JDF_GJPHF>
     * @param rootJDOutDirName <EMBED CLASS='external-html' DATA-FILE-ID=JDF_ROOT>
     * @return This will return the list of files as a Java {@code Stream<String>}.
     * @see FileNode
     * @see FileNode#flattenJustFiles(VarList)
     * @see RetTypeChoice
     */
    public static Stream<String> getJavaPackageHTMLFiles(String rootJDOutDirName)
    {
        return FileNode
            .createRoot(rootJDOutDirName)
            .loadTree(-1, HTML_FILE_FILTER, DIRS_TO_SKIP)
            .flattenJustFiles(RetTypeChoice.FULLPATH_STREAM)
            .filter((String fileName) ->
                fileName.contains("package-summary.html")   ||
                fileName.contains("package-tree.html")      ||
                fileName.contains("package-frame.html")
            );
    }

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=JDF_FRAMESUMM>
     * @param rootJDOutDirName <EMBED CLASS='external-html' DATA-FILE-ID=JDF_ROOT>
     * @return This will return the list of files as a Java {@code Stream<String>}.
     * @see FileNode
     * @see FileNode#flattenJustFiles(VarList)
     * @see RetTypeChoice
     */
    public static Stream<String> getFrameSummaryHTMLFiles(String rootJDOutDirName)
    {
        return FileNode
            .createRoot(rootJDOutDirName)
            .loadTree(-1, HTML_FILE_FILTER, DIRS_TO_SKIP)
            .flattenJustFiles(RetTypeChoice.FULLPATH_STREAM)
            .filter((String fileName) ->
                fileName.contains("package-summary.html")   ||
                fileName.contains("package-frame.html")
            );
    }

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=JDF_GAHF>
     * @param rootJDOutDirName <EMBED CLASS='external-html' DATA-FILE-ID=JDF_ROOT>
     * @return This will return the list of files as a Java {@code Stream<String>}.
     * @see FileNode
     */
    public static Stream<String> getAllHTMLFiles(String rootJDOutDirName)
    {
        return FileNode
            .createRoot(rootJDOutDirName)
            // The 'doc-files/' are user-generated, skip those...
            .loadTree(-1, HTML_FILE_FILTER, (File dir) ->
                    (! dir.getName().equals("doc-files"))
                &&  (! dir.getName().equals("hilite-files"))
            )
            .flattenJustFiles(RetTypeChoice.FULLPATH_STREAM);
    }

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=JDF_GPSHF>
     * @param rootJDOutDirName <EMBED CLASS='external-html' DATA-FILE-ID=JDF_ROOT>
     * @return This will return the list of files as a Java {@code Stream<String>}.
     * @see #getJavaPackageHTMLFiles(String)
     */
    public static Stream<String> getPackageSummaryHTMLFiles(String rootJDOutDirName)
    {
        return JDFiles
            .getJavaPackageHTMLFiles(rootJDOutDirName)
            .filter((String fileName) -> fileName.contains("package-summary.html"));
    }

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=JDF_GPFHF>
     * @param rootJDOutDirName <EMBED CLASS='external-html' DATA-FILE-ID=JDF_ROOT>
     * @return This will return the list of files as a Java {@code Stream<String>}.
     * @see #getJavaPackageHTMLFiles(String)
     */
    public static Stream<String> getPackageFrameHTMLFiles(String rootJDOutDirName)
    {
        return JDFiles
            .getJavaPackageHTMLFiles(rootJDOutDirName)
            .filter((String fileName) -> fileName.contains("package-frame.html"));
    }

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=JDF_GHFIR>
     * @param rootJDOutDirName <EMBED CLASS='external-html' DATA-FILE-ID=JDF_ROOT>
     * @return This will return the list of files as a Java {@code Stream<String>}.
     * @see FileNode
     * @see FileNode#flattenJustFiles(VarList)
     * @see RetTypeChoice
     */
    public static Stream<String> getHTMLFilesInRoot(String rootJDOutDirName)
    {
        return FileNode
            .createRoot(rootJDOutDirName)
            .loadTree(0, HTML_FILE_FILTER, null)
            .flattenJustFiles(FileNode.RetTypeChoice.FULLPATH_STREAM);
    }

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=JDF_GPTHF>
     * @param rootJDOutDirName <EMBED CLASS='external-html' DATA-FILE-ID=JDF_ROOT>
     * @return This will return the list of files as a Java {@code Stream<String>}.
     * @see #getJavaPackageHTMLFiles(String)
     */
    public static Stream<String> getPackageTreeHTMLFiles(String rootJDOutDirName)
    {
        return JDFiles
            .getJavaPackageHTMLFiles(rootJDOutDirName)
            .filter((String fileName) -> fileName.contains("package-tree.html"));
    }
}