Source code: com/tripi/asp/Scripting/Dictionary.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.Scripting;
26
27 import com.tripi.asp.*;
28 import java.util.Enumeration;
29 import java.util.Hashtable;
30 import java.util.Vector;
31
32 /**
33 * Implements the Scripting.Dictionary object available
34 * from Server.CreateObject("Scripting.Dictionary")
35 *
36 * @author Terence Haddock
37 * @version 0.9
38 */
39 public class Dictionary implements SimpleMap
40 {
41 /**
42 * Contents of this dictionary, prepended with '_' to make it
43 * unavailable to ASP scripts
44 */
45 Hashtable _contents = new Hashtable();
46
47 /**
48 * Keys of this dictionary, stored in a vector to preserve
49 * sort order. Prepended with '_' to make it unavailable to ASP
50 * scripts.
51 */
52 Vector _keys = new Vector();
53
54 /**
55 * Constructor
56 */
57 public Dictionary() {};
58
59 /**
60 * Stores a value in this dictionary. Throws an error if the value
61 * already exists.
62 * @param name Name of value
63 * @param value Value to store
64 */
65 public void Add(String name, Object value) throws AspException
66 {
67 if (_contents.containsKey(name)) {
68 throw new RuntimeException("Already contains key: " + name);
69 }
70 put(name, value);
71 }
72
73 /**
74 * Removes a value from this dictionary. Throws an error if the value
75 * does not exist.
76 * @param name Name of value
77 */
78 public void Remove(String name)
79 {
80 if (!_contents.containsKey(name)) {
81 throw new RuntimeException("Key does not exist: " + name);
82 }
83 _keys.remove(name);
84 _contents.remove(name);
85 }
86
87 /**
88 * Tests for a key's existance.
89 * @param name Name of value
90 * @return <b>true</b> if value exists, <b>false</b> otherwise.
91 */
92 public boolean Exists(String name)
93 {
94 return _contents.containsKey(name);
95 }
96
97 /**
98 * Returns number of elements in this dictionary.
99 * @return number of elements in this dictionary.
100 */
101 public int Count()
102 {
103 return _contents.size();
104 }
105
106 /**
107 * Obtains a element from the dictionary.
108 * @param key Key to obtain.
109 * @return value of key
110 * @see SimpleMap#get(Object)
111 * @throws ASPException if an error occurs
112 */
113 public Object get(Object key) throws AspException
114 {
115 String name = Types.coerceToString(key);
116 return _contents.get(name);
117 }
118
119 /**
120 * Stores an element in the dictionary.
121 * @param key Key to store
122 * @param value Value of key
123 * @see SimpleMap#put(Object,Object)
124 * @throws ASPException if an error occurs
125 */
126 public void put(Object key, Object value) throws AspException
127 {
128 String name = Types.coerceToString(key);
129 if (value == null) {
130 value = new NullNode();
131 }
132 if (!_keys.contains(name))
133 {
134 _keys.add(name);
135 }
136 _contents.put(name, value);
137 }
138
139 /**
140 * Obtains the keys stored in this dictionary.
141 * @return Keys of this dictionary.
142 * @see SimpleMap#getKeys
143 * @throws ASPException if an error occurs
144 */
145 public java.util.Enumeration getKeys() throws AspException
146 {
147 return _keys.elements();
148 }
149
150 /**
151 * Obtains the keys of this dictionary. Converts the internal Vector
152 * of keys into an array for conversion to node.
153 * @return Array of keys in this dictionary.
154 * @throws AspException if an error occurs.
155 */
156 public Object[] keys() throws AspException
157 {
158 Object arr[] = new Object[_contents.size()];
159 int i = 0;
160 Vector keyVec = new Vector();
161 Enumeration enKeys = getKeys();
162 while (enKeys.hasMoreElements())
163 {
164 arr[i] = enKeys.nextElement();
165 i++;
166 }
167 return arr;
168 }
169 }
170