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

Quick Search    Search Deep

Source code: com/tripi/asp/DoNode.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  /**
28   * Implements the looping constructs, DO .. UNTIL, WHILE ... DO,
29   * WHILE .. WEND, etc...
30   *
31   * @author Terence Haddock
32   * @version 0.9
33   */
34  public class DoNode implements Node
35  {
36      /** Expression to test */
37      Node        expr;
38  
39      /** Block to execute */
40      BlockNode    code;
41  
42      /** Check expression after loop? */
43      boolean        checkAfter;
44  
45      /** DO .. UNTIL, or DO .. WHILE? */
46      boolean        doUntil;
47  
48      /**
49       * Constructor.
50       *
51       * @param expr Expression to test.
52       * @param code Code to execute in loop.
53       * @param checkAfter check after loop?
54       * @param doUntil DO .. UNTIL or DO .. WHILE?
55       */
56      public DoNode(Node expr, BlockNode code, boolean checkAfter,
57                  boolean doUntil)
58      {
59          this.expr = expr;
60          this.code = code;
61          this.checkAfter = checkAfter;
62          this.doUntil = doUntil;
63      }
64  
65      /**
66       * Dumps this node.
67       * @throws AspException if an error occurs
68       * @see Node#dump() 
69       */
70      public void dump() throws AspException
71      {
72          if (!checkAfter) {
73              System.out.print("DO ");
74              if (doUntil) {
75                  System.out.print("UNTIL");
76              } else {
77                  System.out.print("WHILE");
78              }
79              expr.dump();
80              System.out.println();
81          }
82          code.dump();
83          System.out.print("LOOP");
84          if (checkAfter) {
85              if (doUntil) {
86                  System.out.print("UNTIL ");
87              } else {
88                  System.out.print("WHILE ");
89              }
90              expr.dump();
91              System.out.println();
92          }
93          
94      }
95  
96      /**
97       * Prepares this node for executtion.
98       * @param context Global context
99       * @throws AspException if an error occurs
100      * @see Node#prepare(AspContext)
101      */
102     public void prepare(AspContext context) throws AspException
103     {
104         code.prepare(context);
105     }
106 
107     /**
108      * Executes this node
109      * @param context Current context
110      * @return result of this node's execution
111      * @throws AspException if an error occurs
112      * @see Node#prepare(AspContext)
113      */
114     public Object execute(AspContext context) throws AspException
115     {
116         if (!checkAfter) {
117             // Check when we start.
118             Object obj = expr.execute(context);
119             Boolean val = Types.coerceToBoolean(obj);
120             if (doUntil && val.booleanValue()) return null;
121             if (!doUntil && !val.booleanValue()) return null;
122         }
123         do {
124             // Execute the sub-expression
125             try {
126                 code.execute(context);
127             } catch (AspExitDoException ex) {
128                 // Early exit
129                 return null;
130             }
131             // Check the values now
132             Object obj = expr.execute(context);
133             Boolean val = Types.coerceToBoolean(obj);
134             if (doUntil && val.booleanValue()) return null;
135             if (!doUntil && !val.booleanValue()) return null;
136         } while (true);
137     }
138 };
139