View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2004 Kevin Rogers
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;
22  
23  import org.millscript.commons.vfs.VFile;
24  import org.millscript.millscript.alert.Output;
25  import org.millscript.millscript.conf.Configuration;
26  import org.millscript.millscript.conf.WebsiteConf;
27  import org.millscript.millscript.vm.CompilerState;
28  import org.millscript.millscript.vm.Engine;
29  import org.millscript.millscript.vm.Machine;
30  import org.millscript.millscript.vm.Package;
31  
32  import java.io.Reader;
33  
34  /**
35   * This is class represents a MillScript session.
36   */
37  public abstract class Session {
38  
39      /**
40       * This sessions configuration.
41       */
42      protected Configuration config;
43  
44      /**
45       * This sessions engine.
46       */
47      protected Engine engine;
48  
49      /**
50       * This sessions compiler state.
51       */
52      protected CompilerState state;
53  
54      /**
55       * Constructs a new session.
56       */
57      public Session() {
58          // Set this sessions configuration
59          this.config = new WebsiteConf();
60          // Create a new engine for this session
61          engine = this.config.getStandardEngine();
62          // Create a new main package for the engine
63          Package mainPackage = engine.newPackage( "millscript.main" );
64          // Load standard configuration file
65          mainPackage.loadConf( config.getStandardConfigurationFile() );
66          // Load the website configuration file
67          mainPackage.loadConf( config.getWebsiteConfigurationFile() );
68          // Add the websites inventory to the package, for autoloading
69          mainPackage.addInventory( this.config.getWebsiteInventoryFolder() );
70          // Create a new compiler state.
71          state = new CompilerState( engine );
72          state.changePackage( mainPackage );
73      }
74  
75      /**
76       * Compiles the specified files sequentially.
77       *
78       * @param files a String array holding file names to compile
79       */
80      public void compile( final String[] files ) {
81          for ( int n = 0; n < files.length; n++ ) {
82              this.compile( this.config.getCurrentWorkingFolder().checkVFile( files[ n ] ) );
83          }
84      }
85  
86      /**
87       * Compiles code read from the specified reader, with the specified origin
88       * message.
89       *
90       * @param origin    the origin message for any problems.
91       * @param r the reader to read code from.
92       */
93      public abstract void compile( final String origin, final Reader r );
94  
95      /**
96       * Compiles from the specified virtual file.
97       *
98       * @param file	the virtual file to compile from.
99       */
100     public void compile( final VFile file ) {
101         this.compile( file.toString(), file.getReader() );
102     }
103 
104     /**
105      * Compiles from the specified source.
106      *
107      * @param source	the source to compile from.
108      */
109     public void compile( final Source source ) {
110         this.compile( source.toString(), source.getReader() );
111     }
112 
113     /**
114      * Print statistics for the current session. This is used at the end of a
115      * session, to display total execution time and number of different pages
116      * milled.
117      *
118      * @param startTime the time in milliseconds that the session was started.
119      */
120     public void printStatistics( final long startTime ) {
121         final long currentTime = System.currentTimeMillis();
122         config.getLogger().println( "" );
123         config.getLogger().println( "Execution time          : " + ( currentTime - startTime ) / 1000.0 );
124         if ( Prefs.pageCount > 0 ) {
125             config.getLogger().println( "No. pages milled        : " + Prefs.pageCount );
126         }
127         if ( Prefs.fragCount > 0 ) {
128             config.getLogger().println( "No. fragments milled    : " + Prefs.fragCount );
129         }
130         if ( Prefs.xmlFileCount > 0 ) {
131             config.getLogger().println( "No. XML files milled    : " + Prefs.xmlFileCount );
132         }
133         if ( Prefs.xmlFragCount > 0 ) {
134             config.getLogger().println( "No. XML fragments milled    : " + Prefs.xmlFragCount );
135         }
136         if ( Prefs.linesCount > 0 ) {
137             config.getLogger().println( "No. line files milled   : " + Prefs.linesCount );
138         }
139         if ( Prefs.stringCount > 0 ) {
140             config.getLogger().println( "No. string files milled : " + Prefs.stringCount );
141         }
142         if ( Prefs.dataCount > 0 ) {
143             config.getLogger().println( "No. data files milled   : " + Prefs.dataCount );
144         }
145         if ( Prefs.imageCount > 0 ) {
146             config.getLogger().println( "No. image files milled   : " + Prefs.imageCount );
147         }
148         if ( Prefs.propertiesCount > 0 ) {
149             config.getLogger().println( "No. properties files milled   : " + Prefs.propertiesCount );
150         }
151     }
152 
153     /**
154      * Summarizes any results left in the engine/machine.
155      */
156     public void summarizeResults() {
157         final Machine m = this.engine.getMachine();
158         int n = m.getCount();
159         if ( n != 0 ) {
160             config.getLogger().println( Resources.getFormattedMessage( "n.results", new Integer( n ) ) );
161             for ( int i = 0; i < n; i++ ) {
162                 config.getLogger().print( n + ". <" );
163                 config.getLogger().print( Output.keepShort( m.getIndex( i ) ) );
164                 config.getLogger().println( ">" );
165             }
166         }
167     }
168 
169 }