View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Some portions copyright (C) 2001-2004 Open World Ltd
4   // Copyright (C) 2005 Kevin Rogers
5   //
6   // This file is part of MillScript.
7   //
8   // MillScript is free software; you can redistribute it and/or modify it under
9   // the terms of the GNU General Public License as published by the Free
10  // Software Foundation; either version 2 of the License, or (at your option)
11  // any later version.
12  //
13  // MillScript is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  // more details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA  02111-1307  USA
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          // TODO - Should we only accept a StandardPackage for the constructor?
57          // It's the only place that should declare "folder" anyway...
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 }