View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // 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.loaders;
23  
24  import org.millscript.commons.alert.AlertOrigin;
25  import org.millscript.commons.alert.alerts.Fault;
26  import org.millscript.commons.vfs.VEntry;
27  import org.millscript.millscript.alert.Alerts;
28  import org.millscript.millscript.vm.Package;
29  
30  import java.io.IOException;
31  
32  /**
33   * This is the base class of all loaders in MillScript. A loader is responsible
34   * for compiling the contents of a file into a MillScript structure. e.g. a
35   * text file is compiled to a string, while a template is compiled into a
36   * function wrapping some recursive XML.
37   */
38  public abstract class Loader implements AlertOrigin {
39  
40      /**
41       * The package we want to compile the source file into
42       */
43      protected Package pack;
44  
45      /**
46       * The source we want to compile from
47       */
48      protected VEntry entry;
49  
50      /**
51       * The line number for error reporting.
52       */
53      private int lineNumber = -1;
54  
55      /**
56       * The symbol we want bound after compiling the source file
57       */
58      protected String symbol;
59  
60      /**
61       * Sets the package this loader will import into, the symbol for the loaded
62       * resource and the virtual filesystem entry for the data to compile.
63       *
64       * @param p the package to compile into
65       * @param sym   the symbol for the compiled resource
66       * @param e	the entry of the data to compile
67       * @return  this Loader
68       */
69      public Loader setLoader( final Package p, final String sym, final VEntry e ) {
70          this.pack = p;
71          this.symbol = sym.intern();
72          this.entry = e;
73          return this;
74      }
75  
76      /**
77       * Returns a new instance of this loader, to be used as appropriate.
78       *
79       * @return  a new instance of this loader.
80       */
81      public Loader getInstance() {
82          try {
83              return this.getClass().newInstance();
84          } catch ( InstantiationException e ) {
85              throw(
86                  Alerts.fault(
87                      "Could not construct a loader for this file"
88                  ).mishap()
89              );
90          } catch ( IllegalAccessException e ) {
91              throw(
92                  Alerts.fault(
93                      "Could not access a loader for this file"
94                  ).mishap()
95              );
96          }
97      }
98  
99      /**
100      * @see org.millscript.commons.alert.AlertOrigin#getLineNumber()
101      */
102     public int getLineNumber() {
103         return this.lineNumber;
104     }
105 
106     /**
107      * @see org.millscript.commons.alert.AlertOrigin#getOrigin()
108      */
109     public String getOrigin() {
110         return entry.toString();
111     }
112 
113     /**
114      * Returns the symbol this loader will compile.
115      *
116      * @return  a String holding the symbol for the resource
117      */
118     public String getSymbol() {
119         return symbol;
120     }
121 
122     /**
123      * Causes this loader to load it's value.
124      *
125      * @return  the object that was loaded
126      * @throws IOException  if something goes wrong reading from the source
127      */
128     public abstract Object loadValue() throws IOException;
129 
130     /**
131      * Causes this loader to load it's bindings. This will usually trigger the
132      * loader to load it's value, then compile it into the package.
133      *
134      * @throws IOException  if something goes wrong reading from the source
135      */
136     public abstract void loadBindings() throws IOException;
137 
138     /**
139      * Returns a new loader to load the specified source file into the
140      * specified package, binding a value to the specified symbol.
141      *
142      * @param p     the package to load values into
143      * @param sym   the symbol that must be bound after loading
144      * @param e	the entry of the data to compile
145      * @return  a Loader to compile the source file
146      */
147     public Loader newLoader( final Package p, final String sym, final VEntry e ) {
148         return this.getInstance().setLoader( p, sym, e );
149     }
150 
151     /**
152      * @see org.millscript.commons.alert.AlertOrigin#setContext(java.lang.String, int)
153      */
154     public void setContext( final String s, final int n ) {
155         throw new Fault( "Context cannot be set for a Loader" ).mishap();
156     }
157 
158     /**
159      * @see org.millscript.commons.alert.AlertOrigin#setLineNumber(int)
160      */
161     public void setLineNumber( final int n ) {
162         this.lineNumber = n;
163     }
164 
165     /**
166      * @see org.millscript.commons.alert.AlertOrigin#setOrigin(java.lang.String)
167      */
168     public void setOrigin( final String o ) {
169         throw new Fault( "Origin cannot be set for a Loader" ).mishap();
170     }
171 }