View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2004 Open World Ltd
4   //
5   // This file is part of MillScript.
6   //
7   // MillScript is free software; you can redistribute it and/or modify it under
8   // the terms of the GNU General Public License as published by the Free
9   // Software Foundation; either version 2 of the License, or (at your option)
10  // any later version.
11  //
12  // MillScript is distributed in the hope that it will be useful, but WITHOUT
13  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  // more details.
16  //
17  // You should have received a copy of the GNU General Public License along with
18  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
19  // Place, Suite 330, Boston, MA  02111-1307  USA
20  ////////////////////////////////////////////////////////////////////////////////
21  package org.millscript.millscript.loaders;
22  
23  import org.millscript.commons.util.map.EHashMap;
24  import org.millscript.commons.vfs.VEntry;
25  import org.millscript.commons.vfs.VFolder;
26  import org.millscript.millscript.alert.Alerts;
27  
28  import java.io.IOException;
29  import java.util.Iterator;
30  import java.util.List;
31  
32  /**
33   * This class implements the MillScript map loader. The contents of the map
34   * directory are read, where each filename(without extension) is the key and
35   * the result of autoloading the file(based on extension) is the value.
36   */
37  public class MapLoader extends ValueLoader {
38  
39      /**
40       * @see org.millscript.millscript.loaders.Loader#loadValue()
41       */
42      @Override
43      public Object loadValue() throws IOException {
44          if ( this.entry instanceof VFolder && this.entry.exists() ) {
45              EHashMap< String, Object > result = new EHashMap< String, Object >();
46              List entries = ((VFolder) this.entry).listEntries();
47              Iterator it = entries.iterator();
48              while ( it.hasNext() ) {
49                  VEntry mapEntry = (VEntry) it.next();
50                  Loader loader = this.pack.makeLoaderFor( mapEntry );
51                  if ( loader != null ) {
52                      result.insert(
53                          loader.getSymbol(),
54                          loader.loadValue()
55                      );
56                  }
57              }
58              return result;
59          } else {
60              throw(
61                  Alerts.compile(
62                      "Inventory entry is not a folder",
63                      "Map loader can only be used to load folders"
64                  ).culprit( "entry", this.entry ).mishap()
65              );
66          }
67      }
68  
69  }