1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.millscript.millscript.functions;
23
24 import org.millscript.commons.util.IList;
25 import org.millscript.commons.util.MapIterator;
26 import org.millscript.commons.vfs.VFolder;
27 import org.millscript.millscript.conf.Configuration;
28 import org.millscript.millscript.tools.CastLibrary;
29 import org.millscript.millscript.vm.Package;
30 import org.millscript.millscript.vm.Ref;
31
32 /**
33 * This class provides the basic methods required for a function generating
34 * output. These methods are geared towards obtaining and creating directories
35 * in the script output and determining the current working directory.
36 */
37 public abstract class AbstractOutputFunction extends Function {
38
39 /**
40 * The configuration to use for this function.
41 */
42 protected final Configuration config;
43
44 /**
45 * The Ref for the global folder variable.
46 */
47 private final Ref folderRef;
48
49 /**
50 * Constructs a new abstract output function for the specified package.
51 *
52 * @param pack the package this output function is defined in
53 */
54 protected AbstractOutputFunction( final Package pack ) {
55 this.config = pack.getConfig();
56
57
58 this.folderRef = pack.findIdentFor( null, "folder" ).getRef();
59 }
60
61 /**
62 * Returns a VFolder for the current working output folder, based on the value
63 * of the <code>folder</code> variable.
64 *
65 * @return a VFolder for the current working output folder
66 */
67 public final VFolder getCurrentWorkingFolder() {
68 return this.getWorkingFolder( this.getCurrentWorkingFolderList() );
69 }
70
71 /**
72 * Returns the current working folder list.
73 *
74 * @return a List representing the current working folder
75 */
76 public final IList getCurrentWorkingFolderList() {
77 return CastLibrary.toIList( this.folderRef.value );
78 }
79
80 /**
81 * Returns a VFolder for the specified current working output folder list.
82 *
83 * @param folderList a List specifying the current working folder
84 * @return a VFolder for the specified current working folder
85 */
86 public final VFolder getWorkingFolder( final IList folderList ) {
87 final MapIterator it = folderList.iterator( true );
88 VFolder sofar = this.config.getWebsiteOutputFolder();
89 while ( it.hasNext() ) {
90 sofar = sofar.getVFolder( it.nextValue().toString() );
91 }
92 return sofar;
93 }
94
95 /**
96 * Makes the current working folder, returning a VFolder for the made folder.
97 *
98 * @return a VFolder for the folder that's just been made
99 */
100 public final VFolder makeCurrentWorkingFolder() {
101 return this.getCurrentWorkingFolder().make();
102 }
103
104 }