Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/tripi/asp/AspHandler.java


1   /**
2    * ArrowHead ASP Server 
3    * This is a source file for the ArrowHead ASP Server - an 100% Java
4    * VBScript interpreter and ASP server.
5    *
6    * For more information, see http://www.tripi.com/arrowhead
7    *
8    * Copyright (C) 2002  Terence Haddock
9    *
10   * This program is free software; you can redistribute it and/or modify
11   * it under the terms of the GNU General Public License as published by
12   * the Free Software Foundation; either version 2 of the License, or
13   * (at your option) any later version.
14   *
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Public License for more details.
19   *
20   * You should have received a copy of the GNU General Public License
21   * along with this program; if not, write to the Free Software
22   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23   *
24   */
25  package com.tripi.asp;
26  
27  import org.apache.log4j.Category;
28  
29  import com.tripi.asp.util.*;
30  import com.tripi.asp.parse.*;
31  import java.io.File;
32  import java.io.FileInputStream;
33  import java.io.InputStream;
34  import java.io.FileNotFoundException;
35  import java.io.IOException;
36  import java.net.URL;
37  import java.net.URLConnection;
38  import java.util.Hashtable;
39  import java.util.Map;
40  
41  /**
42   * This class handles the parsing of ASP code.
43   *
44   * @author Terence Haddock
45   * @version 0.9
46   */
47  public class AspHandler
48  {
49      static private Category DBG = Category.getInstance(AspHandler.class);
50  
51      /**
52       * Class to hold cached-file data  
53       */
54      private class CachedScript
55      {
56          /** File factory for this script */
57          AspFileFactory fileFactory;
58  
59          /** Node representation of script */
60          Node    node;
61  
62          /** Last time file was checked */
63          long    checkedTime;
64      }
65  
66      /**
67       * Cache of pre-parsed files.
68       */
69      static private Hashtable cachedScripts = new Hashtable();
70  
71      /**
72       * Current ASP Context.
73       */
74      AspContext context;
75  
76      /**
77       * Display path to ASP file
78       */
79      String filename;
80  
81      /**
82       * Constructor.
83       * @param file Full path to ASP file to parse.
84       * @param filename Display path to ASP file to parse
85       */
86      public AspHandler(AspContext context, String filename)
87      {
88          this.context = context;
89          this.filename = filename;
90      }
91  
92      /**
93       * Parses the current file and returns the base node of the file.
94       *
95       * @return Node representation of parsed file.
96       */
97      public Node parse() throws AspException
98      {
99          CachedScript cachedScript;
100         synchronized(cachedScripts)
101         {
102             cachedScript = (CachedScript)cachedScripts.get(filename);
103             if (cachedScript == null) {
104                 cachedScript = new CachedScript();
105                 cachedScript.fileFactory = new AspFileFactory(context);
106                 cachedScript.node = null;
107 
108                 cachedScripts.put(filename, cachedScript);
109             }
110         }
111         synchronized(cachedScript)
112         {
113             if (cachedScript.node != null)
114             {
115                 long time = System.currentTimeMillis();
116                 if ((time - cachedScript.checkedTime) > 10000)
117                 {
118                     cachedScript.checkedTime = time;
119                     if (!cachedScript.fileFactory.isModified())
120                         return cachedScript.node;
121                 } else {
122                     return cachedScript.node;
123                 }
124             }
125             try {
126                 if (DBG.isDebugEnabled())
127                     DBG.debug("Opening token manager for " + filename);
128 
129                 if (DBG.isDebugEnabled()) DBG.debug("Clearing cache");
130                 cachedScript.fileFactory.clearLoadedFilesCache();
131 
132                 if (DBG.isDebugEnabled()) DBG.debug("Resolving file");
133                 String absoluteFilename = cachedScript.fileFactory.
134                     resolveFile(null, filename, true);
135                 if (DBG.isDebugEnabled()) DBG.debug("Creating TokenManager");
136                 NestedTokenManager tokManager = new NestedTokenManager(
137                     absoluteFilename, cachedScript.fileFactory);
138                 if (DBG.isDebugEnabled()) DBG.debug("Creating Parser");
139                 VBScript script = new VBScript(tokManager);
140                 if (DBG.isDebugEnabled()) DBG.debug("Parsing file");
141                 Node value = script.WholeFile();
142 
143                 if (DBG.isDebugEnabled()) DBG.debug("Updating values");
144                 cachedScript.node = value;
145                 cachedScript.checkedTime = System.currentTimeMillis();
146 
147                 if (DBG.isDebugEnabled()) DBG.debug("Done.");
148                 return value;
149             } catch (AspException ex) {
150                 throw ex;
151             } catch (AspRuntimeSubException ex) {
152                 throw ex.getException();
153             } catch (AspRuntimeException ex) {
154                 throw new AspNestedException(ex);
155             } catch (TokenMgrError ex) {
156                 DebugContext ctx = new DebugContext(ex.getFilename(),
157                     ex.getLineNo());
158                 throw new AspNestedException(ex, ctx);
159             } catch (ParseException ex) {
160                 DebugContext ctx = new DebugContext();
161                 ex.currentToken.fillDebugContext(ctx);
162                 throw new AspNestedException(ex, ctx);
163             }
164         }
165     }
166 }