KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > bridges > alias > AliasEnvironment


1 package rero.bridges.alias;
2  
3 import java.util.*;
4 import java.io.*;
5
6 import sleep.engine.*;
7 import sleep.interfaces.*;
8 import sleep.runtime.*;
9
10 public class AliasEnvironment implements Loadable, Environment
11 {
12     public HashMap aliases; // table to store the actual block associated with the function.
13

14     public AliasEnvironment()
15     {
16         aliases = new HashMap();
17     }
18
19     //
20
// this code is much simpler than the code in DefaultEnvironment mainly because I know that all scripts
21
// in this case will be sharing the same environment so I'm safe to do this. Sleep itself assumes that
22
// scripts may end up being isolated from eachother.
23
//
24
public boolean scriptUnloaded (ScriptInstance si)
25     {
26         String JavaDoc key;
27
28         Iterator i = aliases.keySet().iterator();
29         while (i.hasNext())
30         {
31            key = (String JavaDoc)i.next();
32
33            ScriptAlias alias = (ScriptAlias)aliases.get(key);
34
35            if (!alias.isValid())
36            {
37               while (alias != null && !alias.isValid())
38               {
39                  alias = alias.getPredecessor();
40               }
41
42               if (alias == null)
43               {
44                  i.remove(); // remove the current key
45
}
46               else
47               {
48                  aliases.put(key, alias);
49               }
50               
51            }
52         }
53
54         return true;
55     }
56
57     public boolean scriptLoaded (ScriptInstance si)
58     {
59         Hashtable env = si.getScriptEnvironment().getEnvironment(); // assuming the environment is shared. hah right
60

61         //
62
// tell the environment that we want aliases to be bound here
63
//
64
env.put("alias", this);
65
66         return true;
67     }
68
69     public void bindFunction(ScriptInstance si, String JavaDoc type, String JavaDoc name, Block code)
70     {
71         ScriptAlias temp = null;
72
73         name = name.toUpperCase();
74
75         if (aliases.get(name) != null)
76         {
77             temp = (ScriptAlias)aliases.get(name);
78         }
79
80         aliases.put(name, new ScriptAlias(si, code, temp));
81     }
82
83     public boolean isAlias(String JavaDoc name)
84     {
85         return (aliases.get(name) != null);
86     }
87
88     public ScriptAlias getAlias(String JavaDoc name)
89     {
90         return (ScriptAlias)aliases.get(name);
91     }
92
93     public Collection getAliasList()
94     {
95         return aliases.keySet();
96     }
97 }
98
Popular Tags