KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > script > LocalVariables


1 /*
2    SLEEP - Simple Language for Environment Extension Purposes
3  .---------------------------.
4  | sleep.interfaces.Variable |________________________________________________
5  | |
6    Author: Raphael Mudge (rsmudge@mtu.edu)
7            http://www.csl.mtu.edu/~rsmudge/
8  
9    Description: An interface to allow management of scalars itself. Sleep only
10      allows one variable interface to be used per session. But this is a good
11      way to create global variables and such in Sleep.
12  
13    Documentation:
14  
15    * This software is distributed under the artistic license, see license.txt
16      for more information. *
17  
18  |____________________________________________________________________________|
19  */

20
21 package rero.script;
22  
23 import sleep.interfaces.*;
24 import sleep.runtime.*;
25
26 import rero.ircfw.interfaces.FrameworkConstants; // we take advantage of the $parms constant.
27

28 import rero.util.TokenizedString;
29 import rero.util.StringParser; // gotta love the regex parser. *uNF*
30

31 import java.util.*;
32 import java.util.regex.*;
33
34 public class LocalVariables implements Variable
35 {
36     protected HashMap data = new HashMap();
37
38     protected static Pattern rangePattern = Pattern.compile("\\$(\\d+)\\-(\\d+)"); // \$(\d+)\-(\d+)
39
protected static Pattern rangeFromPattern = Pattern.compile("\\$(\\d+)\\-"); // \$(\d+)\-
40
protected static Pattern rangeToPattern = Pattern.compile("\\$\\-(\\d+)"); // \$\-(\d+)
41
protected static Pattern normalPattern = Pattern.compile("\\$(\\d+)"); // \$(\d+)
42

43     protected String JavaDoc parmsValue = null;
44
45     public void setDataSource(HashMap _data)
46     {
47        //
48
// set another hashmap as the data source. We won't alter/remove values already in place
49
// but we may add to the hashmap
50
//
51
Iterator iter = _data.keySet().iterator();
52        while (iter.hasNext())
53        {
54           String JavaDoc key = iter.next().toString();
55  
56           if (key.equals(FrameworkConstants.$DATA$))
57           {
58               parmsValue = (String JavaDoc)_data.get(key);
59           }
60
61           data.put(key, SleepUtils.getScalar((String JavaDoc)_data.get(key)) );
62        }
63     }
64
65     public boolean scalarExists(String JavaDoc key)
66     {
67        if (data.containsKey(key) || "%localData".equals(key))
68        {
69           return true;
70        }
71
72        //
73
// check for existence of our "dynamic" variables
74
//
75
return parmsValue != null && ((Character.isDigit(key.charAt(1)) || key.charAt(1) == '-'));
76     }
77
78     public Scalar getScalar(String JavaDoc key)
79     {
80        if ("%localData".equals(key))
81           return SleepUtils.getHashWrapper(data);
82
83        Scalar temp = (Scalar)data.get(key);
84
85        if (temp == null && parmsValue != null && (Character.isDigit(key.charAt(1)) || key.charAt(1) == '-'))
86        {
87           StringParser parser;
88           int begin, end;
89
90           TokenizedString tokenizer = new TokenizedString(parmsValue, " ");
91
92           //
93
// look for $n-m pattern and return tokens n to m in a sequence
94
//
95
parser = new StringParser(key, rangePattern);
96           if (parser.matches())
97           {
98              begin = Integer.parseInt(parser.getParsedStrings()[0]);
99              end = Integer.parseInt(parser.getParsedStrings()[1]);
100
101              return SleepUtils.getScalar(tokenizer.getTokenRange(begin, end));
102           }
103
104           //
105
// look for $n- pattern and return tokens n on up in a sequence
106
//
107
parser = new StringParser(key, rangeToPattern);
108           if (parser.matches())
109           {
110              begin = Integer.parseInt(parser.getParsedStrings()[0]);
111
112              return SleepUtils.getScalar(tokenizer.getTokenTo(begin));
113           }
114
115           //
116
// look for $-m pattern and return tokens up to m in a sequence
117
//
118
parser = new StringParser(key, rangeFromPattern);
119           if (parser.matches())
120           {
121              begin = Integer.parseInt(parser.getParsedStrings()[0]);
122
123              return SleepUtils.getScalar(tokenizer.getTokenFrom(begin));
124           }
125
126           //
127
// look for $n pattern and return token n.
128
//
129
parser = new StringParser(key, normalPattern);
130           if (parser.matches())
131           {
132              begin = Integer.parseInt(parser.getParsedStrings()[0]);
133
134              return SleepUtils.getScalar(tokenizer.getToken(begin));
135           }
136        }
137    
138        return temp;
139     }
140
141     public Scalar putScalar(String JavaDoc key, Scalar value)
142     {
143        return (Scalar)data.put(key, value);
144     }
145
146     public void removeScalar(String JavaDoc key)
147     {
148        // for jIRC scripts we don't want the locals to be "alterable" per se.
149
}
150
151
152     //
153
// These two functions are only called in the global instance of the Variable class
154
//
155
public Variable createLocalVariableContainer() { return null; }
156     public Variable createInternalVariableContainer() { return null; }
157 }
158
159
Popular Tags