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
package Torello.Java.Build;

import Torello.Java.FileNodeFilter;
import Torello.Java.ReadOnly.ReadOnlyList;
import Torello.Java.ReadOnly.ROVectorBuilder;

import java.io.File;
import java.util.Objects;

public class JarInclude
{
    // This is a Package-Visible Inner-Class.  It is only usable inside Torello.Java.Build
    // Instances of this class are included in the ReadOnlyList returned by method
    // getAllDesriptors.  This is also a Package-Visible method

    static class Descriptor
    {
        final String workingDirectory, subDirectory;
        final boolean traverseTree;
        final FileNodeFilter fileFilter, dirFilter;

        Descriptor(
            String          workingDirectory,
            String          subDirectory,
            boolean         traverseTree,
            FileNodeFilter  fileFilter,
            FileNodeFilter  dirFilter
        )
        {
            this.workingDirectory   = workingDirectory;
            this.subDirectory       = subDirectory;
            this.traverseTree       = traverseTree;
            this.fileFilter         = fileFilter;
            this.dirFilter          = dirFilter;
        }

        public String toString() { return workingDirectory + subDirectory; }
    }

    /** Build an instance of this class */
    public JarInclude() { }

    private final ROVectorBuilder<JarInclude.Descriptor> descriptors =
        new ROVectorBuilder<>();

    // This is only invoked by class Builder
    ReadOnlyList<JarInclude.Descriptor> getAllDesriptors() { return descriptors.build(); }

    private static final String M1 = "Parameter '";
    private static final String M2 = "' was passed null, but this is not allowed";

    /**
     * Convenience Method.
     * <BR />Adds a Jar-Include Directive that <B>DOES NOT</B> recurse the directory-tree
     * <BR />Invokes: {@link #add(String, String, boolean, FileNodeFilter, FileNodeFilter)}
     */
    public JarInclude add(
        String          workingDirectory,
        String          subDirectory,
        FileNodeFilter  fileFilter
    )
    { return add(workingDirectory, subDirectory, false, fileFilter, null); }

    /**
     * Inserts a request for files to be included in the Tar-Jar Build Stage (Stage 4).
     * 
     * @param workingDirectory When files are added to a {@code '.jar'}-File, the "Working
     * Directory" part of the File-System Path <B>is not included</B> in the name of the files that
     * are inserted.
     * 
     * @param subDirectory The "Sub-Directory" part of the File-System Path <B>is included</B> into
     * the names of any and all files that are inserted in the {@code '.jar'}.
     * 
     * @param traverseTree Indicates whether the the {@code String}-Parameter
     * {@code 'subDirectory'} should be interpreted as a directory-name - <I>or as an entire tree
     * branch</I> whose own sub-directories should be traversed by the file-scanner.
     * 
     * @param fileFilter A filter / "chooser" / specifier for deciding which files residing on the
     * File-System inside {@code 'subDirectory'} (or {@code 'subDirectory'}, and its own
     * sub-directories - in the case that {@code 'traverseTree'} was passed {@code TRUE}), are to
     * be included in the {@code '.jar'}.
     * 
     * <BR /><BR />This filter must return {@code TRUE} if a file this filter is testing
     * <B><I>should</I></B> be inserted into the {@code '.jar'}, and {@code FALSE}, if the file
     * <B><I>should not</I></B> be.
     * 
     * <BR /><BR />This parameter may be passed null, and if it is it will be quietly ignored.
     * When this filter is null, all files that reside within {@code 'subDirectory'} will be 
     * inserted into the {@code '.jar'}-File.
     * 
     * <BR /><BR />If this parameter were passed null, and {@code 'traverseTree'} were passed
     * {@code TRUE}, then all files inside of {@code 'subDirectory'} would be inserted into the
     * {@code '.jar'} - <I>and furthermore, all files in all sub-directories of
     * {@code 'subDirectory'} would also be inserted</I>.
     * 
     * @param dirFilter This filter can only be employed if {@code 'traverseTree'} has been passed
     * {@code TRUE}.
     * 
     * <BR /><BR />When {@code 'traverseTree'} is {@code TRUE} as the directory tree rooted at
     * {@code workingDirectory/subDirectory/} is traversed, each sub-directory that is encountered
     * will be passed to this filter.  When this test is performed, the filter should return 
     * {@code TRUE} to indicate that it would like a particular sub-directory searched, and 
     * {@code FALSE} to indicate that it must be skipped.
     * 
     * <BR /><BR />This parameter may be passed null, and if it is it will be silently ignored.
     * If this parameter is null, and {@code 'traverseTree'} is {@code TRUE}, all sub-directories
     * of {@code workingDirectory/subDirectory/} will be entered / traversed.
     * 
     * <BR /><BR ><B>NOTE:</B> If this parameter is passed a non-null filter, but
     * {@code 'traverseTree'} has been passed {@code FALSE}, then an
     * {@code IllegalArgumentException} will throw.  Parameter {@code 'dirFilter'} has no use or
     * application if the named directory-tree is not going to be traversed!
     * 
     * @return {@code 'this'} instance, for convenience and invocation-chaining.
     * 
     * @throws NullPointerException If either {@code 'workingDirectory'} or {@code 'subDirectory'}
     * is passed null.
     * 
     * @throws IllegalArgumentException If either {@code 'workingDirectory'} or
     * {@code 'subDirectory'} do not name real directories that actually exist on the File-System.
     * 
     * <BR /><BR />This exception will also throw if {@code 'traverseTree'} is passed {@code FALSE}
     * but {@code 'dirFilter'} is non-null.
     */
    public JarInclude add(
            String          workingDirectory,
            String          subDirectory,
            boolean         traverseTree,
            FileNodeFilter  fileFilter,
            FileNodeFilter  dirFilter
        )
    {
        File f;

        Objects.requireNonNull(workingDirectory, M1 + "workingDirectory" + M2);
        Objects.requireNonNull(subDirectory, M1 + "subDirectory" + M2);

        if (workingDirectory.length() > 0)
        {
            f = new File(workingDirectory);

            if (! f.exists()) throw new IllegalArgumentException(
                "The directory-name provided to parameter 'workingDirectory' does not exist on " +
                "the File-System:\n[" + workingDirectory + ']'
            );

            if (! f.isDirectory()) throw new IllegalArgumentException(
                "The directory-name provided to parameter 'workingDirectory' is not the name of " +
                "an actual File-System directory:\n[" + workingDirectory + ']'
            );
        }

        if (! workingDirectory.endsWith(File.separator))
            if (workingDirectory.length() > 0)
                workingDirectory = workingDirectory + File.separator;

        String subDir = workingDirectory + subDirectory;

        if (subDir.length() > 0)
        {
            f = new File(subDir);

            if ((! f.exists()) || (! f.isDirectory())) throw new IllegalArgumentException(
                "The directory-name provided to parameter 'subDirectory' does not exist on the " +
                "File-System as a Sub-Directory of 'workingDirectory':\n" +
                "[" + subDirectory + ']'
            );
        }

        if (! subDirectory.endsWith(File.separator))
            if (subDirectory.length() > 0)
                subDirectory = subDirectory + File.separator;

        if ((! traverseTree) && (dirFilter != null)) throw new IllegalArgumentException(
            "You have passed FALSE to 'traverseTree', but a non-null filter to parameter " +
            "'dirFilter'.  This is not allowed."
        );
        
        this.descriptors.add
            (new Descriptor(workingDirectory, subDirectory, traverseTree, fileFilter, dirFilter));

        return this;
    }
}